8000 stmhal: Add stdin/stdout/stderr objects. · lurch/micropython@86a0304 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86a0304

Browse files
committed
stmhal: Add stdin/stdout/stderr objects.
Available via sys.std{in,out,err}. Basic reading and writing supported. Even sys.stdin.readline!
1 parent e2a48b6 commit 86a0304

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
#define MICROPY_ENABLE_LFN (1)
2121
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
22+
#define MICROPY_MOD_SYS_STDFILES (1)
2223

2324
// extra built in names to add to the global namespace
2425
extern const struct _mp_obj_fun_native_t mp_builtin_help_obj;

stmhal/pybstdio.c

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
#include <stdio.h>
2+
13
#include <stm32f4xx_hal.h>
24

35
#include "misc.h"
46
#include "mpconfig.h"
57
#include "qstr.h"
68
#include "misc.h"
79
#include "obj.h"
10+
#include "stream.h"
811
#include "pybstdio.h"
912
#include "storage.h"
1013
#include "usb.h"
@@ -46,9 +49,97 @@ int stdin_rx_chr(void) {
4649
} else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
4750
return usart_rx_char(pyb_usart_global_debug);
4851
}
49-
HAL_Delay(1);
52+
__WFI();
5053
if (storage_needs_flush()) {
5154
storage_flush();
5255
}
5356
}
5457
}
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

Comments
 (0)
0