From bbb4b9822f094825d7e7fd1f7df716adc2598685 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 10 Apr 2017 17:19:36 +1000 Subject: [PATCH 1/2] py/modmicropython: Add micropython.kbd_intr() function. It controls the character that's used to (asynchronously) raise a KeyboardInterrupt exception. Passing "-1" allows to disable the interception of the interrupt character (as long as a port allows such a behaviour). --- py/modmicropython.c | 12 ++++++++++++ py/mpconfig.h | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/py/modmicropython.c b/py/modmicropython.c index a74e6aa3cb109..d767062301e62 100644 --- a/py/modmicropython.c +++ b/py/modmicropython.c @@ -31,6 +31,7 @@ #include "py/stackctrl.h" #include "py/runtime.h" #include "py/gc.h" +#include "py/mphal.h" // Various builtins specific to MicroPython runtime, // living in micropython module @@ -129,6 +130,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf); #endif +#if MICROPY_KBD_EXCEPTION +STATIC mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) { + mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr); +#endif + #if MICROPY_ENABLE_SCHEDULER STATIC mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) { if (!mp_sched_schedule(function, arg)) { @@ -162,6 +171,9 @@ STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) }, { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) }, #endif + #if MICROPY_KBD_EXCEPTION + { MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) }, + #endif #if MICROPY_ENABLE_SCHEDULER { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) }, #endif diff --git a/py/mpconfig.h b/py/mpconfig.h index 05cb5daaf13e6..b6e24d205581e 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -445,7 +445,7 @@ # endif #endif -// Whether to provide the mp_kbd_exception object +// Whether to provide the mp_kbd_exception object, and micropython.kbd_intr function #ifndef MICROPY_KBD_EXCEPTION #define MICROPY_KBD_EXCEPTION (0) #endif From c7c14f163458046767b539c567a421076ea9a6b7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 18 Apr 2017 16:21:47 +1000 Subject: [PATCH 2/2] tests/micropython: Add test for micropython.kbd_intr(). --- tests/micropython/kbd_intr.py | 13 +++++++++++++ tests/micropython/kbd_intr.py.exp | 0 2 files changed, 13 insertions(+) create mode 100644 tests/micropython/kbd_intr.py create mode 100644 tests/micropython/kbd_intr.py.exp diff --git a/tests/micropython/kbd_intr.py b/tests/micropython/kbd_intr.py new file mode 100644 index 0000000000000..a7ce7464b99ff --- /dev/null +++ b/tests/micropython/kbd_intr.py @@ -0,0 +1,13 @@ +# test the micropython.kbd_intr() function + +import micropython + +try: + micropython.kbd_intr +except AttributeError: + print('SKIP') + import sys + sys.exit() + +# just check we can actually call it +micropython.kbd_intr(3) diff --git a/tests/micropython/kbd_intr.py.exp b/tests/micropython/kbd_intr.py.exp new file mode 100644 index 0000000000000..e69de29bb2d1d