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
button.hpp
Go to the documentation of this file.
1/**
2 * @file button.hpp
3 * @brief Button component: a debounced digital input with edges and a held time
4 *
5 * Button takes logical GPIO readings (true = pressed; Gpio::read() already
6 * applies active-low) and the time since the reading before, and gives the
7 * debounced state, its edges and how long the button has been held.
8 *
9 * Times are ticks of the clock that drives it, the control clock's elapsed
10 * ticks as a scheduler subscriber receives them. It holds no rate and counts
11 * no calls: a duration in milliseconds becomes ticks at the edge, through the
12 * clock.
13 *
14 * @code
15 * using Clock = sbl::control::TargetClock;
16 * sbl::control::input::Button btn{{Clock::ticks_from_ms(5)}};
17 * // or, on a generated Board: board.button1.set_debounce_ticks(Clock::ticks_from_ms(5));
18 * btn.update(gpio_reading, elapsed_ticks);
19 * if (btn.just_pressed()) { ... }
20 * if (btn.held_ticks() > Clock::ticks_from_ms(500)) { ... } // a long press
21 * @endcode
22 */
23
24#ifndef SBL_CONTROL_INPUT_BUTTON_HPP_
25#define SBL_CONTROL_INPUT_BUTTON_HPP_
26
27#include <cstdint>
28
30
31/** @brief Button construction settings */
33 /** How long a changed reading must hold before the state follows it, in ticks. 0 follows the first reading. */
34 uint32_t debounce_ticks = 0;
35};
36
37/**
38 * @brief Debounced button with edge detection and held-time tracking
39 *
40 * @context All public methods are ISR-safe: a pure state machine with no hardware I/O.
41 */
42class Button {
43public:
44 /**
45 * @brief Construct a released button
46 * @param config Settings
47 */
48 explicit Button(const ButtonConfig& config = {}) : debounce_ticks_(config.debounce_ticks) {}
49
50 /** The debounce time, in ticks. A generated Board's buttons start at 0; the app sets it. */
51 void set_debounce_ticks(uint32_t ticks) { debounce_ticks_ = ticks; }
52 uint32_t debounce_ticks() const { return debounce_ticks_; }
53
54 /**
55 * @brief Feed a reading and the time since the one before
56 *
57 * The state changes once readings that differ from it have held for the
58 * debounce time: the first differing reading starts the time at 0, and
59 * each one after it adds its elapsed ticks. A reading that matches the
60 * state starts over.
61 *
62 * @param logical_state true = pressed
63 * @param elapsed_ticks ticks since the previous update
64 */
65 void update(bool logical_state, uint32_t elapsed_ticks) {
66 raw_ = logical_state;
67 if (stable_) {
68 held_ticks_ = saturating_add(held_ticks_, elapsed_ticks);
69 }
70
71 if (logical_state == stable_) {
72 differing_ = false;
73 return;
74 }
75 differed_ticks_ = differing_ ? saturating_add(differed_ticks_, elapsed_ticks) : 0;
76 differing_ = true;
77 if (differed_ticks_ < debounce_ticks_) {
78 return;
79 }
80 stable_ = logical_state;
81 differing_ = false;
82 if (stable_) {
83 pressed_edge_ = true;
84 held_ticks_ = 0;
85 } else {
86 released_edge_ = true;
87 }
88 }
89
90 /** @brief Current debounced state (true = pressed) */
91 bool pressed() const { return stable_; }
92
93 /** @brief Last raw (unfiltered) reading */
94 bool raw() const { return raw_; }
95
96 /** @brief True once after a press. Reading it clears it. */
97 bool just_pressed() {
98 const bool e = pressed_edge_;
99 pressed_edge_ = false;
100 return e;
101 }
102
103 /** @brief True once after a release. Reading it clears it. */
105 const bool e = released_edge_;
106 released_edge_ = false;
107 return e;
108 }
109
110 /**
111 * @brief Ticks since the press was accepted, while held
112 *
113 * 0 at the press, then the elapsed ticks of each update while pressed.
114 * It keeps its value after the release, until the next press. It stops
115 * at the top of its range.
116 */
117 uint32_t held_ticks() const { return held_ticks_; }
118
119 /** @brief Reset all state; the debounce time stays. */
120 void reset() {
121 differed_ticks_ = 0;
122 held_ticks_ = 0;
123 differing_ = false;
124 stable_ = false;
125 raw_ = false;
126 pressed_edge_ = false;
127 released_edge_ = false;
128 }
129
130private:
131 static uint32_t saturating_add(uint32_t a, uint32_t b) { return a + b < a ? UINT32_MAX : a + b; }
132
133 uint32_t debounce_ticks_;
134 uint32_t differed_ticks_ = 0; /**< how long readings have differed from the state */
135 uint32_t held_ticks_ = 0;
136 bool differing_ = false;
137 bool stable_ = false;
138 bool raw_ = false;
139 bool pressed_edge_ = false;
140 bool released_edge_ = false;
141};
142
143} // namespace sbl::control::input
144
145#endif // SBL_CONTROL_INPUT_BUTTON_HPP_
Debounced button with edge detection and held-time tracking.
Definition button.hpp:42
bool raw() const
Last raw (unfiltered) reading.
Definition button.hpp:94
bool pressed() const
Current debounced state (true = pressed)
Definition button.hpp:91
void reset()
Reset all state; the debounce time stays.
Definition button.hpp:120
bool just_pressed()
True once after a press. Reading it clears it.
Definition button.hpp:97
bool just_released()
True once after a release. Reading it clears it.
Definition button.hpp:104
void update(bool logical_state, uint32_t elapsed_ticks)
Feed a reading and the time since the one before.
Definition button.hpp:65
Button(const ButtonConfig &config={})
Construct a released button.
Definition button.hpp:48
void set_debounce_ticks(uint32_t ticks)
Definition button.hpp:51
uint32_t held_ticks() const
Ticks since the press was accepted, while held.
Definition button.hpp:117
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29
Button construction settings.
Definition button.hpp:32