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:06:32 2014 +0000
Revision:
9:970b74eb0407
Parent:
8:4b3ff16d5f91
Oops, uninitialized variable! Not good news.

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 9:970b74eb0407 11 _last_debounce_count = _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 8:4b3ff16d5f91 33 _last_debounce_count = _debounce_count;
kandangath 7:2d73e219dadf 34 _debounce_count = 0;
kandangath 7:2d73e219dadf 35 fCallback();
kandangath 7:2d73e219dadf 36 }
kandangath 7:2d73e219dadf 37
kandangath 3:e4b7033508d1 38 void DebounceInterrupts::_onInterrupt()
kandangath 0:ca5a0fee9f52 39 {
kandangath 7:2d73e219dadf 40 _debounce_count++;
kandangath 7:2d73e219dadf 41 timeout.attach_us(this, &DebounceInterrupts::_callback, _debounce_us);
kandangath 7:2d73e219dadf 42 }
kandangath 7:2d73e219dadf 43
kandangath 7:2d73e219dadf 44 unsigned int DebounceInterrupts::get_debounce()
kandangath 7:2d73e219dadf 45 {
kandangath 8:4b3ff16d5f91 46 return _last_debounce_count;
kandangath 7:2d73e219dadf 47 }