Skip to content

Commit bbb4b98

Browse files
committed
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).
1 parent 29b26f3 commit bbb4b98

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

py/modmicropython.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/stackctrl.h"
3232
#include "py/runtime.h"
3333
#include "py/gc.h"
34+
#include "py/mphal.h"
3435

3536
// Various builtins specific to MicroPython runtime,
3637
// living in micropython module
@@ -129,6 +130,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_
129130
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
130131
#endif
131132

133+
#if MICROPY_KBD_EXCEPTION
134+
STATIC mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) {
135+
mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in));
136+
return mp_const_none;
137+
}
138+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr);
139+
#endif
140+
132141
#if MICROPY_ENABLE_SCHEDULER
133142
STATIC mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) {
134143
if (!mp_sched_schedule(function, arg)) {
@@ -162,6 +171,9 @@ STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
162171
{ MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) },
163172
{ MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) },
164173
#endif
174+
#if MICROPY_KBD_EXCEPTION
175+
{ MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) },
176+
#endif
165177
#if MICROPY_ENABLE_SCHEDULER
166178
{ MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) },
167179
#endif

py/mpconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@
445445
# endif
446446
#endif
447447

448-
// Whether to provide the mp_kbd_exception object
448+
// Whether to provide the mp_kbd_exception object, and micropython.kbd_intr function
449449
#ifndef MICROPY_KBD_EXCEPTION
450450
#define MICROPY_KBD_EXCEPTION (0)
451451
#endif

0 commit comments

Comments
 (0)