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

Fork of DebounceInt by napoleon leoni

Files at this revision

API Documentation at this revision

Comitter:
nleoni
Date:
Tue Feb 25 07:53:23 2014 +0000
Parent:
2:bd5a3171ebc4
Commit message:
Added class method support

Changed in this revision

DebounceInt2.cpp Show annotated file Show diff for this revision Revisions of this file
DebounceInt2.h Show annotated file Show diff for this revision Revisions of this file
--- a/DebounceInt2.cpp	Tue Feb 25 06:37:35 2014 +0000
+++ b/DebounceInt2.cpp	Tue Feb 25 07:53:23 2014 +0000
@@ -53,15 +53,25 @@
 // Inputs: *fhandler(void), pointer to handler function which must be void *fhandler(void)                                                                               //
 //**********************************************************************************************//
 
-    void DebounceInt2::rise(t_fhandler fhandler){     //attach interrupt handler for interrupt on rising edge
+    void DebounceInt2::rise(FunctionPointer fhandler){     //attach interrupt handler for interrupt on rising edge
     
         if(!(this->intRise)){//only allocate the pointer and Interrupt in once
             this->intRise= new InterruptIn(this->pin);
         }
-        this->riseHandler=fhandler;
+        this->riseHandler = new FunctionPointer(fhandler);
         this->intRise->rise(this,&DebounceInt2::debouncePinRise); //This attaches our handler which 
                                                                     //includes the debounce functionality                                                                
     }
+
+    template<typename T>
+    void rise(T* tptr, void (T::*mptr)(void)){
+        if(!(this->intRise)){//only allocate the pointer and Interrupt in once
+            this->intRise= new InterruptIn(this->pin);
+        }
+        this->riseHandler = new FunctionPointer(tptr,mptr);
+        this->intRise->rise(this,&DebounceInt2::debouncePinRise);
+    }
+
     
     void DebounceInt2::disableRise(void){         //rise interrupt handler for rising edge
         this->pinIntMask->maskIntR();
@@ -69,16 +79,27 @@
     void DebounceInt2::enableRise(void){          //unmask rise interrupt handler for rising edge
         this->pinIntMask->unMaskIntR();    
     }
-    void DebounceInt2::fall(t_fhandler fhandler){     //attach interrupt handler for interrupt on falling edge
+
+    void DebounceInt2::fall(FunctionPointer fhandler){     //attach interrupt handler for interrupt on falling edge
     
         if(!(this->intFall)){//only allocate the pointer and Interrupt in once
             this->intFall= new InterruptIn(this->pin);
         }
-        this->fallHandler=fhandler;
+        this->fallHandler = new FunctionPointer(fhandler);
         this->intFall->fall(this,&DebounceInt2::debouncePinFall); //This attaches our handler which 
                                                                     //includes the debounce functionality                                                                
     
     }
+
+    template<typename T>
+    void fall(T* tptr, void (T::*mptr)(void)){
+        if(!(this->Fall)){//only allocate the pointer and Interrupt in once
+            this->intFall= new InterruptIn(this->pin);
+        }
+        this->fallHandler = new FunctionPointer(tptr,mptr);
+        this->intFall->fall(this,&DebounceInt2::debouncePinRise);
+    }
+
     void DebounceInt2::disableFall(void){         //mask fall interrupt handler for rising edge
         this->pinIntMask->maskIntF();    
     }
@@ -91,7 +112,7 @@
     void DebounceInt2::debouncePinRise(void){
         this->pinIntMask->maskIntR();
         this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinRise,this->delay);
-        this->riseHandler();
+        this->riseHandler->call();
     }
     
 //internal callback used to re-enable (unmask) interrupts on rising edge
@@ -104,7 +125,7 @@
     void DebounceInt2::debouncePinFall(void){
         this->pinIntMask->maskIntF();
         this->debounceCallBack.attach_us(this,&DebounceInt2::debounceCallbackPinFall,this->delay);
-        this->fallHandler();
+        this->fallHandler->call();
     }
     
 //internal callback used to re-enable (unmask) interrupts on falling edge
--- a/DebounceInt2.h	Tue Feb 25 06:37:35 2014 +0000
+++ b/DebounceInt2.h	Tue Feb 25 07:53:23 2014 +0000
@@ -43,7 +43,7 @@
 #define _DEBOUNCEINTDELAYDEFAULT 50000 //useconds, this number would be about 50 ms
 #define _MINDBOUNCEDELAY 50
 
-typedef void (*t_fhandler)(void);
+//typedef void (*t_fhandler)(void);
 
 class DebounceInt2 
 {
@@ -55,7 +55,7 @@
     InterruptIn* intRise;
     InterruptIn* intFall;
     bool riseAttached,fallAttached;
-    t_fhandler riseHandler,fallHandler;
+    FunctionPointer *riseHandler,*fallHandler;
     PinName pin;
 
 public://methods
@@ -63,10 +63,14 @@
     DebounceInt2(PinName pin);//Constructor with PinName, delay will remain default value
     DebounceInt2(PinName pin,unsigned int delay_us);//Constructor with PinName and specified delay in micro-seconds
     void setDelay(unsigned int); //method to modify delay for debounce in us
-    void rise(t_fhandler);     //attach interrupt handler for interrupt on rising edge
+    void rise(FunctionPointer);     //attach interrupt handler for interrupt on rising edge
+    template<typename T>
+    void rise(T* tptr, void (T::*mptr)(void));
     void disableRise(void);         //rise interrupt handler for rising edge
     void enableRise(void);          //unmask rise interrupt handler for rising edge
-    void fall(t_fhandler);     //attach interrupt handler for interrupt on falling edge
+    void fall(FunctionPointer);          //attach interrupt handler for interrupt on falling edge
+    template<typename T>
+    void fall(T* tptr, void (T::*mptr)(void));
     void disableFall(void);         //mask fall interrupt handler for rising edge
     void enableFall(void);          //unmask fall interrupt handler for rising edge