napoleon leoni / DebounceInt2

Fork of DebounceInt by napoleon leoni

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebounceInt2.cpp Source File

DebounceInt2.cpp

00001 /* DebounceInt2 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 DebounceInt2 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 "DebounceInt2.h"
00015 
00016 //Default constructor, required but not to be used, will yield error and exit.
00017     DebounceInt2::DebounceInt2(){
00018         exit(1);
00019     }
00020     
00021 //Constructor with PinName, delay will remain default value
00022     DebounceInt2::DebounceInt2(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     DebounceInt2::DebounceInt2(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 DebounceInt2::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 //**********************************************************************************************//
00052 // void DebounceInt2::attachRise(*fhandler){                                                    //
00053 // Inputs: *fhandler(void), pointer to handler function which must be void *fhandler(void)                                                                               //
00054 //**********************************************************************************************//
00055 
00056     void DebounceInt2::rise(FunctionPointer fhandler){     //attach interrupt handler for interrupt on rising edge
00057     
00058         if(!(this->intRise)){//only allocate the pointer and Interrupt in once
00059             this->intRise= new InterruptIn(this->pin);
00060         }
00061         this->riseHandler = new FunctionPointer(fhandler);
00062         this->intRise->rise(this,&DebounceInt2::debouncePinRise); //This attaches our handler which 
00063                                                                     //includes the debounce functionality                                                                
00064     }
00065 
00066     template<typename T>
00067     void rise(T* tptr, void (T::*mptr)(void)){
00068         if(!(this->intRise)){//only allocate the pointer and Interrupt in once
00069             this->intRise= new InterruptIn(this->pin);
00070         }
00071         this->riseHandler = new FunctionPointer(tptr,mptr);
00072         this->intRise->rise(this,&DebounceInt2::debouncePinRise);
00073     }
00074 
00075     
00076     void DebounceInt2::disableRise(void){         //rise interrupt handler for rising edge
00077         this->pinIntMask->maskIntR();
00078     }
00079     void DebounceInt2::enableRise(void){          //unmask rise interrupt handler for rising edge
00080         this->pinIntMask->unMaskIntR();    
00081     }
00082 
00083     void DebounceInt2::fall(FunctionPointer fhandler){     //attach interrupt handler for interrupt on falling edge
00084     
00085         if(!(this->intFall)){//only allocate the pointer and Interrupt in once
00086             this->intFall= new InterruptIn(this->pin);
00087         }
00088         this->fallHandler = new FunctionPointer(fhandler);
00089         this->intFall->fall(this,&DebounceInt2::debouncePinFall); //This attaches our handler which 
00090                                                                     //includes the debounce functionality                                                                
00091     
00092     }
00093 
00094     template<typename T>
00095     void fall(T* tptr, void (T::*mptr)(void)){
00096         if(!(this->Fall)){//only allocate the pointer and Interrupt in once
00097             this->intFall= new InterruptIn(this->pin);
00098         }
00099         this->fallHandler = new FunctionPointer(tptr,mptr);
00100         this->intFall->fall(this,&DebounceInt2::debouncePinRise);
00101     }
00102 
00103     void DebounceInt2::disableFall(void){         //mask fall interrupt handler for rising edge
00104         this->pinIntMask->maskIntF();    
00105     }
00106     void DebounceInt2::enableFall(void){          //unmask fall interrupt handler for rising edge
00107         this->pinIntMask->unMaskIntF();
00108     }
00109 
00110 
00111 //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
00112     void DebounceInt2::debouncePinRise(void){
00113         this->pinIntMask->maskIntR();
00114         this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinRise,this->delay);
00115         this->riseHandler->call();
00116     }
00117     
00118 //internal callback used to re-enable (unmask) interrupts on rising edge
00119     void DebounceInt2::debounceCallbackPinRise(void){
00120         this->pinIntMask->ClrInt();
00121         this->pinIntMask->unMaskIntR();
00122     }
00123 
00124 //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
00125     void DebounceInt2::debouncePinFall(void){
00126         this->pinIntMask->maskIntF();
00127         this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinFall,this->delay);
00128         this->fallHandler->call();
00129     }
00130     
00131 //internal callback used to re-enable (unmask) interrupts on falling edge
00132     void DebounceInt2::debounceCallbackPinFall(void){
00133         this->pinIntMask->ClrInt();
00134         this->pinIntMask->unMaskIntF();
00135     }