From 5e1ea0b5e9cf322310a9f10e4e01cf9b02e3ae38 Mon Sep 17 00:00:00 2001 From: Christian Walther Date: Mon, 1 May 2023 18:18:27 +0200 Subject: [PATCH] ports/embed: Move text output to the application. The implementation of mp_hal_stdout_tx_strn_cooked() does not belong into the library, but into the embedding application. Some applications may not want Python output to go straight to their stdout, or may not even have printf() available. Signed-off-by: Christian Walther --- examples/embedding-full/mpconfigport.h | 3 +++ examples/embedding-full/mphal.c | 12 ++++++++++ ports/embed/port/mpconfigport_common.h | 10 ++++++++ ports/embed/port/mphalport.c | 33 -------------------------- 4 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 ports/embed/port/mphalport.c diff --git a/examples/embedding-full/mpconfigport.h b/examples/embedding-full/mpconfigport.h index ec96e5ad1f..38cd0f2baf 100644 --- a/examples/embedding-full/mpconfigport.h +++ b/examples/embedding-full/mpconfigport.h @@ -30,3 +30,6 @@ // mp_hal_set_interrupt_char() in mphal.c or include // shared/runtime/interrupt_char.c in the build. #define MICROPY_KBD_EXCEPTION (0) + +// We have our own implementation of mp_hal_stdout_tx_strn_cooked(). +#undef MP_PLAT_PRINT_STRN diff --git a/examples/embedding-full/mphal.c b/examples/embedding-full/mphal.c index 32e508b1e9..9af36b44a3 100644 --- a/examples/embedding-full/mphal.c +++ b/examples/embedding-full/mphal.c @@ -3,6 +3,7 @@ * Copyright (c) 2022-2023 Damien P. George */ +#include #include "py/builtin.h" #include "py/compile.h" #include "py/mperrno.h" @@ -24,3 +25,14 @@ mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); #endif + +// Text-mode standard output +void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { + // This is a simplistic implementation for demonstration purposes. A real + // one would probably want to prefix every line, not just at the start of a + // write operation. + static int start_of_line = 1; + if (start_of_line) printf("py: "); + printf("%.*s", (int)len, str); + start_of_line = (len > 0 && (str[len-1] == '\n' || str[len-1] == '\r')); +} diff --git a/ports/embed/port/mpconfigport_common.h b/ports/embed/port/mpconfigport_common.h index 8e19859ed2..0e8ee4847a 100644 --- a/ports/embed/port/mpconfigport_common.h +++ b/ports/embed/port/mpconfigport_common.h @@ -40,3 +40,13 @@ typedef long mp_off_t; #endif #define MICROPY_MPHALPORT_H "port/mphalport.h" + +// Default implementation for applications that want output to go to the system +// printf(). If you don't, or don't have printf() available, or use +// shared/libc/printf.c (MICROPY_USE_INTERNAL_PRINTF) which would be a circular +// definition, #undef this and implement mp_hal_stdout_tx_strn_cooked(). +#define MP_PLAT_PRINT_STRN(str, len) \ + do { \ + extern int printf(const char *fmt, ...); \ + printf("%.*s", (int)len, str); \ + } while (0) diff --git a/ports/embed/port/mphalport.c b/ports/embed/port/mphalport.c deleted file mode 100644 index 8e76a8e22e..0000000000 --- a/ports/embed/port/mphalport.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2022-2023 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include "py/mphal.h" - -// Send string of given length to stdout, converting \n to \r\n. -void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { - printf("%.*s", (int)len, str); -}