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:
26:2df374d23986
Parent:
25:2163ebb21aef
--- a/DebouncedInterrupt.cpp	Wed Apr 26 11:02:18 2017 +0000
+++ b/DebouncedInterrupt.cpp	Thu Apr 27 16:06:12 2017 +0000
@@ -21,7 +21,7 @@
 void DebouncedInterrupt::attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms, bool immediate)
 {
     if(fptr) {
-        _fAttach.attach(fptr);
+        _callback = callback(fptr);
         _last_bounce_count = _bounce_count = 0;
         _debounce_us = 1000*debounce_ms;
         _trigger = trigger;
@@ -56,13 +56,13 @@
     return _last_bounce_count;
 }
 
-void DebouncedInterrupt::_callback()
+void DebouncedInterrupt::_onCallback()
 {
     _last_bounce_count = _bounce_count;
     _bounce_count = 0;
     if (!_immediate) {
         if (_din->read() == (_trigger==IRQ_RISE)) {
-            _fAttach.call();
+            _callback.call();
         }
     }
     _timeout_expired = true;
@@ -75,11 +75,11 @@
     if (_immediate) {
         if (_timeout_expired) {
             _timeout_expired = false;
-            _fAttach.call();
-            _timeout->attach_us(callback(this, &DebouncedInterrupt::_callback), _debounce_us);
+            _callback.call();
+            _timeout->attach_us(callback(this, &DebouncedInterrupt::_onCallback), _debounce_us);
         }
     }
     else {
-        _timeout->attach_us(callback(this, &DebouncedInterrupt::_callback), _debounce_us);
+        _timeout->attach_us(callback(this, &DebouncedInterrupt::_onCallback), _debounce_us);
     }
 }