diff --git a/stmhal/Makefile b/stmhal/Makefile index 938871aefbd7e4fc2e99e43f6a9d83cb2eae3fdc..292bfdd61caf499e54121dc08507cc121bdc77c2 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -72,6 +72,7 @@ SRC_C = \ malloc0.c \ gccollect.c \ pyexec.c \ + input.c \ pybmodule.c \ osmodule.c \ timemodule.c \ diff --git a/stmhal/input.c b/stmhal/input.c new file mode 100644 index 0000000000000000000000000000000000000000..f53018a42297642102ee8e9e68b0f540ec595008 --- /dev/null +++ b/stmhal/input.c @@ -0,0 +1,25 @@ +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "usb.h" + +extern int readline(vstr_t *line, const char *prompt); + +STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) { + if (n_args == 1) { + mp_obj_print(args[0], PRINT_REPR); + } + vstr_t line; + vstr_init(&line, 16); + int ret = readline(&line, ""); + if (line.len == 0 && ret == VCP_CHAR_CTRL_D) { + nlr_jump(mp_obj_new_exception(&mp_type_EOFError)); + } + mp_obj_t o = mp_obj_new_str((const byte*)line.buf, line.len, false); + vstr_clear(&line); + return o; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 8ff80133594c820d58e70bcf5bd57b61a2dde1f9..59c91942a0dfa0f4c32d67a21a7d901029ee43d0 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -19,8 +19,10 @@ #define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ // extra built in names to add to the global namespace +extern const struct _mp_obj_fun_native_t mp_builtin_input_obj; extern const struct _mp_obj_fun_native_t mp_builtin_open_obj; #define MICROPY_EXTRA_BUILTINS \ + { MP_QSTR_input, (mp_obj_t)&mp_builtin_input_obj }, \ { MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj }, // type definitions for the specific machine diff --git a/stmhal/pybmodule.c b/stmhal/pybmodule.c index 09cdf83b8a335420bcadfb26b5d692e5d7e71073..77273298499d4363b9f68ccef74787b7f45feb8a 100644 --- a/stmhal/pybmodule.c +++ b/stmhal/pybmodule.c @@ -196,12 +196,20 @@ STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) { return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report); #if 0 MP_DEFINE_CONST_FUN_OBJ_2(pyb_I2C_obj, pyb_I2C); // TODO put this in i2c.c #endif +extern int stdin_rx_chr(void); + +STATIC mp_obj_t pyb_input(void ) { + return mp_obj_new_int(stdin_rx_chr()); +} + +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_input_obj, pyb_input); + MP_DECLARE_CONST_FUN_OBJ(pyb_source_dir_obj); // defined in main.c MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c @@ -268,6 +276,9 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { #endif #endif + // input + { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&pyb_input_obj }, + // pin mapper { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_map_obj }, diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h index 9961102a35b042895f248023eab78a5d8e04651b..55d4b1b09b0964dd97ed9929f72d01127cbe29eb 100644 --- a/stmhal/qstrdefsport.h +++ b/stmhal/qstrdefsport.h @@ -70,3 +70,6 @@ Q(urandom) // for time module Q(time) Q(sleep) + +// for input +Q(input)