8000 stmhal: Implement os.dupterm (was pyb.repl_uart). · micropython/micropython@83158e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83158e0

Browse files
committed
stmhal: Implement os.dupterm (was pyb.repl_uart).
pyb.repl_uart still exists but points to os.dupterm.
1 parent d8066e9 commit 83158e0

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

stmhal/modpyb.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,6 @@ STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
124124
}
125125
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
126126

127-
/// \function repl_uart(uart)
128-
/// Get or set the UART object that the REPL is repeated on.
129-
STATIC mp_obj_t pyb_repl_uart(mp_uint_t n_args, const mp_obj_t *args) {
130-
if (n_args == 0) {
131-
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
132-
return mp_const_none;
133-
} else {
134-
return MP_STATE_PORT(pyb_stdio_uart);
135-
}
136-
} else {
137-
if (args[0] == mp_const_none) {
138-
MP_STATE_PORT(pyb_stdio_uart) = NULL;
139-
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
140-
MP_STATE_PORT(pyb_stdio_uart) = args[0];
141-
} else {
142-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
143-
}
144-
return mp_const_none;
145-
}
146-
}
147-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
148-
149127
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
150128

151129
STATIC const mp_map_elem_t pyb_module_globals_table[] = {
@@ -165,7 +143,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
165143
{ MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)&machine_sleep_obj },
166144
{ MP_OBJ_NEW_QSTR(MP_QSTR_standby), (mp_obj_t)&machine_deepsleep_obj },
167145
{ MP_OBJ_NEW_QSTR(MP_QSTR_main), (mp_obj_t)&pyb_main_obj },
168-
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&pyb_repl_uart_obj },
146+
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&mod_os_dupterm_obj },
169147

170148
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_mode), (mp_obj_t)&pyb_usb_mode_obj },
171149
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid_mouse), (mp_obj_t)&pyb_usb_hid_mouse_obj },

stmhal/moduos.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "lib/fatfs/diskio.h"
3636
#include "timeutils.h"
3737
#include "rng.h"
38+
#include "uart.h"
3839
#include "file.h"
3940
#include "sdcard.h"
4041
#include "fsusermount.h"
@@ -374,6 +375,28 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
374375
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
375376
#endif
376377

378+
// Get or set the UART object that the REPL is repeated on.
379+
// TODO should accept any object with read/write methods.
380+
STATIC mp_obj_t os_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
381+
if (n_args == 0) {
382+
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
383+
return mp_const_none;
384+
} else {
385+
return MP_STATE_PORT(pyb_stdio_uart);
386+
}
387+
} else {
388+
if (args[0] == mp_const_none) {
389+
MP_STATE_PORT(pyb_stdio_uart) = NULL;
390+
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
391+
MP_STATE_PORT(pyb_stdio_uart) = args[0];
392+
} else {
393+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
394+
}
395+
return mp_const_none;
396+
}
397+
}
398+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, os_dupterm);
399+
377400
STATIC const mp_map_elem_t os_module_globals_table[] = {
378401
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },
379402

@@ -397,6 +420,9 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
397420
#if MICROPY_HW_ENABLE_RNG
398421
{ MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
399422
#endif
423+
424+
// these are MicroPython extensions
425+
{ MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&mod_os_dupterm_obj },
400426
};
401427

402428
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);

stmhal/portmodules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ MP_DECLARE_CONST_FUN_OBJ(time_sleep_ms_obj);
3737
MP_DECLARE_CONST_FUN_OBJ(time_sleep_us_obj);
3838

3939
MP_DECLARE_CONST_FUN_OBJ(mod_os_sync_obj);
40+
MP_DECLARE_CONST_FUN_OBJ(mod_os_dupterm_obj);

stmhal/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ Q(unlink)
400400
Q(sep)
401401
Q(stat)
402402
Q(urandom)
403+
Q(dupterm)
403404

404405
// for time module
405406
Q(utime)

0 commit comments

Comments
 (0)
0