forked from prokey-io/prokey-optimum-firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons.c
executable file
·164 lines (147 loc) · 3.55 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (C) 2014 Pavol Rusnak <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "buttons.h"
#include "util.h"
struct buttonState button;
#if !EMULATOR
uint16_t buttonRead(void) {
return gpio_port_read(BTN_PORT);
}
#endif
void buttonUpdate()
{
uint16_t state;
static uint16_t last_state = BTN_PIN_YES | BTN_PIN_NO | BTN_PIN_UP | BTN_PIN_DOWN;
state = buttonRead();
if ((state & BTN_PIN_YES) == 0) { // Yes button is down
if ((last_state & BTN_PIN_YES) == 0) { // last Yes was down
if (button.YesDown < 2000000000) button.YesDown++;
button.YesUp = false;
} else { // last Yes was up
button.YesDown = 0;
button.YesUp = false;
}
} else { // Yes button is up
if ((last_state & BTN_PIN_YES) == 0) { // last Yes was down
button.YesDown = 0;
button.YesUp = true;
} else { // last Yes was up
button.YesDown = 0;
button.YesUp = false;
}
}
if ((state & BTN_PIN_NO) == 0) { // No button is down
if ((last_state & BTN_PIN_NO) == 0) { // last No was down
if (button.NoDown < 2000000000) button.NoDown++;
button.NoUp = false;
} else { // last No was up
button.NoDown = 0;
button.NoUp = false;
}
} else { // No button is up
if ((last_state & BTN_PIN_NO) == 0) { // last No was down
button.NoDown = 0;
button.NoUp = true;
} else { // last No was up
button.NoDown = 0;
button.NoUp = false;
}
}
//! <ProKey>
if ((state & BTN_PIN_UP) == 0)
{ // No button is down
if ((last_state & BTN_PIN_UP) == 0)
{ // last No was down
if (button.UpDown < 2000000000)
button.UpDown++;
button.UpUp = false;
}
else
{ // last No was up
button.UpDown = 0;
button.UpUp = false;
}
}
else
{ // up button is up
if ((last_state & BTN_PIN_UP) == 0)
{ // last up was down
button.UpDown = 0;
button.UpUp = true;
}
else
{ // last up was up
button.UpDown = 0;
button.UpUp = false;
}
}
if ((state & BTN_PIN_DOWN) == 0)
{ // No button is down
if ((last_state & BTN_PIN_DOWN) == 0)
{ // last No was down
if (button.DownDown < 2000000000)
button.DownDown++;
button.DownUp = false;
}
else
{ // last No was up
button.DownDown = 0;
button.DownUp = false;
}
}
else
{ // up button is up
if ((last_state & BTN_PIN_DOWN) == 0)
{ // last up was down
button.DownDown = 0;
button.DownUp = true;
}
else
{ // last up was up
button.DownDown = 0;
button.DownUp = false;
}
}
//! </ProKey>
last_state = state;
}
//**********************
//
//**********************
eButtons ButtonGet ( eButtons expected )
{
while(true)
{
#if !EMULATOR
delay(100000);
#endif
buttonUpdate();
eButtons b = BTN_NONE;
if( button.YesUp )
b |= BTN_YES;
if( button.NoUp )
b |= BTN_NO;
if( button.DownUp )
b |= BTN_DOWN;
if( button.UpUp )
b |= BTN_UP;
if( expected & b )
return b;
}
}