diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-22 23:53:50 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-22 23:53:50 +0000 |
commit | 02fa0358005fed02e989e7b1ad9b31f7a9ae8637 (patch) | |
tree | 5da32f082d00a1db50881bc2f83cb11e28d0a970 | |
parent | c91097223d0dced28b874002e759e1907e56e054 (diff) | |
download | micropython-02fa0358005fed02e989e7b1ad9b31f7a9ae8637.tar.gz micropython-02fa0358005fed02e989e7b1ad9b31f7a9ae8637.zip |
stmhal: Add input() and pyb.input() functions.
-rw-r--r-- | stmhal/Makefile | 1 | ||||
-rw-r--r-- | stmhal/input.c | 25 | ||||
-rw-r--r-- | stmhal/mpconfigport.h | 2 | ||||
-rw-r--r-- | stmhal/pybmodule.c | 13 | ||||
-rw-r--r-- | stmhal/qstrdefsport.h | 3 |
5 files changed, 43 insertions, 1 deletions
diff --git a/stmhal/Makefile b/stmhal/Makefile index 938871aefb..292bfdd61c 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 0000000000..f53018a422 --- /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 8ff8013359..59c91942a0 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 09cdf83b8a..7727329849 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 9961102a35..55d4b1b09b 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) |