diff options
author | Damien George <damien.p.george@gmail.com> | 2015-05-13 23:49:21 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-05-24 14:31:33 +0100 |
commit | 3c4b5d42817944438c16fe6f1c70447ffc751110 (patch) | |
tree | 7a8c50e63ffda480a91a86245bc6aba02c5b0587 /stmhal/pybstdio.c | |
parent | 968b7dd173d58d917c18dfa7706087db2abcfa6c (diff) | |
download | micropython-3c4b5d42817944438c16fe6f1c70447ffc751110.tar.gz micropython-3c4b5d42817944438c16fe6f1c70447ffc751110.zip |
stmhal: Implement sys.std{in,out,err}.buffer, for raw byte mode.
It's configurable and only enabled for stmhal port.
Diffstat (limited to 'stmhal/pybstdio.c')
-rw-r--r-- | stmhal/pybstdio.c | 39 |
1 files changed, 39 insertions, 0 deletions
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, "<io.FileIO %d>", 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 |