From ecc47d5258acfd903d3aba03b336e248e85ff5c2 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 15 Nov 2017 14:44:14 -0800 Subject: Create supervisor module. It can control autoreload and the rgb status led. --- main.c | 8 +-- ports/atmel-samd/Makefile | 2 +- ports/atmel-samd/bindings/samd/__init__.c | 88 ------------------------------- ports/atmel-samd/mpconfigport.h | 4 +- shared-bindings/index.rst | 1 + shared-bindings/supervisor/__init__.c | 88 +++++++++++++++++++++++++++++++ supervisor/shared/autoreload.c | 12 ++++- supervisor/shared/autoreload.h | 4 ++ 8 files changed, 109 insertions(+), 98 deletions(-) delete mode 100644 ports/atmel-samd/bindings/samd/__init__.c create mode 100644 shared-bindings/supervisor/__init__.c diff --git a/main.c b/main.c index 5f73713cb..7ed93ddf5 100644 --- a/main.c +++ b/main.c @@ -290,18 +290,14 @@ int __attribute__((used)) main(void) { bool first_run = true; for (;;) { if (!skip_repl) { - // The REPL mode can change, or it can request a reload. - bool autoreload_on = autoreload_is_enabled(); - autoreload_disable(); + autoreload_suspend(); new_status_color(REPL_RUNNING); if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { exit_code = pyexec_raw_repl(); } else { exit_code = pyexec_friendly_repl(); } - if (autoreload_on) { - autoreload_enable(); - } + autoreload_resume(); reset_port(); reset_board(); reset_mp(); diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index ccf029a7f..916bc559f 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -221,7 +221,6 @@ SRC_C = \ tick.c \ usb.c \ usb_mass_storage.c \ - bindings/samd/__init__.c \ boards/$(BOARD)/board.c \ boards/$(BOARD)/pins.c \ lib/oofatfs/ff.c \ @@ -314,6 +313,7 @@ SRC_BINDINGS_ENUMS = \ digitalio/Pull.c \ help.c \ math/__init__.c \ + supervisor/__init__.c \ util.c SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ diff --git a/ports/atmel-samd/bindings/samd/__init__.c b/ports/atmel-samd/bindings/samd/__init__.c deleted file mode 100644 index d2bc0e0c0..000000000 --- a/ports/atmel-samd/bindings/samd/__init__.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries - * - * 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 "py/obj.h" - #include "py/runtime.h" - #include "supervisor/shared/autoreload.h" - #include "supervisor/shared/rgb_led_status.h" - -//| :mod:`samd` --- SAMD implementation settings -//| ================================================= -//| -//| .. module:: samd -//| :synopsis: SAMD implementation settings -//| :platform: SAMD21 -//| - -//| .. method:: enable_autoreload() -//| -//| Enable autoreload based on USB file write activity. -//| -STATIC mp_obj_t samd_enable_autoreload(void) { - autoreload_enable(); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_0(samd_enable_autoreload_obj, samd_enable_autoreload); - -//| .. method:: disable_autoreload() -//| -//| Disable autoreload based on USB file write activity until -//| `enable_autoreload` is called. -//| -STATIC mp_obj_t samd_disable_autoreload(void) { - autoreload_disable(); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_0(samd_disable_autoreload_obj, samd_disable_autoreload); - -//| .. method:: set_rgb_status_brightness() -//| -//| Set brightness of status neopixel from 0-255 -//| `set_rgb_status_brightness` is called. -//| -STATIC mp_obj_t samd_set_rgb_status_brightness(mp_obj_t lvl){ - // This must be int. If cast to uint8_t first, will never raise a ValueError. - int brightness_int = mp_obj_get_int(lvl); - if(brightness_int < 0 || brightness_int > 255){ - mp_raise_ValueError("Brightness must be between 0 and 255"); - } - set_rgb_status_brightness((uint8_t)brightness_int); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(samd_set_rgb_status_brightness_obj, samd_set_rgb_status_brightness); - -STATIC const mp_rom_map_elem_t samd_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&samd_enable_autoreload_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&samd_disable_autoreload_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&samd_set_rgb_status_brightness_obj)}, -}; - -STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table); - -const mp_obj_module_t samd_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&samd_module_globals, -}; diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 0728a643b..1db2d5a67 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -168,7 +168,7 @@ extern const struct _mp_obj_module_t cpy_nvm_module; extern const struct _mp_obj_module_t neopixel_write_module; extern const struct _mp_obj_module_t uheap_module; extern const struct _mp_obj_module_t ustack_module; -extern const struct _mp_obj_module_t samd_module; +extern const struct _mp_obj_module_t supervisor_module; extern const struct _mp_obj_module_t gamepad_module; extern const struct _mp_obj_module_t touchio_module; extern const struct _mp_obj_module_t usb_hid_module; @@ -216,7 +216,6 @@ extern const struct _mp_obj_module_t usb_hid_module; // { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, -// { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, #define MICROPY_PORT_BUILTIN_MODULES \ @@ -228,6 +227,7 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&struct_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, EXTRA_BUILTIN_MODULES diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index 229ae38d4..6dced5026 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -32,6 +32,7 @@ Module / Port SAMD21 SAMD21 Express ESP8266 `random` **Yes** **Yes** **Yes** `storage` **Yes** **Yes** **Yes** `struct` **Yes** **Yes** **Yes** +`supervisor` **Yes** **Yes** No `time` **Yes** **Yes** **Yes** `touchio` **Yes** **Yes** No `uheap` Debug Debug Debug diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c new file mode 100644 index 000000000..cbb138beb --- /dev/null +++ b/shared-bindings/supervisor/__init__.c @@ -0,0 +1,88 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016-2017 Scott Shawcroft for Adafruit Industries + * + * 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 "py/obj.h" + #include "py/runtime.h" + #include "supervisor/shared/autoreload.h" + #include "supervisor/shared/rgb_led_status.h" + +//| :mod:`supervisor` --- Supervisor settings +//| ================================================= +//| +//| .. module:: supervisor +//| :synopsis: Supervisor settings +//| :platform: SAMD21 +//| + +//| .. method:: enable_autoreload() +//| +//| Enable autoreload based on USB file write activity. +//| +STATIC mp_obj_t supervisor_enable_autoreload(void) { + autoreload_enable(); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(supervisor_enable_autoreload_obj, supervisor_enable_autoreload); + +//| .. method:: disable_autoreload() +//| +//| Disable autoreload based on USB file write activity until +//| `enable_autoreload` is called. +//| +STATIC mp_obj_t supervisor_disable_autoreload(void) { + autoreload_disable(); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(supervisor_disable_autoreload_obj, supervisor_disable_autoreload); + +//| .. method:: set_rgb_status_brightness() +//| +//| Set brightness of status neopixel from 0-255 +//| `set_rgb_status_brightness` is called. +//| +STATIC mp_obj_t supervisor_set_rgb_status_brightness(mp_obj_t lvl){ + // This must be int. If cast to uint8_t first, will never raise a ValueError. + int brightness_int = mp_obj_get_int(lvl); + if(brightness_int < 0 || brightness_int > 255){ + mp_raise_ValueError("Brightness must be between 0 and 255"); + } + set_rgb_status_brightness((uint8_t)brightness_int); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_set_rgb_status_brightness); + +STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table); + +const mp_obj_module_t supervisor_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&supervisor_module_globals, +}; diff --git a/supervisor/shared/autoreload.c b/supervisor/shared/autoreload.c index b1d0f05a9..39c33c05e 100644 --- a/supervisor/shared/autoreload.c +++ b/supervisor/shared/autoreload.c @@ -31,13 +31,15 @@ volatile uint32_t autoreload_delay_ms = 0; bool autoreload_enabled = false; +static bool autoreload_suspended = false; volatile bool reload_next_character = false; inline void autoreload_tick() { if (autoreload_delay_ms == 0) { return; } - if (autoreload_delay_ms == 1 && autoreload_enabled && !reload_next_character) { + if (autoreload_delay_ms == 1 && autoreload_enabled && + !autoreload_suspended && !reload_next_character) { mp_keyboard_interrupt(); reload_next_character = true; } @@ -53,6 +55,14 @@ void autoreload_disable() { autoreload_enabled = false; } +void autoreload_suspend() { + autoreload_suspended = true; +} + +void autoreload_resume() { + autoreload_suspended = false; +} + inline bool autoreload_is_enabled() { return autoreload_enabled; } diff --git a/supervisor/shared/autoreload.h b/supervisor/shared/autoreload.h index bc3c6d21d..62f9fe073 100644 --- a/supervisor/shared/autoreload.h +++ b/supervisor/shared/autoreload.h @@ -39,4 +39,8 @@ void autoreload_enable(void); void autoreload_disable(void); bool autoreload_is_enabled(void); +// Temporarily turn it off. Used during the REPL. +void autoreload_suspend(void); +void autoreload_resume(void); + #endif // MICROPY_INCLUDED_SUPERVISOR_AUTORELOAD_H -- cgit v1.2.3