From 99f2156e5e8cf520b780cee65780425e33b714c5 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Tue, 8 Nov 2022 22:22:10 +0100 Subject: [PATCH] pe: interrupt computations with AC/ON --- ports/sh/main.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ ports/sh/mphalport.h | 4 +--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/ports/sh/main.c b/ports/sh/main.c index cc1de0b3c..4322449cf 100644 --- a/ports/sh/main.c +++ b/ports/sh/main.c @@ -14,6 +14,7 @@ #include "shared/runtime/pyexec.h" #include +#include #include #include @@ -84,6 +85,50 @@ static bool py_file_filter(struct dirent const *ent) return strendswith(ent->d_name, ".py"); } +static void shell_write_char(int c) +{ + pyexec_event_repl_process_char(c); +} +static void shell_write_str(char const *str) +{ + while(*str) + pyexec_event_repl_process_char(*str++); +} + +static char *path_to_module(char const *path) +{ + if(path[0] == '/') + path++; + + int i, n = strlen(path); + char *module = malloc(n + 1); + if(!module) + return NULL; + + for(i = 0; i < n; i++) { + if(i == n - 3 && !strcmp(path + i, ".py")) + break; + module[i] = (path[i] == '/') ? '.' : path[i]; + } + module[i] = 0; + return module; +} + +/* Filter AC/ON push events asynchronously from the keyboard driver and + interrupt MicroPython instead. */ +static bool async_filter(key_event_t ev) +{ + if(mp_interrupt_char < 0) + return true; + if(ev.type == KEYEV_DOWN && ev.key == KEY_ACON) { + /* This function is designed to be called asynchronously. */ + mp_sched_keyboard_interrupt(); + return false; + } + return true; +} + + int main(int argc, char **argv) { #if DEBUG @@ -93,6 +138,8 @@ int main(int argc, char **argv) //=== Init sequence ===// + keydev_set_async_filter(keydev_std(), async_filter); + pe_shell_console = console_create(8192); /* Set up standard streams */ diff --git a/ports/sh/mphalport.h b/ports/sh/mphalport.h index 5e3dcbada..b9536ee27 100644 --- a/ports/sh/mphalport.h +++ b/ports/sh/mphalport.h @@ -7,9 +7,7 @@ #include #include - -/* Set the interrupt character in the terminal. */ -void mp_hal_set_interrupt_char(char c); +#include "shared/runtime/interrupt_char.h" /* Receive a single character from shell (blocking). */ int mp_hal_stdin_rx_chr(void);