Skip to content

Commit 5f63d8f

Browse files
committed
M5Stack Cardputer keyboard handling improvements.
* Exposed the DemuxKeyMatrix object as board.KEYBOARD. * Implemented the ability to not have the keyboard assigned as serial input (sys.stdin) by specifying M5STACK_CARDPUTER_KEYBOARD_SERIAL=0 in settings.toml. This enables custom handling of individual key up/key down events.
1 parent fb3f3a4 commit 5f63d8f

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

ports/espressif/boards/m5stack_cardputer/board.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// SPDX-License-Identifier: MIT
66

7+
#include "board.h"
78
#include "keymap.h"
89
#include "supervisor/board.h"
910
#include "supervisor/shared/serial.h"
@@ -13,8 +14,8 @@
1314
#include "shared-bindings/microcontroller/Pin.h"
1415
#include "shared-module/displayio/__init__.h"
1516
#include "shared-module/displayio/mipi_constants.h"
17+
#include "shared-module/os/__init__.h"
1618
#include "shared-bindings/board/__init__.h"
17-
#include "shared-bindings/keypad_demux/DemuxKeyMatrix.h"
1819
#include "shared-bindings/keypad/EventQueue.h"
1920
#include "shared-bindings/keypad/Event.h"
2021
#include "supervisor/shared/reload.h"
@@ -23,6 +24,7 @@
2324
#include "shared/runtime/interrupt_char.h"
2425

2526
keypad_demux_demuxkeymatrix_obj_t board_keyboard;
27+
bool board_keyboard_serial_enabled = false;
2628

2729
void update_keyboard(keypad_eventqueue_obj_t *queue);
2830
void keyboard_seq(const char *seq);
@@ -123,7 +125,7 @@ void board_init(void) {
123125
}
124126

125127
void board_serial_init() {
126-
ringbuf_init(&keyqueue, (uint8_t *)keybuf, sizeof(keybuf));
128+
board_keyboard.base.type = &keypad_demux_demuxkeymatrix_type;
127129
common_hal_keypad_demux_demuxkeymatrix_construct(
128130
&board_keyboard, // self
129131
3, // num_row_addr_pins
@@ -135,16 +137,30 @@ void board_serial_init() {
135137
2 // debounce_threshold
136138
);
137139
demuxkeymatrix_never_reset(&board_keyboard);
138-
common_hal_keypad_eventqueue_set_event_handler(board_keyboard.events, update_keyboard);
139140

141+
// Wire the keyboard input to serial input (sys.stdin)
142+
// unless M5STACK_CARDPUTER_KEYBOARD_SERIAL=0 in /settings.toml
143+
mp_int_t enable_keyboard_serial;
144+
os_getenv_err_t enable_keyboard_err = common_hal_os_getenv_int("M5STACK_CARDPUTER_KEYBOARD_SERIAL", &enable_keyboard_serial);
145+
board_keyboard_serial_enabled = (enable_keyboard_err != GETENV_OK) || enable_keyboard_serial > 0;
146+
if (!board_keyboard_serial_enabled) {
147+
return;
148+
}
149+
150+
ringbuf_init(&keyqueue, (uint8_t *)keybuf, sizeof(keybuf));
151+
common_hal_keypad_eventqueue_set_event_handler(board_keyboard.events, update_keyboard);
140152
}
141153

142154
bool board_serial_connected() {
143-
return true;
155+
return board_keyboard_serial_enabled;
144156
}
145157

146158
uint32_t board_serial_bytes_available() {
147-
return ringbuf_num_filled(&keyqueue);
159+
if (board_keyboard_serial_enabled) {
160+
return ringbuf_num_filled(&keyqueue);
161+
} else {
162+
return 0;
163+
}
148164
}
149165

150166
void keyboard_seq(const char *seq) {
@@ -219,7 +235,11 @@ void update_keyboard(keypad_eventqueue_obj_t *queue) {
219235
}
220236

221237
char board_serial_read() {
222-
return ringbuf_get(&keyqueue);
238+
if (board_keyboard_serial_enabled) {
239+
return ringbuf_get(&keyqueue);
240+
} else {
241+
return 0;
242+
}
223243
}
224244

225245
// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "shared-bindings/keypad_demux/DemuxKeyMatrix.h"
8+
9+
extern keypad_demux_demuxkeymatrix_obj_t board_keyboard;

ports/espressif/boards/m5stack_cardputer/pins.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// SPDX-License-Identifier: MIT
66

7+
#include "board.h"
78
#include "shared-bindings/board/__init__.h"
89

910
#include "shared-module/displayio/__init__.h"
@@ -80,6 +81,10 @@ static const mp_rom_map_elem_t board_module_globals_table[] = {
8081
{ MP_ROM_QSTR(MP_QSTR_TFT_SPI), MP_ROM_PTR(&board_spi_obj) },
8182
{ MP_ROM_QSTR(MP_QSTR_SD_SPI), MP_ROM_PTR(&board_sd_spi_obj) },
8283

83-
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)}
84+
// Display object
85+
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)},
86+
87+
// Keyboard object
88+
{ MP_ROM_QSTR(MP_QSTR_KEYBOARD), MP_ROM_PTR(&board_keyboard)},
8489
};
8590
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);

0 commit comments

Comments
 (0)