ISF15ACP4 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
isf15acp4_button.hpp
Go to the documentation of this file.
1
14#ifndef ISF15ACP4_BUTTON_HPP
15#define ISF15ACP4_BUTTON_HPP
16
17#include <cstdint>
18
19#include "isf15acp4_types.hpp"
20
21namespace isf15acp4 {
22
26 uint32_t timestamp_ms{
27 0};
28 uint32_t held_ms{0};
29 bool was_long{false};
31 false};
32};
33
45public:
50 explicit constexpr ButtonDebouncer(const ButtonConfig& config = {}) noexcept : config_(config) {}
51
56 constexpr void Configure(const ButtonConfig& config) noexcept {
57 config_ = config;
58 Reset();
59 }
60
62 [[nodiscard]] constexpr const ButtonConfig& GetConfig() const noexcept {
63 return config_;
64 }
65
67 constexpr void Reset() noexcept {
68 started_ = false;
69 pressed_ = false;
70 changing_ = false;
71 long_sent_ = false;
72 repeat_armed_ = false;
73 }
74
82 constexpr ButtonUpdate Update(bool down, uint32_t now_ms) noexcept {
83 ButtonUpdate u{};
84 u.timestamp_ms = now_ms;
85 if (!started_) {
86 started_ = true;
87 pressed_ = down;
88 pressed_at_ms_ = now_ms;
89 changing_ = false;
90 return u;
91 }
92 if (down != pressed_) {
93 if (!changing_) {
94 changing_ = true;
95 changed_at_ms_ = now_ms;
96 }
97 if ((now_ms - changed_at_ms_) >= config_.debounce_ms) {
98 return accept(down, now_ms);
99 }
100 return u;
101 }
102 changing_ = false;
103 if (pressed_) {
104 return holdEvent(now_ms);
105 }
106 return u;
107 }
108
110 [[nodiscard]] constexpr bool IsDown() const noexcept {
111 return pressed_;
112 }
114 [[nodiscard]] constexpr uint32_t PressCount() const noexcept {
115 return press_count_;
116 }
117
118private:
119 constexpr ButtonUpdate accept(bool down, uint32_t now_ms) noexcept {
120 ButtonUpdate u{};
121 u.timestamp_ms = changed_at_ms_;
122 changing_ = false;
123 pressed_ = down;
124 if (down) {
125 pressed_at_ms_ = changed_at_ms_;
126 long_sent_ = false;
127 repeat_armed_ = (config_.repeat_interval_ms != 0U);
128 repeat_due_ms_ = config_.repeat_delay_ms;
129 press_count_ = press_count_ + 1U;
130 u.event = ButtonEvent::Pressed;
131 } else {
132 u.event = ButtonEvent::Released;
133 u.held_ms = changed_at_ms_ - pressed_at_ms_;
134 u.was_long = long_sent_;
135 repeat_armed_ = false;
136 }
137 static_cast<void>(now_ms);
138 return u;
139 }
140
141 constexpr ButtonUpdate holdEvent(uint32_t now_ms) noexcept {
142 ButtonUpdate u{};
143 u.timestamp_ms = now_ms;
144 const uint32_t held = now_ms - pressed_at_ms_;
145 u.held_ms = held;
146 if (!long_sent_ && (held >= config_.long_press_ms)) {
147 long_sent_ = true;
148 u.event = ButtonEvent::LongPress;
149 } else if (repeat_armed_ && (held >= repeat_due_ms_)) {
150 // The next repeat is one interval after this poll: a slow poll never bursts.
151 repeat_due_ms_ = held + config_.repeat_interval_ms;
152 u.event = ButtonEvent::Repeat;
153 } else {
154 // Nothing is due.
155 }
156 return u;
157 }
158
159 ButtonConfig config_;
160 uint32_t pressed_at_ms_{0};
161 uint32_t changed_at_ms_{0};
162 uint32_t repeat_due_ms_{0};
163 uint32_t press_count_{0};
164 bool started_{false};
165 bool pressed_{false};
166 bool changing_{false};
167 bool long_sent_{false};
168 bool repeat_armed_{false};
169};
170
171namespace detail {
173constexpr bool DebounceSelfCheck() noexcept {
174 ButtonConfig cfg{};
175 cfg.debounce_ms = 10;
176 ButtonDebouncer d(cfg);
177 static_cast<void>(d.Update(false, 0U));
178 static_cast<void>(d.Update(true, 100U));
179 const ButtonUpdate early = d.Update(true, 109U);
180 const ButtonUpdate edge = d.Update(true, 110U);
181 return (early.event == ButtonEvent::None) && (edge.event == ButtonEvent::Pressed) &&
182 (edge.timestamp_ms == 100U);
183}
184static_assert(DebounceSelfCheck(),
185 "debounce: accepted after debounce_ms, stamped at the first sample");
186} // namespace detail
187
188} // namespace isf15acp4
189
190#endif // ISF15ACP4_BUTTON_HPP
Debounces a switch sampled at any rate and classifies the press.
Definition isf15acp4_button.hpp:44
constexpr bool IsDown() const noexcept
Definition isf15acp4_button.hpp:110
constexpr ButtonDebouncer(const ButtonConfig &config={}) noexcept
A debouncer with the given configuration, switch open.
Definition isf15acp4_button.hpp:50
constexpr void Reset() noexcept
Forget the state: the next Update() adopts its level without an event.
Definition isf15acp4_button.hpp:67
constexpr void Configure(const ButtonConfig &config) noexcept
Take a new configuration and forget the state (the next sample is adopted again).
Definition isf15acp4_button.hpp:56
constexpr uint32_t PressCount() const noexcept
Definition isf15acp4_button.hpp:114
constexpr const ButtonConfig & GetConfig() const noexcept
Definition isf15acp4_button.hpp:62
constexpr ButtonUpdate Update(bool down, uint32_t now_ms) noexcept
Feed one sample.
Definition isf15acp4_button.hpp:82
ButtonEvent
What a switch poll can report.
Definition isf15acp4_types.hpp:215
@ LongPress
Held for long_press_ms; once per press.
@ None
Nothing happened.
@ Repeat
Held on: one per repeat_interval_ms after repeat_delay_ms.
@ Pressed
The switch closed and stayed closed for the debounce time.
@ Released
The switch opened and stayed open for the debounce time.
Public types of the ISF15ACP4 driver: errors, Result<T>, the display and button configuration,...
The ISF15ACP4 driver: everything it declares lives here; internals in detail.
Definition isf15acp4.hpp:31
Switch handling: polarity, debounce, long press and repeat.
Definition isf15acp4_types.hpp:286
uint32_t debounce_ms
Time a level must be stable to count, 1 to 1000.
Definition isf15acp4_types.hpp:288
uint32_t repeat_interval_ms
0 = no repeat; else 10 to 10000 between Repeat events.
Definition isf15acp4_types.hpp:290
uint32_t long_press_ms
Hold time of LongPress, above the debounce time, to 60000.
Definition isf15acp4_types.hpp:289
uint32_t repeat_delay_ms
Hold time of the first Repeat, to 60000.
Definition isf15acp4_types.hpp:291
One result of ButtonDebouncer::Update().
Definition isf15acp4_button.hpp:24
bool woke_display
Set by the driver's Poll(): this press woke a dimmed or sleeping display.
Definition isf15acp4_button.hpp:30
uint32_t held_ms
Released, LongPress and Repeat: how long the switch was down.
Definition isf15acp4_button.hpp:28
bool was_long
Released: the press had reached long_press_ms.
Definition isf15acp4_button.hpp:29
uint32_t timestamp_ms
When the level first changed (Pressed, Released), else the poll time.
Definition isf15acp4_button.hpp:26