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:
17:96a51b236ba0
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.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 14:da09706b92f5 11 _din = new DigitalIn(pin);
kandangath 10:cb16d2957f8d 12 }
kandangath 10:cb16d2957f8d 13
kandangath 10:cb16d2957f8d 14 DebouncedInterrupt::~DebouncedInterrupt()
kandangath 10:cb16d2957f8d 15 {
kandangath 14:da09706b92f5 16 delete _in;
kandangath 14:da09706b92f5 17 delete _din;
kandangath 10:cb16d2957f8d 18 }
kandangath 10:cb16d2957f8d 19
kandangath 10:cb16d2957f8d 20 void DebouncedInterrupt::attach(void (*fptr)(void),
kandangath 4:19689187352e 21 const interruptTrigger& trigger,
kandangath 0:ca5a0fee9f52 22 const unsigned int& debounce_ms)
kandangath 0:ca5a0fee9f52 23 {
kandangath 10:cb16d2957f8d 24 if(fptr) {
kandangath 10:cb16d2957f8d 25 fCallback = fptr;
kandangath 13:09b53a088a9c 26 _last_bounce_count = _bounce_count = 0;
kandangath 10:cb16d2957f8d 27
kandangath 10:cb16d2957f8d 28 switch(trigger) {
kandangath 10:cb16d2957f8d 29 case INT_RISE:
kandangath 10:cb16d2957f8d 30 _in->rise(this, &DebouncedInterrupt::_onInterrupt);
kandangath 10:cb16d2957f8d 31 break;
kandangath 10:cb16d2957f8d 32 case INT_FALL:
kandangath 10:cb16d2957f8d 33 _in->fall(this, &DebouncedInterrupt::_onInterrupt);
kandangath 10:cb16d2957f8d 34 break;
kandangath 10:cb16d2957f8d 35 default:
kandangath 10:cb16d2957f8d 36 break;
kandangath 10:cb16d2957f8d 37 }
kandangath 4:19689187352e 38
kandangath 10:cb16d2957f8d 39 _debounce_us = 1000*debounce_ms;
kandangath 0:ca5a0fee9f52 40 }
kandangath 0:ca5a0fee9f52 41 }
kandangath 0:ca5a0fee9f52 42
kandangath 10:cb16d2957f8d 43 void DebouncedInterrupt::reset()
kandangath 0:ca5a0fee9f52 44 {
kandangath 10:cb16d2957f8d 45 timeout.detach();
kandangath 0:ca5a0fee9f52 46 }
kandangath 0:ca5a0fee9f52 47
kandangath 13:09b53a088a9c 48 unsigned int DebouncedInterrupt::get_bounce()
kandangath 10:cb16d2957f8d 49 {
kandangath 13:09b53a088a9c 50 return _last_bounce_count;
kandangath 10:cb16d2957f8d 51 }
kandangath 10:cb16d2957f8d 52
kandangath 10:cb16d2957f8d 53 void DebouncedInterrupt::_callback()
kandangath 7:2d73e219dadf 54 {
kandangath 13:09b53a088a9c 55 _last_bounce_count = _bounce_count;
kandangath 13:09b53a088a9c 56 _bounce_count = 0;
kandangath 14:da09706b92f5 57 if(_din) {
kandangath 14:da09706b92f5 58 fCallback();
kandangath 14:da09706b92f5 59 }
kandangath 7:2d73e219dadf 60 }
kandangath 7:2d73e219dadf 61
kandangath 10:cb16d2957f8d 62 void DebouncedInterrupt::_onInterrupt()
kandangath 0:ca5a0fee9f52 63 {
kandangath 13:09b53a088a9c 64 _bounce_count++;
kandangath 10:cb16d2957f8d 65 timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
kandangath 7:2d73e219dadf 66 }
kandangath 7:2d73e219dadf 67