Skip to content

Commit

Permalink
Further improvements to M5Stack Cardputer keyboard integration.
Browse files Browse the repository at this point in the history
 - Move the keyboard-related code from board.c to
   cardputer_keyboard.c, exposed to python as cardputer_keyboard.
 - Remove board.KEYBOARD, provide cardputer_keyboard.KEYBOARD instead.
 - Provide additional methods in cardputer_keyboard:
    - detach_serial()
    - attach_serial()
    - key_to_char(key, shift)
  • Loading branch information
konstantint committed Aug 25, 2024
1 parent 5f63d8f commit 7a99a70
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 169 deletions.
156 changes: 2 additions & 154 deletions ports/espressif/boards/m5stack_cardputer/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,19 @@
//
// SPDX-License-Identifier: MIT

#include "board.h"
#include "keymap.h"
#include "mpconfigboard.h"
#include "supervisor/board.h"
#include "supervisor/shared/serial.h"
#include "mpconfigboard.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/fourwire/FourWire.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/displayio/mipi_constants.h"
#include "shared-module/os/__init__.h"
#include "shared-bindings/board/__init__.h"
#include "shared-bindings/keypad/EventQueue.h"
#include "shared-bindings/keypad/Event.h"
#include "supervisor/shared/reload.h"
#include "py/runtime.h"
#include "py/ringbuf.h"
#include "shared/runtime/interrupt_char.h"

keypad_demux_demuxkeymatrix_obj_t board_keyboard;
bool board_keyboard_serial_enabled = false;

void update_keyboard(keypad_eventqueue_obj_t *queue);
void keyboard_seq(const char *seq);

const mcu_pin_obj_t *row_addr_pins[] = {
&pin_GPIO8,
&pin_GPIO9,
&pin_GPIO11,
};

const mcu_pin_obj_t *column_pins[] = {
&pin_GPIO13,
&pin_GPIO15,
&pin_GPIO3,
&pin_GPIO4,
&pin_GPIO5,
&pin_GPIO6,
&pin_GPIO7
};

keypad_event_obj_t event;

char keystate[56];
ringbuf_t keyqueue;
char keybuf[32];

#define DELAY 0x80

Expand All @@ -73,6 +40,7 @@ uint8_t display_init_sequence[] = {
};


// Overrides the weakly linked function from supervisor/shared/board.c
void board_init(void) {
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus;
Expand Down Expand Up @@ -124,124 +92,4 @@ void board_init(void) {
);
}

void board_serial_init() {
board_keyboard.base.type = &keypad_demux_demuxkeymatrix_type;
common_hal_keypad_demux_demuxkeymatrix_construct(
&board_keyboard, // self
3, // num_row_addr_pins
row_addr_pins, // row_addr_pins
7, // num_column_pins
column_pins, // column_pins
0.01f, // interval
20, // max_events
2 // debounce_threshold
);
demuxkeymatrix_never_reset(&board_keyboard);

// Wire the keyboard input to serial input (sys.stdin)
// unless M5STACK_CARDPUTER_KEYBOARD_SERIAL=0 in /settings.toml
mp_int_t enable_keyboard_serial;
os_getenv_err_t enable_keyboard_err = common_hal_os_getenv_int("M5STACK_CARDPUTER_KEYBOARD_SERIAL", &enable_keyboard_serial);
board_keyboard_serial_enabled = (enable_keyboard_err != GETENV_OK) || enable_keyboard_serial > 0;
if (!board_keyboard_serial_enabled) {
return;
}

ringbuf_init(&keyqueue, (uint8_t *)keybuf, sizeof(keybuf));
common_hal_keypad_eventqueue_set_event_handler(board_keyboard.events, update_keyboard);
}

bool board_serial_connected() {
return board_keyboard_serial_enabled;
}

uint32_t board_serial_bytes_available() {
if (board_keyboard_serial_enabled) {
return ringbuf_num_filled(&keyqueue);
} else {
return 0;
}
}

void keyboard_seq(const char *seq) {
while (*seq) {
ringbuf_put(&keyqueue, *seq++);
}
}

void update_keyboard(keypad_eventqueue_obj_t *queue) {
uint8_t ascii = 0;

if (common_hal_keypad_eventqueue_get_length(queue) == 0) {
return;
}

while (common_hal_keypad_eventqueue_get_into(queue, &event)) {
if (event.pressed) {
keystate[event.key_number] = 1;

if (keystate[KEY_CTRL]) {
if (keystate[KEY_ALT] && keystate[KEY_BACKSPACE]) {
reload_initiate(RUN_REASON_REPL_RELOAD);
}
ascii = keymap[event.key_number];
if (ascii >= 'a' && ascii <= 'z') {
ascii -= 'a' - 1;
}

if (ascii == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
}
} else if (keystate[KEY_SHIFT]) {
ascii = keymap_shifted[event.key_number];
} else if (keystate[KEY_FN] && event.key_number != KEY_FN) {
switch (event.key_number | FN_MOD) {
case KEY_DOWN:
keyboard_seq("\e[B");
break;
case KEY_UP:
keyboard_seq("\e[A");
break;
case KEY_DELETE:
keyboard_seq("\e[3~");
break;
case KEY_LEFT:
keyboard_seq("\e[D");
break;
case KEY_RIGHT:
keyboard_seq("\e[C");
break;
case KEY_ESC:
ringbuf_put(&keyqueue, '\e');
break;
}
} else {
ascii = keymap[event.key_number];
}

if (ascii > 0) {
if (keystate[KEY_ALT]) {
ringbuf_put(&keyqueue, '\e');
} else if (keystate[KEY_OPT]) {
ringbuf_put(&keyqueue, '\x10');
}
ringbuf_put(&keyqueue, ascii);
}

} else {
keystate[event.key_number] = 0;
}
}
}

char board_serial_read() {
if (board_keyboard_serial_enabled) {
return ringbuf_get(&keyqueue);
} else {
return 0;
}
}

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

// TODO: Should we turn off the display when asleep, in board_deinit() ?
9 changes: 0 additions & 9 deletions ports/espressif/boards/m5stack_cardputer/board.h

This file was deleted.

Loading

0 comments on commit 7a99a70

Please sign in to comment.