|
| 1 | +#include <stdio.h> |
| 2 | + |
1 | 3 | #include <stm32f4xx_hal.h>
|
2 | 4 |
|
3 | 5 | #include "misc.h"
|
4 | 6 | #include "mpconfig.h"
|
5 | 7 | #include "qstr.h"
|
6 | 8 | #include "misc.h"
|
7 | 9 | #include "obj.h"
|
| 10 | +#include "stream.h" |
8 | 11 | #include "pybstdio.h"
|
9 | 12 | #include "storage.h"
|
10 | 13 | #include "usb.h"
|
@@ -46,9 +49,97 @@ int stdin_rx_chr(void) {
|
46 | 49 | } else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
|
47 | 50 | return usart_rx_char(pyb_usart_global_debug);
|
48 | 51 | }
|
49 |
| - HAL_Delay(1); |
| 52 | + __WFI(); |
50 | 53 | if (storage_needs_flush()) {
|
51 | 54 | storage_flush();
|
52 | 55 | }
|
53 | 56 | }
|
54 | 57 | }
|
| 58 | + |
| 59 | + |
| 60 | +/******************************************************************************/ |
| 61 | +// Micro Python bindings |
| 62 | + |
| 63 | +#define STDIO_FD_IN (0) |
| 64 | +#define STDIO_FD_OUT (1) |
| 65 | +#define STDIO_FD_ERR (2) |
| 66 | + |
| 67 | +typedef struct _pyb_stdio_obj_t { |
| 68 | + mp_obj_base_t base; |
| 69 | + int fd; |
| 70 | +} pyb_stdio_obj_t; |
| 71 | + |
| 72 | +void stdio_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 73 | + pyb_stdio_obj_t *self = self_in; |
| 74 | + printf("<io.FileIO %d>", self->fd); |
| 75 | +} |
| 76 | + |
| 77 | +STATIC machine_int_t stdio_read(mp_obj_t self_in, void *buf, machine_uint_t size, int *errcode) { |
| 78 | + pyb_stdio_obj_t *self = self_in; |
| 79 | + if (self->fd == STDIO_FD_IN) { |
| 80 | + for (uint i = 0; i < size; i++) { |
| 81 | + int c = stdin_rx_chr(); |
| 82 | + if (c == '\r') { |
| 83 | + c = '\n'; |
| 84 | + } |
| 85 | + ((byte*)buf)[i] = c; |
| 86 | + } |
| 87 | + *errcode = 0; |
| 88 | + return size; |
| 89 | + } else { |
| 90 | + *errcode = 1; |
| 91 | + return 0; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +STATIC machine_int_t stdio_write(mp_obj_t self_in, const void *buf, machine_uint_t size, int *errcode) { |
| 96 | + pyb_stdio_obj_t *self = self_in; |
| 97 | + if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) { |
| 98 | + stdout_tx_strn(buf, size); |
| 99 | + *errcode = 0; |
| 100 | + return size; |
| 101 | + } else { |
| 102 | + *errcode = 1; |
| 103 | + return 0; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +mp_obj_t stdio_obj___exit__(uint n_args, const mp_obj_t *args) { |
| 108 | + return mp_const_none; |
| 109 | +} |
| 110 | +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__); |
| 111 | + |
| 112 | +// TODO gc hook to close the file if not already closed |
| 113 | + |
| 114 | +STATIC const mp_map_elem_t stdio_locals_dict_table[] = { |
| 115 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, |
| 116 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, |
| 117 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, |
| 118 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 119 | + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&mp_identity_obj }, |
| 120 | + { MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&mp_identity_obj }, |
| 121 | + { MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj }, |
| 122 | + { MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&stdio_obj___exit___obj }, |
| 123 | +}; |
| 124 | + |
| 125 | +STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table); |
| 126 | + |
| 127 | +STATIC const mp_stream_p_t stdio_obj_stream_p = { |
| 128 | + .read = stdio_read, |
| 129 | + .write = stdio_write, |
| 130 | +}; |
| 131 | + |
| 132 | +STATIC const mp_obj_type_t stdio_obj_type = { |
| 133 | + { &mp_type_type }, |
| 134 | + .name = MP_QSTR_FileIO, |
| 135 | + // TODO .make_new? |
| 136 | + .print = stdio_obj_print, |
| 137 | + .getiter = mp_identity, |
| 138 | + .iternext = mp_stream_unbuffered_iter, |
| 139 | + .stream_p = &stdio_obj_stream_p, |
| 140 | + .locals_dict = (mp_obj_t)&stdio_locals_dict, |
| 141 | +}; |
| 142 | + |
| 143 | +const pyb_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN}; |
| 144 | +const pyb_stdio_obj_t mp_sys_stdout_obj = {{&stdio_obj_type}, .fd = STDIO_FD_OUT}; |
| 145 | +const pyb_stdio_obj_t mp_sys_stderr_obj = {{&stdio_obj_type}, .fd = STDIO_FD_ERR}; |
0 commit comments