Skip to content

Commit 02fa035

Browse files
committed
stmhal: Add input() and pyb.input() functions.
1 parent c910972 commit 02fa035

5 files changed

Lines changed: 43 additions & 1 deletion

File tree

stmhal/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ SRC_C = \
7272
malloc0.c \
7373
gccollect.c \
7474
pyexec.c \
75+
input.c \
7576
pybmodule.c \
7677
osmodule.c \
7778
timemodule.c \

stmhal/input.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "nlr.h"
2+
#include "misc.h"
3+
#include "mpconfig.h"
4+
#include "qstr.h"
5+
#include "obj.h"
6+
#include "usb.h"
7+
8+
extern int readline(vstr_t *line, const char *prompt);
9+
10+
STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
11+
if (n_args == 1) {
12+
mp_obj_print(args[0], PRINT_REPR);
13+
}
14+
vstr_t line;
15+
vstr_init(&line, 16);
16+
int ret = readline(&line, "");
17+
if (line.len == 0 && ret == VCP_CHAR_CTRL_D) {
18+
nlr_jump(mp_obj_new_exception(&mp_type_EOFError));
19+
}
20+
mp_obj_t o = mp_obj_new_str((const byte*)line.buf, line.len, false);
21+
vstr_clear(&line);
22+
return o;
23+
}
24+
25+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

stmhal/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
2020

2121
// extra built in names to add to the global namespace
22+
extern const struct _mp_obj_fun_native_t mp_builtin_input_obj;
2223
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
2324
#define MICROPY_EXTRA_BUILTINS \
25+
{ MP_QSTR_input, (mp_obj_t)&mp_builtin_input_obj }, \
2426
{ MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj },
2527

2628
// type definitions for the specific machine

stmhal/pybmodule.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,20 @@ STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
196196
return mp_const_none;
197197
}
198198

199-
MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
199+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
200200

201201
#if 0
202202
MP_DEFINE_CONST_FUN_OBJ_2(pyb_I2C_obj, pyb_I2C); // TODO put this in i2c.c
203203
#endif
204204

205+
extern int stdin_rx_chr(void);
206+
207+
STATIC mp_obj_t pyb_input(void ) {
208+
return mp_obj_new_int(stdin_rx_chr());
209+
}
210+
211+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_input_obj, pyb_input);
212+
205213
MP_DECLARE_CONST_FUN_OBJ(pyb_source_dir_obj); // defined in main.c
206214
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
207215

@@ -268,6 +276,9 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
268276
#endif
269277
#endif
270278

279+
// input
280+
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&pyb_input_obj },
281+
271282
// pin mapper
272283
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_map_obj },
273284

stmhal/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ Q(urandom)
7070
// for time module
7171
Q(time)
7272
Q(sleep)
73+
74+
// for input
75+
Q(input)

0 commit comments

Comments
 (0)