|
| 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); |
0 commit comments