Skip to content

Commit 5fd2ebb

Browse files
committed
stmhal: Update help function.
1 parent 24ff063 commit 5fd2ebb

File tree

4 files changed

+93
-31
lines changed

4 files changed

+93
-31
lines changed

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+
help.c \
7576
input.c \
7677
pybmodule.c \
7778
osmodule.c \

stmhal/help.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
3+
#include "nlr.h"
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "qstr.h"
7+
#include "obj.h"
8+
#include "map.h"
9+
10+
STATIC const char *help_text =
11+
"Welcome to Micro Python!\n"
12+
"\n"
13+
"For online help please visit http://micropython.org/help/.\n"
14+
"\n"
15+
"Specific commands for the board:\n"
16+
" pyb.info() -- print some general information\n"
17+
" pyb.gc() -- run the garbage collector\n"
18+
" pyb.repl_info(<val>) -- enable/disable printing of info after each command\n"
19+
" pyb.delay(<n>) -- wait for n milliseconds\n"
20+
" pyb.udelay(<n>) -- wait for n microseconds\n"
21+
" pyb.switch() -- return True/False if switch pressed or not\n"
22+
" pyb.Led(<n>) -- create Led object for LED n (n=1,2,3,4)\n"
23+
" Led methods: on(), off(), toggle(), intensity(<n>)\n"
24+
" pyb.Servo(<n>) -- create Servo object for servo n (n=1,2,3,4)\n"
25+
" Servo methods: angle(<x>)\n"
26+
" pyb.Accel() -- create an Accelerometer object\n"
27+
" Accelerometer methods: x(), y(), z(), tilt()\n"
28+
" pyb.rng() -- get a 30-bit hardware random number\n"
29+
" pyb.gpio(<port>) -- get port value (port='A4' for example)\n"
30+
" pyb.gpio(<port>, <val>) -- set port value, True or False, 1 or 0\n"
31+
" pyb.ADC(<port>) -- make an analog port object (port='C0' for example)\n"
32+
" ADC methods: read()\n"
33+
"\n"
34+
"Control commands:\n"
35+
" CTRL-A -- on a blank line, enter raw REPL mode\n"
36+
" CTRL-B -- on a blank line, enter normal REPL mode\n"
37+
" CTRL-C -- interrupt a running program\n"
38+
" CTRL-D -- on a blank line, do a soft reset of the board\n"
39+
;
40+
41+
STATIC void pyb_help_print_info_about_object(mp_obj_t name_o, const char *name_str, mp_obj_t value) {
42+
if (name_o != MP_OBJ_NULL) {
43+
printf(" ");
44+
mp_obj_print(name_o, PRINT_STR);
45+
printf(" -- ");
46+
} else {
47+
printf(" %s -- ", name_str);
48+
}
49+
mp_obj_print(value, PRINT_STR);
50+
printf("\n");
51+
}
52+
53+
STATIC mp_obj_t pyb_help(uint n_args, const mp_obj_t *args) {
54+
if (n_args == 0) {
55+
// print a general help message
56+
printf("%s", help_text);
57+
58+
} else {
59+
// try to print something sensible about the given object
60+
61+
printf("object ");
62+
mp_obj_print(args[0], PRINT_STR);
63+
printf(" is of type %s\n", mp_obj_get_type_str(args[0]));
64+
65+
mp_obj_type_t *type = mp_obj_get_type(args[0]);
66+
mp_map_t *map = NULL;
67+
if (type == &mp_type_module) {
68+
map = mp_obj_module_get_globals(args[0]);
69+
} else if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)) {
70+
map = mp_obj_dict_get_map(type->locals_dict);
71+
}
72+
if (map != NULL) {
73+
for (uint i = 0; i < map->alloc; i++) {
74+
if (map->table[i].key != MP_OBJ_NULL) {
75+
pyb_help_print_info_about_object(map->table[i].key, NULL, map->table[i].value);
76+
}
77+
}
78+
}
79+
80+
if (type->methods != NULL) {
81+
for (const mp_method_t *meth = type->methods; meth->name != NULL; meth++) {
82+
pyb_help_print_info_about_object(MP_OBJ_NULL, meth->name, (mp_obj_t)meth->fun);
83+
}
84+
}
85+
}
86+
87+
return mp_const_none;
88+
}
89+
90+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, pyb_help);

stmhal/main.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,34 +121,6 @@ static const char fresh_main_py[] =
121121
"# main.py -- put your code here!\n"
122122
;
123123

124-
static const char *help_text =
125-
"Welcome to Micro Python!\n\n"
126-
"This is a *very* early version of Micro Python and has minimal functionality.\n\n"
127-
"Specific commands for the board:\n"
128-
" pyb.info() -- print some general information\n"
129-
" pyb.gc() -- run the garbage collector\n"
130-
" pyb.repl_info(<val>) -- enable/disable printing of info after each command\n"
131-
" pyb.delay(<n>) -- wait for n milliseconds\n"
132-
" pyb.udelay(<n>) -- wait for n microseconds\n"
133-
" pyb.Led(<n>) -- create Led object for LED n (n=1,2)\n"
134-
" Led methods: on(), off()\n"
135-
" pyb.Servo(<n>) -- create Servo object for servo n (n=1,2,3,4)\n"
136-
" Servo methods: angle(<x>)\n"
137-
" pyb.switch() -- return True/False if switch pressed or not\n"
138-
" pyb.accel() -- get accelerometer values\n"
139-
" pyb.rand() -- get a 16-bit random number\n"
140-
" pyb.gpio(<port>) -- get port value (port='A4' for example)\n"
141-
" pyb.gpio(<port>, <val>) -- set port value, True or False, 1 or 0\n"
142-
" pyb.ADC(<port>) -- make an analog port object (port='C0' for example)\n"
143-
" ADC methods: read()\n"
144-
;
145-
146-
// get some help about available functions
147-
static mp_obj_t pyb_help(void) {
148-
printf("%s", help_text);
149-
return mp_const_none;
150-
}
151-
152124
int main(void) {
153125
// TODO disable JTAG
154126

@@ -268,9 +240,6 @@ int main(void) {
268240

269241
pin_map_init();
270242

271-
// add some functions to the builtin Python namespace
272-
rt_store_name(MP_QSTR_help, rt_make_function_n(0, pyb_help));
273-
274243
// we pre-import the pyb module
275244
// probably shouldn't do this, so we are compatible with CPython
276245
rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);

stmhal/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
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_help_obj;
2223
extern const struct _mp_obj_fun_native_t mp_builtin_input_obj;
2324
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
2425
#define MICROPY_EXTRA_BUILTINS \
26+
{ MP_QSTR_help, (mp_obj_t)&mp_builtin_help_obj }, \
2527
{ MP_QSTR_input, (mp_obj_t)&mp_builtin_input_obj }, \
2628
{ MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj },
2729

0 commit comments

Comments
 (0)