napoleon leoni / DebounceInt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebounceInt.h Source File

DebounceInt.h

00001 /* DebounceInt Library */
00002 /* This class enables debouncing a GPIO interrupt from a specific MBED pin            */
00003 /* It does so by using the InterruptMask library to temporarily mask the interrupt    */
00004 /* from the specified pin for a user specified delay specified in us. After the delay */
00005 /* time has passed the interrupt is unmasked (reenabled) and any pending interrupts   */
00006 /* also cleared. The library gives you the flexibiity to debounce on either the       */
00007 /* rising or falling edge interrupts.                                                 */
00008 // Usage:
00009 // Declare a DebounceInt object with a valid mbed pin (p5-p30 except p19 and p20) as 
00010 // an argument. Within your interrupt service routine call the appropriate debounce
00011 // method debouncePinRise or debouncePinFall. 
00012 // Requires the InterruptMask library available at: https://mbed.org/users/nleoni/code/InterruptMask/
00013 
00014 //
00015 // Example Code: toggles LED1 on falling edge interrupt
00016 //#include "mbed.h"
00017 //#include "DebounceInt.h"
00018 //DigitalOut myled(LED1);
00019 //InterruptIn upPressed(p15);
00020 //DebounceInt *upPressedDebounce; //debounce object to be used within interrupt for joystick up
00021 //                                //note that pointer is defined here to have global variable
00022 //                                //but constructor call must be within the main function.
00023 //int main() {
00024 //  upPressedDebounce = new DebounceInt(p15,50000); //create DebounceInt object for p15 interrupt
00025 //                                                  //with a 50000 us delay (interrupt disabled to avoid bounce)                                    
00026 //  upPressed.fall(ISRup);            //attach joystick up click on falling edge, that is when
00027 //                                    //user releases the joystick the action will happen
00028 //  while(1){
00029 //  }
00030 //}
00031 //void ISRup(void){
00032 //    upPressedDebounce->debouncePinFall(); //call debounce method on pin fall, will disable interrupt for 
00033 //                                          //specified delay
00034 //    myled=!myled;
00035 //}
00036 
00037 
00038 #include "mbed.h"
00039 #include "InterruptMask.h"
00040 
00041 #ifndef DEBOUNCEINT
00042 #define DEBOUNCEINT
00043 #define _DEBOUNCEINTDELAYDEFAULT 50000 //useconds, this number would be about 50 ms
00044 #define _MINDBOUNCEDELAY 5
00045 class DebounceInt{
00046 public://members
00047 private://members
00048     InterruptMask* pinIntMask; //Internal InterruptMask object
00049     unsigned int delay; //this is the debounce delay, during which the interrupt in question is masked
00050     Timeout debounceCallBack;
00051 
00052 public://methods
00053     DebounceInt();     //Default constructor, required but not to be used, will yield error and exit.
00054     DebounceInt(PinName pin);//Constructor with PinName, delay will remain default value
00055     DebounceInt(PinName pin,unsigned int delay_us);//Constructor with PinName and specified delay in micro-seconds
00056     void setDelay(unsigned int); //method to modify delay for debounce in us
00057     void debouncePinRise(void);  //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
00058     void debouncePinFall(void);  //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
00059     
00060 private://methods
00061     void debounceCallbackPinRise(void); //internal callback used to re-enable (unmask) interrupts on rising edge
00062     void debounceCallbackPinFall(void); //internal callback used to re-enable (unmask) interrupts on falling edge
00063     
00064 };
00065 
00066 #endif