A button monitor that generates events in a UiEventQueue. More...
A button monitor that generates events in a UiEventQueue.
This class monitors a number of buttons and detects changes in their state. When a change is detected, an event is added to a UiEventQueue. If required, software debouncing can be applied.
This class can monitor an arbitrary number of buttons or switches, as configured by its template argument numButtons
. Each of the buttons is identified by an ID number from 0 .. numButtons - 1
. This number will also be used when events are posted to the UiEventQueue. It's suggested to define an enum in your project like this:
enum ButtonId { bttnOkay = 0, bttnCancel = 1, bttnStart = 2 };
In different projects, diffent ways of reading the button states will be used. That's why this class uses a generic backend that you'll have to write. The BackendType class will provide the source data for each button or switch. An instance of this backend must be supplied via the constructor. It must implement the following public function via which the ButtonMonitor will request the current state of the button:
bool IsButtonPressed(uint16_t buttonId);
BackendType | The class type of the backend that will supply button states. |
numButtons | The number of buttons to monitor. |
#include <ButtonMonitor.h>
Public Member Functions | |
ButtonMonitor () | |
void | Init (UiEventQueue &queueToAddEventsTo, BackendType &backend, uint16_t debounceTimeoutMs=50, uint32_t doubleClickTimeoutMs=500, uint32_t retriggerTimeoutMs=2000, uint32_t retriggerPeriodMs=50) |
void | Process () |
bool | IsButtonPressed (uint16_t buttonId) const |
BackendType & | GetBackend () |
uint16_t | GetNumButtonsMonitored () const |
|
inline |
|
inline |
Returns the BackendType that is used by the monitor.
|
inline |
Returns the number of buttons that are monitored by this class.
|
inline |
Initialises the ButtonMonitor.
queueToAddEventsTo | The UiEventQueue to which events should be posted. |
backend | The backend that supplies the current state of each button. |
debounceTimeoutMs | A event is posted to the queue if the button state doesn't change for debounceTimeoutMs . Can be 0 to disable debouncing. |
doubleClickTimeoutMs | The timeout for detecting double clicks. |
retriggerTimeoutMs | The timeout after which a button will be retriggered when held down. 0 to disable retriggering. |
retriggerPeriodMs | The speed with which a button will be retriggered when held down. |
|
inline |
Returns true, if the given button is currently pressed.
buttonId | The unique ID of the button (< numButtons) |
|
inline |
Checks the value of each button and generates messages for the UIEventQueue. Call this at regular intervals, ideally from your main() idle loop.