M5Utility 0.0.3 git rev:e82ed02
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
37 Status(const uint16_t hold_ms = 500, const uint16_t debounce_ms = 10)
38 : _msecHold{hold_ms}, _msecDebounce{debounce_ms}
39 {
40 }
41
45 inline void setDebounceThreshold(const uint32_t msec)
46 {
47 _msecDebounce = msec;
48 }
50 inline void setHoldThreshold(const uint32_t msec)
51 {
52 _msecHold = msec;
53 }
55 inline uint32_t getDebounceThreshold(void) const
56 {
57 return _msecDebounce;
58 }
60 inline uint32_t getHoldThreshold(void) const
61 {
62 return _msecHold;
63 }
65
69 bool isPressed(void) const
70 {
71 return _press;
72 }
74 bool isReleased(void) const
75 {
76 return !_press;
77 }
79 bool isHolding(void) const
80 {
81 return _press == 2;
82 }
84 bool wasPressed(void) const
85 {
86 return !_oldPress && _press;
87 }
89 bool wasReleased(void) const
90 {
91 return _oldPress && !_press;
92 }
94 bool wasClicked(void) const
95 {
96 return _currentState == button_state_t::state_clicked;
97 }
99 bool wasHold(void) const
100 {
101 return _currentState == button_state_t::state_hold;
102 }
104 bool wasSingleClicked(void) const
105 {
106 return _currentState == button_state_t::state_decide_click_count && _clickCount == 1;
107 }
109 bool wasDoubleClicked(void) const
110 {
111 return _currentState == button_state_t::state_decide_click_count && _clickCount == 2;
112 }
114 bool wasDecideClickCount(void) const
115 {
116 return _currentState == button_state_t::state_decide_click_count;
117 }
119 uint8_t getClickCount(void) const
120 {
121 return _clickCount;
122 }
124 bool wasChangePressed(void) const
125 {
126 return ((bool)_press) != ((bool)_oldPress);
127 }
129 bool wasReleasedAfterHold(void) const
130 {
131 return !_press && _oldPress == 2;
132 }
134 bool wasReleaseFor(const uint32_t ms) const
135 {
136 return _oldPress && !_press && _lastHoldPeriod >= ms;
137 }
139 bool pressedFor(const uint32_t ms) const
140 {
141 return (_press && _lastMsec - _lastChange >= ms);
142 }
144 bool releasedFor(const uint32_t ms) const
145 {
146 return (!_press && _lastMsec - _lastChange >= ms);
147 }
149
152 void setRawState(const uint32_t msec, const bool press);
153 void setState(const uint32_t msec, const button_state_t state);
154 inline button_state_t getState(void) const
155 {
156 return _currentState;
157 }
158 inline uint32_t lastChange(void) const
159 {
160 return _lastChange;
161 }
162 inline uint32_t getUpdateMsec(void) const
163 {
164 return _lastMsec;
165 }
167
168private:
169 uint16_t _msecHold{500}, _msecDebounce{10};
170 uint32_t _lastMsec{}, _lastChange{}, _lastRawChange{}, _lastClicked{};
171 uint16_t _lastHoldPeriod{};
172 button_state_t _currentState{button_state_t::state_nochange};
173 bool _raw_press{};
174 // 0:release 1:click 2:holding
175 uint8_t _press{}, _oldPress{}, _clickCount{};
176};
177
178} // namespace button
179} // namespace utility
180} // namespace m5
181
182#endif
bool wasReleased(void) const
Returns true if button was released.
Definition button_status.hpp:89
bool wasDecideClickCount(void) const
Returns true when some time has passed since the button was multiple clicked.
Definition button_status.hpp:114
bool wasSingleClicked(void) const
Returns true when some time has passed since the button was single clicked.
Definition button_status.hpp:104
bool pressedFor(const uint32_t ms) const
Is pressed for more than the specified time?
Definition button_status.hpp:139
bool isHolding(void) const
Returns true if the button is currently held pressed.
Definition button_status.hpp:79
bool wasClicked(void) const
Returns true when the button is pressed briefly and released.
Definition button_status.hpp:94
bool wasDoubleClicked(void) const
Returns true when some time has passed since the button was double clicked.
Definition button_status.hpp:109
void setHoldThreshold(const uint32_t msec)
Set time to be considered hold(ms)
Definition button_status.hpp:50
bool isReleased(void) const
Is released?
Definition button_status.hpp:74
void setDebounceThreshold(const uint32_t msec)
Set debounce time(ms)
Definition button_status.hpp:45
bool wasReleasedAfterHold(void) const
Pressed and released a button for more than the set hold time?
Definition button_status.hpp:129
bool wasReleaseFor(const uint32_t ms) const
Was it pressed for more than the specified time?
Definition button_status.hpp:134
bool wasHold(void) const
Returns true when the button has been held pressed for a while.
Definition button_status.hpp:99
button_state_t
Button status.
Definition button_status.hpp:30
bool wasPressed(void) const
Returns true if button was pressed.
Definition button_status.hpp:84
uint8_t getClickCount(void) const
Gets the number of consecutive button clicks.
Definition button_status.hpp:119
bool releasedFor(const uint32_t ms) const
Is released for more than the specified time?
Definition button_status.hpp:144
bool wasChangePressed(void) const
Has the button press state changed?
Definition button_status.hpp:124
Status(const uint16_t hold_ms=500, const uint16_t debounce_ms=10)
Constructor.
Definition button_status.hpp:37
bool isPressed(void) const
Is pressed?
Definition button_status.hpp:69
uint32_t getDebounceThreshold(void) const
Gets the debounce time(ms)
Definition button_status.hpp:55
uint32_t getHoldThreshold(void) const
Gets the time to be considered hold(ms)
Definition button_status.hpp:60
Top level namespace of M5.
Definition bit_segment.hpp:17
For utilities.