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:
Wed Feb 19 01:47:45 2014 +0000
Revision:
18:e6e9bc6402b7
Parent:
17:96a51b236ba0
Child:
19:d12a5b89ac3a
Support checking rise and fall of signals

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 18:e6e9bc6402b7 20 void DebouncedInterrupt::attach(void (*fptr)(void), const gpio_irq_event trigger, const unsigned int& debounce_ms)
kandangath 0:ca5a0fee9f52 21 {
kandangath 10:cb16d2957f8d 22 if(fptr) {
kandangath 10:cb16d2957f8d 23 fCallback = fptr;
kandangath 13:09b53a088a9c 24 _last_bounce_count = _bounce_count = 0;
kandangath 17:96a51b236ba0 25 _debounce_us = 1000*debounce_ms;
kandangath 18:e6e9bc6402b7 26 _trigger = trigger;
kandangath 10:cb16d2957f8d 27
kandangath 18:e6e9bc6402b7 28 switch(trigger)
kandangath 18:e6e9bc6402b7 29 {
kandangath 18:e6e9bc6402b7 30 case IRQ_RISE:
kandangath 18:e6e9bc6402b7 31 _in->rise(this, &DebouncedInterrupt::_onInterrupt);
kandangath 18:e6e9bc6402b7 32 break;
kandangath 18:e6e9bc6402b7 33 case IRQ_FALL:
kandangath 18:e6e9bc6402b7 34 _in->fall(this, &DebouncedInterrupt::_onInterrupt);
kandangath 18:e6e9bc6402b7 35 break;
kandangath 18:e6e9bc6402b7 36 case IRQ_NONE:
kandangath 18:e6e9bc6402b7 37 reset(); // Unexpected. Clear callbacks.
kandangath 18:e6e9bc6402b7 38 break;
kandangath 18:e6e9bc6402b7 39 }
kandangath 18:e6e9bc6402b7 40 } else {
kandangath 18:e6e9bc6402b7 41 reset();
kandangath 0:ca5a0fee9f52 42 }
kandangath 0:ca5a0fee9f52 43 }
kandangath 0:ca5a0fee9f52 44
kandangath 10:cb16d2957f8d 45 void DebouncedInterrupt::reset()
kandangath 0:ca5a0fee9f52 46 {
kandangath 10:cb16d2957f8d 47 timeout.detach();
kandangath 0:ca5a0fee9f52 48 }
kandangath 0:ca5a0fee9f52 49
kandangath 13:09b53a088a9c 50 unsigned int DebouncedInterrupt::get_bounce()
kandangath 10:cb16d2957f8d 51 {
kandangath 13:09b53a088a9c 52 return _last_bounce_count;
kandangath 10:cb16d2957f8d 53 }
kandangath 10:cb16d2957f8d 54
kandangath 10:cb16d2957f8d 55 void DebouncedInterrupt::_callback()
kandangath 7:2d73e219dadf 56 {
kandangath 13:09b53a088a9c 57 _last_bounce_count = _bounce_count;
kandangath 13:09b53a088a9c 58 _bounce_count = 0;
kandangath 18:e6e9bc6402b7 59 unsigned int expected_state = (_trigger==IRQ_RISE)?1:0;
kandangath 18:e6e9bc6402b7 60 if(_din->read() == expected_state) {
kandangath 14:da09706b92f5 61 fCallback();
kandangath 14:da09706b92f5 62 }
kandangath 7:2d73e219dadf 63 }
kandangath 7:2d73e219dadf 64
kandangath 10:cb16d2957f8d 65 void DebouncedInterrupt::_onInterrupt()
kandangath 0:ca5a0fee9f52 66 {
kandangath 13:09b53a088a9c 67 _bounce_count++;
kandangath 10:cb16d2957f8d 68 timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
kandangath 7:2d73e219dadf 69 }