A replacement for InterruptIn that debounces the interrupt.
Dependents: D7A_Demo-Get-started CVtoOSCConverter EE3501keypad D7A_Localisation ... more
Fork of DebouncedInterrupt by
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) {} }
DebouncedInterrupt.cpp@26:2df374d23986, 2017-04-27 (annotated)
- Committer:
- Jeej
- Date:
- Thu Apr 27 16:06:12 2017 +0000
- Revision:
- 26:2df374d23986
- Parent:
- 25:2163ebb21aef
Updated deprecated callback.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
kandangath | 10:cb16d2957f8d | 7 | DebouncedInterrupt::DebouncedInterrupt(PinName pin) |
kandangath | 10:cb16d2957f8d | 8 | { |
kandangath | 10:cb16d2957f8d | 9 | _in = new InterruptIn(pin); |
Jeej | 25:2163ebb21aef | 10 | _din = new DigitalIn(pin, PullUp); |
Jeej | 23:cd4042093f85 | 11 | _timeout = new Timeout; |
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; |
Jeej | 23:cd4042093f85 | 18 | delete _timeout; |
kandangath | 10:cb16d2957f8d | 19 | } |
kandangath | 10:cb16d2957f8d | 20 | |
Jeej | 23:cd4042093f85 | 21 | void DebouncedInterrupt::attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms, bool immediate) |
kandangath | 0:ca5a0fee9f52 | 22 | { |
kandangath | 10:cb16d2957f8d | 23 | if(fptr) { |
Jeej | 26:2df374d23986 | 24 | _callback = callback(fptr); |
kandangath | 13:09b53a088a9c | 25 | _last_bounce_count = _bounce_count = 0; |
kandangath | 17:96a51b236ba0 | 26 | _debounce_us = 1000*debounce_ms; |
kandangath | 18:e6e9bc6402b7 | 27 | _trigger = trigger; |
Jeej | 23:cd4042093f85 | 28 | _timeout_expired = true; |
Jeej | 23:cd4042093f85 | 29 | _immediate = immediate; |
kandangath | 10:cb16d2957f8d | 30 | |
kandangath | 18:e6e9bc6402b7 | 31 | switch(trigger) |
kandangath | 18:e6e9bc6402b7 | 32 | { |
kandangath | 18:e6e9bc6402b7 | 33 | case IRQ_RISE: |
Jeej | 25:2163ebb21aef | 34 | _in->rise(callback(this, &DebouncedInterrupt::_onInterrupt)); |
kandangath | 18:e6e9bc6402b7 | 35 | break; |
kandangath | 18:e6e9bc6402b7 | 36 | case IRQ_FALL: |
Jeej | 25:2163ebb21aef | 37 | _in->fall(callback(this, &DebouncedInterrupt::_onInterrupt)); |
kandangath | 18:e6e9bc6402b7 | 38 | break; |
kandangath | 18:e6e9bc6402b7 | 39 | case IRQ_NONE: |
kandangath | 18:e6e9bc6402b7 | 40 | reset(); // Unexpected. Clear callbacks. |
kandangath | 18:e6e9bc6402b7 | 41 | break; |
kandangath | 18:e6e9bc6402b7 | 42 | } |
kandangath | 18:e6e9bc6402b7 | 43 | } else { |
kandangath | 18:e6e9bc6402b7 | 44 | reset(); |
kandangath | 0:ca5a0fee9f52 | 45 | } |
kandangath | 0:ca5a0fee9f52 | 46 | } |
kandangath | 0:ca5a0fee9f52 | 47 | |
kandangath | 10:cb16d2957f8d | 48 | void DebouncedInterrupt::reset() |
kandangath | 0:ca5a0fee9f52 | 49 | { |
Jeej | 23:cd4042093f85 | 50 | _timeout->detach(); |
Jeej | 23:cd4042093f85 | 51 | _timeout_expired = true; |
kandangath | 0:ca5a0fee9f52 | 52 | } |
kandangath | 0:ca5a0fee9f52 | 53 | |
kandangath | 13:09b53a088a9c | 54 | unsigned int DebouncedInterrupt::get_bounce() |
kandangath | 10:cb16d2957f8d | 55 | { |
kandangath | 13:09b53a088a9c | 56 | return _last_bounce_count; |
kandangath | 10:cb16d2957f8d | 57 | } |
kandangath | 10:cb16d2957f8d | 58 | |
Jeej | 26:2df374d23986 | 59 | void DebouncedInterrupt::_onCallback() |
kandangath | 7:2d73e219dadf | 60 | { |
kandangath | 13:09b53a088a9c | 61 | _last_bounce_count = _bounce_count; |
kandangath | 13:09b53a088a9c | 62 | _bounce_count = 0; |
Jeej | 23:cd4042093f85 | 63 | if (!_immediate) { |
Jeej | 23:cd4042093f85 | 64 | if (_din->read() == (_trigger==IRQ_RISE)) { |
Jeej | 26:2df374d23986 | 65 | _callback.call(); |
Jeej | 23:cd4042093f85 | 66 | } |
kandangath | 14:da09706b92f5 | 67 | } |
Jeej | 23:cd4042093f85 | 68 | _timeout_expired = true; |
kandangath | 7:2d73e219dadf | 69 | } |
kandangath | 7:2d73e219dadf | 70 | |
kandangath | 10:cb16d2957f8d | 71 | void DebouncedInterrupt::_onInterrupt() |
kandangath | 0:ca5a0fee9f52 | 72 | { |
kandangath | 13:09b53a088a9c | 73 | _bounce_count++; |
Jeej | 23:cd4042093f85 | 74 | |
Jeej | 23:cd4042093f85 | 75 | if (_immediate) { |
Jeej | 23:cd4042093f85 | 76 | if (_timeout_expired) { |
Jeej | 23:cd4042093f85 | 77 | _timeout_expired = false; |
Jeej | 26:2df374d23986 | 78 | _callback.call(); |
Jeej | 26:2df374d23986 | 79 | _timeout->attach_us(callback(this, &DebouncedInterrupt::_onCallback), _debounce_us); |
Jeej | 23:cd4042093f85 | 80 | } |
Jeej | 23:cd4042093f85 | 81 | } |
Jeej | 23:cd4042093f85 | 82 | else { |
Jeej | 26:2df374d23986 | 83 | _timeout->attach_us(callback(this, &DebouncedInterrupt::_onCallback), _debounce_us); |
Jeej | 23:cd4042093f85 | 84 | } |
kandangath | 7:2d73e219dadf | 85 | } |