From 972938c7213e33bb70ea8a9650f256ae28e62c1e Mon Sep 17 00:00:00 2001 From: Christian Walther Date: Sat, 30 Mar 2024 23:26:51 +0100 Subject: [PATCH] examples/embedding-full: Enable mphal-backed sys.stdin/out/err. Signed-off-by: Christian Walther --- examples/embedding-full/main.c | 8 ++++- examples/embedding-full/micropython_embed.mk | 5 ++++ examples/embedding-full/mpconfigport.h | 2 +- examples/embedding-full/mphal.c | 31 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/examples/embedding-full/main.c b/examples/embedding-full/main.c index 0b1d2adf64..f3002c9d6d 100644 --- a/examples/embedding-full/main.c +++ b/examples/embedding-full/main.c @@ -28,6 +28,12 @@ static const char *example_2 = "help('modules')\n" "import sys\n" "help(sys)\n" + "print('sys.stdin.read(3):', repr(sys.stdin.read(3)))\n" + "print('sys.stdin.buffer.read(3):', repr(sys.stdin.buffer.read(3)))\n" + "sys.stdout.write('hello stdout text\\n')\n" + "sys.stdout.buffer.write(b'hello stdout binary\\n')\n" + "sys.stdout.write('hello stderr text\\n')\n" + "sys.stdout.buffer.write(b'hello stderr binary\\n')\n" "import os\n" "help(os)\n" "print('os.uname():', os.uname())\n" @@ -51,7 +57,7 @@ static const char *example_2 = ; // This array is the MicroPython GC heap. -static char heap[8 * 1024]; +static char heap[10 * 1024]; int main() { #if MICROPY_STACK_CHECK diff --git a/examples/embedding-full/micropython_embed.mk b/examples/embedding-full/micropython_embed.mk index 786582030e..e21c88d1fe 100644 --- a/examples/embedding-full/micropython_embed.mk +++ b/examples/embedding-full/micropython_embed.mk @@ -14,6 +14,11 @@ EMBED_EXTRA += \ shared/timeutils/timeutils.h \ shared/timeutils/modtime_mphal.h +# Include source for mphal-backed stdio in the output. +# Disable when using POSIX-backed stdio (MICROPY_VFS_POSIX). +EMBED_EXTRA += \ + shared/runtime/sys_stdio_mphal.c + # Freeze Python modules. FROZEN_MANIFEST ?= manifest.py diff --git a/examples/embedding-full/mpconfigport.h b/examples/embedding-full/mpconfigport.h index adbaa48e07..bc326a35fd 100644 --- a/examples/embedding-full/mpconfigport.h +++ b/examples/embedding-full/mpconfigport.h @@ -40,7 +40,7 @@ // Can be enabled once either shared/runtime/sys_stdio_mphal.c or // extmod/vfs_posix_file.c is included in the build. -#define MICROPY_PY_SYS_STDFILES (0) +//#define MICROPY_PY_SYS_STDFILES (0) // Can be enabled if you provide an implementation of // mp_hal_set_interrupt_char() in mphal.c or include diff --git a/examples/embedding-full/mphal.c b/examples/embedding-full/mphal.c index 47b39e31ec..76654c6155 100644 --- a/examples/embedding-full/mphal.c +++ b/examples/embedding-full/mphal.c @@ -7,6 +7,9 @@ #include "py/builtin.h" #include "py/compile.h" #include "py/mperrno.h" +#if MICROPY_PY_SYS_STDFILES +#include "py/stream.h" +#endif #if !MICROPY_VFS @@ -71,3 +74,31 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { printf("%.*s", (int)len, str); start_of_line = (len > 0 && (str[len-1] == '\n' || str[len-1] == '\r')); } + +#if MICROPY_PY_SYS_STDFILES + +// Binary-mode standard input +// Receive single character, blocking until one is available. +int mp_hal_stdin_rx_chr(void) { + return 'X'; +} + +// Binary-mode standard output +// Send the string of given length. +mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { + printf("tx: %.*s", (int)len, str); + return len; +} + +uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { + uintptr_t ret = 0; + if ((poll_flags & MP_STREAM_POLL_RD) /* && can_read */) { + ret |= MP_STREAM_POLL_RD; + } + if ((poll_flags & MP_STREAM_POLL_WR) /* && can_write */) { + ret |= MP_STREAM_POLL_WR; + } + return ret; +} + +#endif