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.
PushDuration.cpp
- Committer:
- jensstruemper
- Date:
- 2016-05-20
- Revision:
- 1:28c022f61c0b
- Parent:
- 0:19efa3fea668
File content as of revision 1:28c022f61c0b:
#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; }