Skip to content

Commit

Permalink
stmhal: Implement os.dupterm (was pyb.repl_uart).
Browse files Browse the repository at this point in the history
pyb.repl_uart still exists but points to os.dupterm.
  • Loading branch information
dpgeorge committed Oct 19, 2015
1 parent d8066e9 commit 83158e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
24 changes: 1 addition & 23 deletions stmhal/modpyb.c
Expand Up @@ -124,28 +124,6 @@ STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);

/// \function repl_uart(uart)
/// Get or set the UART object that the REPL is repeated on.
STATIC mp_obj_t pyb_repl_uart(mp_uint_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
return mp_const_none;
} else {
return MP_STATE_PORT(pyb_stdio_uart);
}
} else {
if (args[0] == mp_const_none) {
MP_STATE_PORT(pyb_stdio_uart) = NULL;
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
MP_STATE_PORT(pyb_stdio_uart) = args[0];
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
}
return mp_const_none;
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);

MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c

STATIC const mp_map_elem_t pyb_module_globals_table[] = {
Expand All @@ -165,7 +143,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)&machine_sleep_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_standby), (mp_obj_t)&machine_deepsleep_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_main), (mp_obj_t)&pyb_main_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&pyb_repl_uart_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&mod_os_dupterm_obj },

{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_mode), (mp_obj_t)&pyb_usb_mode_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid_mouse), (mp_obj_t)&pyb_usb_hid_mouse_obj },
Expand Down
26 changes: 26 additions & 0 deletions stmhal/moduos.c
Expand Up @@ -35,6 +35,7 @@
#include "lib/fatfs/diskio.h"
#include "timeutils.h"
#include "rng.h"
#include "uart.h"
#include "file.h"
#include "sdcard.h"
#include "fsusermount.h"
Expand Down Expand Up @@ -374,6 +375,28 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
#endif

// Get or set the UART object that the REPL is repeated on.
// TODO should accept any object with read/write methods.

This comment has been minimized.

Copy link
@danicampora

danicampora Oct 20, 2015

Member

@dpgeorge fyi, this is already fully implemented in cc3200

This comment has been minimized.

Copy link
@dpgeorge

dpgeorge Oct 20, 2015

Author Member

@danicampora ok, thanks! I'll update it when I have a chance.

STATIC mp_obj_t os_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
return mp_const_none;
} else {
return MP_STATE_PORT(pyb_stdio_uart);
}
} else {
if (args[0] == mp_const_none) {
MP_STATE_PORT(pyb_stdio_uart) = NULL;
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
MP_STATE_PORT(pyb_stdio_uart) = args[0];
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
}
return mp_const_none;
}
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, os_dupterm);

STATIC const mp_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },

Expand All @@ -397,6 +420,9 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
#if MICROPY_HW_ENABLE_RNG
{ MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
#endif

// these are MicroPython extensions
{ MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&mod_os_dupterm_obj },
};

STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
Expand Down
1 change: 1 addition & 0 deletions stmhal/portmodules.h
Expand Up @@ -37,3 +37,4 @@ MP_DECLARE_CONST_FUN_OBJ(time_sleep_ms_obj);
MP_DECLARE_CONST_FUN_OBJ(time_sleep_us_obj);

MP_DECLARE_CONST_FUN_OBJ(mod_os_sync_obj);
MP_DECLARE_CONST_FUN_OBJ(mod_os_dupterm_obj);
1 change: 1 addition & 0 deletions stmhal/qstrdefsport.h
Expand Up @@ -400,6 +400,7 @@ Q(unlink)
Q(sep)
Q(stat)
Q(urandom)
Q(dupterm)

// for time module
Q(utime)
Expand Down

0 comments on commit 83158e0

Please sign in to comment.