stmhal: Implement sys.std{in,out,err}.buffer, for raw byte mode.

It's configurable and only enabled for stmhal port.
This commit is contained in:
Damien George 2015-05-13 23:49:21 +01:00
parent 968b7dd173
commit 3c4b5d4281
4 changed files with 48 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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