roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
push_button.h
Go to the documentation of this file.
1#pragma once
2
4#include "roo_time.h"
5
6namespace roo_control {
7
8/// Represents a human-pressable momentary button.
9///
10/// This class implements debouncing and provides a simple form of gesture
11/// detection.
12class PushButton : public BinarySelector {
13 public:
14 PushButton(BinarySelector& selector,
16
17 bool getState(BinaryLogicalState& result) const override {
18 if (!has_state_) return false;
19 result = state_;
20 return true;
21 }
22
23 /// Needs to be called periodically (every 5-10 ms).
24 void tick();
25
26 protected:
27 /// Hooks called from tick().
28 ///
29 /// If you call tick() from a time-sensitive context, you may want to hand
30 /// off the events to a queue in the implementation of these methods.
31
32 virtual void onDown() {}
33 virtual void onUp() {}
34 virtual void onClick() {}
35 virtual void onLongPress() {}
36 virtual void onDoubleClick() {}
37
38 private:
39 BinarySelector& selector_;
40 const BinaryLogicalState idle_state_;
41
42 bool has_state_;
43 BinaryLogicalState state_;
44 roo_time::Uptime last_state_change_time_;
45 roo_time::Uptime prev_state_change_time_;
46 roo_time::Uptime last_click_time_;
47 bool is_pressed_;
48};
49
50} // namespace roo_control
Represents a human-pressable momentary button.
Definition push_button.h:12
virtual void onUp()
Definition push_button.h:33
bool getState(BinaryLogicalState &result) const override
Retrieves the current state, or returns false when it cannot be read.
Definition push_button.h:17
virtual void onDown()
Hooks called from tick().
Definition push_button.h:32
virtual void onLongPress()
Definition push_button.h:35
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
BinaryLogicalState
Binary logical state used by selectors and switches.