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:
kandangath
Date:
Fri Feb 21 16:50:56 2014 +0000
Revision:
20:996ea2fc8d2d
Parent:
18:e6e9bc6402b7
Child:
21:34b95e1b2bf3
Updated Documentation

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 20:996ea2fc8d2d 7
kandangath 20:996ea2fc8d2d 8 /**
kandangath 20:996ea2fc8d2d 9 typedef enum {
kandangath 20:996ea2fc8d2d 10 IRQ_NONE,
kandangath 20:996ea2fc8d2d 11 IRQ_RISE,
kandangath 20:996ea2fc8d2d 12 IRQ_FALL
kandangath 20:996ea2fc8d2d 13 } gpio_irq_event;
kandangath 20:996ea2fc8d2d 14 **/
kandangath 0:ca5a0fee9f52 15
kandangath 15:948e85b22efe 16 /** A replacement for InterruptIn that debounces the interrupt
kandangath 15:948e85b22efe 17 *
kandangath 20:996ea2fc8d2d 18 * Example:
kandangath 15:948e85b22efe 19 * @code
kandangath 15:948e85b22efe 20 *
kandangath 15:948e85b22efe 21 * #include "DebouncedInterrupt.h"
kandangath 15:948e85b22efe 22 *
kandangath 15:948e85b22efe 23 * DebouncedInterrupt up_button(p15);
kandangath 15:948e85b22efe 24 *
kandangath 15:948e85b22efe 25 * void onUp()
kandangath 15:948e85b22efe 26 * {
kandangath 15:948e85b22efe 27 * // Do Something
kandangath 15:948e85b22efe 28 * }
kandangath 15:948e85b22efe 29 *
kandangath 15:948e85b22efe 30 * int main()
kandangath 15:948e85b22efe 31 * {
kandangath 18:e6e9bc6402b7 32 * up_button.attach(&onUp,IRQ_FALL, 100);
kandangath 15:948e85b22efe 33 * while(1) {
kandangath 15:948e85b22efe 34 * ...
kandangath 15:948e85b22efe 35 * }
kandangath 15:948e85b22efe 36 * }
kandangath 15:948e85b22efe 37 * @endcode
kandangath 15:948e85b22efe 38 */
kandangath 10:cb16d2957f8d 39 class DebouncedInterrupt {
kandangath 0:ca5a0fee9f52 40 private:
kandangath 7:2d73e219dadf 41 unsigned int _debounce_us;
kandangath 10:cb16d2957f8d 42 InterruptIn *_in;
kandangath 14:da09706b92f5 43 DigitalIn *_din;
kandangath 18:e6e9bc6402b7 44 gpio_irq_event _trigger;
kandangath 7:2d73e219dadf 45
kandangath 13:09b53a088a9c 46 // Diagnostics
kandangath 13:09b53a088a9c 47 volatile unsigned int _bounce_count;
kandangath 13:09b53a088a9c 48 volatile unsigned int _last_bounce_count;
kandangath 13:09b53a088a9c 49
kandangath 0:ca5a0fee9f52 50 void (*fCallback)(void);
kandangath 3:e4b7033508d1 51 void _onInterrupt(void);
kandangath 7:2d73e219dadf 52 void _callback(void);
kandangath 0:ca5a0fee9f52 53 public:
kandangath 10:cb16d2957f8d 54 DebouncedInterrupt(PinName pin);
kandangath 10:cb16d2957f8d 55 ~DebouncedInterrupt();
kandangath 10:cb16d2957f8d 56
kandangath 10:cb16d2957f8d 57 // Start monitoring the interupt and attach a callback
kandangath 18:e6e9bc6402b7 58 void attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t& debounce_ms=10);
kandangath 10:cb16d2957f8d 59
kandangath 10:cb16d2957f8d 60 // Stop monitoring the interrupt
kandangath 10:cb16d2957f8d 61 void reset();
kandangath 10:cb16d2957f8d 62
kandangath 10:cb16d2957f8d 63
kandangath 7:2d73e219dadf 64 /*
kandangath 13:09b53a088a9c 65 * Get number of bounces
kandangath 13:09b53a088a9c 66 * @return: bounce count
kandangath 7:2d73e219dadf 67 */
kandangath 13:09b53a088a9c 68 unsigned int get_bounce();
kandangath 0:ca5a0fee9f52 69 };
kandangath 0:ca5a0fee9f52 70 #endif