Skip to content

Commit e956f29

Browse files
Updated to use ringbuf and an event callback.
1 parent 064ac2d commit e956f29

File tree

7 files changed

+54
-49
lines changed

7 files changed

+54
-49
lines changed

ports/espressif/boards/m5stack_cardputer/board.c

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "keymap.h"
2828
#include "supervisor/board.h"
29-
#include "supervisor/serial.h"
29+
#include "supervisor/shared/serial.h"
3030
#include "mpconfigboard.h"
3131
#include "shared-bindings/busio/SPI.h"
3232
#include "shared-bindings/fourwire/FourWire.h"
@@ -39,13 +39,14 @@
3939
#include "shared-bindings/keypad/Event.h"
4040
#include "supervisor/shared/reload.h"
4141
#include "py/runtime.h"
42+
#include "py/ringbuf.h"
43+
#include "shared/runtime/interrupt_char.h"
4244

4345
fourwire_fourwire_obj_t board_display_obj;
4446
keypad_demux_demuxkeymatrix_obj_t board_keyboard;
4547

46-
void update_keyboard(void);
47-
void queue_key(char c);
48-
void queue_seq(const char *seq);
48+
void update_keyboard(keypad_eventqueue_obj_t *queue);
49+
void keyboard_seq(const char *seq);
4950

5051
const mcu_pin_obj_t *row_addr_pins[] = {
5152
&pin_GPIO8,
@@ -66,9 +67,8 @@ const mcu_pin_obj_t *column_pins[] = {
6667
keypad_event_obj_t event;
6768

6869
char keystate[56];
69-
char keyqueue[16];
70-
int keyqueue_head = 0;
71-
int keyqueue_tail = 0;
70+
ringbuf_t keyqueue;
71+
char keybuf[32];
7272

7373
#define DELAY 0x80
7474

@@ -144,6 +144,7 @@ void board_init(void) {
144144
}
145145

146146
void board_serial_init() {
147+
ringbuf_init(&keyqueue, (uint8_t *)keybuf, sizeof(keybuf));
147148
common_hal_keypad_demux_demuxkeymatrix_construct(
148149
&board_keyboard, // self
149150
3, // num_row_addr_pins
@@ -155,42 +156,32 @@ void board_serial_init() {
155156
2 // debounce_threshold
156157
);
157158
demuxkeymatrix_never_reset(&board_keyboard);
159+
common_hal_keypad_eventqueue_set_event_handler(board_keyboard.events, update_keyboard);
160+
158161
}
159162

160163
bool board_serial_connected() {
161164
return true;
162165
}
163166

164-
bool board_serial_bytes_available() {
165-
update_keyboard();
166-
if (keyqueue_head != keyqueue_tail) {
167-
return true;
168-
} else {
169-
return false;
170-
}
171-
}
172-
173-
void queue_key(char c) {
174-
if ((keyqueue_head + 1) % 16 != keyqueue_tail) {
175-
keyqueue[keyqueue_head] = c;
176-
keyqueue_head = (keyqueue_head + 1) % 16;
177-
}
167+
uint32_t board_serial_bytes_available() {
168+
return ringbuf_num_filled(&keyqueue);
178169
}
179170

180-
void queue_seq(const char *seq) {
171+
void keyboard_seq(const char *seq) {
181172
while (*seq) {
182-
queue_key(*seq++);
173+
ringbuf_put(&keyqueue, *seq++);
183174
}
184175
}
185176

186-
void update_keyboard() {
187-
char ascii = 0;
177+
void update_keyboard(keypad_eventqueue_obj_t *queue) {
178+
uint8_t ascii = 0;
188179

189-
if (common_hal_keypad_eventqueue_get_length(board_keyboard.events) == 0) {
180+
if (common_hal_keypad_eventqueue_get_length(queue) == 0) {
190181
return;
191182
}
192183

193-
while (common_hal_keypad_eventqueue_get_into(board_keyboard.events, &event)) {
184+
while (common_hal_keypad_eventqueue_get_into(queue, &event)) {
194185
if (event.pressed) {
195186
keystate[event.key_number] = 1;
196187

@@ -202,27 +193,31 @@ void update_keyboard() {
202193
if (ascii >= 'a' && ascii <= 'z') {
203194
ascii -= 'a' - 1;
204195
}
196+
197+
if (ascii == mp_interrupt_char) {
198+
mp_sched_keyboard_interrupt();
199+
}
205200
} else if (keystate[KEY_SHIFT]) {
206201
ascii = keymap_shifted[event.key_number];
207202
} else if (keystate[KEY_FN] && event.key_number != KEY_FN) {
208203
switch (event.key_number | FN_MOD) {
209204
case KEY_DOWN:
210-
queue_seq("\e[B");
205+
keyboard_seq("\e[B");
211206
break;
212207
case KEY_UP:
213-
queue_seq("\e[A");
208+
keyboard_seq("\e[A");
214209
break;
215210
case KEY_DELETE:
216-
queue_seq("\e[3~");
211+
keyboard_seq("\e[3~");
217212
break;
218213
case KEY_LEFT:
219-
queue_seq("\e[D");
214+
keyboard_seq("\e[D");
220215
break;
221216
case KEY_RIGHT:
222-
queue_seq("\e[C");
217+
keyboard_seq("\e[C");
223218
break;
224219
case KEY_ESC:
225-
queue_key('\e');
220+
ringbuf_put(&keyqueue, '\e');
226221
break;
227222
}
228223
} else {
@@ -231,9 +226,9 @@ void update_keyboard() {
231226

232227
if (ascii > 0) {
233228
if (keystate[KEY_ALT]) {
234-
queue_key('\e');
229+
ringbuf_put(&keyqueue, '\e');
235230
}
236-
queue_key(ascii);
231+
ringbuf_put(&keyqueue, ascii);
237232
}
238233

239234
} else {
@@ -243,14 +238,7 @@ void update_keyboard() {
243238
}
244239

245240
char board_serial_read() {
246-
update_keyboard();
247-
if (keyqueue_head != keyqueue_tail) {
248-
char c = keyqueue[keyqueue_tail];
249-
keyqueue_tail = (keyqueue_tail + 1) % 16;
250-
return c;
251-
} else {
252-
return -1;
253-
}
241+
return ringbuf_get(&keyqueue);
254242
}
255243

256244
// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.

ports/espressif/boards/m5stack_cardputer/keymap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ int keymap_shifted[56] = {
173173
'%', // KEY_5 -> '%'
174174
'&', // KEY_7 -> '&'
175175
'(', // KEY_9 -> '('
176-
'_', // KEY_UNDERSCORE -> '-'
177-
'\x7F',// KEY_BACKSPACE
176+
'-', // KEY_UNDERSCORE -> '-'
177+
'\b',// KEY_BACKSPACE
178178
0, // KEY_CTRL
179179
0, // KEY_ALT
180180
'X', // KEY_X

shared-bindings/keypad/EventQueue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ bool common_hal_keypad_eventqueue_get_into(keypad_eventqueue_obj_t *self, keypad
4242
bool common_hal_keypad_eventqueue_get_overflowed(keypad_eventqueue_obj_t *self);
4343
void common_hal_keypad_eventqueue_set_overflowed(keypad_eventqueue_obj_t *self, bool overflowed);
4444

45+
void common_hal_keypad_eventqueue_set_event_handler(keypad_eventqueue_obj_t *self, void (*event_handler)(keypad_eventqueue_obj_t *));
46+
4547
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENTQUEUE_H

shared-module/keypad/EventQueue.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void common_hal_keypad_eventqueue_construct(keypad_eventqueue_obj_t *self, size_
3737
// Event queue is 16-bit values.
3838
ringbuf_alloc(&self->encoded_events, max_events * (sizeof(uint16_t) + sizeof(mp_obj_t)));
3939
self->overflowed = false;
40+
self->event_handler = NULL;
4041
}
4142

4243
bool common_hal_keypad_eventqueue_get_into(keypad_eventqueue_obj_t *self, keypad_event_obj_t *event) {
@@ -79,6 +80,10 @@ size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self) {
7980
return ringbuf_num_filled(&self->encoded_events);
8081
}
8182

83+
void common_hal_keypad_eventqueue_set_event_handler(keypad_eventqueue_obj_t *self, void (*event_handler)(keypad_eventqueue_obj_t *)) {
84+
self->event_handler = event_handler;
85+
}
86+
8287
bool keypad_eventqueue_record(keypad_eventqueue_obj_t *self, mp_uint_t key_number, bool pressed, mp_obj_t timestamp) {
8388
if (ringbuf_num_empty(&self->encoded_events) == 0) {
8489
// Queue is full. Set the overflow flag. The caller will decide what else to do.
@@ -93,5 +98,9 @@ bool keypad_eventqueue_record(keypad_eventqueue_obj_t *self, mp_uint_t key_numbe
9398
ringbuf_put16(&self->encoded_events, encoded_event);
9499
ringbuf_put_n(&self->encoded_events, (uint8_t *)&timestamp, sizeof(mp_obj_t));
95100

101+
if (self->event_handler) {
102+
self->event_handler(self);
103+
}
104+
96105
return true;
97106
}

shared-module/keypad/EventQueue.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
#include "py/obj.h"
3131
#include "py/ringbuf.h"
3232

33-
typedef struct _keypad_eventqueue_obj_t {
33+
typedef struct _keypad_eventqueue_obj_t keypad_eventqueue_obj_t;
34+
35+
struct _keypad_eventqueue_obj_t {
3436
mp_obj_base_t base;
3537
ringbuf_t encoded_events;
3638
bool overflowed;
37-
} keypad_eventqueue_obj_t;
39+
void (*event_handler)(keypad_eventqueue_obj_t *);
40+
};
3841

3942
bool keypad_eventqueue_record(keypad_eventqueue_obj_t *self, mp_uint_t key_number, bool pressed, mp_obj_t timestamp);
4043

supervisor/shared/serial.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ MP_WEAK char board_serial_read(void) {
121121
return -1;
122122
}
123123

124-
MP_WEAK bool board_serial_bytes_available(void) {
124+
MP_WEAK uint32_t board_serial_bytes_available(void) {
125125
return false;
126126
}
127127

@@ -330,6 +330,9 @@ uint32_t serial_bytes_available(void) {
330330
count += tud_cdc_available();
331331
#endif
332332

333+
// Board-specific serial input.
334+
count += board_serial_bytes_available();
335+
333336
// Port-specific serial input.
334337
count += port_serial_bytes_available();
335338

supervisor/shared/serial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void board_serial_early_init(void);
6565
void board_serial_init(void);
6666
bool board_serial_connected(void);
6767
char board_serial_read(void);
68-
bool board_serial_bytes_available(void);
68+
uint32_t board_serial_bytes_available(void);
6969
void board_serial_write_substring(const char *text, uint32_t length);
7070

7171
int console_uart_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));

0 commit comments

Comments
 (0)