|
4 | 4 | * The MIT License (MIT) |
5 | 5 | * |
6 | 6 | * Copyright (c) 2016 Paul Sokolovsky |
| 7 | + * Copyright (c) 2017 Damien P. George |
7 | 8 | * |
8 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | 10 | * of this software and associated documentation files (the "Software"), to deal |
|
31 | 32 | #include "py/objtuple.h" |
32 | 33 | #include "py/objarray.h" |
33 | 34 | #include "py/stream.h" |
| 35 | +#include "lib/utils/interrupt_char.h" |
34 | 36 |
|
35 | 37 | #if MICROPY_PY_OS_DUPTERM |
36 | 38 |
|
37 | | -void mp_uos_deactivate(const char *msg, mp_obj_t exc) { |
38 | | - mp_obj_t term = MP_STATE_PORT(term_obj); |
39 | | - MP_STATE_PORT(term_obj) = NULL; |
| 39 | +void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) { |
| 40 | + mp_obj_t term = MP_STATE_VM(dupterm_objs[dupterm_idx]); |
| 41 | + MP_STATE_VM(dupterm_objs[dupterm_idx]) = MP_OBJ_NULL; |
40 | 42 | mp_printf(&mp_plat_print, msg); |
41 | 43 | if (exc != MP_OBJ_NULL) { |
42 | 44 | mp_obj_print_exception(&mp_plat_print, exc); |
43 | 45 | } |
44 | 46 | mp_stream_close(term); |
45 | 47 | } |
46 | 48 |
|
| 49 | +int mp_uos_dupterm_rx_chr(void) { |
| 50 | + for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) { |
| 51 | + if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + nlr_buf_t nlr; |
| 56 | + if (nlr_push(&nlr) == 0) { |
| 57 | + mp_obj_t readinto_m[3]; |
| 58 | + mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_readinto, readinto_m); |
| 59 | + readinto_m[2] = MP_STATE_VM(dupterm_arr_obj); |
| 60 | + mp_obj_t res = mp_call_method_n_kw(1, 0, readinto_m); |
| 61 | + if (res == mp_const_none) { |
| 62 | + nlr_pop(); |
| 63 | + } else if (res == MP_OBJ_NEW_SMALL_INT(0)) { |
| 64 | + mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL); |
| 65 | + nlr_pop(); |
| 66 | + } else { |
| 67 | + mp_buffer_info_t bufinfo; |
| 68 | + mp_get_buffer_raise(MP_STATE_VM(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ); |
| 69 | + nlr_pop(); |
| 70 | + if (*(byte*)bufinfo.buf == mp_interrupt_char) { |
| 71 | + // Signal keyboard interrupt to be raised as soon as the VM resumes |
| 72 | + mp_keyboard_interrupt(); |
| 73 | + return -2; |
| 74 | + } |
| 75 | + return *(byte*)bufinfo.buf; |
| 76 | + } |
| 77 | + } else { |
| 78 | + mp_uos_deactivate(idx, "dupterm: Exception in read() method, deactivating: ", nlr.ret_val); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // No chars available |
| 83 | + return -1; |
| 84 | +} |
| 85 | + |
47 | 86 | void mp_uos_dupterm_tx_strn(const char *str, size_t len) { |
48 | | - if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { |
| 87 | + for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) { |
| 88 | + if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) { |
| 89 | + continue; |
| 90 | + } |
49 | 91 | nlr_buf_t nlr; |
50 | 92 | if (nlr_push(&nlr) == 0) { |
51 | 93 | mp_obj_t write_m[3]; |
52 | | - mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m); |
| 94 | + mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_write, write_m); |
53 | 95 |
|
54 | | - mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); |
| 96 | + mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_VM(dupterm_arr_obj)); |
55 | 97 | void *org_items = arr->items; |
56 | 98 | arr->items = (void*)str; |
57 | 99 | arr->len = len; |
58 | | - write_m[2] = MP_STATE_PORT(dupterm_arr_obj); |
| 100 | + write_m[2] = MP_STATE_VM(dupterm_arr_obj); |
59 | 101 | mp_call_method_n_kw(1, 0, write_m); |
60 | | - arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); |
| 102 | + arr = MP_OBJ_TO_PTR(MP_STATE_VM(dupterm_arr_obj)); |
61 | 103 | arr->items = org_items; |
62 | 104 | arr->len = 1; |
63 | 105 | nlr_pop(); |
64 | 106 | } else { |
65 | | - mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val); |
| 107 | + mp_uos_deactivate(idx, "dupterm: Exception in write() method, deactivating: ", nlr.ret_val); |
66 | 108 | } |
67 | 109 | } |
68 | 110 | } |
69 | 111 |
|
70 | 112 | STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) { |
71 | | - if (n_args == 0) { |
72 | | - if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) { |
73 | | - return mp_const_none; |
74 | | - } else { |
75 | | - return MP_STATE_PORT(term_obj); |
76 | | - } |
| 113 | + mp_int_t idx = 0; |
| 114 | + if (n_args == 2) { |
| 115 | + idx = mp_obj_get_int(args[1]); |
| 116 | + } |
| 117 | + |
| 118 | + if (idx < 0 || idx >= MICROPY_PY_OS_DUPTERM) { |
| 119 | + mp_raise_ValueError("invalid dupterm index"); |
| 120 | + } |
| 121 | + |
| 122 | + mp_obj_t previous_obj = MP_STATE_VM(dupterm_objs[idx]); |
| 123 | + if (previous_obj == MP_OBJ_NULL) { |
| 124 | + previous_obj = mp_const_none; |
| 125 | + } |
| 126 | + if (args[0] == mp_const_none) { |
| 127 | + MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL; |
77 | 128 | } else { |
78 | | - if (args[0] == mp_const_none) { |
79 | | - MP_STATE_PORT(term_obj) = MP_OBJ_NULL; |
80 | | - } else { |
81 | | - MP_STATE_PORT(term_obj) = args[0]; |
82 | | - if (MP_STATE_PORT(dupterm_arr_obj) == MP_OBJ_NULL) { |
83 | | - MP_STATE_PORT(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); |
84 | | - } |
| 129 | + MP_STATE_VM(dupterm_objs[idx]) = args[0]; |
| 130 | + if (MP_STATE_VM(dupterm_arr_obj) == MP_OBJ_NULL) { |
| 131 | + MP_STATE_VM(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); |
85 | 132 | } |
86 | | - return mp_const_none; |
87 | 133 | } |
| 134 | + |
| 135 | + return previous_obj; |
88 | 136 | } |
89 | | -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 0, 1, mp_uos_dupterm); |
| 137 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 1, 2, mp_uos_dupterm); |
90 | 138 |
|
91 | 139 | #endif |
0 commit comments