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:
Tue Feb 18 20:38:50 2014 +0000
Revision:
17:96a51b236ba0
Parent:
16:7eaa188de0f9
Child:
18:e6e9bc6402b7
Monitor both rising and falling edges

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kandangath 0:ca5a0fee9f52 1
kandangath 15:948e85b22efe 2 /** A replacement for InterruptIn that debounces the interrupt
kandangath 15:948e85b22efe 3 * Anil Kandangath
kandangath 15:948e85b22efe 4 *
kandangath 15:948e85b22efe 5 * @code
kandangath 15:948e85b22efe 6 *
kandangath 15:948e85b22efe 7 * #include "DebouncedInterrupt.h"
kandangath 15:948e85b22efe 8 *
kandangath 15:948e85b22efe 9 * DebouncedInterrupt up_button(p15);
kandangath 15:948e85b22efe 10 *
kandangath 15:948e85b22efe 11 * void onUp()
kandangath 15:948e85b22efe 12 * {
kandangath 15:948e85b22efe 13 * // Do Something
kandangath 15:948e85b22efe 14 * }
kandangath 15:948e85b22efe 15 *
kandangath 15:948e85b22efe 16 * int main()
kandangath 15:948e85b22efe 17 * {
kandangath 17:96a51b236ba0 18 * up_button.attach(&onUp, 100);
kandangath 15:948e85b22efe 19 * while(1) {
kandangath 15:948e85b22efe 20 * ...
kandangath 15:948e85b22efe 21 * }
kandangath 15:948e85b22efe 22 * }
kandangath 15:948e85b22efe 23 * @endcode
kandangath 15:948e85b22efe 24 */
kandangath 0:ca5a0fee9f52 25
kandangath 10:cb16d2957f8d 26 #ifndef DEBOUNCED_INTERRUPT_H
kandangath 10:cb16d2957f8d 27 #define DEBOUNCED_INTERRUPT_H
kandangath 0:ca5a0fee9f52 28
kandangath 0:ca5a0fee9f52 29 #include <stdint.h>
kandangath 0:ca5a0fee9f52 30 #include "mbed.h"
kandangath 0:ca5a0fee9f52 31
kandangath 10:cb16d2957f8d 32 class DebouncedInterrupt {
kandangath 0:ca5a0fee9f52 33 private:
kandangath 7:2d73e219dadf 34 unsigned int _debounce_us;
kandangath 10:cb16d2957f8d 35 InterruptIn *_in;
kandangath 14:da09706b92f5 36 DigitalIn *_din;
kandangath 7:2d73e219dadf 37
kandangath 13:09b53a088a9c 38 // Diagnostics
kandangath 13:09b53a088a9c 39 volatile unsigned int _bounce_count;
kandangath 13:09b53a088a9c 40 volatile unsigned int _last_bounce_count;
kandangath 13:09b53a088a9c 41
kandangath 0:ca5a0fee9f52 42 void (*fCallback)(void);
kandangath 3:e4b7033508d1 43 void _onInterrupt(void);
kandangath 7:2d73e219dadf 44 void _callback(void);
kandangath 0:ca5a0fee9f52 45 public:
kandangath 10:cb16d2957f8d 46 DebouncedInterrupt(PinName pin);
kandangath 10:cb16d2957f8d 47 ~DebouncedInterrupt();
kandangath 10:cb16d2957f8d 48
kandangath 10:cb16d2957f8d 49 // Start monitoring the interupt and attach a callback
kandangath 17:96a51b236ba0 50 void attach(void (*fptr)(void), const uint32_t& debounce_ms=10);
kandangath 10:cb16d2957f8d 51
kandangath 10:cb16d2957f8d 52 // Stop monitoring the interrupt
kandangath 10:cb16d2957f8d 53 void reset();
kandangath 10:cb16d2957f8d 54
kandangath 10:cb16d2957f8d 55
kandangath 7:2d73e219dadf 56 /*
kandangath 13:09b53a088a9c 57 * Get number of bounces
kandangath 13:09b53a088a9c 58 * @return: bounce count
kandangath 7:2d73e219dadf 59 */
kandangath 13:09b53a088a9c 60 unsigned int get_bounce();
kandangath 0:ca5a0fee9f52 61 };
kandangath 0:ca5a0fee9f52 62 #endif