diff --git a/inc/genhdr/qstrdefs.generated.h b/inc/genhdr/qstrdefs.generated.h index ca91f3c0d..e15bfd773 100644 --- a/inc/genhdr/qstrdefs.generated.h +++ b/inc/genhdr/qstrdefs.generated.h @@ -244,6 +244,8 @@ QDEF(MP_QSTR_sleep, (const byte*)"\xea\x05" "sleep") QDEF(MP_QSTR_random, (const byte*)"\xbe\x06" "random") QDEF(MP_QSTR_system_time, (const byte*)"\xca\x0b" "system_time") QDEF(MP_QSTR_panic, (const byte*)"\xd0\x05" "panic") +QDEF(MP_QSTR_this, (const byte*)"\xa3\x04" "this") +QDEF(MP_QSTR_authors, (const byte*)"\x63\x07" "authors") QDEF(MP_QSTR_MicroBitPin, (const byte*)"\x77\x0b" "MicroBitPin") QDEF(MP_QSTR_set_digital_value, (const byte*)"\x56\x11" "set_digital_value") QDEF(MP_QSTR_get_digital_value, (const byte*)"\x42\x11" "get_digital_value") diff --git a/inc/microbit/modmicrobit.h b/inc/microbit/modmicrobit.h index d4d48c959..770374579 100644 --- a/inc/microbit/modmicrobit.h +++ b/inc/microbit/modmicrobit.h @@ -64,6 +64,7 @@ MP_DECLARE_CONST_FUN_OBJ(microbit_sleep_obj); MP_DECLARE_CONST_FUN_OBJ(microbit_random_obj); MP_DECLARE_CONST_FUN_OBJ(microbit_system_time_obj); MP_DECLARE_CONST_FUN_OBJ(microbit_panic_obj); +MP_DECLARE_CONST_FUN_OBJ(microbit_accelerometer_get_x_obj); extern const mp_obj_module_t microbit_module; diff --git a/inc/microbit/mpconfigport.h b/inc/microbit/mpconfigport.h index 3d3c9984a..fc9270f46 100644 --- a/inc/microbit/mpconfigport.h +++ b/inc/microbit/mpconfigport.h @@ -75,8 +75,10 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj; // extra builtin modules to add to the list of known ones extern const struct _mp_obj_module_t microbit_module; +extern const struct _mp_obj_module_t this_module; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_microbit), (mp_obj_t)µbit_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_this), (mp_obj_t)&this_module }, \ #define MP_STATE_PORT MP_STATE_VM diff --git a/inc/microbit/qstrdefsport.h b/inc/microbit/qstrdefsport.h index f032d67d4..a7046ec97 100644 --- a/inc/microbit/qstrdefsport.h +++ b/inc/microbit/qstrdefsport.h @@ -8,6 +8,9 @@ Q(random) Q(system_time) Q(panic) +Q(this) +Q(authors) + Q(MicroBitPin) Q(set_digital_value) Q(get_digital_value) diff --git a/source/microbit/help.c b/source/microbit/help.c index 1ee4e3c7a..66b070223 100644 --- a/source/microbit/help.c +++ b/source/microbit/help.c @@ -33,19 +33,19 @@ STATIC const char *help_text = "Welcome to MicroPython on the micro:bit!\n" "\n" -"Be brave! Break things! Learn and have fun! :-)\n" -"\n" "Type 'import microbit', press return and try these commands:\n" " microbit.display.scroll('Hello')\n" " microbit.system_time()\n" " microbit.sleep(1000)\n" " microbit.button_a.is_pressed()\n" "What do these commands do? Can you improve them? HINT: use the up and down\n" -"arrow keys to get your command history (saves you typing).\n" +"arrow keys to get your command history. Press the TAB key to auto-complete\n" +"unfinished words (so 'mi' becomes 'microbit' after you press TAB). These\n" +"tricks save a lot of typing and look cool!\n" "\n" "Explore:\n" "Type 'help(something)' to find out about it. Type 'dir(something)' to see what\n" -"it can do.\n" +"it can do. For goodness sake, don't type 'import this'.\n" "\n" "Stuff to explore:\n" " microbit.accelerometer -- detect the device's position (orientation)\n" @@ -53,14 +53,18 @@ STATIC const char *help_text = " microbit.button_b.is_pressed() -- is button B pressed? (True or False)\n" " microbit.compass -- detect the device's heading\n" " microbit.display -- display things (pixels, characters, words)\n" -" microbit.panic() -- go into panic mode (requires a restart)\n" -" microbit.random(n) -- get a random number between 0 and n\n" +" microbit.io -- control the gold input/output (IO) pins\n" +" microbit.panic() -- enter panic mode (requires a restart)\n" +" microbit.random(n) -- get a random number between 0 and n-1\n" " microbit.sleep(n) -- wait for n milliseconds (1 second = 1000)\n" " microbit.system_time() -- get the number of milliseconds since reset\n" "\n" "Control commands:\n" " CTRL-C -- stop a running program\n" " CTRL-D -- on a blank line, do a soft reset of the micro:bit\n" +"\n" +"For more information about Python, visit: http://python.org/\n" +"To find out about MicroPython, visit: http://micropython.org/\n" ; typedef struct _mp_doc_t { @@ -73,11 +77,13 @@ STATIC const mp_doc_t help_table_types[] = { }; STATIC const mp_doc_t help_table_instances[] = { - {µbit_module, "microbit module\n"}, - {µbit_sleep_obj, "sleep(ms) -- sleep for the given number of milliseconds\n"}, - {µbit_random_obj, "random(max) -- return a random integer between 0 and max-1\n"}, - {µbit_system_time_obj, "system_time() -- return an integer represeting the milliseconds since reset\n"}, - {µbit_panic_obj, "panic([code]) -- enter panic mode!\n"}, + {µbit_module, "A toolbox of code to control the micro:bit hardware\n"}, + {µbit_panic_obj, "panic() -- enter panic mode (requires a restart)\n"}, + {µbit_random_obj, "random(n) -- return a random number between 0 and n-1\n"}, + {µbit_sleep_obj, "sleep(n) -- wait for n milliseconds (1 second = 1000)\n"}, + {µbit_system_time_obj, "system_time() -- get the number of milliseconds since reset\n"}, + {µbit_accelerometer_obj, "A toolbox of code to detect the device's position (orientation)\n"}, + {µbit_accelerometer_get_x_obj, "Foo!\n"}, }; STATIC void pyb_help_print_info_about_object(mp_obj_t name_o, mp_obj_t value) { diff --git a/source/microbit/microbitaccelerometer.cpp b/source/microbit/microbitaccelerometer.cpp index c84ef80c6..73890fe48 100644 --- a/source/microbit/microbitaccelerometer.cpp +++ b/source/microbit/microbitaccelerometer.cpp @@ -40,19 +40,19 @@ mp_obj_t microbit_accelerometer_get_x(mp_obj_t self_in) { microbit_accelerometer_obj_t *self = (microbit_accelerometer_obj_t*)self_in; return mp_obj_new_int(self->accelerometer->getX()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_x_obj, microbit_accelerometer_get_x); +MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_x_obj, microbit_accelerometer_get_x); mp_obj_t microbit_accelerometer_get_y(mp_obj_t self_in) { microbit_accelerometer_obj_t *self = (microbit_accelerometer_obj_t*)self_in; return mp_obj_new_int(self->accelerometer->getY()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_y_obj, microbit_accelerometer_get_y); +MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_y_obj, microbit_accelerometer_get_y); mp_obj_t microbit_accelerometer_get_z(mp_obj_t self_in) { microbit_accelerometer_obj_t *self = (microbit_accelerometer_obj_t*)self_in; return mp_obj_new_int(self->accelerometer->getZ()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_z_obj, microbit_accelerometer_get_z); +MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_z_obj, microbit_accelerometer_get_z); STATIC const mp_map_elem_t microbit_accelerometer_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_get_x), (mp_obj_t)µbit_accelerometer_get_x_obj }, diff --git a/source/microbit/modthis.cpp b/source/microbit/modthis.cpp new file mode 100644 index 000000000..a599bca84 --- /dev/null +++ b/source/microbit/modthis.cpp @@ -0,0 +1,79 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2015 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "MicroBit.h" + +extern "C" { + +#include "py/obj.h" + +STATIC mp_obj_t this__init__(void) { + STATIC const char *this_text = +"The Zen of MicroPython, by Nicholas H.Tollervey\n" +"\n" +"Code,\n" +"Hack it,\n" +"Less is more,\n" +"Keep it simple,\n" +"Small is beautiful,\n" +"\n" +"Be brave! Break things! Learn and have fun!\n" +"Express yourself with MicroPython.\n" +"\n" +"Happy hacking! :-)\n"; + mp_printf(&mp_plat_print, "%s", this_text); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(this___init___obj, this__init__); + +STATIC mp_obj_t this_authors(void) { + /* + If you contribute code to this project, add your name here. + */ + STATIC const char *authors_text = +"MicroPython on the micro:bit is brought to you by:\n" +"Damien P.George and Nicholas H.Tollervey\n"; + mp_printf(&mp_plat_print, "%s", authors_text); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(this_authors_obj, this_authors); + +STATIC const mp_map_elem_t this_module_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_this) }, + { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&this___init___obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_authors), (mp_obj_t)&this_authors_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(this_module_globals, this_module_globals_table); + +const mp_obj_module_t this_module = { + .base = { &mp_type_module }, + .name = MP_QSTR_this, + .globals = (mp_obj_dict_t*)&this_module_globals, +}; + +}