|
10 | 10 | #include "py/mphal.h" |
11 | 11 | #include "py/objmodule.h" |
12 | 12 | #include "py/runtime.h" |
| 13 | +#include "py/stream.h" |
13 | 14 |
|
14 | 15 | #include <pbio/int_math.h> |
15 | 16 |
|
@@ -83,6 +84,24 @@ STATIC mp_obj_t pb_module_tools_wait(size_t n_args, const mp_obj_t *pos_args, mp |
83 | 84 | } |
84 | 85 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_module_tools_wait_obj, 0, pb_module_tools_wait); |
85 | 86 |
|
| 87 | +/** |
| 88 | + * Reads one byte from stdin without blocking. |
| 89 | + * |
| 90 | + * @returns The integer value of the byte read or @c None if no data is available. |
| 91 | + */ |
| 92 | +STATIC mp_obj_t pb_module_tools_read_input_byte(void) { |
| 93 | + if (!(mp_hal_stdio_poll(MP_STREAM_POLL_RD) & MP_STREAM_POLL_RD)) { |
| 94 | + // No bytes available. |
| 95 | + return mp_const_none; |
| 96 | + } |
| 97 | + |
| 98 | + // REVISIT: In theory, this should not block if mp_hal_stdio_poll() and |
| 99 | + // mp_hal_stdin_rx_chr() are implemented correctly and nothing happens |
| 100 | + // in a thread/interrupt/kernel that changes the state. |
| 101 | + return MP_OBJ_NEW_SMALL_INT(mp_hal_stdin_rx_chr()); |
| 102 | +} |
| 103 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pb_module_tools_read_input_byte_obj, pb_module_tools_read_input_byte); |
| 104 | + |
86 | 105 | STATIC mp_obj_t pb_module_tools_run_task(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
87 | 106 | PB_PARSE_ARGS_FUNCTION(n_args, pos_args, kw_args, |
88 | 107 | PB_ARG_REQUIRED(task), |
@@ -127,6 +146,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_module_tools_run_task_obj, 1, pb_module_too |
127 | 146 | STATIC const mp_rom_map_elem_t tools_globals_table[] = { |
128 | 147 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tools) }, |
129 | 148 | { MP_ROM_QSTR(MP_QSTR_wait), MP_ROM_PTR(&pb_module_tools_wait_obj) }, |
| 149 | + { MP_ROM_QSTR(MP_QSTR_read_input_byte), MP_ROM_PTR(&pb_module_tools_read_input_byte_obj) }, |
130 | 150 | { MP_ROM_QSTR(MP_QSTR_run_task), MP_ROM_PTR(&pb_module_tools_run_task_obj) }, |
131 | 151 | { MP_ROM_QSTR(MP_QSTR_StopWatch), MP_ROM_PTR(&pb_type_StopWatch) }, |
132 | 152 | { MP_ROM_QSTR(MP_QSTR_multitask), MP_ROM_PTR(&pb_type_Task) }, |
|
0 commit comments