-
Notifications
You must be signed in to change notification settings - Fork 2
/
input_handler.cpp
55 lines (42 loc) · 1.54 KB
/
input_handler.cpp
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
#include "input_handler.h"
static const uint8_t all_buttons[] = { LEFT_BUTTON, RIGHT_BUTTON, UP_BUTTON, DOWN_BUTTON, A_BUTTON, B_BUTTON };
static unsigned int index_for_button(uint8_t button) {
unsigned int index = 0;
while (all_buttons[index] != button && index < NUM_BUTTONS) index++;
return index;
}
InputHandler::InputHandler()
{
const unsigned long now = millis();
for (unsigned int button_idx = 0; button_idx < NUM_BUTTONS; button_idx++) {
_last_seen_state[button_idx] = false;
_last_seen_time[button_idx] = now;
}
}
unsigned long InputHandler::last_time_for_button(uint8_t button)
{
unsigned int index = index_for_button(button);
return _last_seen_time[index];
}
bool InputHandler::last_state_for_button(uint8_t button)
{
unsigned int index = index_for_button(button);
return _last_seen_state[index];
}
void InputHandler::handle_input(Arduboy& adb)
{
const unsigned long now = millis();
for (unsigned int button_idx = 0; button_idx < NUM_BUTTONS; button_idx++) {
uint8_t button = all_buttons[button_idx];
bool last_state = last_state_for_button(button);
unsigned long last_time = last_time_for_button(button);
bool down = adb.pressed(button);
if (down || down != last_state) {
if ( (now - last_time) > repeat_rate || down != last_state ) {
if (button_handler) button_handler(button, down);
_last_seen_time[button_idx] = now;
}
_last_seen_state[button_idx] = down;
}
}
}