Skip to content

Commit ecc47d5

Browse files
committed
Create supervisor module.
It can control autoreload and the rgb status led.
1 parent 2d34450 commit ecc47d5

File tree

7 files changed

+39
-28
lines changed

7 files changed

+39
-28
lines changed

main.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,14 @@ int __attribute__((used)) main(void) {
290290
bool first_run = true;
291291
for (;;) {
292292
if (!skip_repl) {
293-
// The REPL mode can change, or it can request a reload.
294-
bool autoreload_on = autoreload_is_enabled();
295-
autoreload_disable();
293+
autoreload_suspend();
296294
new_status_color(REPL_RUNNING);
297295
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
298296
exit_code = pyexec_raw_repl();
299297
} else {
300298
exit_code = pyexec_friendly_repl();
301299
}
302-
if (autoreload_on) {
303-
autoreload_enable();
304-
}
300+
autoreload_resume();
305301
reset_port();
306302
reset_board();
307303
reset_mp();

ports/atmel-samd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ SRC_C = \
221221
tick.c \
222222
usb.c \
223223
usb_mass_storage.c \
224-
bindings/samd/__init__.c \
225224
boards/$(BOARD)/board.c \
226225
boards/$(BOARD)/pins.c \
227226
lib/oofatfs/ff.c \
@@ -314,6 +313,7 @@ SRC_BINDINGS_ENUMS = \
314313
digitalio/Pull.c \
315314
help.c \
316315
math/__init__.c \
316+
supervisor/__init__.c \
317317
util.c
318318

319319
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \

ports/atmel-samd/mpconfigport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ extern const struct _mp_obj_module_t cpy_nvm_module;
168168
extern const struct _mp_obj_module_t neopixel_write_module;
169169
extern const struct _mp_obj_module_t uheap_module;
170170
extern const struct _mp_obj_module_t ustack_module;
171-
extern const struct _mp_obj_module_t samd_module;
171+
extern const struct _mp_obj_module_t supervisor_module;
172172
extern const struct _mp_obj_module_t gamepad_module;
173173
extern const struct _mp_obj_module_t touchio_module;
174174
extern const struct _mp_obj_module_t usb_hid_module;
@@ -216,7 +216,6 @@ extern const struct _mp_obj_module_t usb_hid_module;
216216
// { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module },
217217
// { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
218218
// { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module },
219-
// { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module },
220219

221220

222221
#define MICROPY_PORT_BUILTIN_MODULES \
@@ -228,6 +227,7 @@ extern const struct _mp_obj_module_t usb_hid_module;
228227
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
229228
{ MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \
230229
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&struct_module }, \
230+
{ MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \
231231
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module }, \
232232
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
233233
EXTRA_BUILTIN_MODULES

shared-bindings/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Module / Port SAMD21 SAMD21 Express ESP8266
3232
`random` **Yes** **Yes** **Yes**
3333
`storage` **Yes** **Yes** **Yes**
3434
`struct` **Yes** **Yes** **Yes**
35+
`supervisor` **Yes** **Yes** No
3536
`time` **Yes** **Yes** **Yes**
3637
`touchio` **Yes** **Yes** No
3738
`uheap` Debug Debug Debug
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2016-2017 Scott Shawcroft for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -28,41 +28,41 @@
2828
#include "supervisor/shared/autoreload.h"
2929
#include "supervisor/shared/rgb_led_status.h"
3030

31-
//| :mod:`samd` --- SAMD implementation settings
31+
//| :mod:`supervisor` --- Supervisor settings
3232
//| =================================================
3333
//|
34-
//| .. module:: samd
35-
//| :synopsis: SAMD implementation settings
34+
//| .. module:: supervisor
35+
//| :synopsis: Supervisor settings
3636
//| :platform: SAMD21
3737
//|
3838

3939
//| .. method:: enable_autoreload()
4040
//|
4141
//| Enable autoreload based on USB file write activity.
4242
//|
43-
STATIC mp_obj_t samd_enable_autoreload(void) {
43+
STATIC mp_obj_t supervisor_enable_autoreload(void) {
4444
autoreload_enable();
4545
return mp_const_none;
4646
}
47-
MP_DEFINE_CONST_FUN_OBJ_0(samd_enable_autoreload_obj, samd_enable_autoreload);
47+
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_enable_autoreload_obj, supervisor_enable_autoreload);
4848

4949
//| .. method:: disable_autoreload()
5050
//|
5151
//| Disable autoreload based on USB file write activity until
5252
//| `enable_autoreload` is called.
5353
//|
54-
STATIC mp_obj_t samd_disable_autoreload(void) {
54+
STATIC mp_obj_t supervisor_disable_autoreload(void) {
5555
autoreload_disable();
5656
return mp_const_none;
5757
}
58-
MP_DEFINE_CONST_FUN_OBJ_0(samd_disable_autoreload_obj, samd_disable_autoreload);
58+
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_disable_autoreload_obj, supervisor_disable_autoreload);
5959

6060
//| .. method:: set_rgb_status_brightness()
6161
//|
6262
//| Set brightness of status neopixel from 0-255
6363
//| `set_rgb_status_brightness` is called.
6464
//|
65-
STATIC mp_obj_t samd_set_rgb_status_brightness(mp_obj_t lvl){
65+
STATIC mp_obj_t supervisor_set_rgb_status_brightness(mp_obj_t lvl){
6666
// This must be int. If cast to uint8_t first, will never raise a ValueError.
6767
int brightness_int = mp_obj_get_int(lvl);
6868
if(brightness_int < 0 || brightness_int > 255){
@@ -71,18 +71,18 @@ STATIC mp_obj_t samd_set_rgb_status_brightness(mp_obj_t lvl){
7171
set_rgb_status_brightness((uint8_t)brightness_int);
7272
return mp_const_none;
7373
}
74-
MP_DEFINE_CONST_FUN_OBJ_1(samd_set_rgb_status_brightness_obj, samd_set_rgb_status_brightness);
74+
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_set_rgb_status_brightness);
7575

76-
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
77-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
78-
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&samd_enable_autoreload_obj)},
79-
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&samd_disable_autoreload_obj)},
80-
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&samd_set_rgb_status_brightness_obj)},
76+
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
77+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
78+
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj)},
79+
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj)},
80+
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj)},
8181
};
8282

83-
STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table);
83+
STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table);
8484

85-
const mp_obj_module_t samd_module = {
85+
const mp_obj_module_t supervisor_module = {
8686
.base = { &mp_type_module },
87-
.globals = (mp_obj_dict_t*)&samd_module_globals,
87+
.globals = (mp_obj_dict_t*)&supervisor_module_globals,
8888
};

supervisor/shared/autoreload.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131

3232
volatile uint32_t autoreload_delay_ms = 0;
3333
bool autoreload_enabled = false;
34+
static bool autoreload_suspended = false;
3435
volatile bool reload_next_character = false;
3536

3637
inline void autoreload_tick() {
3738
if (autoreload_delay_ms == 0) {
3839
return;
3940
}
40-
if (autoreload_delay_ms == 1 && autoreload_enabled && !reload_next_character) {
41+
if (autoreload_delay_ms == 1 && autoreload_enabled &&
42+
!autoreload_suspended && !reload_next_character) {
4143
mp_keyboard_interrupt();
4244
reload_next_character = true;
4345
}
@@ -53,6 +55,14 @@ void autoreload_disable() {
5355
autoreload_enabled = false;
5456
}
5557

58+
void autoreload_suspend() {
59+
autoreload_suspended = true;
60+
}
61+
62+
void autoreload_resume() {
63+
autoreload_suspended = false;
64+
}
65+
5666
inline bool autoreload_is_enabled() {
5767
return autoreload_enabled;
5868
}

supervisor/shared/autoreload.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ void autoreload_enable(void);
3939
void autoreload_disable(void);
4040
bool autoreload_is_enabled(void);
4141

42+
// Temporarily turn it off. Used during the REPL.
43+
void autoreload_suspend(void);
44+
void autoreload_resume(void);
45+
4246
#endif // MICROPY_INCLUDED_SUPERVISOR_AUTORELOAD_H

0 commit comments

Comments
 (0)