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) {}
}

Files at this revision

API Documentation at this revision

Comitter:
Jeej
Date:
Thu Apr 27 16:06:12 2017 +0000
Parent:
25:2163ebb21aef
Commit message:
Updated deprecated callback.

Changed in this revision

DebouncedInterrupt.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedInterrupt.h Show annotated file Show diff for this revision Revisions of this file
diff -r 2163ebb21aef -r 2df374d23986 DebouncedInterrupt.cpp
--- 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);
     }
 }
diff -r 2163ebb21aef -r 2df374d23986 DebouncedInterrupt.h
--- a/DebouncedInterrupt.h	Wed Apr 26 11:02:18 2017 +0000
+++ b/DebouncedInterrupt.h	Thu Apr 27 16:06:12 2017 +0000
@@ -56,7 +56,7 @@
     volatile unsigned int _last_bounce_count;
     
     void _onInterrupt(void);
-    void _callback(void);
+    void _onCallback(void);
 public:
     DebouncedInterrupt(PinName pin);
     ~DebouncedInterrupt();
@@ -66,7 +66,7 @@
     
     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));
+        _callback = callback(obj, method);
         _last_bounce_count = _bounce_count = 0;
         _debounce_us = 1000*debounce_ms;
         _trigger = trigger;
@@ -96,6 +96,6 @@
     unsigned int get_bounce();
 protected:
 //    https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/FunctionPointer.h
-    Callback<void()> _fAttach;
+    Callback<void()> _callback;
 };
 #endif
\ No newline at end of file