M5Unified
Button_Class.hpp
Go to the documentation of this file.
1 // Copyright (c) M5Stack. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #ifndef __M5_BUTTON_CLASS_H__
5 #define __M5_BUTTON_CLASS_H__
6 
7 #include <cstdint>
8 
9 namespace m5
10 {
12  {
13  public:
14  enum button_state_t : std::uint8_t
19  };
20 
22  bool wasClicked(void) const { return _currentState == state_clicked; }
23 
25  bool wasHold(void) const { return _currentState == state_hold; }
26 
28  bool wasSingleClicked(void) const { return _currentState == state_decide_click_count && _clickCount == 1; }
29 
31  bool wasDoubleClicked(void) const { return _currentState == state_decide_click_count && _clickCount == 2; }
32 
34  bool wasDecideClickCount(void) const { return _currentState == state_decide_click_count; }
35 
36  [[deprecated("use wasDecideClickCount()")]]
37  bool wasDeciedClickCount(void) const { return wasDecideClickCount(); }
38 
39  std::uint8_t getClickCount(void) const { return _clickCount; }
40 
42  bool isHolding(void) const { return _press == 2; }
43  bool wasChangePressed(void) const { return ((bool)_press) != ((bool)_oldPress); }
44 
45  bool isPressed(void) const { return _press; }
46  bool isReleased(void) const { return !_press; }
47  bool wasPressed(void) const { return !_oldPress && _press; }
48  bool wasReleased(void) const { return _oldPress && !_press; }
49  bool wasReleasedAfterHold(void) const { return !_press && _oldPress == 2; }
50  bool wasReleaseFor(std::uint32_t ms) const { return _oldPress && !_press && _lastHoldPeriod >= ms; }
51 
52  [[deprecated("use wasReleaseFor()")]]
53  bool wasReleasefor(std::uint32_t ms) const { return wasReleaseFor(ms); }
54  bool pressedFor(std::uint32_t ms) const { return (_press && _lastMsec - _lastChange >= ms); }
55  bool releasedFor(std::uint32_t ms) const { return (!_press && _lastMsec - _lastChange >= ms); }
56 
57  void setDebounceThresh(std::uint32_t msec) { _msecDebounce = msec; }
58  void setHoldThresh(std::uint32_t msec) { _msecHold = msec; }
59 
60  void setRawState(std::uint32_t msec, bool press);
61  void setState(std::uint32_t msec, button_state_t state);
62  button_state_t getState(void) const { return _currentState; }
63  std::uint32_t lastChange(void) const { return _lastChange; }
64 
65  std::uint32_t getDebounceThresh(void) const { return _msecDebounce; }
66  std::uint32_t getHoldThresh(void) const { return _msecHold; }
67 
68  std::uint32_t getUpdateMsec(void) const { return _lastMsec; }
69  private:
70  std::uint32_t _lastMsec = 0;
71  std::uint32_t _lastChange = 0;
72  std::uint32_t _lastRawChange = 0;
73  std::uint32_t _lastClicked = 0;
74  std::uint16_t _msecDebounce = 10;
75  std::uint16_t _msecHold = 500;
76  std::uint16_t _lastHoldPeriod = 0;
77  button_state_t _currentState = state_nochange; // 0:nochange 1:click 2:hold
78  bool _raw_press = false;
79  std::uint8_t _press = 0; // 0:release 1:click 2:holding
80  std::uint8_t _oldPress = 0;
81  std::uint8_t _clickCount = 0;
82  };
83 
84 }
85 
86 #endif
bool isPressed(void) const
bool wasChangePressed(void) const
bool wasHold(void) const
Returns true when the button has been held pressed for a while.
button_state_t getState(void) const
std::uint8_t getClickCount(void) const
bool wasDeciedClickCount(void) const
bool wasReleasefor(std::uint32_t ms) const
std::uint32_t getUpdateMsec(void) const
void setDebounceThresh(std::uint32_t msec)
bool releasedFor(std::uint32_t ms) const
bool wasDecideClickCount(void) const
Returns true when some time has passed since the button was multiple clicked.
bool wasClicked(void) const
Returns true when the button is pressed briefly and released.
void setState(std::uint32_t msec, button_state_t state)
Definition: Button_Class.cpp:8
std::uint32_t getDebounceThresh(void) const
std::uint32_t getHoldThresh(void) const
void setHoldThresh(std::uint32_t msec)
bool isHolding(void) const
Returns true if the button is currently held pressed.
bool wasPressed(void) const
bool wasDoubleClicked(void) const
Returns true when some time has passed since the button was double clicked.
bool pressedFor(std::uint32_t ms) const
bool wasReleasedAfterHold(void) const
bool wasSingleClicked(void) const
Returns true when some time has passed since the button was single clicked.
bool wasReleased(void) const
std::uint32_t lastChange(void) const
bool isReleased(void) const
bool wasReleaseFor(std::uint32_t ms) const
void setRawState(std::uint32_t msec, bool press)
Definition: M5Unified.cpp:48