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:
Jeej
Date:
Thu Nov 19 15:37:39 2015 +0000
Revision:
23:cd4042093f85
Parent:
22:9733f886810a
Child:
24:4e095ebbcfad
Fixed Timeout bug that appeared on NUCLEO-L152RE (don't know about other platforms) after mbed update.; Added the possibility to trigger the callback immediately at the interrupt.

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"
Jeej 23:cd4042093f85 5 #include "dbg.h"
kandangath 0:ca5a0fee9f52 6
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);
Jeej 23:cd4042093f85 12 _timeout = new Timeout;
kandangath 10:cb16d2957f8d 13 }
kandangath 10:cb16d2957f8d 14
kandangath 10:cb16d2957f8d 15 DebouncedInterrupt::~DebouncedInterrupt()
kandangath 10:cb16d2957f8d 16 {
kandangath 14:da09706b92f5 17 delete _in;
kandangath 14:da09706b92f5 18 delete _din;
Jeej 23:cd4042093f85 19 delete _timeout;
kandangath 10:cb16d2957f8d 20 }
kandangath 10:cb16d2957f8d 21
Jeej 23:cd4042093f85 22 void DebouncedInterrupt::attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms, bool immediate)
kandangath 0:ca5a0fee9f52 23 {
kandangath 10:cb16d2957f8d 24 if(fptr) {
kandangath 22:9733f886810a 25 _fAttach.attach(fptr);
kandangath 13:09b53a088a9c 26 _last_bounce_count = _bounce_count = 0;
kandangath 17:96a51b236ba0 27 _debounce_us = 1000*debounce_ms;
kandangath 18:e6e9bc6402b7 28 _trigger = trigger;
Jeej 23:cd4042093f85 29 _timeout_expired = true;
Jeej 23:cd4042093f85 30 _immediate = immediate;
kandangath 10:cb16d2957f8d 31
kandangath 18:e6e9bc6402b7 32 switch(trigger)
kandangath 18:e6e9bc6402b7 33 {
kandangath 18:e6e9bc6402b7 34 case IRQ_RISE:
kandangath 22:9733f886810a 35 _in->rise(this, &DebouncedInterrupt::_onInterrupt);
kandangath 18:e6e9bc6402b7 36 break;
kandangath 18:e6e9bc6402b7 37 case IRQ_FALL:
kandangath 22:9733f886810a 38 _in->fall(this, &DebouncedInterrupt::_onInterrupt);
kandangath 18:e6e9bc6402b7 39 break;
kandangath 18:e6e9bc6402b7 40 case IRQ_NONE:
kandangath 18:e6e9bc6402b7 41 reset(); // Unexpected. Clear callbacks.
kandangath 18:e6e9bc6402b7 42 break;
kandangath 18:e6e9bc6402b7 43 }
kandangath 18:e6e9bc6402b7 44 } else {
kandangath 18:e6e9bc6402b7 45 reset();
kandangath 0:ca5a0fee9f52 46 }
kandangath 0:ca5a0fee9f52 47 }
kandangath 0:ca5a0fee9f52 48
kandangath 10:cb16d2957f8d 49 void DebouncedInterrupt::reset()
kandangath 0:ca5a0fee9f52 50 {
Jeej 23:cd4042093f85 51 _timeout->detach();
Jeej 23:cd4042093f85 52 _timeout_expired = true;
kandangath 0:ca5a0fee9f52 53 }
kandangath 0:ca5a0fee9f52 54
kandangath 13:09b53a088a9c 55 unsigned int DebouncedInterrupt::get_bounce()
kandangath 10:cb16d2957f8d 56 {
kandangath 13:09b53a088a9c 57 return _last_bounce_count;
kandangath 10:cb16d2957f8d 58 }
kandangath 10:cb16d2957f8d 59
kandangath 10:cb16d2957f8d 60 void DebouncedInterrupt::_callback()
kandangath 7:2d73e219dadf 61 {
kandangath 13:09b53a088a9c 62 _last_bounce_count = _bounce_count;
kandangath 13:09b53a088a9c 63 _bounce_count = 0;
Jeej 23:cd4042093f85 64 if (!_immediate) {
Jeej 23:cd4042093f85 65 if (_din->read() == (_trigger==IRQ_RISE)) {
Jeej 23:cd4042093f85 66 _fAttach.call();
Jeej 23:cd4042093f85 67 }
kandangath 14:da09706b92f5 68 }
Jeej 23:cd4042093f85 69 _timeout_expired = true;
kandangath 7:2d73e219dadf 70 }
kandangath 7:2d73e219dadf 71
kandangath 10:cb16d2957f8d 72 void DebouncedInterrupt::_onInterrupt()
kandangath 0:ca5a0fee9f52 73 {
kandangath 13:09b53a088a9c 74 _bounce_count++;
Jeej 23:cd4042093f85 75
Jeej 23:cd4042093f85 76 if (_immediate) {
Jeej 23:cd4042093f85 77 if (_timeout_expired) {
Jeej 23:cd4042093f85 78 _timeout_expired = false;
Jeej 23:cd4042093f85 79 _fAttach.call();
Jeej 23:cd4042093f85 80 _timeout->attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
Jeej 23:cd4042093f85 81 }
Jeej 23:cd4042093f85 82 }
Jeej 23:cd4042093f85 83 else {
Jeej 23:cd4042093f85 84 _timeout->attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
Jeej 23:cd4042093f85 85 }
kandangath 7:2d73e219dadf 86 }