roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
push_button.cpp
Go to the documentation of this file.
2
3namespace roo_control {
4
5using roo_time::Duration;
6using roo_time::Millis;
7using roo_time::Uptime;
8
9static constexpr Duration kDebounceInterval = Millis(20);
10static constexpr Duration kLongPressInterval = Millis(500);
11static constexpr Duration kDoubleClickDeadline = Millis(500);
12
14 : selector_(selector),
15 idle_state_(idle_state),
16 has_state_(false),
17 last_state_change_time_(Uptime::Now()),
18 prev_state_change_time_(last_state_change_time_),
19 last_click_time_(last_state_change_time_),
20 is_pressed_(false) {}
21
23 Uptime now = Uptime::Now();
24 if (now - last_state_change_time_ < kDebounceInterval) return;
25 BinaryLogicalState new_state;
26 if (!selector_.getState(new_state)) return;
27 if (has_state_ && new_state == state_) {
28 // Maybe long-press?
29 if (is_pressed_ && now - last_state_change_time_ > kLongPressInterval) {
31 is_pressed_ = false;
32 }
33 has_state_ = true;
34 return;
35 }
36 prev_state_change_time_ = last_state_change_time_;
37 last_state_change_time_ = now;
38 state_ = new_state;
39 if (state_ != idle_state_) {
40 is_pressed_ = true;
41 onDown();
42 } else {
43 onUp();
44 if (is_pressed_) {
45 if (now - last_click_time_ < kDoubleClickDeadline) {
47 } else {
48 onClick();
49 }
50 last_click_time_ = now;
51 is_pressed_ = false;
52 }
53 }
54 has_state_ = true;
55}
56
57} // namespace roo_control
virtual void onUp()
Definition push_button.h:33
virtual void onDown()
Hooks called from tick().
Definition push_button.h:32
virtual void onLongPress()
Definition push_button.h:35
PushButton(BinarySelector &selector, BinaryLogicalState idle_state=BINARY_STATE_HIGH)
virtual void onDoubleClick()
Definition push_button.h:36
void tick()
Needs to be called periodically (every 5-10 ms).
virtual void onClick()
Definition push_button.h:34
virtual bool getState(State &result) const =0
Retrieves the current state, or returns false when it cannot be read.
static constexpr Duration kLongPressInterval
static constexpr Duration kDebounceInterval
static constexpr Duration kDoubleClickDeadline
BinaryLogicalState
Binary logical state used by selectors and switches.