forked from Nazrax/DreamMaskV4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.c
78 lines (64 loc) · 1.68 KB
/
buttons.c
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
#include "globals.h"
#include "buttons.h"
static void _update(button_t *button);
void buttons_age() {
int i;
bool_t allButtonsUp = true;
for(i=0; i<BUTTON_COUNT; i++) {
buttons[i].old = buttons[i].current;
if (buttons[i].current == DOWN)
allButtonsUp = false;
}
if (allButtonsUp)
doublePress = longDoublePress = reallyLongDoublePress = false;
}
void buttons_update() {
int i;
bool_t allButtonsDown = true;
uint32_t time = 0;
for(i=0; i<BUTTON_COUNT; i++) {
_update(&buttons[i]);
if (buttons[i].update_time > time)
time = buttons[i].update_time;
if (buttons[i].current == UP)
allButtonsDown = false;
}
if (allButtonsDown) {
doublePress = true;
uint32_t duration = clock_ticks - time;
if (duration > TICKS_PER_SECOND * 4) {
if (!reallyLongDoublePress) {
reallyLongDoublePress = true;
}
} else if (duration > TICKS_PER_SECOND * 2) {
if (!longDoublePress) {
longDoublePress = true;
}
}
}
}
bool_t pressed(button_t *button) {
return (button->old == DOWN) && (button->current == UP);
}
static void _update(button_t *button) {
button_state_t state;
if (button->port == PB) {
state = !(PINB & _BV(button->pin));
} else if (button->port == PC) {
state = !(PINC & _BV(button->pin));
} else if (button->port == PD) {
state = !(PIND & _BV(button->pin));
} else {
state = 0;
// ERROR
}
if (state != button->current) {
if (button->update_time + 1 < clock_ticks) {
button->old = button->current;
button->current = state;
button->update_time = clock_ticks;
if (state == DOWN)
button->down_time = clock_ticks;
}
}
}