Improvement to the DebounceInt library providing an easier to use interface much more alike the usual InterrupIn interface
Fork of DebounceInt by
DebounceInt2.cpp@3:3b5c7faadd5c, 2014-02-25 (annotated)
- Committer:
- nleoni
- Date:
- Tue Feb 25 07:53:23 2014 +0000
- Revision:
- 3:3b5c7faadd5c
- Parent:
- 2:bd5a3171ebc4
Added class method support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nleoni | 1:3e9de727a215 | 1 | /* DebounceInt2 Library */ |
nleoni | 0:afa153f7249f | 2 | /* This class enables debouncing a GPIO interrupt from a specific MBED pin */ |
nleoni | 0:afa153f7249f | 3 | /* It does so by using the InterruptMask library to temporarily mask the interrupt */ |
nleoni | 0:afa153f7249f | 4 | /* from the specified pin for a user specified delay specified in us. After the delay */ |
nleoni | 0:afa153f7249f | 5 | /* time has passed the interrupt is unmasked (reenabled) and any pending interrupts */ |
nleoni | 0:afa153f7249f | 6 | /* also cleared. The library gives you the flexibiity to debounce on either the */ |
nleoni | 0:afa153f7249f | 7 | /* rising or falling edge interrupts. */ |
nleoni | 0:afa153f7249f | 8 | // Usage: |
nleoni | 1:3e9de727a215 | 9 | // Declare a DebounceInt2 object with a valid mbed pin (p5-p30 except p19 and p20) as |
nleoni | 0:afa153f7249f | 10 | // an argument. Within your interrupt service routine call the appropriate debounce |
nleoni | 0:afa153f7249f | 11 | // method debouncePinRise or debouncePinFall. |
nleoni | 0:afa153f7249f | 12 | // Requires the InterruptMask library available at: https://mbed.org/users/nleoni/code/InterruptMask/ |
nleoni | 0:afa153f7249f | 13 | |
nleoni | 1:3e9de727a215 | 14 | #include "DebounceInt2.h" |
nleoni | 0:afa153f7249f | 15 | |
nleoni | 0:afa153f7249f | 16 | //Default constructor, required but not to be used, will yield error and exit. |
nleoni | 1:3e9de727a215 | 17 | DebounceInt2::DebounceInt2(){ |
nleoni | 0:afa153f7249f | 18 | exit(1); |
nleoni | 0:afa153f7249f | 19 | } |
nleoni | 0:afa153f7249f | 20 | |
nleoni | 0:afa153f7249f | 21 | //Constructor with PinName, delay will remain default value |
nleoni | 1:3e9de727a215 | 22 | DebounceInt2::DebounceInt2(PinName pin){ |
nleoni | 0:afa153f7249f | 23 | // Inputs: |
nleoni | 0:afa153f7249f | 24 | // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise |
nleoni | 0:afa153f7249f | 25 | this->pinIntMask = new InterruptMask(pin); |
nleoni | 0:afa153f7249f | 26 | this->setDelay(_DEBOUNCEINTDELAYDEFAULT); |
nleoni | 0:afa153f7249f | 27 | } |
nleoni | 0:afa153f7249f | 28 | |
nleoni | 0:afa153f7249f | 29 | //Constructor with PinName and specified delay in micro-seconds |
nleoni | 1:3e9de727a215 | 30 | DebounceInt2::DebounceInt2(PinName pin,unsigned int delay_us){ |
nleoni | 0:afa153f7249f | 31 | // Inputs: |
nleoni | 0:afa153f7249f | 32 | // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise |
nleoni | 0:afa153f7249f | 33 | // unsigned int delay: Expected positive delay, will force delay to be larger |
nleoni | 0:afa153f7249f | 34 | // than minimum delay _MINDBOUNCEDELAY |
nleoni | 0:afa153f7249f | 35 | this->pinIntMask = new InterruptMask(pin); |
nleoni | 0:afa153f7249f | 36 | this->setDelay(delay_us); |
nleoni | 0:afa153f7249f | 37 | } |
nleoni | 0:afa153f7249f | 38 | |
nleoni | 0:afa153f7249f | 39 | //method to modify delay for debounce in us |
nleoni | 1:3e9de727a215 | 40 | void DebounceInt2::setDelay(unsigned int delay_us){ |
nleoni | 0:afa153f7249f | 41 | // Inputs: |
nleoni | 0:afa153f7249f | 42 | // unsigned int delay: Expected positive delay, will force delay to be larger |
nleoni | 0:afa153f7249f | 43 | // than minimum delay _MINDBOUNCEDELAY |
nleoni | 0:afa153f7249f | 44 | if(delay_us>(_MINDBOUNCEDELAY) ){ |
nleoni | 0:afa153f7249f | 45 | this->delay=delay_us; |
nleoni | 0:afa153f7249f | 46 | } else { |
nleoni | 0:afa153f7249f | 47 | this->delay=_MINDBOUNCEDELAY; |
nleoni | 0:afa153f7249f | 48 | } |
nleoni | 0:afa153f7249f | 49 | } |
nleoni | 0:afa153f7249f | 50 | |
nleoni | 1:3e9de727a215 | 51 | //**********************************************************************************************// |
nleoni | 1:3e9de727a215 | 52 | // void DebounceInt2::attachRise(*fhandler){ // |
nleoni | 1:3e9de727a215 | 53 | // Inputs: *fhandler(void), pointer to handler function which must be void *fhandler(void) // |
nleoni | 1:3e9de727a215 | 54 | //**********************************************************************************************// |
nleoni | 1:3e9de727a215 | 55 | |
nleoni | 3:3b5c7faadd5c | 56 | void DebounceInt2::rise(FunctionPointer fhandler){ //attach interrupt handler for interrupt on rising edge |
nleoni | 1:3e9de727a215 | 57 | |
nleoni | 1:3e9de727a215 | 58 | if(!(this->intRise)){//only allocate the pointer and Interrupt in once |
nleoni | 1:3e9de727a215 | 59 | this->intRise= new InterruptIn(this->pin); |
nleoni | 1:3e9de727a215 | 60 | } |
nleoni | 3:3b5c7faadd5c | 61 | this->riseHandler = new FunctionPointer(fhandler); |
nleoni | 2:bd5a3171ebc4 | 62 | this->intRise->rise(this,&DebounceInt2::debouncePinRise); //This attaches our handler which |
nleoni | 1:3e9de727a215 | 63 | //includes the debounce functionality |
nleoni | 1:3e9de727a215 | 64 | } |
nleoni | 3:3b5c7faadd5c | 65 | |
nleoni | 3:3b5c7faadd5c | 66 | template<typename T> |
nleoni | 3:3b5c7faadd5c | 67 | void rise(T* tptr, void (T::*mptr)(void)){ |
nleoni | 3:3b5c7faadd5c | 68 | if(!(this->intRise)){//only allocate the pointer and Interrupt in once |
nleoni | 3:3b5c7faadd5c | 69 | this->intRise= new InterruptIn(this->pin); |
nleoni | 3:3b5c7faadd5c | 70 | } |
nleoni | 3:3b5c7faadd5c | 71 | this->riseHandler = new FunctionPointer(tptr,mptr); |
nleoni | 3:3b5c7faadd5c | 72 | this->intRise->rise(this,&DebounceInt2::debouncePinRise); |
nleoni | 3:3b5c7faadd5c | 73 | } |
nleoni | 3:3b5c7faadd5c | 74 | |
nleoni | 1:3e9de727a215 | 75 | |
nleoni | 1:3e9de727a215 | 76 | void DebounceInt2::disableRise(void){ //rise interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 77 | this->pinIntMask->maskIntR(); |
nleoni | 1:3e9de727a215 | 78 | } |
nleoni | 1:3e9de727a215 | 79 | void DebounceInt2::enableRise(void){ //unmask rise interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 80 | this->pinIntMask->unMaskIntR(); |
nleoni | 1:3e9de727a215 | 81 | } |
nleoni | 3:3b5c7faadd5c | 82 | |
nleoni | 3:3b5c7faadd5c | 83 | void DebounceInt2::fall(FunctionPointer fhandler){ //attach interrupt handler for interrupt on falling edge |
nleoni | 1:3e9de727a215 | 84 | |
nleoni | 1:3e9de727a215 | 85 | if(!(this->intFall)){//only allocate the pointer and Interrupt in once |
nleoni | 1:3e9de727a215 | 86 | this->intFall= new InterruptIn(this->pin); |
nleoni | 1:3e9de727a215 | 87 | } |
nleoni | 3:3b5c7faadd5c | 88 | this->fallHandler = new FunctionPointer(fhandler); |
nleoni | 2:bd5a3171ebc4 | 89 | this->intFall->fall(this,&DebounceInt2::debouncePinFall); //This attaches our handler which |
nleoni | 1:3e9de727a215 | 90 | //includes the debounce functionality |
nleoni | 1:3e9de727a215 | 91 | |
nleoni | 1:3e9de727a215 | 92 | } |
nleoni | 3:3b5c7faadd5c | 93 | |
nleoni | 3:3b5c7faadd5c | 94 | template<typename T> |
nleoni | 3:3b5c7faadd5c | 95 | void fall(T* tptr, void (T::*mptr)(void)){ |
nleoni | 3:3b5c7faadd5c | 96 | if(!(this->Fall)){//only allocate the pointer and Interrupt in once |
nleoni | 3:3b5c7faadd5c | 97 | this->intFall= new InterruptIn(this->pin); |
nleoni | 3:3b5c7faadd5c | 98 | } |
nleoni | 3:3b5c7faadd5c | 99 | this->fallHandler = new FunctionPointer(tptr,mptr); |
nleoni | 3:3b5c7faadd5c | 100 | this->intFall->fall(this,&DebounceInt2::debouncePinRise); |
nleoni | 3:3b5c7faadd5c | 101 | } |
nleoni | 3:3b5c7faadd5c | 102 | |
nleoni | 1:3e9de727a215 | 103 | void DebounceInt2::disableFall(void){ //mask fall interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 104 | this->pinIntMask->maskIntF(); |
nleoni | 1:3e9de727a215 | 105 | } |
nleoni | 1:3e9de727a215 | 106 | void DebounceInt2::enableFall(void){ //unmask fall interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 107 | this->pinIntMask->unMaskIntF(); |
nleoni | 1:3e9de727a215 | 108 | } |
nleoni | 1:3e9de727a215 | 109 | |
nleoni | 1:3e9de727a215 | 110 | |
nleoni | 0:afa153f7249f | 111 | //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge |
nleoni | 1:3e9de727a215 | 112 | void DebounceInt2::debouncePinRise(void){ |
nleoni | 0:afa153f7249f | 113 | this->pinIntMask->maskIntR(); |
nleoni | 1:3e9de727a215 | 114 | this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinRise,this->delay); |
nleoni | 3:3b5c7faadd5c | 115 | this->riseHandler->call(); |
nleoni | 0:afa153f7249f | 116 | } |
nleoni | 0:afa153f7249f | 117 | |
nleoni | 0:afa153f7249f | 118 | //internal callback used to re-enable (unmask) interrupts on rising edge |
nleoni | 1:3e9de727a215 | 119 | void DebounceInt2::debounceCallbackPinRise(void){ |
nleoni | 0:afa153f7249f | 120 | this->pinIntMask->ClrInt(); |
nleoni | 0:afa153f7249f | 121 | this->pinIntMask->unMaskIntR(); |
nleoni | 0:afa153f7249f | 122 | } |
nleoni | 0:afa153f7249f | 123 | |
nleoni | 0:afa153f7249f | 124 | //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge |
nleoni | 1:3e9de727a215 | 125 | void DebounceInt2::debouncePinFall(void){ |
nleoni | 0:afa153f7249f | 126 | this->pinIntMask->maskIntF(); |
nleoni | 1:3e9de727a215 | 127 | this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinFall,this->delay); |
nleoni | 3:3b5c7faadd5c | 128 | this->fallHandler->call(); |
nleoni | 0:afa153f7249f | 129 | } |
nleoni | 0:afa153f7249f | 130 | |
nleoni | 0:afa153f7249f | 131 | //internal callback used to re-enable (unmask) interrupts on falling edge |
nleoni | 1:3e9de727a215 | 132 | void DebounceInt2::debounceCallbackPinFall(void){ |
nleoni | 0:afa153f7249f | 133 | this->pinIntMask->ClrInt(); |
nleoni | 0:afa153f7249f | 134 | this->pinIntMask->unMaskIntF(); |
nleoni | 0:afa153f7249f | 135 | } |