Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sbl::control::input::Button Class Reference

Debounced button with edge detection and held-time tracking. More...

Detailed Description

Debounced button with edge detection and held-time tracking.

Context
All public methods are ISR-safe: a pure state machine with no hardware I/O.

Definition at line 42 of file button.hpp.

#include <button.hpp>

Public Member Functions

 Button (const ButtonConfig &config={})
 Construct a released button.
 
void set_debounce_ticks (uint32_t ticks)
 
uint32_t debounce_ticks () const
 
void update (bool logical_state, uint32_t elapsed_ticks)
 Feed a reading and the time since the one before.
 
bool pressed () const
 Current debounced state (true = pressed)
 
bool raw () const
 Last raw (unfiltered) reading.
 
bool just_pressed ()
 True once after a press. Reading it clears it.
 
bool just_released ()
 True once after a release. Reading it clears it.
 
uint32_t held_ticks () const
 Ticks since the press was accepted, while held.
 
void reset ()
 Reset all state; the debounce time stays.
 

Constructor & Destructor Documentation

◆ Button()

sbl::control::input::Button::Button ( const ButtonConfig &  config = {})
inlineexplicit

Construct a released button.

Parameters
configSettings

Definition at line 48 of file button.hpp.

Member Function Documentation

◆ set_debounce_ticks()

void sbl::control::input::Button::set_debounce_ticks ( uint32_t  ticks)
inline

The debounce time, in ticks. A generated Board's buttons start at 0; the app sets it.

Definition at line 51 of file button.hpp.

◆ debounce_ticks()

uint32_t sbl::control::input::Button::debounce_ticks ( ) const
inline

Definition at line 52 of file button.hpp.

◆ update()

void sbl::control::input::Button::update ( bool  logical_state,
uint32_t  elapsed_ticks 
)
inline

Feed a reading and the time since the one before.

The state changes once readings that differ from it have held for the debounce time: the first differing reading starts the time at 0, and each one after it adds its elapsed ticks. A reading that matches the state starts over.

Parameters
logical_statetrue = pressed
elapsed_ticksticks since the previous update

Definition at line 65 of file button.hpp.

◆ pressed()

bool sbl::control::input::Button::pressed ( ) const
inline

Current debounced state (true = pressed)

Definition at line 91 of file button.hpp.

◆ raw()

bool sbl::control::input::Button::raw ( ) const
inline

Last raw (unfiltered) reading.

Definition at line 94 of file button.hpp.

◆ just_pressed()

bool sbl::control::input::Button::just_pressed ( )
inline

True once after a press. Reading it clears it.

Definition at line 97 of file button.hpp.

◆ just_released()

bool sbl::control::input::Button::just_released ( )
inline

True once after a release. Reading it clears it.

Definition at line 104 of file button.hpp.

◆ held_ticks()

uint32_t sbl::control::input::Button::held_ticks ( ) const
inline

Ticks since the press was accepted, while held.

0 at the press, then the elapsed ticks of each update while pressed. It keeps its value after the release, until the next press. It stops at the top of its range.

Definition at line 117 of file button.hpp.

◆ reset()

void sbl::control::input::Button::reset ( )
inline

Reset all state; the debounce time stays.

Definition at line 120 of file button.hpp.


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