M5Utility 0.3.1 git rev:a8c5625
Loading...
Searching...
No Matches
button_status.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_BUTTON_STATUS_HPP
11#define M5_UTILITY_BUTTON_STATUS_HPP
12
13#include <cstdint>
14
15namespace m5 {
16namespace utility {
17namespace button {
18
24class Status {
25public:
30 enum class button_state_t : uint8_t { state_nochange, state_clicked, state_hold, state_decide_click_count };
31
39 explicit Status(const uint16_t hold_ms = 500, const uint16_t debounce_ms = 10, const uint16_t double_click_ms = 0)
40 : _msecHold{hold_ms}, _msecDebounce{debounce_ms}, _msecDoubleClick{double_click_ms}
41 {
42 }
43
47 inline void setDebounceThreshold(const uint32_t msec)
48 {
49 _msecDebounce = static_cast<uint16_t>(msec);
50 }
52 inline void setHoldThreshold(const uint32_t msec)
53 {
54 _msecHold = static_cast<uint16_t>(msec);
55 }
62 inline void setDoubleClickThreshold(const uint32_t msec)
63 {
64 _msecDoubleClick = static_cast<uint16_t>(msec);
65 }
67 inline uint32_t getDebounceThreshold(void) const
68 {
69 return _msecDebounce;
70 }
72 inline uint32_t getHoldThreshold(void) const
73 {
74 return _msecHold;
75 }
80 inline uint32_t getDoubleClickThreshold(void) const
81 {
82 return _msecDoubleClick ? _msecDoubleClick : _msecHold;
83 }
85
89 bool isPressed(void) const
90 {
91 return _press;
92 }
94 bool isReleased(void) const
95 {
96 return !_press;
97 }
99 bool isHolding(void) const
100 {
101 return _press == 2;
102 }
104 bool wasPressed(void) const
105 {
106 return !_oldPress && _press;
107 }
109 bool wasReleased(void) const
110 {
111 return _oldPress && !_press;
112 }
114 bool wasClicked(void) const
115 {
116 return _currentState == button_state_t::state_clicked;
117 }
119 bool wasHold(void) const
120 {
121 return _currentState == button_state_t::state_hold;
122 }
124 bool wasSingleClicked(void) const
125 {
126 return _currentState == button_state_t::state_decide_click_count && _clickCount == 1;
127 }
129 bool wasDoubleClicked(void) const
130 {
131 return _currentState == button_state_t::state_decide_click_count && _clickCount == 2;
132 }
134 bool wasDecideClickCount(void) const
135 {
136 return _currentState == button_state_t::state_decide_click_count;
137 }
139 uint8_t getClickCount(void) const
140 {
141 return _clickCount;
142 }
144 bool wasChangePressed(void) const
145 {
146 return ((bool)_press) != ((bool)_oldPress);
147 }
149 bool wasReleasedAfterHold(void) const
150 {
151 return !_press && _oldPress == 2;
152 }
154 bool wasReleaseFor(const uint32_t ms) const
155 {
156 return _oldPress && !_press && _lastHoldPeriod >= ms;
157 }
159 bool pressedFor(const uint32_t ms) const
160 {
161 return (_press && _lastMsec - _lastChange >= ms);
162 }
164 bool releasedFor(const uint32_t ms) const
165 {
166 return (!_press && _lastMsec - _lastChange >= ms);
167 }
169
172 void setRawState(const uint32_t msec, const bool press);
173 void setState(const uint32_t msec, const button_state_t state);
174 inline button_state_t getState(void) const
175 {
176 return _currentState;
177 }
178 inline uint32_t lastChange(void) const
179 {
180 return _lastChange;
181 }
182 inline uint32_t getUpdateMsec(void) const
183 {
184 return _lastMsec;
185 }
187
188private:
189 uint16_t _msecHold{500}, _msecDebounce{10};
190 // 0 follows _msecHold, which is what this class did before the threshold was separated
191 uint16_t _msecDoubleClick{};
192 uint32_t _lastMsec{}, _lastChange{}, _lastRawChange{}, _lastClicked{};
193 uint16_t _lastHoldPeriod{};
194 button_state_t _currentState{button_state_t::state_nochange};
195 bool _raw_press{};
196 // 0:release 1:click 2:holding
197 uint8_t _press{}, _oldPress{}, _clickCount{};
198};
199
200} // namespace button
201} // namespace utility
202} // namespace m5
203
204#endif
bool wasReleased(void) const
Returns true if button was released.
Definition button_status.hpp:109
bool wasDecideClickCount(void) const
Returns true when some time has passed since the button was multiple clicked.
Definition button_status.hpp:134
bool wasSingleClicked(void) const
Returns true when some time has passed since the button was single clicked.
Definition button_status.hpp:124
Status(const uint16_t hold_ms=500, const uint16_t debounce_ms=10, const uint16_t double_click_ms=0)
Constructor.
Definition button_status.hpp:39
bool pressedFor(const uint32_t ms) const
Is pressed for more than the specified time?
Definition button_status.hpp:159
uint32_t getDoubleClickThreshold(void) const
Gets the time within which a second click still counts as a multiple click(ms)
Definition button_status.hpp:80
bool isHolding(void) const
Returns true if the button is currently held pressed.
Definition button_status.hpp:99
bool wasClicked(void) const
Returns true when the button is pressed briefly and released.
Definition button_status.hpp:114
bool wasDoubleClicked(void) const
Returns true when some time has passed since the button was double clicked.
Definition button_status.hpp:129
void setHoldThreshold(const uint32_t msec)
Set time to be considered hold(ms)
Definition button_status.hpp:52
bool isReleased(void) const
Is released?
Definition button_status.hpp:94
void setDebounceThreshold(const uint32_t msec)
Set debounce time(ms)
Definition button_status.hpp:47
bool wasReleasedAfterHold(void) const
Pressed and released a button for more than the set hold time?
Definition button_status.hpp:149
bool wasReleaseFor(const uint32_t ms) const
Was it pressed for more than the specified time?
Definition button_status.hpp:154
bool wasHold(void) const
Returns true when the button has been held pressed for a while.
Definition button_status.hpp:119
button_state_t
Button status.
Definition button_status.hpp:30
bool wasPressed(void) const
Returns true if button was pressed.
Definition button_status.hpp:104
uint8_t getClickCount(void) const
Gets the number of consecutive button clicks.
Definition button_status.hpp:139
bool releasedFor(const uint32_t ms) const
Is released for more than the specified time?
Definition button_status.hpp:164
bool wasChangePressed(void) const
Has the button press state changed?
Definition button_status.hpp:144
bool isPressed(void) const
Is pressed?
Definition button_status.hpp:89
uint32_t getDebounceThreshold(void) const
Gets the debounce time(ms)
Definition button_status.hpp:67
void setDoubleClickThreshold(const uint32_t msec)
Set the time within which a second click still counts as a multiple click(ms)
Definition button_status.hpp:62
uint32_t getHoldThreshold(void) const
Gets the time to be considered hold(ms)
Definition button_status.hpp:72
Top level namespace of M5.
Definition base64.cpp:39
For utilities.