forked from solderparty/i2c_puppet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.c
415 lines (328 loc) · 9.44 KB
/
keyboard.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "app_config.h"
#include "fifo.h"
#include "keyboard.h"
#include "reg.h"
#include <pico/stdlib.h>
#define LIST_SIZE 10 // size of the list keeping track of all the pressed keys
struct entry
{
char chr;
char alt;
enum key_mod mod;
};
struct list_item
{
const struct entry *p_entry;
uint32_t hold_start_time;
enum key_state state;
bool mods[KEY_MOD_ID_LAST];
char effective_key;
};
static const uint8_t row_pins[NUM_OF_ROWS] =
{
PINS_ROWS
};
static const uint8_t col_pins[NUM_OF_COLS] =
{
PINS_COLS
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static const struct entry kbd_entries[][NUM_OF_COLS] =
{
{ { KEY_JOY_CENTER }, { 'W', '1' }, { 'G', '/' }, { 'S', '4' }, { 'L', '"' }, { 'H' , ':' } },
{ { }, { 'Q', '#' }, { 'R', '3' }, { 'E', '2' }, { 'O', '+' }, { 'U', '_' } },
{ { KEY_BTN_LEFT1 }, { '~', '0' }, { 'F', '6' }, { .mod = KEY_MOD_ID_SHL }, { 'K', '\'' }, { 'J', ';' } },
{ { }, { ' ', '\t' }, { 'C', '9' }, { 'Z', '7' }, { 'M', '.' }, { 'N', ',' } },
{ { KEY_BTN_LEFT2 }, { .mod = KEY_MOD_ID_SYM }, { 'T', '(' }, { 'D', '5' }, { 'I', '-' }, { 'Y', ')' } },
{ { KEY_BTN_RIGHT1 }, { .mod = KEY_MOD_ID_ALT }, { 'V', '?' }, { 'X', '8' }, { '$', '`' }, { 'B', '!' } },
{ { }, { 'A', '*' }, { .mod = KEY_MOD_ID_SHR }, { 'P', '@' }, { '\b' }, { '\n', '|' } },
};
#if NUM_OF_BTNS > 0
static const struct entry btn_entries[NUM_OF_BTNS] =
{
BTN_KEYS
};
static const uint8_t btn_pins[NUM_OF_BTNS] =
{
PINS_BTNS
};
#endif
#pragma GCC diagnostic pop
static struct
{
struct key_lock_callback *lock_callbacks;
struct key_callback *key_callbacks;
struct list_item list[LIST_SIZE];
bool mods[KEY_MOD_ID_LAST];
bool capslock_changed;
bool capslock;
bool numlock_changed;
bool numlock;
} self;
static void transition_to(struct list_item * const p_item, const enum key_state next_state)
{
const struct entry * const p_entry = p_item->p_entry;
p_item->state = next_state;
if (!p_entry)
return;
if (p_item->effective_key == '\0') {
char key = p_entry->chr;
switch (p_entry->mod) {
case KEY_MOD_ID_ALT:
if (reg_is_bit_set(REG_ID_CFG, CFG_REPORT_MODS))
key = KEY_MOD_ALT;
break;
case KEY_MOD_ID_SHL:
if (reg_is_bit_set(REG_ID_CFG, CFG_REPORT_MODS))
key = KEY_MOD_SHL;
break;
case KEY_MOD_ID_SHR:
if (reg_is_bit_set(REG_ID_CFG, CFG_REPORT_MODS))
key = KEY_MOD_SHR;
break;
case KEY_MOD_ID_SYM:
if (reg_is_bit_set(REG_ID_CFG, CFG_REPORT_MODS))
key = KEY_MOD_SYM;
break;
default:
{
if (reg_is_bit_set(REG_ID_CFG, CFG_USE_MODS)) {
const bool shift = (self.mods[KEY_MOD_ID_SHL] || self.mods[KEY_MOD_ID_SHR]) | self.capslock;
const bool alt = self.mods[KEY_MOD_ID_ALT] | self.numlock;
const bool is_button = (key <= KEY_BTN_RIGHT1) || ((key >= KEY_BTN_LEFT2) && (key <= KEY_BTN_RIGHT2));
if (alt && !is_button) {
key = p_entry->alt;
} else if (!shift && (key >= 'A' && key <= 'Z')) {
key = (key + ' ');
}
}
break;
}
}
p_item->effective_key = key;
}
if (p_item->effective_key == '\0')
return;
keyboard_inject_event(p_item->effective_key, next_state);
}
static void next_item_state(struct list_item * const p_item, const bool pressed)
{
switch (p_item->state) {
case KEY_STATE_IDLE:
if (pressed) {
if (p_item->p_entry->mod != KEY_MOD_ID_NONE)
self.mods[p_item->p_entry->mod] = true;
if (!self.capslock_changed && self.mods[KEY_MOD_ID_SHR] && self.mods[KEY_MOD_ID_ALT]) {
self.capslock = true;
self.capslock_changed = true;
}
if (!self.numlock_changed && self.mods[KEY_MOD_ID_SHL] && self.mods[KEY_MOD_ID_ALT]) {
self.numlock = true;
self.numlock_changed = true;
}
if (!self.capslock_changed && (self.mods[KEY_MOD_ID_SHL] || self.mods[KEY_MOD_ID_SHR])) {
self.capslock = false;
self.capslock_changed = true;
}
if (!self.numlock_changed && (self.mods[KEY_MOD_ID_SHL] || self.mods[KEY_MOD_ID_SHR])) {
self.numlock = false;
self.numlock_changed = true;
}
if (!self.mods[KEY_MOD_ID_ALT]) {
self.capslock_changed = false;
self.numlock_changed = false;
}
if (self.lock_callbacks && (self.capslock_changed || self.numlock_changed)) {
struct key_lock_callback *cb = self.lock_callbacks;
while (cb) {
cb->func(self.capslock_changed, self.numlock_changed);
cb = cb->next;
}
}
transition_to(p_item, KEY_STATE_PRESSED);
p_item->hold_start_time = to_ms_since_boot(get_absolute_time());
}
break;
case KEY_STATE_PRESSED:
if ((to_ms_since_boot(get_absolute_time()) - p_item->hold_start_time) > (reg_get_value(REG_ID_HLD) * 10)) {
transition_to(p_item, KEY_STATE_HOLD);
} else if(!pressed) {
transition_to(p_item, KEY_STATE_RELEASED);
}
break;
case KEY_STATE_HOLD:
if (!pressed)
transition_to(p_item, KEY_STATE_RELEASED);
break;
case KEY_STATE_RELEASED:
{
if (p_item->p_entry->mod != KEY_MOD_ID_NONE)
self.mods[p_item->p_entry->mod] = false;
p_item->p_entry = NULL;
p_item->effective_key = '\0';
transition_to(p_item, KEY_STATE_IDLE);
break;
}
}
}
static int64_t timer_task(alarm_id_t id, void *user_data)
{
(void)id;
(void)user_data;
for (uint32_t c = 0; c < NUM_OF_COLS; ++c) {
gpio_pull_up(col_pins[c]);
gpio_put(col_pins[c], 0);
gpio_set_dir(col_pins[c], GPIO_OUT);
for (uint32_t r = 0; r < NUM_OF_ROWS; ++r) {
const bool pressed = (gpio_get(row_pins[r]) == 0);
const int32_t key_idx = (int32_t)((r * NUM_OF_COLS) + c);
int32_t list_idx = -1;
for (int32_t i = 0; i < LIST_SIZE; ++i) {
if (self.list[i].p_entry != &((const struct entry*)kbd_entries)[key_idx])
continue;
list_idx = i;
break;
}
if (list_idx > -1) {
next_item_state(&self.list[list_idx], pressed);
continue;
}
if (!pressed)
continue;
for (uint32_t i = 0 ; i < LIST_SIZE; ++i) {
if (self.list[i].p_entry != NULL)
continue;
self.list[i].p_entry = &((const struct entry*)kbd_entries)[key_idx];
self.list[i].effective_key = '\0';
self.list[i].state = KEY_STATE_IDLE;
next_item_state(&self.list[i], pressed);
break;
}
}
gpio_put(col_pins[c], 1);
gpio_disable_pulls(col_pins[c]);
gpio_set_dir(col_pins[c], GPIO_IN);
}
#if NUM_OF_BTNS > 0
for (uint32_t b = 0; b < NUM_OF_BTNS; ++b) {
const bool pressed = (gpio_get(btn_pins[b]) == 0);
int32_t list_idx = -1;
for (int32_t i = 0; i < LIST_SIZE; ++i) {
if (self.list[i].p_entry != &((const struct entry*)btn_entries)[b])
continue;
list_idx = i;
break;
}
if (list_idx > -1) {
next_item_state(&self.list[list_idx], pressed);
continue;
}
if (!pressed)
continue;
for (uint32_t i = 0 ; i < LIST_SIZE; ++i) {
if (self.list[i].p_entry != NULL)
continue;
self.list[i].p_entry = &((const struct entry*)btn_entries)[b];
self.list[i].effective_key = '\0';
self.list[i].state = KEY_STATE_IDLE;
next_item_state(&self.list[i], pressed);
break;
}
}
#endif
// negative value means interval since last alarm time
return -(reg_get_value(REG_ID_FRQ) * 1000);
}
void keyboard_inject_event(char key, enum key_state state)
{
const struct fifo_item item = { key, state };
if (!fifo_enqueue(item)) {
if (reg_is_bit_set(REG_ID_CFG, CFG_OVERFLOW_INT))
reg_set_bit(REG_ID_INT, INT_OVERFLOW);
if (reg_is_bit_set(REG_ID_CFG, CFG_OVERFLOW_ON))
fifo_enqueue_force(item);
}
struct key_callback *cb = self.key_callbacks;
while (cb) {
cb->func(key, state);
cb = cb->next;
}
}
bool keyboard_is_key_down(char key)
{
for (int32_t i = 0; i < LIST_SIZE; ++i) {
struct list_item *item = &self.list[i];
if (item->p_entry == NULL)
continue;
if ((item->state != KEY_STATE_PRESSED) && (item->state != KEY_STATE_HOLD))
continue;
if (item->effective_key != key)
continue;
return true;
}
return false;
}
bool keyboard_is_mod_on(enum key_mod mod)
{
return self.mods[mod];
}
void keyboard_add_key_callback(struct key_callback *callback)
{
// first callback
if (!self.key_callbacks) {
self.key_callbacks = callback;
return;
}
// find last and insert after
struct key_callback *cb = self.key_callbacks;
while (cb->next)
cb = cb->next;
cb->next = callback;
}
void keyboard_add_lock_callback(struct key_lock_callback *callback)
{
// first callback
if (!self.lock_callbacks) {
self.lock_callbacks = callback;
return;
}
// find last and insert after
struct key_lock_callback *cb = self.lock_callbacks;
while (cb->next)
cb = cb->next;
cb->next = callback;
}
bool keyboard_get_capslock(void)
{
return self.capslock;
}
bool keyboard_get_numlock(void)
{
return self.numlock;
}
void keyboard_init(void)
{
for (int i = 0; i < KEY_MOD_ID_LAST; ++i)
self.mods[i] = false;
// rows
for (uint32_t i = 0; i < NUM_OF_ROWS; ++i) {
gpio_init(row_pins[i]);
gpio_pull_up(row_pins[i]);
gpio_set_dir(row_pins[i], GPIO_IN);
}
// cols
for(uint32_t i = 0; i < NUM_OF_COLS; ++i) {
gpio_init(col_pins[i]);
gpio_set_dir(col_pins[i], GPIO_IN);
}
// btns
#if NUM_OF_BTNS > 0
for(uint32_t i = 0; i < NUM_OF_BTNS; ++i) {
gpio_init(btn_pins[i]);
gpio_pull_up(btn_pins[i]);
gpio_set_dir(btn_pins[i], GPIO_IN);
}
#endif
add_alarm_in_ms(reg_get_value(REG_ID_FRQ), timer_task, NULL, true);
}