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 16:32:33 2014 +0000
Revision:
10:cb16d2957f8d
Parent:
DebounceInterrupts.cpp@9:970b74eb0407
Child:
13:09b53a088a9c
Allow DebouncedInterrupt to be set with just the PinName

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kandangath 10:cb16d2957f8d 1 /**
kandangath 10:cb16d2957f8d 2 * DebouncedInterrupt.cpp
kandangath 10:cb16d2957f8d 3 **/
kandangath 10:cb16d2957f8d 4 #include "DebouncedInterrupt.h"
kandangath 0:ca5a0fee9f52 5
kandangath 0:ca5a0fee9f52 6 Timeout timeout;
kandangath 0:ca5a0fee9f52 7
kandangath 10:cb16d2957f8d 8 DebouncedInterrupt::DebouncedInterrupt(PinName pin)
kandangath 10:cb16d2957f8d 9 {
kandangath 10:cb16d2957f8d 10 _in = new InterruptIn(pin);
kandangath 10:cb16d2957f8d 11 }
kandangath 10:cb16d2957f8d 12
kandangath 10:cb16d2957f8d 13 DebouncedInterrupt::~DebouncedInterrupt()
kandangath 10:cb16d2957f8d 14 {
kandangath 10:cb16d2957f8d 15 }
kandangath 10:cb16d2957f8d 16
kandangath 10:cb16d2957f8d 17 void DebouncedInterrupt::attach(void (*fptr)(void),
kandangath 4:19689187352e 18 const interruptTrigger& trigger,
kandangath 0:ca5a0fee9f52 19 const unsigned int& debounce_ms)
kandangath 0:ca5a0fee9f52 20 {
kandangath 10:cb16d2957f8d 21 if(fptr) {
kandangath 10:cb16d2957f8d 22 fCallback = fptr;
kandangath 10:cb16d2957f8d 23 _last_debounce_count = _debounce_count = 0;
kandangath 10:cb16d2957f8d 24
kandangath 10:cb16d2957f8d 25 switch(trigger) {
kandangath 10:cb16d2957f8d 26 case INT_RISE:
kandangath 10:cb16d2957f8d 27 _in->rise(this, &DebouncedInterrupt::_onInterrupt);
kandangath 10:cb16d2957f8d 28 break;
kandangath 10:cb16d2957f8d 29 case INT_FALL:
kandangath 10:cb16d2957f8d 30 _in->fall(this, &DebouncedInterrupt::_onInterrupt);
kandangath 10:cb16d2957f8d 31 break;
kandangath 10:cb16d2957f8d 32 default:
kandangath 10:cb16d2957f8d 33 break;
kandangath 10:cb16d2957f8d 34 }
kandangath 4:19689187352e 35
kandangath 10:cb16d2957f8d 36 _debounce_us = 1000*debounce_ms;
kandangath 0:ca5a0fee9f52 37 }
kandangath 0:ca5a0fee9f52 38 }
kandangath 0:ca5a0fee9f52 39
kandangath 10:cb16d2957f8d 40 void DebouncedInterrupt::reset()
kandangath 0:ca5a0fee9f52 41 {
kandangath 10:cb16d2957f8d 42 timeout.detach();
kandangath 0:ca5a0fee9f52 43 }
kandangath 0:ca5a0fee9f52 44
kandangath 10:cb16d2957f8d 45 unsigned int DebouncedInterrupt::get_debounce()
kandangath 10:cb16d2957f8d 46 {
kandangath 10:cb16d2957f8d 47 return _last_debounce_count;
kandangath 10:cb16d2957f8d 48 }
kandangath 10:cb16d2957f8d 49
kandangath 10:cb16d2957f8d 50 void DebouncedInterrupt::_callback()
kandangath 7:2d73e219dadf 51 {
kandangath 8:4b3ff16d5f91 52 _last_debounce_count = _debounce_count;
kandangath 7:2d73e219dadf 53 _debounce_count = 0;
kandangath 7:2d73e219dadf 54 fCallback();
kandangath 7:2d73e219dadf 55 }
kandangath 7:2d73e219dadf 56
kandangath 10:cb16d2957f8d 57 void DebouncedInterrupt::_onInterrupt()
kandangath 0:ca5a0fee9f52 58 {
kandangath 7:2d73e219dadf 59 _debounce_count++;
kandangath 10:cb16d2957f8d 60 timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
kandangath 7:2d73e219dadf 61 }
kandangath 7:2d73e219dadf 62