Skip to content

Commit

Permalink
Merge pull request #9529 from konstantint/m5stack-cardputer-keyboard-fix
Browse files Browse the repository at this point in the history
M5Stack Cardputer keyboard handling improvements.
  • Loading branch information
tannewt authored Sep 4, 2024
2 parents 3d93006 + 4fb31c4 commit eb2054b
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 137 deletions.
136 changes: 2 additions & 134 deletions ports/espressif/boards/m5stack_cardputer/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,19 @@
//
// SPDX-License-Identifier: MIT

#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-bindings/board/__init__.h"
#include "shared-bindings/keypad_demux/DemuxKeyMatrix.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;

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 @@ -71,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 @@ -122,106 +92,4 @@ void board_init(void) {
);
}

void board_serial_init() {
ringbuf_init(&keyqueue, (uint8_t *)keybuf, sizeof(keybuf));
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);
common_hal_keypad_eventqueue_set_event_handler(board_keyboard.events, update_keyboard);

}

bool board_serial_connected() {
return true;
}

uint32_t board_serial_bytes_available() {
return ringbuf_num_filled(&keyqueue);
}

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() {
return ringbuf_get(&keyqueue);
}

// 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() ?
Loading

0 comments on commit eb2054b

Please sign in to comment.