DebounceIN library allows debouncing a GPIO pin interrupt
DebounceInt.h@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 | // |
nleoni | 0:afa153f7249f | 15 | // Example Code: toggles LED1 on falling edge interrupt |
nleoni | 0:afa153f7249f | 16 | //#include "mbed.h" |
nleoni | 0:afa153f7249f | 17 | //#include "DebounceInt.h" |
nleoni | 0:afa153f7249f | 18 | //DigitalOut myled(LED1); |
nleoni | 0:afa153f7249f | 19 | //InterruptIn upPressed(p15); |
nleoni | 0:afa153f7249f | 20 | //DebounceInt *upPressedDebounce; //debounce object to be used within interrupt for joystick up |
nleoni | 0:afa153f7249f | 21 | // //note that pointer is defined here to have global variable |
nleoni | 0:afa153f7249f | 22 | // //but constructor call must be within the main function. |
nleoni | 0:afa153f7249f | 23 | //int main() { |
nleoni | 0:afa153f7249f | 24 | // upPressedDebounce = new DebounceInt(p15,50000); //create DebounceInt object for p15 interrupt |
nleoni | 0:afa153f7249f | 25 | // //with a 50000 us delay (interrupt disabled to avoid bounce) |
nleoni | 0:afa153f7249f | 26 | // upPressed.fall(ISRup); //attach joystick up click on falling edge, that is when |
nleoni | 0:afa153f7249f | 27 | // //user releases the joystick the action will happen |
nleoni | 0:afa153f7249f | 28 | // while(1){ |
nleoni | 0:afa153f7249f | 29 | // } |
nleoni | 0:afa153f7249f | 30 | //} |
nleoni | 0:afa153f7249f | 31 | //void ISRup(void){ |
nleoni | 0:afa153f7249f | 32 | // upPressedDebounce->debouncePinFall(); //call debounce method on pin fall, will disable interrupt for |
nleoni | 0:afa153f7249f | 33 | // //specified delay |
nleoni | 0:afa153f7249f | 34 | // myled=!myled; |
nleoni | 0:afa153f7249f | 35 | //} |
nleoni | 0:afa153f7249f | 36 | |
nleoni | 0:afa153f7249f | 37 | |
nleoni | 0:afa153f7249f | 38 | #include "mbed.h" |
nleoni | 0:afa153f7249f | 39 | #include "InterruptMask.h" |
nleoni | 0:afa153f7249f | 40 | |
nleoni | 0:afa153f7249f | 41 | #ifndef DEBOUNCEINT |
nleoni | 0:afa153f7249f | 42 | #define DEBOUNCEINT |
nleoni | 0:afa153f7249f | 43 | #define _DEBOUNCEINTDELAYDEFAULT 50000 //useconds, this number would be about 50 ms |
nleoni | 0:afa153f7249f | 44 | #define _MINDBOUNCEDELAY 5 |
nleoni | 0:afa153f7249f | 45 | class DebounceInt{ |
nleoni | 0:afa153f7249f | 46 | public://members |
nleoni | 0:afa153f7249f | 47 | private://members |
nleoni | 0:afa153f7249f | 48 | InterruptMask* pinIntMask; //Internal InterruptMask object |
nleoni | 0:afa153f7249f | 49 | unsigned int delay; //this is the debounce delay, during which the interrupt in question is masked |
nleoni | 0:afa153f7249f | 50 | Timeout debounceCallBack; |
nleoni | 0:afa153f7249f | 51 | |
nleoni | 0:afa153f7249f | 52 | public://methods |
nleoni | 0:afa153f7249f | 53 | DebounceInt(); //Default constructor, required but not to be used, will yield error and exit. |
nleoni | 0:afa153f7249f | 54 | DebounceInt(PinName pin);//Constructor with PinName, delay will remain default value |
nleoni | 0:afa153f7249f | 55 | DebounceInt(PinName pin,unsigned int delay_us);//Constructor with PinName and specified delay in micro-seconds |
nleoni | 0:afa153f7249f | 56 | void setDelay(unsigned int); //method to modify delay for debounce in us |
nleoni | 0:afa153f7249f | 57 | void debouncePinRise(void); //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge |
nleoni | 0:afa153f7249f | 58 | void debouncePinFall(void); //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge |
nleoni | 0:afa153f7249f | 59 | |
nleoni | 0:afa153f7249f | 60 | private://methods |
nleoni | 0:afa153f7249f | 61 | void debounceCallbackPinRise(void); //internal callback used to re-enable (unmask) interrupts on rising edge |
nleoni | 0:afa153f7249f | 62 | void debounceCallbackPinFall(void); //internal callback used to re-enable (unmask) interrupts on falling edge |
nleoni | 0:afa153f7249f | 63 | |
nleoni | 0:afa153f7249f | 64 | }; |
nleoni | 0:afa153f7249f | 65 | |
nleoni | 0:afa153f7249f | 66 | #endif |