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) {}
}
Committer:
Jeej
Date:
Thu Nov 19 15:37:39 2015 +0000
Revision:
23:cd4042093f85
Parent:
22:9733f886810a
Child:
25:2163ebb21aef
Fixed Timeout bug that appeared on NUCLEO-L152RE (don't know about other platforms) after mbed update.; Added the possibility to trigger the callback immediately at the interrupt.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kandangath 20:996ea2fc8d2d 1
kandangath 20:996ea2fc8d2d 2 #ifndef DEBOUNCED_INTERRUPT_H
kandangath 20:996ea2fc8d2d 3 #define DEBOUNCED_INTERRUPT_H
kandangath 20:996ea2fc8d2d 4
kandangath 20:996ea2fc8d2d 5 #include <stdint.h>
kandangath 20:996ea2fc8d2d 6 #include "mbed.h"
kandangath 22:9733f886810a 7 #include "FunctionPointer.h"
kandangath 20:996ea2fc8d2d 8
kandangath 20:996ea2fc8d2d 9 /**
kandangath 20:996ea2fc8d2d 10 typedef enum {
kandangath 20:996ea2fc8d2d 11 IRQ_NONE,
kandangath 20:996ea2fc8d2d 12 IRQ_RISE,
kandangath 20:996ea2fc8d2d 13 IRQ_FALL
kandangath 20:996ea2fc8d2d 14 } gpio_irq_event;
kandangath 20:996ea2fc8d2d 15 **/
kandangath 0:ca5a0fee9f52 16
kandangath 15:948e85b22efe 17 /** A replacement for InterruptIn that debounces the interrupt
kandangath 15:948e85b22efe 18 *
kandangath 20:996ea2fc8d2d 19 * Example:
kandangath 15:948e85b22efe 20 * @code
kandangath 15:948e85b22efe 21 *
kandangath 15:948e85b22efe 22 * #include "DebouncedInterrupt.h"
kandangath 15:948e85b22efe 23 *
Jeej 23:cd4042093f85 24 * DebouncedInterrupt up_button(USER_BUTTON);
kandangath 15:948e85b22efe 25 *
kandangath 15:948e85b22efe 26 * void onUp()
kandangath 15:948e85b22efe 27 * {
kandangath 15:948e85b22efe 28 * // Do Something
kandangath 15:948e85b22efe 29 * }
kandangath 15:948e85b22efe 30 *
kandangath 15:948e85b22efe 31 * int main()
kandangath 15:948e85b22efe 32 * {
Jeej 23:cd4042093f85 33 * // Will immediatly call function and ignore other interrupts until timeout
Jeej 23:cd4042093f85 34 * up_button.attach(&onUp, IRQ_FALL, 1000, true);
Jeej 23:cd4042093f85 35 *
Jeej 23:cd4042093f85 36 * // Will call function only if button has been held for the specified time
Jeej 23:cd4042093f85 37 * //up_button.attach(&onUp, IRQ_FALL, 500, false);
Jeej 23:cd4042093f85 38 *
kandangath 15:948e85b22efe 39 * while(1) {
kandangath 15:948e85b22efe 40 * ...
kandangath 15:948e85b22efe 41 * }
kandangath 15:948e85b22efe 42 * }
kandangath 15:948e85b22efe 43 * @endcode
kandangath 15:948e85b22efe 44 */
kandangath 10:cb16d2957f8d 45 class DebouncedInterrupt {
kandangath 0:ca5a0fee9f52 46 private:
kandangath 7:2d73e219dadf 47 unsigned int _debounce_us;
Jeej 23:cd4042093f85 48 bool _immediate;
Jeej 23:cd4042093f85 49 bool _timeout_expired;
kandangath 10:cb16d2957f8d 50 InterruptIn *_in;
kandangath 14:da09706b92f5 51 DigitalIn *_din;
kandangath 18:e6e9bc6402b7 52 gpio_irq_event _trigger;
Jeej 23:cd4042093f85 53 Timeout *_timeout;
kandangath 7:2d73e219dadf 54
kandangath 13:09b53a088a9c 55 // Diagnostics
kandangath 13:09b53a088a9c 56 volatile unsigned int _bounce_count;
kandangath 13:09b53a088a9c 57 volatile unsigned int _last_bounce_count;
kandangath 13:09b53a088a9c 58
kandangath 3:e4b7033508d1 59 void _onInterrupt(void);
kandangath 7:2d73e219dadf 60 void _callback(void);
kandangath 0:ca5a0fee9f52 61 public:
kandangath 10:cb16d2957f8d 62 DebouncedInterrupt(PinName pin);
kandangath 10:cb16d2957f8d 63 ~DebouncedInterrupt();
kandangath 10:cb16d2957f8d 64
kandangath 10:cb16d2957f8d 65 // Start monitoring the interupt and attach a callback
Jeej 23:cd4042093f85 66 void attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false);
kandangath 21:34b95e1b2bf3 67
kandangath 21:34b95e1b2bf3 68 template<typename T>
Jeej 23:cd4042093f85 69 void attach(T* tptr, void (T::*mptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false) {
kandangath 22:9733f886810a 70 _fAttach.attach(tptr, mptr);
kandangath 22:9733f886810a 71 _last_bounce_count = _bounce_count = 0;
kandangath 22:9733f886810a 72 _debounce_us = 1000*debounce_ms;
kandangath 22:9733f886810a 73 _trigger = trigger;
kandangath 22:9733f886810a 74
kandangath 22:9733f886810a 75 switch(trigger)
kandangath 22:9733f886810a 76 {
kandangath 22:9733f886810a 77 case IRQ_RISE:
kandangath 22:9733f886810a 78 _in->rise(tptr, &DebouncedInterrupt::_onInterrupt);
kandangath 22:9733f886810a 79 break;
kandangath 22:9733f886810a 80 case IRQ_FALL:
kandangath 22:9733f886810a 81 _in->fall(tptr, &DebouncedInterrupt::_onInterrupt);
kandangath 22:9733f886810a 82 break;
kandangath 22:9733f886810a 83 case IRQ_NONE:
kandangath 22:9733f886810a 84 reset(); // Unexpected. Clear callbacks.
kandangath 22:9733f886810a 85 break;
kandangath 22:9733f886810a 86 }
kandangath 22:9733f886810a 87 }
kandangath 10:cb16d2957f8d 88
kandangath 10:cb16d2957f8d 89 // Stop monitoring the interrupt
kandangath 10:cb16d2957f8d 90 void reset();
kandangath 10:cb16d2957f8d 91
kandangath 10:cb16d2957f8d 92
kandangath 7:2d73e219dadf 93 /*
kandangath 13:09b53a088a9c 94 * Get number of bounces
kandangath 13:09b53a088a9c 95 * @return: bounce count
kandangath 7:2d73e219dadf 96 */
kandangath 13:09b53a088a9c 97 unsigned int get_bounce();
kandangath 22:9733f886810a 98 protected:
kandangath 22:9733f886810a 99 // https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/FunctionPointer.h
kandangath 22:9733f886810a 100 FunctionPointer _fAttach;
kandangath 0:ca5a0fee9f52 101 };
kandangath 0:ca5a0fee9f52 102 #endif