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:
23:cd4042093f85
Parent:
22:9733f886810a
Child:
25:2163ebb21aef
--- a/DebouncedInterrupt.h	Tue Feb 25 23:44:37 2014 +0000
+++ b/DebouncedInterrupt.h	Thu Nov 19 15:37:39 2015 +0000
@@ -21,7 +21,7 @@
  *
  * #include "DebouncedInterrupt.h"
  *
- * DebouncedInterrupt up_button(p15);
+ * DebouncedInterrupt up_button(USER_BUTTON);
  * 
  * void onUp()
  * {
@@ -30,7 +30,12 @@
  * 
  * int main()
  * {
- *     up_button.attach(&onUp,IRQ_FALL, 100);
+ *     // 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) {
  *         ...
  *     }
@@ -40,9 +45,12 @@
 class DebouncedInterrupt {
 private:
     unsigned int _debounce_us;
+    bool _immediate;
+    bool _timeout_expired;
     InterruptIn *_in;
     DigitalIn *_din;
     gpio_irq_event _trigger;
+    Timeout *_timeout;
     
     // Diagnostics
     volatile unsigned int _bounce_count;
@@ -55,10 +63,10 @@
     ~DebouncedInterrupt();
     
     // Start monitoring the interupt and attach a callback
-    void attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t& debounce_ms=10);
+    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) {
+    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);
         _last_bounce_count = _bounce_count = 0;
         _debounce_us = 1000*debounce_ms;