napoleon leoni / DebounceInt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebounceInt.cpp Source File

DebounceInt.cpp

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 #include "DebounceInt.h"
00015 
00016 //Default constructor, required but not to be used, will yield error and exit.
00017     DebounceInt::DebounceInt(){
00018         exit(1);
00019     }
00020     
00021 //Constructor with PinName, delay will remain default value
00022     DebounceInt::DebounceInt(PinName pin){
00023     // Inputs:
00024     // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise
00025         this->pinIntMask = new InterruptMask(pin);
00026         this->setDelay(_DEBOUNCEINTDELAYDEFAULT);    
00027     }
00028 
00029 //Constructor with PinName and specified delay in micro-seconds
00030     DebounceInt::DebounceInt(PinName pin,unsigned int delay_us){
00031     // Inputs:
00032     // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise
00033     // unsigned int delay: Expected positive delay, will force delay to be larger
00034     // than minimum delay _MINDBOUNCEDELAY
00035         this->pinIntMask = new InterruptMask(pin);        
00036         this->setDelay(delay_us);
00037     }
00038 
00039 //method to modify delay for debounce in us
00040     void DebounceInt::setDelay(unsigned int delay_us){
00041     // Inputs:
00042     // unsigned int delay: Expected positive delay, will force delay to be larger
00043     // than minimum delay _MINDBOUNCEDELAY
00044         if(delay_us>(_MINDBOUNCEDELAY) ){
00045             this->delay=delay_us;
00046         } else { 
00047             this->delay=_MINDBOUNCEDELAY;
00048         }
00049     } 
00050 
00051 //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
00052     void DebounceInt::debouncePinRise(void){
00053         this->pinIntMask->maskIntR();
00054         this->debounceCallBack.attach_us(this,&DebounceInt::debounceCallbackPinRise,this->delay);
00055     }
00056     
00057 //internal callback used to re-enable (unmask) interrupts on rising edge
00058     void DebounceInt::debounceCallbackPinRise(void){
00059         this->pinIntMask->ClrInt();
00060         this->pinIntMask->unMaskIntR();
00061     }
00062 
00063 //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
00064     void DebounceInt::debouncePinFall(void){
00065         this->pinIntMask->maskIntF();
00066         this->debounceCallBack.attach_us(this,&DebounceInt::debounceCallbackPinFall,this->delay);
00067     }
00068     
00069 //internal callback used to re-enable (unmask) interrupts on falling edge
00070     void DebounceInt::debounceCallbackPinFall(void){
00071         this->pinIntMask->ClrInt();
00072         this->pinIntMask->unMaskIntF();
00073     }