Skip to content

Commit 30b7344

Browse files
committed
extmod/moduos_dupterm: Make mp_uos_dupterm_tx_strn() function reusable.
Function to actually spool output terminal data to dupterm object.
1 parent 00ee84e commit 30b7344

5 files changed

Lines changed: 20 additions & 20 deletions

File tree

extmod/misc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727

2828
// This file contains cumulative declarations for extmod/ .
2929

30+
#include <stddef.h>
3031
#include "py/runtime.h"
3132

3233
MP_DECLARE_CONST_FUN_OBJ(mp_uos_dupterm_obj);
34+
35+
#if MICROPY_PY_OS_DUPTERM
36+
void mp_uos_dupterm_tx_strn(const char *str, size_t len);
37+
#else
38+
#define mp_uos_dupterm_tx_strn(s, l)
39+
#endif

extmod/moduos_dupterm.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434

3535
#if MICROPY_PY_OS_DUPTERM
3636

37+
void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
38+
if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
39+
mp_obj_t write_m[3];
40+
mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m);
41+
write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str);
42+
mp_call_method_n_kw(1, 0, write_m);
43+
}
44+
}
45+
3746
STATIC mp_obj_t mp_uos_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
3847
if (n_args == 0) {
3948
if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) {

unix/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "py/gc.h"
4646
#include "py/stackctrl.h"
4747
#include "py/mphal.h"
48+
#include "extmod/misc.h"
4849
#include "genhdr/mpversion.h"
4950
#include "input.h"
5051

@@ -61,7 +62,7 @@ long heap_size = 1024*1024 * (sizeof(mp_uint_t) / 4);
6162
STATIC void stderr_print_strn(void *env, const char *str, size_t len) {
6263
(void)env;
6364
ssize_t dummy = write(STDERR_FILENO, str, len);
64-
mp_hal_dupterm_tx_strn(str, len);
65+
mp_uos_dupterm_tx_strn(str, len);
6566
(void)dummy;
6667
}
6768

unix/mpconfigport.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,6 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
243243

244244
#define MP_STATE_PORT MP_STATE_VM
245245

246-
#if MICROPY_PY_OS_DUPTERM
247-
#include <stddef.h>
248-
void mp_hal_dupterm_tx_strn(const char *str, size_t len);
249-
#else
250-
#define mp_hal_dupterm_tx_strn(s, l)
251-
#endif
252-
253246
#define MICROPY_PORT_ROOT_POINTERS \
254247
const char *readline_hist[50]; \
255248
mp_obj_t keyboard_interrupt_obj; \

unix/unix_mphal.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "py/mpstate.h"
3333
#include "py/mphal.h"
3434
#include "py/runtime.h"
35+
#include "extmod/misc.h"
3536

3637
#ifndef _WIN32
3738
#include <signal.h>
@@ -107,17 +108,6 @@ void mp_hal_stdio_mode_orig(void) {
107108

108109
#endif
109110

110-
#if MICROPY_PY_OS_DUPTERM
111-
void mp_hal_dupterm_tx_strn(const char *str, size_t len) {
112-
if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
113-
mp_obj_t write_m[3];
114-
mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m);
115-
write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str);
116-
mp_call_method_n_kw(1, 0, write_m);
117-
}
118-
}
119-
#endif
120-
121111
int mp_hal_stdin_rx_chr(void) {
122112
unsigned char c;
123113
#if MICROPY_PY_OS_DUPTERM
@@ -152,7 +142,7 @@ int mp_hal_stdin_rx_chr(void) {
152142

153143
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
154144
int ret = write(1, str, len);
155-
mp_hal_dupterm_tx_strn(str, len);
145+
mp_uos_dupterm_tx_strn(str, len);
156146
(void)ret; // to suppress compiler warning
157147
}
158148

0 commit comments

Comments
 (0)