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:
24:4e095ebbcfad
Child:
26:2df374d23986
--- a/DebouncedInterrupt.cpp	Thu Nov 19 17:43:15 2015 +0000
+++ b/DebouncedInterrupt.cpp	Wed Apr 26 11:02:18 2017 +0000
@@ -7,7 +7,7 @@
 DebouncedInterrupt::DebouncedInterrupt(PinName pin)
 {
     _in = new InterruptIn(pin);
-    _din = new DigitalIn(pin);
+    _din = new DigitalIn(pin, PullUp);
     _timeout = new Timeout;
 }
 
@@ -31,10 +31,10 @@
         switch(trigger)
         {
             case IRQ_RISE:
-                _in->rise(this, &DebouncedInterrupt::_onInterrupt);
+                _in->rise(callback(this, &DebouncedInterrupt::_onInterrupt));
                 break;
             case IRQ_FALL:
-                _in->fall(this, &DebouncedInterrupt::_onInterrupt);
+                _in->fall(callback(this, &DebouncedInterrupt::_onInterrupt));
                 break;
             case IRQ_NONE:
                 reset(); // Unexpected. Clear callbacks.
@@ -76,10 +76,10 @@
         if (_timeout_expired) {
             _timeout_expired = false;
             _fAttach.call();
-            _timeout->attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
+            _timeout->attach_us(callback(this, &DebouncedInterrupt::_callback), _debounce_us);
         }
     }
     else {
-        _timeout->attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
+        _timeout->attach_us(callback(this, &DebouncedInterrupt::_callback), _debounce_us);
     }
 }