M5Unified
Button_Class.cpp
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 #include "Button_Class.hpp"
5 
6 namespace m5
7 {
8  void Button_Class::setState(std::uint32_t msec, button_state_t state)
9  {
10  if (_currentState == state_decide_click_count)
11  {
12  _clickCount = 0;
13  }
14 
15  _lastMsec = msec;
16  bool flg_timeout = (msec - _lastClicked > _msecHold);
17  switch (state)
18  {
19  case state_nochange:
20  if (flg_timeout && !_press && _clickCount)
21  {
22  if (_oldPress == 0 && _currentState == state_nochange)
23  {
25  }
26  else { _clickCount = 0; }
27  }
28  break;
29 
30  case state_clicked:
31  ++_clickCount;
32  _lastClicked = msec;
33  break;
34 
35  default:
36  break;
37  }
38  _currentState = state;
39  }
40 
41  void Button_Class::setRawState(std::uint32_t msec, bool press)
42  {
43  button_state_t state = button_state_t::state_nochange;
44  bool disable_db = (msec - _lastMsec) > _msecDebounce;
45  auto oldPress = _press;
46  _oldPress = oldPress;
47  if (_raw_press != press)
48  {
49  _raw_press = press;
50  _lastRawChange = msec;
51  }
52  if (disable_db || msec - _lastRawChange >= _msecDebounce)
53  {
54  if (press != (0 != oldPress))
55  {
56  _lastChange = msec;
57  }
58 
59  if (press)
60  {
61  std::uint32_t holdPeriod = msec - _lastChange;
62  _lastHoldPeriod = holdPeriod;
63  if (!oldPress)
64  {
65  _press = 1;
66  } else
67  if (oldPress == 1 && (holdPeriod >= _msecHold))
68  {
69  _press = 2;
70  state = button_state_t::state_hold;
71  }
72  }
73  else
74  {
75  _press = 0;
76  if (oldPress == 1)
77  {
78  state = button_state_t::state_clicked;
79  }
80  }
81  }
82  setState(msec, state);
83  }
84 }
void setState(std::uint32_t msec, button_state_t state)
Definition: Button_Class.cpp:8
void setRawState(std::uint32_t msec, bool press)
Definition: M5Unified.cpp:48