summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-13 19:00:56 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-13 19:00:56 +0100
commit86a03044e343433115bbbe37df544fc1305f36d3 (patch)
tree7676a23be4f68b5cf151a64c50c8e10f06f5b6ee
parente2a48b66c2a94375d3af00500073e05a5e5fd75a (diff)
downloadmicropython-86a03044e343433115bbbe37df544fc1305f36d3.tar.gz
micropython-86a03044e343433115bbbe37df544fc1305f36d3.zip
stmhal: Add stdin/stdout/stderr objects.
Available via sys.std{in,out,err}. Basic reading and writing supported. Even sys.stdin.readline!
-rw-r--r--stmhal/mpconfigport.h1
-rw-r--r--stmhal/pybstdio.c93
2 files changed, 93 insertions, 1 deletions
diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h
index 6a3182346c..d7045c42f7 100644
--- a/stmhal/mpconfigport.h
+++ b/stmhal/mpconfigport.h
@@ -19,6 +19,7 @@
*/
#define MICROPY_ENABLE_LFN (1)
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
+#define MICROPY_MOD_SYS_STDFILES (1)
// extra built in names to add to the global namespace
extern const struct _mp_obj_fun_native_t mp_builtin_help_obj;
diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c
index 95582d330e..d40cebf947 100644
--- a/stmhal/pybstdio.c
+++ b/stmhal/pybstdio.c
@@ -1,3 +1,5 @@
+#include <stdio.h>
+
#include <stm32f4xx_hal.h>
#include "misc.h"
@@ -5,6 +7,7 @@
#include "qstr.h"
#include "misc.h"
#include "obj.h"
+#include "stream.h"
#include "pybstdio.h"
#include "storage.h"
#include "usb.h"
@@ -46,9 +49,97 @@ int stdin_rx_chr(void) {
} else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
return usart_rx_char(pyb_usart_global_debug);
}
- HAL_Delay(1);
+ __WFI();
if (storage_needs_flush()) {
storage_flush();
}
}
}
+
+
+/******************************************************************************/
+// Micro Python bindings
+
+#define STDIO_FD_IN (0)
+#define STDIO_FD_OUT (1)
+#define STDIO_FD_ERR (2)
+
+typedef struct _pyb_stdio_obj_t {
+ mp_obj_base_t base;
+ int fd;
+} pyb_stdio_obj_t;
+
+void stdio_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
+ pyb_stdio_obj_t *self = self_in;
+ printf("<io.FileIO %d>", self->fd);
+}
+
+STATIC machine_int_t stdio_read(mp_obj_t self_in, void *buf, machine_uint_t size, int *errcode) {
+ pyb_stdio_obj_t *self = self_in;
+ if (self->fd == STDIO_FD_IN) {
+ for (uint i = 0; i < size; i++) {
+ int c = stdin_rx_chr();
+ if (c == '\r') {
+ c = '\n';
+ }
+ ((byte*)buf)[i] = c;
+ }
+ *errcode = 0;
+ return size;
+ } else {
+ *errcode = 1;
+ return 0;
+ }
+}
+
+STATIC machine_int_t stdio_write(mp_obj_t self_in, const void *buf, machine_uint_t size, int *errcode) {
+ pyb_stdio_obj_t *self = self_in;
+ if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
+ stdout_tx_strn(buf, size);
+ *errcode = 0;
+ return size;
+ } else {
+ *errcode = 1;
+ return 0;
+ }
+}
+
+mp_obj_t stdio_obj___exit__(uint n_args, const mp_obj_t *args) {
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__);
+
+// TODO gc hook to close the file if not already closed
+
+STATIC const mp_map_elem_t stdio_locals_dict_table[] = {
+ { 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_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
+ { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&mp_identity_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&mp_identity_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&stdio_obj___exit___obj },
+};
+
+STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
+
+STATIC const mp_stream_p_t stdio_obj_stream_p = {
+ .read = stdio_read,
+ .write = stdio_write,
+};
+
+STATIC const mp_obj_type_t stdio_obj_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_FileIO,
+ // TODO .make_new?
+ .print = stdio_obj_print,
+ .getiter = mp_identity,
+ .iternext = mp_stream_unbuffered_iter,
+ .stream_p = &stdio_obj_stream_p,
+ .locals_dict = (mp_obj_t)&stdio_locals_dict,
+};
+
+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};