From 651d5715e497e5ac49514b6f94b0e29f132e8dbd Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Mon, 29 Jan 2018 18:19:38 +0100 Subject: [PATCH] [python] Adding mp_hal_input --- apps/code/console_controller.cpp | 4 ++++ apps/code/console_controller.h | 1 + python/Makefile | 2 +- python/port/builtins.c | 28 ++++++++++++++++++++++++++++ python/port/genhdr/qstrdefs.in.h | 1 + python/port/import_helper.c | 7 ------- python/port/mpconfigport.h | 3 ++- python/port/mphalport.h | 1 + python/port/port.cpp | 5 +++++ python/port/port.h | 4 ++++ 10 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 python/port/builtins.c delete mode 100644 python/port/import_helper.c diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index eba0b1396..8ba7ba65f 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -79,6 +79,10 @@ void ConsoleController::runAndPrintForCommand(const char * command) { m_consoleStore.deleteLastLineIfEmpty(); } +const char * ConsoleController::input() { + return "1+1"; +} + void ConsoleController::removeExtensionIfAny(char * name) { int nameLength = strlen(name); if (nameLength<4) { diff --git a/apps/code/console_controller.h b/apps/code/console_controller.h index 1037ca76e..a8f7e0944 100644 --- a/apps/code/console_controller.h +++ b/apps/code/console_controller.h @@ -62,6 +62,7 @@ public: // MicroPython::ExecutionEnvironment void displaySandbox() override; void printText(const char * text, size_t length) override; + const char * input() override; private: static constexpr int LineCellType = 0; diff --git a/python/Makefile b/python/Makefile index 24c466bdd..63bb7d593 100644 --- a/python/Makefile +++ b/python/Makefile @@ -184,7 +184,7 @@ python/src/extmod/modurandom.o: SFLAGS += -DMP_QSTR_urandom="MP_QSTR_random" port_objs += $(addprefix python/port/,\ port.o \ - import_helper.o\ + builtins.o\ interrupt_helper.o \ modkandinsky.o \ modkandinsky_impl.o \ diff --git a/python/port/builtins.c b/python/port/builtins.c new file mode 100644 index 000000000..c19c6a44a --- /dev/null +++ b/python/port/builtins.c @@ -0,0 +1,28 @@ +#include "py/builtin.h" +#include "py/obj.h" +#include +#include "mphalport.h" + +mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); + +mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { + if (n_args == 1) { + mp_obj_print(args[0], PRINT_STR); + } + /*vstr_t line; + vstr_init(&line, 16); + int ret = mp_hal_readline(&line, ""); + if (ret == CHAR_CTRL_C) { + nlr_raise(mp_obj_new_exception(&mp_type_KeyboardInterrupt)); + } + if (line.len == 0 && ret == CHAR_CTRL_D) { + nlr_raise(mp_obj_new_exception(&mp_type_EOFError)); + } + */ + const char * input = mp_hal_input(); + return mp_obj_new_str(input, strlen(input), false); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 2158cb3fa..36b15e227 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -176,6 +176,7 @@ Q(hex) Q(id) Q(imag) Q(index) +Q(input) Q(insert) Q(int) Q(isalpha) diff --git a/python/port/import_helper.c b/python/port/import_helper.c deleted file mode 100644 index 6b3566160..000000000 --- a/python/port/import_helper.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "py/builtin.h" -#include "py/obj.h" - -mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); diff --git a/python/port/mpconfigport.h b/python/port/mpconfigport.h index 32b69ab30..cb9d5020c 100644 --- a/python/port/mpconfigport.h +++ b/python/port/mpconfigport.h @@ -76,7 +76,8 @@ typedef long mp_off_t; // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, // We need to provide a declaration/definition of alloca() #include diff --git a/python/port/mphalport.h b/python/port/mphalport.h index c549bfd7a..b4afe8fdc 100644 --- a/python/port/mphalport.h +++ b/python/port/mphalport.h @@ -4,5 +4,6 @@ extern int mp_interrupt_char; void mp_hal_set_interrupt_char(int c); void mp_keyboard_interrupt(void); +const char * mp_hal_input(void); #endif diff --git a/python/port/port.cpp b/python/port/port.cpp index 82f04ecb6..5b6459fe5 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -170,3 +170,8 @@ void mp_hal_stdout_tx_strn_cooked(const char * str, size_t len) { assert(sCurrentExecutionEnvironment != nullptr); sCurrentExecutionEnvironment->printText(str, len); } + +const char * mp_hal_input() { + assert(sCurrentExecutionEnvironment != nullptr); + return sCurrentExecutionEnvironment->input(); +} diff --git a/python/port/port.h b/python/port/port.h index b35144f95..5d9e95705 100644 --- a/python/port/port.h +++ b/python/port/port.h @@ -13,6 +13,9 @@ public: ExecutionEnvironment(); static ExecutionEnvironment * currentExecutionEnvironment(); void runCode(const char * ); + virtual const char * input() { + return nullptr; + } virtual void displaySandbox() { } virtual void printText(const char * text, size_t length) { @@ -31,5 +34,6 @@ void registerScriptProvider(ScriptProvider * s); // mp_lexer_new_from_file -> Ask the context about a file // mp_import_stat // mp_hal_stdout_tx_strn_cooked -> Tell the context Python printed text +// mp_hal_input #endif