Skip to content

daisy::ButtonMonitor

Module: LIBDAISY / UI

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

#include <ButtonMonitor.h>

Public Functions

Name
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

Detailed Description

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

A button monitor that generates events in a UiEventQueue.

Template Parameters:

  • BackendType The class type of the backend that will supply button states.
  • numButtons The number of buttons to monitor.

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);

Public Functions Documentation

function ButtonMonitor

inline ButtonMonitor()

function Init

inline void Init(
    UiEventQueue & queueToAddEventsTo,
    BackendType & backend,
    uint16_t debounceTimeoutMs =50,
    uint32_t doubleClickTimeoutMs =500,
    uint32_t retriggerTimeoutMs =2000,
    uint32_t retriggerPeriodMs =50
)

Parameters:

  • 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.

Initialises the ButtonMonitor.


function Process

inline void Process()

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


function IsButtonPressed

inline bool IsButtonPressed(
    uint16_t buttonId
) const

Parameters:

  • buttonId The unique ID of the button (< numButtons)

Returns true, if the given button is currently pressed.


function GetBackend

inline BackendType & GetBackend()

Returns the BackendType that is used by the monitor.


function GetNumButtonsMonitored

inline uint16_t GetNumButtonsMonitored() const

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

---

Updated on 2024-01-03 at 19:41:01 +0000