stmhal: Add input() and pyb.input() functions.

This commit is contained in:
Damien George 2014-03-22 23:53:50 +00:00
parent c91097223d
commit 02fa035800
5 changed files with 43 additions and 1 deletions

View File

@ -72,6 +72,7 @@ SRC_C = \
malloc0.c \
gccollect.c \
pyexec.c \
input.c \
pybmodule.c \
osmodule.c \
timemodule.c \

25
stmhal/input.c Normal file
View File

@ -0,0 +1,25 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "usb.h"
extern int readline(vstr_t *line, const char *prompt);
STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
if (n_args == 1) {
mp_obj_print(args[0], PRINT_REPR);
}
vstr_t line;
vstr_init(&line, 16);
int ret = readline(&line, "");
if (line.len == 0 && ret == VCP_CHAR_CTRL_D) {
nlr_jump(mp_obj_new_exception(&mp_type_EOFError));
}
mp_obj_t o = mp_obj_new_str((const byte*)line.buf, line.len, false);
vstr_clear(&line);
return o;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

View File

@ -19,8 +19,10 @@
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
// extra built in names to add to the global namespace
extern const struct _mp_obj_fun_native_t mp_builtin_input_obj;
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
#define MICROPY_EXTRA_BUILTINS \
{ MP_QSTR_input, (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj },
// type definitions for the specific machine

View File

@ -196,12 +196,20 @@ STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
#if 0
MP_DEFINE_CONST_FUN_OBJ_2(pyb_I2C_obj, pyb_I2C); // TODO put this in i2c.c
#endif
extern int stdin_rx_chr(void);
STATIC mp_obj_t pyb_input(void ) {
return mp_obj_new_int(stdin_rx_chr());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_input_obj, pyb_input);
MP_DECLARE_CONST_FUN_OBJ(pyb_source_dir_obj); // defined in main.c
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
@ -268,6 +276,9 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
#endif
#endif
// input
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&pyb_input_obj },
// pin mapper
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_map_obj },

View File

@ -70,3 +70,6 @@ Q(urandom)
// for time module
Q(time)
Q(sleep)
// for input
Q(input)