diff --git a/py/mpconfig.h b/py/mpconfig.h index 560fdce9ce..5065e36828 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -594,6 +594,11 @@ typedef double mp_float_t; #define MICROPY_PY_SYS_STDFILES (0) #endif +// Whether to provide sys.{stdin,stdout,stderr}.buffer object +// This is implemented per-port +#ifndef MICROPY_PY_SYS_STDIO_BUFFER +#define MICROPY_PY_SYS_STDIO_BUFFER (0) +#endif // Extended modules diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 4b5218eb5c..2f51d470dd 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -448,6 +448,9 @@ Q(platform) Q(stdin) Q(stdout) Q(stderr) +#if MICROPY_PY_SYS_STDIO_BUFFER +Q(buffer) +#endif Q(version) Q(version_info) #if MICROPY_PY_ATTRTUPLE diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 26c969cd55..8d6031555c 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -66,6 +66,7 @@ #define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_STDFILES (1) +#define MICROPY_PY_SYS_STDIO_BUFFER (1) #define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1) #define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1) #define MICROPY_PY_CMATH (1) diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c index baa3e77e4d..de0a3a0e3e 100644 --- a/stmhal/pybstdio.c +++ b/stmhal/pybstdio.c @@ -48,6 +48,10 @@ typedef struct _pyb_stdio_obj_t { int fd; } pyb_stdio_obj_t; +#if MICROPY_PY_SYS_STDIO_BUFFER +STATIC const pyb_stdio_obj_t stdio_buffer_obj; +#endif + void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { pyb_stdio_obj_t *self = self_in; mp_printf(print, "", self->fd); @@ -89,6 +93,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_o // TODO gc hook to close the file if not already closed STATIC const mp_map_elem_t stdio_locals_dict_table[] = { +#if MICROPY_PY_SYS_STDIO_BUFFER + { MP_OBJ_NEW_QSTR(MP_QSTR_buffer), (mp_obj_t)&stdio_buffer_obj }, +#endif { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, @@ -123,3 +130,35 @@ STATIC const mp_obj_type_t stdio_obj_type = { 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}; + +#if MICROPY_PY_SYS_STDIO_BUFFER +STATIC mp_uint_t stdio_buffer_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { + for (uint i = 0; i < size; i++) { + ((byte*)buf)[i] = mp_hal_stdin_rx_chr(); + } + return size; +} + +STATIC mp_uint_t stdio_buffer_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { + mp_hal_stdout_tx_strn(buf, size); + return size; +} + +STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = { + .read = stdio_buffer_read, + .write = stdio_buffer_write, + .is_text = false, +}; + +STATIC const mp_obj_type_t stdio_buffer_obj_type = { + { &mp_type_type }, + .name = MP_QSTR_FileIO, + .print = stdio_obj_print, + .getiter = mp_identity, + .iternext = mp_stream_unbuffered_iter, + .stream_p = &stdio_buffer_obj_stream_p, + .locals_dict = (mp_obj_t)&stdio_locals_dict, +}; + +STATIC const pyb_stdio_obj_t stdio_buffer_obj = {{&stdio_buffer_obj_type}, .fd = 0}; // fd unused +#endif