napoleon leoni / DebounceIntRTOS

Fork of DebounceInt by napoleon leoni

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebounceIntRTOS.cpp Source File

DebounceIntRTOS.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 "DebounceIntRTOS.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->debounceCallBackRise = new RtosTimer(&DebounceInt::wrapperDebouncePinRise, osTimerPeriodic, this);
00027         this->debounceCallBackFall = new RtosTimer(&DebounceInt::wrapperDebouncePinFall, osTimerPeriodic, this);
00028         this->setDelay(_DEBOUNCEINTDELAYDEFAULT);
00029     }
00030     
00031     
00032 //Constructor with PinName and specified delay in micro-seconds
00033     DebounceInt::DebounceInt(PinName pin,unsigned int delay_ms){
00034     // Inputs:
00035     // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise
00036     // unsigned int delay: Expected positive delay, will force delay to be larger
00037     // than minimum delay _MINDBOUNCEDELAY
00038         this->pinIntMask = new InterruptMask(pin);        
00039         this->debounceCallBackRise = new RtosTimer(&DebounceInt::wrapperDebouncePinRise, osTimerPeriodic, this);
00040         this->debounceCallBackFall = new RtosTimer(&DebounceInt::wrapperDebouncePinFall, osTimerPeriodic, this);
00041         this->setDelay(delay_ms);
00042     }
00043 
00044 //method to modify delay for debounce in us
00045     void DebounceInt::setDelay(unsigned int delay_ms){
00046     // Inputs:
00047     // unsigned int delay: Expected positive delay, will force delay to be larger
00048     // than minimum delay _MINDBOUNCEDELAY
00049         if(delay_ms>(_MINDBOUNCEDELAY) ){
00050             this->delay=delay_ms;
00051         } else { 
00052             this->delay=_MINDBOUNCEDELAY;
00053         }
00054     } 
00055 
00056 //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
00057     void DebounceInt::debouncePinRise(void){
00058         //this->pinIntMask->maskIntR();
00059         this->debounceCallBackRise->start(this->delay);
00060     }
00061     
00062     void DebounceInt::wrapperDebouncePinRise(void const *ptr2Object){
00063         DebounceInt* instance = (DebounceInt*) ptr2Object;    
00064         instance->debounceCallbackPinRise();
00065     }
00066 
00067     void DebounceInt::wrapperDebouncePinFall(void const *ptr2Object){
00068         DebounceInt* instance = (DebounceInt*) ptr2Object;    
00069         instance->debounceCallbackPinFall();
00070     }
00071 
00072 //internal callback used to re-enable (unmask) interrupts on rising edge
00073     void DebounceInt::debounceCallbackPinRise(void){
00074         this->pinIntMask->ClrInt();
00075         this->pinIntMask->unMaskIntR();
00076         this->debounceCallBackRise->stop();
00077 
00078     }
00079 
00080 //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
00081     void DebounceInt::debouncePinFall(void){
00082         //this->pinIntMask->maskIntF();
00083         this->debounceCallBackFall->start(this->delay);
00084     }
00085     
00086 //internal callback used to re-enable (unmask) interrupts on falling edge
00087     void DebounceInt::debounceCallbackPinFall(void){
00088         this->pinIntMask->ClrInt();
00089         this->pinIntMask->unMaskIntF();
00090         this->debounceCallBackFall->stop();
00091 
00092     }