PushDuration maps n callback functions to the duration of a button press. E.g. foo() is called when a button is released after 1 second where bar() is called after 3 seconds.
Fork of PushDuration by
Diff: PushDuration.cpp
- Revision:
- 0:19efa3fea668
- Child:
- 2:126fed923ada
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PushDuration.cpp Fri May 20 15:05:10 2016 +0000 @@ -0,0 +1,53 @@ +#include "mbed.h" +#include "PushDuration.h" +/** + * React() function is called by release() when the button is released. It traverses through the two + * dimensional key vale array and matches the push duration with the corresponding callback function. +**/ +void ButtonHandler::react(int counter) const { + const action *a = mTable.table; + for (std::size_t i=mTable.size; i; --i, ++a) { + if ((counter >= a->counter_limit) && counter < (a+1)->counter_limit ) { + a->transition(); + return; + } + } +} + +/** + * Configures the button pin mode, attaches callback functions for rise and fall + * and enables IRQ for the given pin. +**/ +void ButtonHandler::enable() { + buttonPin.mode(PullUp); + wait(0.01); + buttonPin.rise(this, &ButtonHandler::release); + buttonPin.fall(this, &ButtonHandler::press); + buttonPin.enable_irq(); +} + +/** + * + * + * +**/ + +void ButtonHandler::disable() { + buttonPin.disable_irq(); + ticker.detach(); +} + +void ButtonHandler::press() { + counter = 0; + ticker.attach(this, &ButtonHandler::secondsCount, intervalInSeconds); +} + +void ButtonHandler::secondsCount() { + ++counter; +} + +void ButtonHandler::release() { + ticker.detach(); + react(counter); + counter = 0; +} \ No newline at end of file