-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
95 lines (75 loc) · 1.52 KB
/
Input.cpp
File metadata and controls
95 lines (75 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Input.h"
#include <array>
#include <cstddef>
#include <Button2.h>
#include "AppConfig.h"
namespace input
{
namespace
{
Button2 s_button(app_config::kPinButton);
std::array<ButtonEvent, 8> s_queue{};
std::size_t s_head = 0;
std::size_t s_tail = 0;
void pushEvent(ButtonEvent event)
{
const std::size_t next = (s_head + 1U) % s_queue.size();
if (next == s_tail)
{
return;
}
s_queue[s_head] = event;
s_head = next;
}
void onPressed(Button2 &)
{
pushEvent(ButtonEvent::PressStarted);
}
void onReleased(Button2 &)
{
pushEvent(ButtonEvent::PressReleased);
}
void onClick(Button2 &)
{
pushEvent(ButtonEvent::ShortPress);
}
void onDoubleClick(Button2 &)
{
pushEvent(ButtonEvent::DoublePress);
}
void onLongDetected(Button2 &)
{
pushEvent(ButtonEvent::LongPressArmed);
}
void onLongClick(Button2 &)
{
pushEvent(ButtonEvent::LongPress);
}
} // namespace
void begin()
{
s_button.begin(app_config::kPinButton);
s_button.setLongClickTime(app_config::kButtonLongPressMs);
s_button.setPressedHandler(onPressed);
s_button.setReleasedHandler(onReleased);
s_button.setClickHandler(onClick);
s_button.setDoubleClickHandler(onDoubleClick);
s_button.setLongClickDetectedHandler(onLongDetected);
s_button.setLongClickHandler(onLongClick);
}
void tick()
{
s_button.loop();
}
bool pollEvent(ButtonEvent &event)
{
if (s_head == s_tail)
{
event = ButtonEvent::None;
return false;
}
event = s_queue[s_tail];
s_tail = (s_tail + 1U) % s_queue.size();
return true;
}
} // namespace input