diff options
author | Damien George <damien.p.george@gmail.com> | 2015-02-13 15:04:53 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-02-13 15:04:53 +0000 |
commit | 0b32e50365b6e36474e183a7240ccfc8fa56830a (patch) | |
tree | 101cf35f6844c50672af4f15b5866e226bcb47d9 /stmhal/pybstdio.c | |
parent | c385a639e6316cba4ff0e7a859325807857d8f00 (diff) | |
download | micropython-0b32e50365b6e36474e183a7240ccfc8fa56830a.tar.gz micropython-0b32e50365b6e36474e183a7240ccfc8fa56830a.zip |
stmhal: Make pybstdio usable by other ports, and use it.
Now all ports can use pybstdio.c to provide sys.stdin/stdout/stderr, so
long as they implement mp_hal_stdin_* and mp_hal_stdout_* functions.
Diffstat (limited to 'stmhal/pybstdio.c')
-rw-r--r-- | stmhal/pybstdio.c | 67 |
1 files changed, 5 insertions, 62 deletions
diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c index d8ccc1be0a..6b99049817 100644 --- a/stmhal/pybstdio.c +++ b/stmhal/pybstdio.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2013, 2014, 2015 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 @@ -25,70 +25,17 @@ */ #include <stdio.h> -#include <stdint.h> #include <string.h> #include <errno.h> -#include "py/mpstate.h" #include "py/obj.h" #include "py/stream.h" -#include "usb.h" -#include "uart.h" -#include "pybstdio.h" #include MICROPY_HAL_H // TODO make stdin, stdout and stderr writable objects so they can // be changed by Python code. This requires some changes, as these // objects are in a read-only module (py/modsys.c). -void stdout_tx_str(const char *str) { - stdout_tx_strn(str, strlen(str)); -} - -void stdout_tx_strn(const char *str, mp_uint_t len) { - if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { - uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len); - } -#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD - lcd_print_strn(str, len); -#endif - if (usb_vcp_is_enabled()) { - usb_vcp_send_strn(str, len); - } -} - -void stdout_tx_strn_cooked(const char *str, mp_uint_t len) { - // send stdout to UART and USB CDC VCP - if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { - uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len); - } - if (usb_vcp_is_enabled()) { - usb_vcp_send_strn_cooked(str, len); - } -} - -int stdin_rx_chr(void) { - for (;;) { -#if 0 -#ifdef USE_HOST_MODE - pyb_usb_host_process(); - int c = pyb_usb_host_get_keyboard(); - if (c != 0) { - return c; - } -#endif -#endif - - byte c; - if (usb_vcp_recv_byte(&c) != 0) { - return c; - } else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) { - return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart)); - } - __WFI(); - } -} - /******************************************************************************/ // Micro Python bindings @@ -110,7 +57,7 @@ STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *er pyb_stdio_obj_t *self = self_in; if (self->fd == STDIO_FD_IN) { for (uint i = 0; i < size; i++) { - int c = stdin_rx_chr(); + int c = mp_hal_stdin_rx_chr(); if (c == '\r') { c = '\n'; } @@ -126,7 +73,7 @@ STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *er STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { pyb_stdio_obj_t *self = self_in; if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) { - stdout_tx_strn_cooked(buf, size); + mp_hal_stdout_tx_strn_cooked(buf, size); return size; } else { *errcode = EPERM; @@ -134,10 +81,10 @@ STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, } } -mp_obj_t stdio_obj___exit__(mp_uint_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t stdio_obj___exit__(mp_uint_t n_args, const mp_obj_t *args) { return mp_const_none; } -static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__); // TODO gc hook to close the file if not already closed @@ -171,10 +118,6 @@ STATIC const mp_obj_type_t stdio_obj_type = { .locals_dict = (mp_obj_t)&stdio_locals_dict, }; -/// \moduleref sys -/// \constant stdin - standard input (connected to USB VCP, and optional UART object) -/// \constant stdout - standard output (connected to USB VCP, and optional UART object) -/// \constant stderr - standard error (connected to USB VCP, and optional UART object) const pyb_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN}; const pyb_stdio_obj_t mp_sys_stdout_obj = {{&stdio_obj_type}, .fd = STDIO_FD_OUT}; const pyb_stdio_obj_t mp_sys_stderr_obj = {{&stdio_obj_type}, .fd = STDIO_FD_ERR}; |