libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
daisy::ButtonMonitor< BackendType, numButtons > Class Template Reference

A button monitor that generates events in a UiEventQueue. More...

Detailed Description

template<typename BackendType, uint32_t numButtons>
class daisy::ButtonMonitor< BackendType, numButtons >

A button monitor that generates events in a UiEventQueue.

Author
jelliesen

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);
Template Parameters
BackendTypeThe class type of the backend that will supply button states.
numButtonsThe 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
 
BackendTypeGetBackend ()
 
uint16_t GetNumButtonsMonitored () const
 

Constructor & Destructor Documentation

◆ ButtonMonitor()

template<typename BackendType , uint32_t numButtons>
daisy::ButtonMonitor< BackendType, numButtons >::ButtonMonitor ( )
inline

Member Function Documentation

◆ GetBackend()

template<typename BackendType , uint32_t numButtons>
BackendType & daisy::ButtonMonitor< BackendType, numButtons >::GetBackend ( )
inline

Returns the BackendType that is used by the monitor.

◆ GetNumButtonsMonitored()

template<typename BackendType , uint32_t numButtons>
uint16_t daisy::ButtonMonitor< BackendType, numButtons >::GetNumButtonsMonitored ( ) const
inline

Returns the number of buttons that are monitored by this class.

◆ Init()

template<typename BackendType , uint32_t numButtons>
void daisy::ButtonMonitor< BackendType, numButtons >::Init ( UiEventQueue queueToAddEventsTo,
BackendType backend,
uint16_t  debounceTimeoutMs = 50,
uint32_t  doubleClickTimeoutMs = 500,
uint32_t  retriggerTimeoutMs = 2000,
uint32_t  retriggerPeriodMs = 50 
)
inline

Initialises the ButtonMonitor.

Parameters
queueToAddEventsToThe UiEventQueue to which events should be posted.
backendThe backend that supplies the current state of each button.
debounceTimeoutMsA event is posted to the queue if the button state doesn't change for debounceTimeoutMs. Can be 0 to disable debouncing.
doubleClickTimeoutMsThe timeout for detecting double clicks.
retriggerTimeoutMsThe timeout after which a button will be retriggered when held down. 0 to disable retriggering.
retriggerPeriodMsThe speed with which a button will be retriggered when held down.

◆ IsButtonPressed()

template<typename BackendType , uint32_t numButtons>
bool daisy::ButtonMonitor< BackendType, numButtons >::IsButtonPressed ( uint16_t  buttonId) const
inline

Returns true, if the given button is currently pressed.

Parameters
buttonIdThe unique ID of the button (< numButtons)

◆ Process()

template<typename BackendType , uint32_t numButtons>
void daisy::ButtonMonitor< BackendType, numButtons >::Process ( )
inline

Checks the value of each button and generates messages for the UIEventQueue. Call this at regular intervals, ideally from your main() idle loop.


The documentation for this class was generated from the following file: