diff --git a/mp-s7-src/CMakeLists.txt b/mp-s7-src/CMakeLists.txt index 52cd35e..be08e4a 100644 --- a/mp-s7-src/CMakeLists.txt +++ b/mp-s7-src/CMakeLists.txt @@ -25,12 +25,23 @@ install(TARGETS RetroConsole DESTINATION lib) #micropython/lib/utils - library add_library(mp_lib_utils ../micropython/lib/utils/stdout_helpers.c + ../micropython/lib/utils/pyexec.h + ../micropython/lib/utils/pyexec.c ) target_include_directories(mp_lib_utils PUBLIC ../micropython/ ../micropython/ports/minimal/build/ ./ ) +add_library(mp_lib_readline + ../micropython/lib/mp-readline/readline.h + ../micropython/lib/mp-readline/readline.c + ) +target_include_directories(mp_lib_readline PUBLIC + ../micropython/ + ../micropython/ports/minimal/build/ + ./ + ) #micropython/py - main codebase file(GLOB mp_py_SRC @@ -39,7 +50,7 @@ file(GLOB mp_py_SRC ../micropython/ports/minimal/build/_frozen_mpy.c mpconfigport.h mphalport.h - stringio.c + stringio.cc ) add_library(mp_py ${mp_py_SRC}) target_include_directories(mp_py PUBLIC @@ -56,8 +67,8 @@ file(GLOB micropython_SRC mpconfigport.h mphalport.h mphalport.c - stringio.c - main.c + stringio.cc + main.cc ) add_application(MPTests ${micropython_SRC}) @@ -70,7 +81,7 @@ target_include_directories(MPTests target_link_libraries(MPTests RetroConsole mp_lib_utils + mp_lib_readline mp_py - # MP ) diff --git a/mp-s7-src/main.c b/mp-s7-src/main.cc similarity index 78% rename from mp-s7-src/main.c rename to mp-s7-src/main.cc index 7cf415c..1bef969 100644 --- a/mp-s7-src/main.c +++ b/mp-s7-src/main.cc @@ -1,8 +1,12 @@ -//#include "retro/Console.h" +#include "retro/Console.h" + #include #include #include +//Here we start to merge the C code from the MP minimal example with the C++ code from the Retro68 Console example +extern "C" { + #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" @@ -10,6 +14,11 @@ #include "py/mperrno.h" #include "lib/utils/pyexec.h" +} + +extern "C" int mp_hal_stdin_rx_chr(void); +extern "C" void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len); + #if MICROPY_ENABLE_COMPILER void do_str(const char *src, mp_parse_input_kind_t input_kind) { nlr_buf_t nlr; @@ -32,12 +41,11 @@ static char *stack_top; static char heap[2048]; #endif -/* + namespace retro { void InitConsole(); } -*/ int main() { @@ -49,11 +57,19 @@ int main() #endif mp_init(); -/* + retro::InitConsole(); std::string out = "Hello, world.\nEnter \"exit\" to quit.\n"; retro::Console::currentInstance->write(out.data(), out.size()); + pyexec_event_repl_init(); + for (;;) { + int c = mp_hal_stdin_rx_chr(); + if (pyexec_event_repl_process_char(c)) { + break; + } + } + std::string in; do { @@ -61,11 +77,14 @@ int main() out = "You Entered: " + in; retro::Console::currentInstance->write(out.data(), out.size()); } while(in != "exit\n"); + + + mp_deinit(); return 0; -*/ + } -void gc_collect(void) { +extern "C" void gc_collect(void) { // WARNING: This gc_collect implementation doesn't try to get root // pointers from CPU registers, and thus may function incorrectly. void *dummy; @@ -74,4 +93,3 @@ void gc_collect(void) { gc_collect_end(); gc_dump_info(); } - diff --git a/mp-s7-src/mpconfigport.h b/mp-s7-src/mpconfigport.h index 350f67a..2364707 100644 --- a/mp-s7-src/mpconfigport.h +++ b/mp-s7-src/mpconfigport.h @@ -24,7 +24,7 @@ #define MICROPY_DEBUG_PRINTERS (0) #define MICROPY_ENABLE_GC (1) #define MICROPY_GC_ALLOC_THRESHOLD (0) -#define MICROPY_REPL_EVENT_DRIVEN (0) +#define MICROPY_REPL_EVENT_DRIVEN (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_HELPER_LEXER_UNIX (0) #define MICROPY_ENABLE_SOURCE_LINE (0) diff --git a/mp-s7-src/stringio.c b/mp-s7-src/stringio.c deleted file mode 100644 index a30dd0c..0000000 --- a/mp-s7-src/stringio.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include "py/mpconfig.h" - -/* - * Core UART functions to implement for a port - */ - -// Receive single character -int mp_hal_stdin_rx_chr(void) { - unsigned char c = 0; - int r = read(0, &c, 1); - (void)r; - return c; -} - -// Send string of given length -void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { - int r = write(1, str, len); - (void)r; -} diff --git a/mp-s7-src/stringio.cc b/mp-s7-src/stringio.cc new file mode 100644 index 0000000..920faa7 --- /dev/null +++ b/mp-s7-src/stringio.cc @@ -0,0 +1,22 @@ +//#include "retro/Console.h" + +extern "C" { +#include +#include "py/mpconfig.h" +} + +extern "C" int mp_hal_stdin_rx_chr(void); +extern "C" void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len); + +// Receive single character +int mp_hal_stdin_rx_chr(void) { + unsigned char c = 0; + return c; +} + +// Send string of given length +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { + //int r = write(1, str, len); + //retro::Console::currentInstance->write(str, len); + //(void)r; +}