Improvement to the DebounceInt library providing an easier to use interface much more alike the usual InterrupIn interface

Fork of DebounceInt by napoleon leoni

Committer:
nleoni
Date:
Tue Feb 25 05:07:21 2014 +0000
Revision:
1:3e9de727a215
Parent:
DebounceInt.h@0:afa153f7249f
Child:
2:bd5a3171ebc4
Debounced Interrupt class. Implements a debounced interrupt library that enhances the mbed InterruptIn library allowing the user to mask/unmask interrupts. The debounce feature may be disabled (TB implemented)

Who changed what in which revision?

UserRevisionLine numberNew 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 1:3e9de727a215 41 #ifndef DebounceInt2
nleoni 1:3e9de727a215 42 #define DebounceInt2
nleoni 0:afa153f7249f 43 #define _DEBOUNCEINTDELAYDEFAULT 50000 //useconds, this number would be about 50 ms
nleoni 0:afa153f7249f 44 #define _MINDBOUNCEDELAY 5
nleoni 1:3e9de727a215 45 class DebounceInt2{
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 1:3e9de727a215 51 InterruptIn *intRise,*intFall;
nleoni 1:3e9de727a215 52 bool riseAttached,fallAttached;
nleoni 1:3e9de727a215 53 void *riseHandler(void),*fallHandler(void);
nleoni 0:afa153f7249f 54
nleoni 0:afa153f7249f 55 public://methods
nleoni 1:3e9de727a215 56 DebounceInt2(); //Default constructor, required but not to be used, will yield error and exit.
nleoni 1:3e9de727a215 57 DebounceInt2(PinName pin);//Constructor with PinName, delay will remain default value
nleoni 1:3e9de727a215 58 DebounceInt2(PinName pin,unsigned int delay_us);//Constructor with PinName and specified delay in micro-seconds
nleoni 0:afa153f7249f 59 void setDelay(unsigned int); //method to modify delay for debounce in us
nleoni 1:3e9de727a215 60 void attachRise(*fhandler); //attach interrupt handler for interrupt on rising edge
nleoni 1:3e9de727a215 61 void disableRise(void); //rise interrupt handler for rising edge
nleoni 1:3e9de727a215 62 void enableRise(void); //unmask rise interrupt handler for rising edge
nleoni 1:3e9de727a215 63 void attachFall(*fhandler); //attach interrupt handler for interrupt on falling edge
nleoni 1:3e9de727a215 64 void disableFall(void); //mask fall interrupt handler for rising edge
nleoni 1:3e9de727a215 65 void enableFall(void); //unmask fall interrupt handler for rising edge
nleoni 1:3e9de727a215 66
nleoni 1:3e9de727a215 67 private://methods
nleoni 0:afa153f7249f 68 void debouncePinRise(void); //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
nleoni 0:afa153f7249f 69 void debouncePinFall(void); //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
nleoni 0:afa153f7249f 70 void debounceCallbackPinRise(void); //internal callback used to re-enable (unmask) interrupts on rising edge
nleoni 0:afa153f7249f 71 void debounceCallbackPinFall(void); //internal callback used to re-enable (unmask) interrupts on falling edge
nleoni 0:afa153f7249f 72
nleoni 0:afa153f7249f 73 };
nleoni 0:afa153f7249f 74
nleoni 0:afa153f7249f 75 #endif