napoleon leoni / DebounceIntRTOS

Fork of DebounceInt by napoleon leoni

Committer:
nleoni
Date:
Tue Feb 25 19:55:04 2014 +0000
Revision:
1:519b4ce3b082
Parent:
0:afa153f7249f
Updated to support interrupt debounce when operating with ; RTOS threads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nleoni 0:afa153f7249f 1 /* DebounceInt 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 0:afa153f7249f 8 // Usage:
nleoni 0:afa153f7249f 9 // Declare a DebounceInt 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 #include "DebounceInt.h"
nleoni 0:afa153f7249f 15
nleoni 0:afa153f7249f 16 //Default constructor, required but not to be used, will yield error and exit.
nleoni 0:afa153f7249f 17 DebounceInt::DebounceInt(){
nleoni 0:afa153f7249f 18 exit(1);
nleoni 0:afa153f7249f 19 }
nleoni 0:afa153f7249f 20
nleoni 0:afa153f7249f 21 //Constructor with PinName, delay will remain default value
nleoni 0:afa153f7249f 22 DebounceInt::DebounceInt(PinName pin){
nleoni 0:afa153f7249f 23 // Inputs:
nleoni 0:afa153f7249f 24 // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise
nleoni 0:afa153f7249f 25 this->pinIntMask = new InterruptMask(pin);
nleoni 1:519b4ce3b082 26 this->debounceCallBackRise = new RtosTimer(&DebounceInt::wrapperDebouncePinRise, osTimerOnce, this);
nleoni 1:519b4ce3b082 27 this->debounceCallBackFall = new RtosTimer(&DebounceInt::wrapperDebouncePinFall, osTimerOnce, this);
nleoni 1:519b4ce3b082 28 this->setDelay(_DEBOUNCEINTDELAYDEFAULT);
nleoni 0:afa153f7249f 29 }
nleoni 1:519b4ce3b082 30
nleoni 1:519b4ce3b082 31
nleoni 0:afa153f7249f 32 //Constructor with PinName and specified delay in micro-seconds
nleoni 1:519b4ce3b082 33 DebounceInt::DebounceInt(PinName pin,unsigned int delay_ms){
nleoni 0:afa153f7249f 34 // Inputs:
nleoni 0:afa153f7249f 35 // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise
nleoni 0:afa153f7249f 36 // unsigned int delay: Expected positive delay, will force delay to be larger
nleoni 0:afa153f7249f 37 // than minimum delay _MINDBOUNCEDELAY
nleoni 0:afa153f7249f 38 this->pinIntMask = new InterruptMask(pin);
nleoni 1:519b4ce3b082 39 this->setDelay(delay_ms);
nleoni 0:afa153f7249f 40 }
nleoni 0:afa153f7249f 41
nleoni 0:afa153f7249f 42 //method to modify delay for debounce in us
nleoni 1:519b4ce3b082 43 void DebounceInt::setDelay(unsigned int delay_ms){
nleoni 0:afa153f7249f 44 // Inputs:
nleoni 0:afa153f7249f 45 // unsigned int delay: Expected positive delay, will force delay to be larger
nleoni 0:afa153f7249f 46 // than minimum delay _MINDBOUNCEDELAY
nleoni 1:519b4ce3b082 47 if(delay_ms>(_MINDBOUNCEDELAY) ){
nleoni 1:519b4ce3b082 48 this->delay=delay_ms;
nleoni 0:afa153f7249f 49 } else {
nleoni 0:afa153f7249f 50 this->delay=_MINDBOUNCEDELAY;
nleoni 0:afa153f7249f 51 }
nleoni 0:afa153f7249f 52 }
nleoni 0:afa153f7249f 53
nleoni 0:afa153f7249f 54 //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
nleoni 0:afa153f7249f 55 void DebounceInt::debouncePinRise(void){
nleoni 0:afa153f7249f 56 this->pinIntMask->maskIntR();
nleoni 1:519b4ce3b082 57 this->debounceCallBackRise->start(this->delay);
nleoni 0:afa153f7249f 58 }
nleoni 0:afa153f7249f 59
nleoni 1:519b4ce3b082 60 void DebounceInt::wrapperDebouncePinRise(void const *ptr2Object){
nleoni 1:519b4ce3b082 61 DebounceInt* instance = (DebounceInt*) ptr2Object;
nleoni 1:519b4ce3b082 62 instance->debounceCallbackPinRise();
nleoni 1:519b4ce3b082 63 }
nleoni 1:519b4ce3b082 64
nleoni 1:519b4ce3b082 65 void DebounceInt::wrapperDebouncePinFall(void const *ptr2Object){
nleoni 1:519b4ce3b082 66 DebounceInt* instance = (DebounceInt*) ptr2Object;
nleoni 1:519b4ce3b082 67 instance->debounceCallbackPinFall();
nleoni 1:519b4ce3b082 68 }
nleoni 1:519b4ce3b082 69
nleoni 0:afa153f7249f 70 //internal callback used to re-enable (unmask) interrupts on rising edge
nleoni 0:afa153f7249f 71 void DebounceInt::debounceCallbackPinRise(void){
nleoni 0:afa153f7249f 72 this->pinIntMask->ClrInt();
nleoni 0:afa153f7249f 73 this->pinIntMask->unMaskIntR();
nleoni 0:afa153f7249f 74 }
nleoni 0:afa153f7249f 75
nleoni 0:afa153f7249f 76 //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
nleoni 0:afa153f7249f 77 void DebounceInt::debouncePinFall(void){
nleoni 0:afa153f7249f 78 this->pinIntMask->maskIntF();
nleoni 1:519b4ce3b082 79 this->debounceCallBackFall->start(this->delay);
nleoni 0:afa153f7249f 80 }
nleoni 0:afa153f7249f 81
nleoni 0:afa153f7249f 82 //internal callback used to re-enable (unmask) interrupts on falling edge
nleoni 0:afa153f7249f 83 void DebounceInt::debounceCallbackPinFall(void){
nleoni 0:afa153f7249f 84 this->pinIntMask->ClrInt();
nleoni 0:afa153f7249f 85 this->pinIntMask->unMaskIntF();
nleoni 0:afa153f7249f 86 }