A replacement for InterruptIn that debounces the interrupt.

Dependents:   D7A_Demo-Get-started CVtoOSCConverter EE3501keypad D7A_Localisation ... more

Fork of DebouncedInterrupt by Anil Kandangath

Example code:

#include "DebouncedInterrupt.h"

DebouncedInterrupt up_button(USER_BUTTON);

void onUp()
{
    // Do Something
}

int main()
{
    // Will immediatly call function and ignore other interrupts until timeout
    up_button.attach(&onUp, IRQ_FALL, 1000, true);

    // Will call function only if button has been held for the specified time
    //up_button.attach(&onUp, IRQ_FALL, 500, false);

    while(1) {}
}
Revision:
25:2163ebb21aef
Parent:
23:cd4042093f85
Child:
26:2df374d23986
--- a/DebouncedInterrupt.h	Thu Nov 19 17:43:15 2015 +0000
+++ b/DebouncedInterrupt.h	Wed Apr 26 11:02:18 2017 +0000
@@ -4,7 +4,6 @@
 
 #include <stdint.h>
 #include "mbed.h"
-#include "FunctionPointer.h"
 
 /**
 typedef enum {
@@ -65,9 +64,9 @@
     // Start monitoring the interupt and attach a callback
     void attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false);
     
-    template<typename T>
-    void attach(T* tptr, void (T::*mptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false) {
-        _fAttach.attach(tptr, mptr);
+    template<typename T, typename M>
+    void attach(T *obj, M method, const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false) {
+        _fAttach.attach(callback(obj, method));
         _last_bounce_count = _bounce_count = 0;
         _debounce_us = 1000*debounce_ms;
         _trigger = trigger;
@@ -75,10 +74,10 @@
         switch(trigger)
         {
             case IRQ_RISE:
-                _in->rise(tptr, &DebouncedInterrupt::_onInterrupt);
+                _in->rise(callback(this, &DebouncedInterrupt::_onInterrupt));
                 break;
             case IRQ_FALL:
-                _in->fall(tptr, &DebouncedInterrupt::_onInterrupt);
+                _in->fall(callback(this, &DebouncedInterrupt::_onInterrupt));
                 break;
             case IRQ_NONE:
                 reset(); // Unexpected. Clear callbacks.
@@ -97,6 +96,6 @@
     unsigned int get_bounce();
 protected:
 //    https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/FunctionPointer.h
-    FunctionPointer _fAttach;
+    Callback<void()> _fAttach;
 };
 #endif
\ No newline at end of file