Improvement to the DebounceInt library providing an easier to use interface much more alike the usual InterrupIn interface
Fork of DebounceInt by
DebounceInt.cpp@0:afa153f7249f, 2014-02-18 (annotated)
- Committer:
- nleoni
- Date:
- Tue Feb 18 16:52:49 2014 +0000
- Revision:
- 0:afa153f7249f
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nleoni | 0:afa153f7249f | 1 | /* DebounceInt 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 | 0:afa153f7249f | 9 | // Declare a DebounceInt 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 | 0:afa153f7249f | 14 | #include "DebounceInt.h" |
nleoni | 0:afa153f7249f | 15 | |
nleoni | 0:afa153f7249f | 16 | //Default constructor, required but not to be used, will yield error and exit. |
nleoni | 0:afa153f7249f | 17 | DebounceInt::DebounceInt(){ |
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 | 0:afa153f7249f | 22 | DebounceInt::DebounceInt(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 | 0:afa153f7249f | 30 | DebounceInt::DebounceInt(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 | 0:afa153f7249f | 40 | void DebounceInt::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 | 0:afa153f7249f | 51 | //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge |
nleoni | 0:afa153f7249f | 52 | void DebounceInt::debouncePinRise(void){ |
nleoni | 0:afa153f7249f | 53 | this->pinIntMask->maskIntR(); |
nleoni | 0:afa153f7249f | 54 | this->debounceCallBack.attach_us(this,&DebounceInt::debounceCallbackPinRise,this->delay); |
nleoni | 0:afa153f7249f | 55 | } |
nleoni | 0:afa153f7249f | 56 | |
nleoni | 0:afa153f7249f | 57 | //internal callback used to re-enable (unmask) interrupts on rising edge |
nleoni | 0:afa153f7249f | 58 | void DebounceInt::debounceCallbackPinRise(void){ |
nleoni | 0:afa153f7249f | 59 | this->pinIntMask->ClrInt(); |
nleoni | 0:afa153f7249f | 60 | this->pinIntMask->unMaskIntR(); |
nleoni | 0:afa153f7249f | 61 | } |
nleoni | 0:afa153f7249f | 62 | |
nleoni | 0:afa153f7249f | 63 | //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge |
nleoni | 0:afa153f7249f | 64 | void DebounceInt::debouncePinFall(void){ |
nleoni | 0:afa153f7249f | 65 | this->pinIntMask->maskIntF(); |
nleoni | 0:afa153f7249f | 66 | this->debounceCallBack.attach_us(this,&DebounceInt::debounceCallbackPinFall,this->delay); |
nleoni | 0:afa153f7249f | 67 | } |
nleoni | 0:afa153f7249f | 68 | |
nleoni | 0:afa153f7249f | 69 | //internal callback used to re-enable (unmask) interrupts on falling edge |
nleoni | 0:afa153f7249f | 70 | void DebounceInt::debounceCallbackPinFall(void){ |
nleoni | 0:afa153f7249f | 71 | this->pinIntMask->ClrInt(); |
nleoni | 0:afa153f7249f | 72 | this->pinIntMask->unMaskIntF(); |
nleoni | 0:afa153f7249f | 73 | } |