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@21:34b95e1b2bf3, 2014-02-25 (annotated)
- Committer:
- kandangath
- Date:
- Tue Feb 25 07:13:29 2014 +0000
- Revision:
- 21:34b95e1b2bf3
- Parent:
- 19:d12a5b89ac3a
- Child:
- 22:9733f886810a
Added option to attach to a class member
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 | 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 | 21:34b95e1b2bf3 | 22 | attach(this, fptr, trigger, debounce_ms); |
kandangath | 21:34b95e1b2bf3 | 23 | } |
kandangath | 21:34b95e1b2bf3 | 24 | |
kandangath | 21:34b95e1b2bf3 | 25 | template<typename T> |
kandangath | 21:34b95e1b2bf3 | 26 | void DebouncedInterrupt::attach(T *cptr, void (*fptr)(void), const gpio_irq_event trigger, const unsigned int& debounce_ms) |
kandangath | 21:34b95e1b2bf3 | 27 | { |
kandangath | 10:cb16d2957f8d | 28 | if(fptr) { |
kandangath | 10:cb16d2957f8d | 29 | fCallback = fptr; |
kandangath | 13:09b53a088a9c | 30 | _last_bounce_count = _bounce_count = 0; |
kandangath | 17:96a51b236ba0 | 31 | _debounce_us = 1000*debounce_ms; |
kandangath | 18:e6e9bc6402b7 | 32 | _trigger = trigger; |
kandangath | 10:cb16d2957f8d | 33 | |
kandangath | 18:e6e9bc6402b7 | 34 | switch(trigger) |
kandangath | 18:e6e9bc6402b7 | 35 | { |
kandangath | 18:e6e9bc6402b7 | 36 | case IRQ_RISE: |
kandangath | 21:34b95e1b2bf3 | 37 | _in->rise(cptr, &DebouncedInterrupt::_onInterrupt); |
kandangath | 18:e6e9bc6402b7 | 38 | break; |
kandangath | 18:e6e9bc6402b7 | 39 | case IRQ_FALL: |
kandangath | 21:34b95e1b2bf3 | 40 | _in->fall(cptr, &DebouncedInterrupt::_onInterrupt); |
kandangath | 18:e6e9bc6402b7 | 41 | break; |
kandangath | 18:e6e9bc6402b7 | 42 | case IRQ_NONE: |
kandangath | 18:e6e9bc6402b7 | 43 | reset(); // Unexpected. Clear callbacks. |
kandangath | 18:e6e9bc6402b7 | 44 | break; |
kandangath | 18:e6e9bc6402b7 | 45 | } |
kandangath | 18:e6e9bc6402b7 | 46 | } else { |
kandangath | 18:e6e9bc6402b7 | 47 | reset(); |
kandangath | 0:ca5a0fee9f52 | 48 | } |
kandangath | 0:ca5a0fee9f52 | 49 | } |
kandangath | 0:ca5a0fee9f52 | 50 | |
kandangath | 10:cb16d2957f8d | 51 | void DebouncedInterrupt::reset() |
kandangath | 0:ca5a0fee9f52 | 52 | { |
kandangath | 10:cb16d2957f8d | 53 | timeout.detach(); |
kandangath | 0:ca5a0fee9f52 | 54 | } |
kandangath | 0:ca5a0fee9f52 | 55 | |
kandangath | 13:09b53a088a9c | 56 | unsigned int DebouncedInterrupt::get_bounce() |
kandangath | 10:cb16d2957f8d | 57 | { |
kandangath | 13:09b53a088a9c | 58 | return _last_bounce_count; |
kandangath | 10:cb16d2957f8d | 59 | } |
kandangath | 10:cb16d2957f8d | 60 | |
kandangath | 10:cb16d2957f8d | 61 | void DebouncedInterrupt::_callback() |
kandangath | 7:2d73e219dadf | 62 | { |
kandangath | 13:09b53a088a9c | 63 | _last_bounce_count = _bounce_count; |
kandangath | 13:09b53a088a9c | 64 | _bounce_count = 0; |
kandangath | 19:d12a5b89ac3a | 65 | if(_din->read() == (_trigger==IRQ_RISE)) { |
kandangath | 14:da09706b92f5 | 66 | fCallback(); |
kandangath | 14:da09706b92f5 | 67 | } |
kandangath | 7:2d73e219dadf | 68 | } |
kandangath | 7:2d73e219dadf | 69 | |
kandangath | 10:cb16d2957f8d | 70 | void DebouncedInterrupt::_onInterrupt() |
kandangath | 0:ca5a0fee9f52 | 71 | { |
kandangath | 13:09b53a088a9c | 72 | _bounce_count++; |
kandangath | 10:cb16d2957f8d | 73 | timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us); |
kandangath | 7:2d73e219dadf | 74 | } |