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 17:26:42 2014 +0000
Revision:
14:da09706b92f5
Parent:
13:09b53a088a9c
Child:
15:948e85b22efe
Also poll pin value at the end of the debounce period

Who changed what in which revision?

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