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) {} }
Revision 25:2163ebb21aef, committed 2017-04-26
- Comitter:
- Jeej
- Date:
- Wed Apr 26 11:02:18 2017 +0000
- Parent:
- 24:4e095ebbcfad
- Child:
- 26:2df374d23986
- Commit message:
- Use mbed callback
Changed in this revision
DebouncedInterrupt.cpp | Show annotated file Show diff for this revision Revisions of this file |
DebouncedInterrupt.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/DebouncedInterrupt.cpp Thu Nov 19 17:43:15 2015 +0000 +++ b/DebouncedInterrupt.cpp Wed Apr 26 11:02:18 2017 +0000 @@ -7,7 +7,7 @@ DebouncedInterrupt::DebouncedInterrupt(PinName pin) { _in = new InterruptIn(pin); - _din = new DigitalIn(pin); + _din = new DigitalIn(pin, PullUp); _timeout = new Timeout; } @@ -31,10 +31,10 @@ switch(trigger) { case IRQ_RISE: - _in->rise(this, &DebouncedInterrupt::_onInterrupt); + _in->rise(callback(this, &DebouncedInterrupt::_onInterrupt)); break; case IRQ_FALL: - _in->fall(this, &DebouncedInterrupt::_onInterrupt); + _in->fall(callback(this, &DebouncedInterrupt::_onInterrupt)); break; case IRQ_NONE: reset(); // Unexpected. Clear callbacks. @@ -76,10 +76,10 @@ if (_timeout_expired) { _timeout_expired = false; _fAttach.call(); - _timeout->attach_us(this, &DebouncedInterrupt::_callback, _debounce_us); + _timeout->attach_us(callback(this, &DebouncedInterrupt::_callback), _debounce_us); } } else { - _timeout->attach_us(this, &DebouncedInterrupt::_callback, _debounce_us); + _timeout->attach_us(callback(this, &DebouncedInterrupt::_callback), _debounce_us); } }
--- a/DebouncedInterrupt.h Thu Nov 19 17:43:15 2015 +0000 +++ b/DebouncedInterrupt.h Wed Apr 26 11:02:18 2017 +0000 @@ -4,7 +4,6 @@ #include <stdint.h> #include "mbed.h" -#include "FunctionPointer.h" /** typedef enum { @@ -65,9 +64,9 @@ // Start monitoring the interupt and attach a callback void attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false); - template<typename T> - void attach(T* tptr, void (T::*mptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false) { - _fAttach.attach(tptr, mptr); + template<typename T, typename M> + void attach(T *obj, M method, const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false) { + _fAttach.attach(callback(obj, method)); _last_bounce_count = _bounce_count = 0; _debounce_us = 1000*debounce_ms; _trigger = trigger; @@ -75,10 +74,10 @@ switch(trigger) { case IRQ_RISE: - _in->rise(tptr, &DebouncedInterrupt::_onInterrupt); + _in->rise(callback(this, &DebouncedInterrupt::_onInterrupt)); break; case IRQ_FALL: - _in->fall(tptr, &DebouncedInterrupt::_onInterrupt); + _in->fall(callback(this, &DebouncedInterrupt::_onInterrupt)); break; case IRQ_NONE: reset(); // Unexpected. Clear callbacks. @@ -97,6 +96,6 @@ unsigned int get_bounce(); protected: // https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/FunctionPointer.h - FunctionPointer _fAttach; + Callback<void()> _fAttach; }; #endif \ No newline at end of file