Sound Byte Libs 29c5ff3
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 — debounced digital input with edge detection
4 * @ingroup components
5 *
6 * Button takes logical GPIO readings (true = pressed) and provides
7 * debounced state, edge detection, and held-duration tracking.
8 * No active_low logic needed — Gpio::read() already returns logical value.
9 *
10 * Usage:
11 * sbl::components::control::Button btn;
12 * btn.update(gpio_reading); // Feed logical state each tick
13 * if (btn.just_pressed()) { ... } // Rising edge (auto-resets)
14 * if (btn.held_count() > 500) { ... } // Long press
15 */
16
17#ifndef SBL_COMPONENTS_CONTROL_BUTTON_HPP_
18#define SBL_COMPONENTS_CONTROL_BUTTON_HPP_
19
20#include <cstdint>
21
22namespace sbl {
23namespace components {
24namespace control {
25
27 uint8_t debounce_count = 5; ///< Consecutive identical readings to accept
28};
29
30/**
31 * @brief Debounced button with edge detection and held-duration tracking
32 *
33 * @note All public methods are ISR-safe — pure state machine with no hardware I/O.
34 */
35class Button {
36public:
37 explicit Button(const ButtonConfig& config = {})
38 : threshold_(config.debounce_count),
39 counter_(0), stable_(false), raw_(false),
40 pressed_edge_(false), released_edge_(false),
41 held_ticks_(0) {}
42
43 /**
44 * @brief Feed a new logical GPIO reading
45 *
46 * Call once per update cycle. Debounce: the stable state only changes
47 * when `debounce_count` consecutive identical readings are seen.
48 *
49 * @param logical_state true = pressed (Gpio::read already handles active_low)
50 */
51 void update(bool logical_state) {
52 raw_ = logical_state;
53
54 if (logical_state == stable_) {
55 // Input matches stable — reset debounce counter
56 counter_ = 0;
57 } else {
58 // Input differs from stable — count toward transition
59 ++counter_;
60 if (counter_ >= threshold_) {
61 // Transition accepted
62 bool was_pressed = stable_;
63 stable_ = logical_state;
64 counter_ = 0;
65
66 if (stable_ && !was_pressed) {
67 pressed_edge_ = true;
68 held_ticks_ = 0;
69 } else if (!stable_ && was_pressed) {
70 released_edge_ = true;
71 }
72 }
73 }
74
75 // Count held ticks while pressed
76 if (stable_) {
77 ++held_ticks_;
78 }
79 }
80
81 /** @brief Current debounced state (true = pressed) */
82 bool pressed() const { return stable_; }
83
84 /** @brief Last raw (unfiltered) reading */
85 bool raw() const { return raw_; }
86
87 /**
88 * @brief True if button was just pressed (rising edge)
89 *
90 * Resets on read — call once per update cycle.
91 */
92 bool just_pressed() {
93 bool e = pressed_edge_;
94 pressed_edge_ = false;
95 return e;
96 }
97
98 /**
99 * @brief True if button was just released (falling edge)
100 *
101 * Resets on read — call once per update cycle.
102 */
104 bool e = released_edge_;
105 released_edge_ = false;
106 return e;
107 }
108
109 /**
110 * @brief Ticks the button has been held down
111 *
112 * Returns raw tick count (caller knows update rate).
113 * Resets to 0 on press, continues counting while held.
114 */
115 uint32_t held_count() const { return held_ticks_; }
116
117 /** @brief Reset all state */
118 void reset() {
119 counter_ = 0;
120 stable_ = false;
121 raw_ = false;
122 pressed_edge_ = false;
123 released_edge_ = false;
124 held_ticks_ = 0;
125 }
126
127private:
128 uint8_t threshold_;
129 uint8_t counter_;
130 bool stable_;
131 bool raw_;
132 bool pressed_edge_;
133 bool released_edge_;
134 uint32_t held_ticks_;
135};
136
137} // namespace control
138} // namespace components
139} // namespace sbl
140
141#endif // SBL_COMPONENTS_CONTROL_BUTTON_HPP_
Debounced button with edge detection and held-duration tracking.
Definition button.hpp:35
bool just_pressed()
True if button was just pressed (rising edge)
Definition button.hpp:92
bool raw() const
Last raw (unfiltered) reading.
Definition button.hpp:85
bool just_released()
True if button was just released (falling edge)
Definition button.hpp:103
void reset()
Reset all state.
Definition button.hpp:118
bool pressed() const
Current debounced state (true = pressed)
Definition button.hpp:82
Button(const ButtonConfig &config={})
Definition button.hpp:37
void update(bool logical_state)
Feed a new logical GPIO reading.
Definition button.hpp:51
uint32_t held_count() const
Ticks the button has been held down.
Definition button.hpp:115
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
uint8_t debounce_count
Consecutive identical readings to accept.
Definition button.hpp:27