Improvement to the DebounceInt library providing an easier to use interface much more alike the usual InterrupIn interface

Fork of DebounceInt by napoleon leoni

Committer:
nleoni
Date:
Tue Feb 25 06:37:35 2014 +0000
Revision:
2:bd5a3171ebc4
Parent:
1:3e9de727a215
Child:
3:3b5c7faadd5c
Compiles. Fixed types for function pointers

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nleoni 1:3e9de727a215 1 /* DebounceInt2 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 1:3e9de727a215 9 // Declare a DebounceInt2 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 1:3e9de727a215 14 #include "DebounceInt2.h"
nleoni 0:afa153f7249f 15
nleoni 0:afa153f7249f 16 //Default constructor, required but not to be used, will yield error and exit.
nleoni 1:3e9de727a215 17 DebounceInt2::DebounceInt2(){
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 1:3e9de727a215 22 DebounceInt2::DebounceInt2(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 0:afa153f7249f 26 this->setDelay(_DEBOUNCEINTDELAYDEFAULT);
nleoni 0:afa153f7249f 27 }
nleoni 0:afa153f7249f 28
nleoni 0:afa153f7249f 29 //Constructor with PinName and specified delay in micro-seconds
nleoni 1:3e9de727a215 30 DebounceInt2::DebounceInt2(PinName pin,unsigned int delay_us){
nleoni 0:afa153f7249f 31 // Inputs:
nleoni 0:afa153f7249f 32 // PinName pin: Expected Proper Mbed Pin (e.g. p12), program will exit otherwise
nleoni 0:afa153f7249f 33 // unsigned int delay: Expected positive delay, will force delay to be larger
nleoni 0:afa153f7249f 34 // than minimum delay _MINDBOUNCEDELAY
nleoni 0:afa153f7249f 35 this->pinIntMask = new InterruptMask(pin);
nleoni 0:afa153f7249f 36 this->setDelay(delay_us);
nleoni 0:afa153f7249f 37 }
nleoni 0:afa153f7249f 38
nleoni 0:afa153f7249f 39 //method to modify delay for debounce in us
nleoni 1:3e9de727a215 40 void DebounceInt2::setDelay(unsigned int delay_us){
nleoni 0:afa153f7249f 41 // Inputs:
nleoni 0:afa153f7249f 42 // unsigned int delay: Expected positive delay, will force delay to be larger
nleoni 0:afa153f7249f 43 // than minimum delay _MINDBOUNCEDELAY
nleoni 0:afa153f7249f 44 if(delay_us>(_MINDBOUNCEDELAY) ){
nleoni 0:afa153f7249f 45 this->delay=delay_us;
nleoni 0:afa153f7249f 46 } else {
nleoni 0:afa153f7249f 47 this->delay=_MINDBOUNCEDELAY;
nleoni 0:afa153f7249f 48 }
nleoni 0:afa153f7249f 49 }
nleoni 0:afa153f7249f 50
nleoni 1:3e9de727a215 51 //**********************************************************************************************//
nleoni 1:3e9de727a215 52 // void DebounceInt2::attachRise(*fhandler){ //
nleoni 1:3e9de727a215 53 // Inputs: *fhandler(void), pointer to handler function which must be void *fhandler(void) //
nleoni 1:3e9de727a215 54 //**********************************************************************************************//
nleoni 1:3e9de727a215 55
nleoni 2:bd5a3171ebc4 56 void DebounceInt2::rise(t_fhandler fhandler){ //attach interrupt handler for interrupt on rising edge
nleoni 1:3e9de727a215 57
nleoni 1:3e9de727a215 58 if(!(this->intRise)){//only allocate the pointer and Interrupt in once
nleoni 1:3e9de727a215 59 this->intRise= new InterruptIn(this->pin);
nleoni 1:3e9de727a215 60 }
nleoni 1:3e9de727a215 61 this->riseHandler=fhandler;
nleoni 2:bd5a3171ebc4 62 this->intRise->rise(this,&DebounceInt2::debouncePinRise); //This attaches our handler which
nleoni 1:3e9de727a215 63 //includes the debounce functionality
nleoni 1:3e9de727a215 64 }
nleoni 1:3e9de727a215 65
nleoni 1:3e9de727a215 66 void DebounceInt2::disableRise(void){ //rise interrupt handler for rising edge
nleoni 1:3e9de727a215 67 this->pinIntMask->maskIntR();
nleoni 1:3e9de727a215 68 }
nleoni 1:3e9de727a215 69 void DebounceInt2::enableRise(void){ //unmask rise interrupt handler for rising edge
nleoni 1:3e9de727a215 70 this->pinIntMask->unMaskIntR();
nleoni 1:3e9de727a215 71 }
nleoni 2:bd5a3171ebc4 72 void DebounceInt2::fall(t_fhandler fhandler){ //attach interrupt handler for interrupt on falling edge
nleoni 1:3e9de727a215 73
nleoni 1:3e9de727a215 74 if(!(this->intFall)){//only allocate the pointer and Interrupt in once
nleoni 1:3e9de727a215 75 this->intFall= new InterruptIn(this->pin);
nleoni 1:3e9de727a215 76 }
nleoni 1:3e9de727a215 77 this->fallHandler=fhandler;
nleoni 2:bd5a3171ebc4 78 this->intFall->fall(this,&DebounceInt2::debouncePinFall); //This attaches our handler which
nleoni 1:3e9de727a215 79 //includes the debounce functionality
nleoni 1:3e9de727a215 80
nleoni 1:3e9de727a215 81 }
nleoni 1:3e9de727a215 82 void DebounceInt2::disableFall(void){ //mask fall interrupt handler for rising edge
nleoni 1:3e9de727a215 83 this->pinIntMask->maskIntF();
nleoni 1:3e9de727a215 84 }
nleoni 1:3e9de727a215 85 void DebounceInt2::enableFall(void){ //unmask fall interrupt handler for rising edge
nleoni 1:3e9de727a215 86 this->pinIntMask->unMaskIntF();
nleoni 1:3e9de727a215 87 }
nleoni 1:3e9de727a215 88
nleoni 1:3e9de727a215 89
nleoni 0:afa153f7249f 90 //method to be called within Interrupt Service Routine to debounce GPIO pin on rising edge
nleoni 1:3e9de727a215 91 void DebounceInt2::debouncePinRise(void){
nleoni 0:afa153f7249f 92 this->pinIntMask->maskIntR();
nleoni 1:3e9de727a215 93 this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinRise,this->delay);
nleoni 1:3e9de727a215 94 this->riseHandler();
nleoni 0:afa153f7249f 95 }
nleoni 0:afa153f7249f 96
nleoni 0:afa153f7249f 97 //internal callback used to re-enable (unmask) interrupts on rising edge
nleoni 1:3e9de727a215 98 void DebounceInt2::debounceCallbackPinRise(void){
nleoni 0:afa153f7249f 99 this->pinIntMask->ClrInt();
nleoni 0:afa153f7249f 100 this->pinIntMask->unMaskIntR();
nleoni 0:afa153f7249f 101 }
nleoni 0:afa153f7249f 102
nleoni 0:afa153f7249f 103 //method to be called within Interrupt Service Routine to debounce GPIO pin on falling edge
nleoni 1:3e9de727a215 104 void DebounceInt2::debouncePinFall(void){
nleoni 0:afa153f7249f 105 this->pinIntMask->maskIntF();
nleoni 1:3e9de727a215 106 this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinFall,this->delay);
nleoni 1:3e9de727a215 107 this->fallHandler();
nleoni 0:afa153f7249f 108 }
nleoni 0:afa153f7249f 109
nleoni 0:afa153f7249f 110 //internal callback used to re-enable (unmask) interrupts on falling edge
nleoni 1:3e9de727a215 111 void DebounceInt2::debounceCallbackPinFall(void){
nleoni 0:afa153f7249f 112 this->pinIntMask->ClrInt();
nleoni 0:afa153f7249f 113 this->pinIntMask->unMaskIntF();
nleoni 0:afa153f7249f 114 }