44//
55// SPDX-License-Identifier: MIT
66
7- #include "board.h"
8- #include "keymap.h"
7+ #include "mpconfigboard.h"
98#include "supervisor/board.h"
109#include "supervisor/shared/serial.h"
11- #include "mpconfigboard.h"
1210#include "shared-bindings/busio/SPI.h"
1311#include "shared-bindings/fourwire/FourWire.h"
1412#include "shared-bindings/microcontroller/Pin.h"
1513#include "shared-module/displayio/__init__.h"
1614#include "shared-module/displayio/mipi_constants.h"
17- #include "shared-module/os/__init__.h"
1815#include "shared-bindings/board/__init__.h"
19- #include "shared-bindings/keypad/EventQueue.h"
20- #include "shared-bindings/keypad/Event.h"
21- #include "supervisor/shared/reload.h"
2216#include "py/runtime.h"
2317#include "py/ringbuf.h"
2418#include "shared/runtime/interrupt_char.h"
2519
26- keypad_demux_demuxkeymatrix_obj_t board_keyboard ;
27- bool board_keyboard_serial_enabled = false;
28-
29- void update_keyboard (keypad_eventqueue_obj_t * queue );
30- void keyboard_seq (const char * seq );
31-
32- const mcu_pin_obj_t * row_addr_pins [] = {
33- & pin_GPIO8 ,
34- & pin_GPIO9 ,
35- & pin_GPIO11 ,
36- };
37-
38- const mcu_pin_obj_t * column_pins [] = {
39- & pin_GPIO13 ,
40- & pin_GPIO15 ,
41- & pin_GPIO3 ,
42- & pin_GPIO4 ,
43- & pin_GPIO5 ,
44- & pin_GPIO6 ,
45- & pin_GPIO7
46- };
47-
48- keypad_event_obj_t event ;
49-
50- char keystate [56 ];
51- ringbuf_t keyqueue ;
52- char keybuf [32 ];
5320
5421#define DELAY 0x80
5522
@@ -73,6 +40,7 @@ uint8_t display_init_sequence[] = {
7340};
7441
7542
43+ // Overrides the weakly linked function from supervisor/shared/board.c
7644void board_init (void ) {
7745 busio_spi_obj_t * spi = common_hal_board_create_spi (0 );
7846 fourwire_fourwire_obj_t * bus = & allocate_display_bus ()-> fourwire_bus ;
@@ -124,124 +92,4 @@ void board_init(void) {
12492 );
12593}
12694
127- void board_serial_init () {
128- board_keyboard .base .type = & keypad_demux_demuxkeymatrix_type ;
129- common_hal_keypad_demux_demuxkeymatrix_construct (
130- & board_keyboard , // self
131- 3 , // num_row_addr_pins
132- row_addr_pins , // row_addr_pins
133- 7 , // num_column_pins
134- column_pins , // column_pins
135- 0.01f , // interval
136- 20 , // max_events
137- 2 // debounce_threshold
138- );
139- demuxkeymatrix_never_reset (& board_keyboard );
140-
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 );
152- }
153-
154- bool board_serial_connected () {
155- return board_keyboard_serial_enabled ;
156- }
157-
158- uint32_t board_serial_bytes_available () {
159- if (board_keyboard_serial_enabled ) {
160- return ringbuf_num_filled (& keyqueue );
161- } else {
162- return 0 ;
163- }
164- }
165-
166- void keyboard_seq (const char * seq ) {
167- while (* seq ) {
168- ringbuf_put (& keyqueue , * seq ++ );
169- }
170- }
171-
172- void update_keyboard (keypad_eventqueue_obj_t * queue ) {
173- uint8_t ascii = 0 ;
174-
175- if (common_hal_keypad_eventqueue_get_length (queue ) == 0 ) {
176- return ;
177- }
178-
179- while (common_hal_keypad_eventqueue_get_into (queue , & event )) {
180- if (event .pressed ) {
181- keystate [event .key_number ] = 1 ;
182-
183- if (keystate [KEY_CTRL ]) {
184- if (keystate [KEY_ALT ] && keystate [KEY_BACKSPACE ]) {
185- reload_initiate (RUN_REASON_REPL_RELOAD );
186- }
187- ascii = keymap [event .key_number ];
188- if (ascii >= 'a' && ascii <= 'z' ) {
189- ascii -= 'a' - 1 ;
190- }
191-
192- if (ascii == mp_interrupt_char ) {
193- mp_sched_keyboard_interrupt ();
194- }
195- } else if (keystate [KEY_SHIFT ]) {
196- ascii = keymap_shifted [event .key_number ];
197- } else if (keystate [KEY_FN ] && event .key_number != KEY_FN ) {
198- switch (event .key_number | FN_MOD ) {
199- case KEY_DOWN :
200- keyboard_seq ("\e[B" );
201- break ;
202- case KEY_UP :
203- keyboard_seq ("\e[A" );
204- break ;
205- case KEY_DELETE :
206- keyboard_seq ("\e[3~" );
207- break ;
208- case KEY_LEFT :
209- keyboard_seq ("\e[D" );
210- break ;
211- case KEY_RIGHT :
212- keyboard_seq ("\e[C" );
213- break ;
214- case KEY_ESC :
215- ringbuf_put (& keyqueue , '\e' );
216- break ;
217- }
218- } else {
219- ascii = keymap [event .key_number ];
220- }
221-
222- if (ascii > 0 ) {
223- if (keystate [KEY_ALT ]) {
224- ringbuf_put (& keyqueue , '\e' );
225- } else if (keystate [KEY_OPT ]) {
226- ringbuf_put (& keyqueue , '\x10' );
227- }
228- ringbuf_put (& keyqueue , ascii );
229- }
230-
231- } else {
232- keystate [event .key_number ] = 0 ;
233- }
234- }
235- }
236-
237- char board_serial_read () {
238- if (board_keyboard_serial_enabled ) {
239- return ringbuf_get (& keyqueue );
240- } else {
241- return 0 ;
242- }
243- }
244-
245- // Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.
246-
24795// TODO: Should we turn off the display when asleep, in board_deinit() ?
0 commit comments