Improvement to the DebounceInt library providing an easier to use interface much more alike the usual InterrupIn interface
Fork of DebounceInt by
DebounceInt2.h@2:bd5a3171ebc4, 2014-02-25 (annotated)
- Committer:
- nleoni
- Date:
- Tue Feb 25 06:37:35 2014 +0000
- Revision:
- 2:bd5a3171ebc4
- Parent:
- 1:3e9de727a215
- Child:
- 3:3b5c7faadd5c
Compiles. Fixed types for function pointers
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 | 1:3e9de727a215 | 8 | // Deprecated 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 | 0:afa153f7249f | 14 | // |
nleoni | 0:afa153f7249f | 15 | // Example Code: toggles LED1 on falling edge interrupt |
nleoni | 0:afa153f7249f | 16 | //#include "mbed.h" |
nleoni | 1:3e9de727a215 | 17 | //#include "DebounceInt2.h" |
nleoni | 0:afa153f7249f | 18 | //DigitalOut myled(LED1); |
nleoni | 0:afa153f7249f | 19 | //InterruptIn upPressed(p15); |
nleoni | 1:3e9de727a215 | 20 | //DebounceInt2 *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 | 1:3e9de727a215 | 24 | // upPressedDebounce = new DebounceInt2(p15,50000); //create DebounceInt2 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 | 2:bd5a3171ebc4 | 41 | #ifndef DebounceInt2Exclude |
nleoni | 2:bd5a3171ebc4 | 42 | #define DebounceInt2Exclude |
nleoni | 0:afa153f7249f | 43 | #define _DEBOUNCEINTDELAYDEFAULT 50000 //useconds, this number would be about 50 ms |
nleoni | 2:bd5a3171ebc4 | 44 | #define _MINDBOUNCEDELAY 50 |
nleoni | 2:bd5a3171ebc4 | 45 | |
nleoni | 2:bd5a3171ebc4 | 46 | typedef void (*t_fhandler)(void); |
nleoni | 2:bd5a3171ebc4 | 47 | |
nleoni | 2:bd5a3171ebc4 | 48 | class DebounceInt2 |
nleoni | 2:bd5a3171ebc4 | 49 | { |
nleoni | 0:afa153f7249f | 50 | public://members |
nleoni | 0:afa153f7249f | 51 | private://members |
nleoni | 0:afa153f7249f | 52 | InterruptMask* pinIntMask; //Internal InterruptMask object |
nleoni | 0:afa153f7249f | 53 | unsigned int delay; //this is the debounce delay, during which the interrupt in question is masked |
nleoni | 0:afa153f7249f | 54 | Timeout debounceCallBack; |
nleoni | 2:bd5a3171ebc4 | 55 | InterruptIn* intRise; |
nleoni | 2:bd5a3171ebc4 | 56 | InterruptIn* intFall; |
nleoni | 1:3e9de727a215 | 57 | bool riseAttached,fallAttached; |
nleoni | 2:bd5a3171ebc4 | 58 | t_fhandler riseHandler,fallHandler; |
nleoni | 2:bd5a3171ebc4 | 59 | PinName pin; |
nleoni | 0:afa153f7249f | 60 | |
nleoni | 0:afa153f7249f | 61 | public://methods |
nleoni | 1:3e9de727a215 | 62 | DebounceInt2(); //Default constructor, required but not to be used, will yield error and exit. |
nleoni | 1:3e9de727a215 | 63 | DebounceInt2(PinName pin);//Constructor with PinName, delay will remain default value |
nleoni | 1:3e9de727a215 | 64 | DebounceInt2(PinName pin,unsigned int delay_us);//Constructor with PinName and specified delay in micro-seconds |
nleoni | 0:afa153f7249f | 65 | void setDelay(unsigned int); //method to modify delay for debounce in us |
nleoni | 2:bd5a3171ebc4 | 66 | void rise(t_fhandler); //attach interrupt handler for interrupt on rising edge |
nleoni | 1:3e9de727a215 | 67 | void disableRise(void); //rise interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 68 | void enableRise(void); //unmask rise interrupt handler for rising edge |
nleoni | 2:bd5a3171ebc4 | 69 | void fall(t_fhandler); //attach interrupt handler for interrupt on falling edge |
nleoni | 1:3e9de727a215 | 70 | void disableFall(void); //mask fall interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 71 | void enableFall(void); //unmask fall interrupt handler for rising edge |
nleoni | 1:3e9de727a215 | 72 | |
nleoni | 1:3e9de727a215 | 73 | private://methods |
nleoni | 0:afa153f7249f | 74 | void debouncePinRise(void); //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge |
nleoni | 0:afa153f7249f | 75 | void debouncePinFall(void); //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge |
nleoni | 0:afa153f7249f | 76 | void debounceCallbackPinRise(void); //internal callback used to re-enable (unmask) interrupts on rising edge |
nleoni | 0:afa153f7249f | 77 | void debounceCallbackPinFall(void); //internal callback used to re-enable (unmask) interrupts on falling edge |
nleoni | 0:afa153f7249f | 78 | |
nleoni | 0:afa153f7249f | 79 | }; |
nleoni | 0:afa153f7249f | 80 | |
nleoni | 0:afa153f7249f | 81 | #endif |