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 07:00:07 2014 +0000
Revision:
7:2d73e219dadf
Parent:
6:e1461ccdf3c0
Child:
8:4b3ff16d5f91
Add counter for debounced interrupts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kandangath 0:ca5a0fee9f52 1 #include "DebounceInterrupts.h"
kandangath 0:ca5a0fee9f52 2
kandangath 0:ca5a0fee9f52 3 Timeout timeout;
kandangath 0:ca5a0fee9f52 4
kandangath 0:ca5a0fee9f52 5 DebounceInterrupts::DebounceInterrupts(void (*fptr)(void),
kandangath 0:ca5a0fee9f52 6 InterruptIn *interruptIn,
kandangath 4:19689187352e 7 const interruptTrigger& trigger,
kandangath 0:ca5a0fee9f52 8 const unsigned int& debounce_ms)
kandangath 0:ca5a0fee9f52 9 {
kandangath 0:ca5a0fee9f52 10 fCallback = fptr;
kandangath 7:2d73e219dadf 11 _debounce_count = 0;
kandangath 4:19689187352e 12
kandangath 4:19689187352e 13 switch(trigger) {
kandangath 4:19689187352e 14 case INT_RISE:
kandangath 3:e4b7033508d1 15 interruptIn->rise(this, &DebounceInterrupts::_onInterrupt);
kandangath 4:19689187352e 16 break;
kandangath 4:19689187352e 17 case INT_FALL:
kandangath 3:e4b7033508d1 18 interruptIn->fall(this, &DebounceInterrupts::_onInterrupt);
kandangath 4:19689187352e 19 break;
kandangath 4:19689187352e 20 default:
kandangath 4:19689187352e 21 break;
kandangath 0:ca5a0fee9f52 22 }
kandangath 4:19689187352e 23
kandangath 7:2d73e219dadf 24 _debounce_us = 1000*debounce_ms;
kandangath 0:ca5a0fee9f52 25 }
kandangath 0:ca5a0fee9f52 26
kandangath 0:ca5a0fee9f52 27 DebounceInterrupts::~DebounceInterrupts()
kandangath 0:ca5a0fee9f52 28 {
kandangath 0:ca5a0fee9f52 29 }
kandangath 0:ca5a0fee9f52 30
kandangath 7:2d73e219dadf 31 void DebounceInterrupts::_callback()
kandangath 7:2d73e219dadf 32 {
kandangath 7:2d73e219dadf 33 _debounce_count = 0;
kandangath 7:2d73e219dadf 34 fCallback();
kandangath 7:2d73e219dadf 35 }
kandangath 7:2d73e219dadf 36
kandangath 3:e4b7033508d1 37 void DebounceInterrupts::_onInterrupt()
kandangath 0:ca5a0fee9f52 38 {
kandangath 7:2d73e219dadf 39 _debounce_count++;
kandangath 7:2d73e219dadf 40 timeout.attach_us(this, &DebounceInterrupts::_callback, _debounce_us);
kandangath 7:2d73e219dadf 41 }
kandangath 7:2d73e219dadf 42
kandangath 7:2d73e219dadf 43 unsigned int DebounceInterrupts::get_debounce()
kandangath 7:2d73e219dadf 44 {
kandangath 7:2d73e219dadf 45 return _debounce_count;
kandangath 7:2d73e219dadf 46 }