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 <cwalther@gmx.ch>
pull/11430/head
Christian Walther 2023-05-01 18:18:27 +02:00
rodzic 16db37e7c6
commit 5e1ea0b5e9
4 zmienionych plików z 25 dodań i 33 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -3,6 +3,7 @@
* Copyright (c) 2022-2023 Damien P. George
*/
#include <stdio.h>
#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'));
}

Wyświetl plik

@ -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)

Wyświetl plik

@ -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 <stdio.h>
#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);
}