-
Notifications
You must be signed in to change notification settings - Fork 8
/
keys.c
executable file
·266 lines (182 loc) · 5.71 KB
/
keys.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "keys.h"
#define MAX_GPIO_KEYS 64
#define MAX_KP_ROWS 12
#define MAX_KP_COLS 8
struct KeyGpio {
uint32_t sdlKey; //0 for inval
int8_t gpioNum;
bool activeHigh;
};
struct KeyMatrix {
uint32_t sdlKey; //0 for inval
bool isDown;
};
struct Keypad {
struct SocGpio *gpio;
struct KeyGpio gpios[MAX_GPIO_KEYS];
int8_t kpGpioRow[MAX_KP_ROWS]; //<0 for inval
int8_t kpGpioCol[MAX_KP_COLS]; //<0 for inval
struct KeyMatrix km[MAX_KP_ROWS][MAX_KP_COLS];
bool recalcing, matrixHasPullUps;
};
static void keypadPrvMatrixRecalc(struct Keypad *kp)
{
int i, j;
kp->recalcing = true;
//calc input rows
for (i = 0; i < MAX_KP_ROWS; i++) {
bool rowState = kp->matrixHasPullUps;
bool haveStrong = false;
if (kp->kpGpioRow[i] < 0)
continue;
if (socGpioGetState(kp->gpio, kp->kpGpioRow[i]) != SocGpioStateHiZ)
continue;
for (j = 0; j < MAX_KP_COLS; j++) {
uint8_t colSta;
if (kp->kpGpioCol[j] < 0)
continue;
if (!kp->km[i][j].isDown)
continue;
colSta = socGpioGetState(kp->gpio, kp->kpGpioCol[j]);
if (colSta == SocGpioStateLow || colSta == SocGpioStateHigh) {
bool colHi = colSta == SocGpioStateHigh;
if (haveStrong && !rowState != !colHi)
fprintf(stderr, "row %u (%u) being pulled in different directions\n", i, kp->kpGpioRow[i]);
haveStrong = true;
rowState = rowState && colHi;
}
}
socGpioSetState(kp->gpio, kp->kpGpioRow[i], rowState);
}
//calc input cols
for (j = 0; j < MAX_KP_COLS; j++) {
bool colState = kp->matrixHasPullUps;
bool haveStrong = false;
if (kp->kpGpioCol[j] < 0)
continue;
if (socGpioGetState(kp->gpio, kp->kpGpioCol[j]) != SocGpioStateHiZ)
continue;
for (i = 0; i < MAX_KP_ROWS; i++) {
uint8_t rowSta;
if (kp->kpGpioRow[i] < 0)
continue;
if (!kp->km[i][j].isDown)
continue;
rowSta = socGpioGetState(kp->gpio, kp->kpGpioRow[i]);
if (rowSta == SocGpioStateLow || rowSta == SocGpioStateHigh) {
bool rowHi = rowSta == SocGpioStateHigh;
if (haveStrong && !rowHi != !colState)
fprintf(stderr, "col %u (%u) being pulled in different directions\n", j, kp->kpGpioCol[j]);
haveStrong = true;
colState = rowHi && colState;
}
}
socGpioSetState(kp->gpio, kp->kpGpioCol[j], colState);
}
kp->recalcing = false;
}
static void keypadPrvGpioDirsChanged(void* userData)
{
struct Keypad *kp = (struct Keypad*)userData;
if (!kp->recalcing)
keypadPrvMatrixRecalc(kp);
}
struct Keypad* keypadInit(struct SocGpio *gpio, bool matrixHasPullUps)
{
struct Keypad *kp = (struct Keypad*)malloc(sizeof(*kp));
unsigned i;
if (!kp)
ERR("cannot alloc Keypad");
memset(kp, 0, sizeof (*kp));
kp->gpio = gpio;
kp->matrixHasPullUps = matrixHasPullUps;
for (i = 0; i < MAX_KP_ROWS; i++)
kp->kpGpioRow[i] = -1;
for (i = 0; i < MAX_KP_COLS; i++)
kp->kpGpioCol[i] = -1;
socGpioSetDirsChangedNotif(kp->gpio, keypadPrvGpioDirsChanged, kp);
return kp;
}
void keypadSdlKeyEvt(struct Keypad *kp, uint32_t sdlKey, bool wentDown)
{
int i, j;
for (i = 0; i < MAX_GPIO_KEYS; i++) {
if (kp->gpios[i].sdlKey == sdlKey && kp->gpios[i].gpioNum >= 0) {
socGpioSetState(kp->gpio, kp->gpios[i].gpioNum, (wentDown && kp->gpios[i].activeHigh) || (!wentDown && !kp->gpios[i].activeHigh));
}
}
for (i = 0; i < MAX_KP_ROWS; i++) {
if (kp->kpGpioRow[i] < 0)
continue;
for (j = 0; j < MAX_KP_COLS; j++) {
if (kp->kpGpioCol[j] < 0)
continue;
if (kp->km[i][j].sdlKey != sdlKey)
continue;
kp->km[i][j].isDown = wentDown;
}
}
keypadPrvMatrixRecalc(kp);
}
static void keypadPrvGpioChanged(void* userData, uint32_t gpio, bool oldState, bool newState)
{
struct Keypad *kp = (struct Keypad*)userData;
(void)gpio;
(void)oldState;
(void)newState;
if (!kp->recalcing)
keypadPrvMatrixRecalc(kp);
}
static bool keypadDefineRowOrCol(struct Keypad *kp, unsigned idx, int8_t *arr, unsigned max, int8_t gpioNum)
{
if (idx >= max)
return false;
if (arr[idx] >= 0)
return false;
arr[idx] = gpioNum;
socGpioSetNotif(kp->gpio, gpioNum, keypadPrvGpioChanged, kp);
socGpioSetState(kp->gpio, gpioNum, kp->matrixHasPullUps);
keypadPrvMatrixRecalc(kp);
return true;
}
bool keypadDefineRow(struct Keypad *kp, unsigned rowIdx, int8_t gpio)
{
return keypadDefineRowOrCol(kp, rowIdx, kp->kpGpioRow, MAX_KP_ROWS, gpio);
}
bool keypadDefineCol(struct Keypad *kp, unsigned colIdx, int8_t gpio)
{
return keypadDefineRowOrCol(kp, colIdx, kp->kpGpioCol, MAX_KP_COLS, gpio);
}
bool keypadAddGpioKey(struct Keypad *kp, uint32_t sdlKey, int8_t gpioNum, bool activeHigh)
{
unsigned i;
for (i = 0; i < MAX_GPIO_KEYS; i++) {
if (!kp->gpios[i].sdlKey) {
kp->gpios[i].sdlKey = sdlKey;
kp->gpios[i].gpioNum = gpioNum;
kp->gpios[i].activeHigh = activeHigh;
socGpioSetState(kp->gpio, gpioNum, !activeHigh);
return true;
}
}
return false;
}
bool keypadAddMatrixKey(struct Keypad *kp, uint32_t sdlKey, unsigned row, unsigned col)
{
//coords must be valid
if (row >= MAX_KP_ROWS || col >= MAX_KP_COLS)
return false;
//and rows and cols must be hooked up
if (kp->kpGpioRow[row] < 0 || kp->kpGpioCol[col] < 0)
return false;
//must be unused
if (kp->km[row][col].sdlKey)
return false;
kp->km[row][col].sdlKey = sdlKey;
kp->km[row][col].isDown = false;
return true;
}