Skip to content

Commit 5e01500

Browse files
committed
Add supervisor.runtime.autoreload
This replaces supervisor.enable_autoreload() and supervisor.disable_autoreload(). It also allows user code to get the current autoreload state. Replaces #5352 and part of #5414
1 parent e7d72b1 commit 5e01500

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

shared-bindings/supervisor/Runtime.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "shared-bindings/supervisor/RunReason.h"
3434
#include "shared-bindings/supervisor/Runtime.h"
3535

36+
#include "supervisor/shared/reload.h"
37+
3638
#if (CIRCUITPY_USB)
3739
#include "tusb.h"
3840
#endif
@@ -105,7 +107,7 @@ void supervisor_set_run_reason(supervisor_run_reason_t run_reason) {
105107
}
106108

107109
//| run_reason: RunReason
108-
//| """Returns why CircuitPython started running this particular time."""
110+
//| """Why CircuitPython started running this particular time."""
109111
//|
110112
STATIC mp_obj_t supervisor_runtime_get_run_reason(mp_obj_t self) {
111113
return cp_enum_find(&supervisor_run_reason_type, _run_reason);
@@ -115,11 +117,34 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_run_reason_obj, supervisor_runt
115117
MP_PROPERTY_GETTER(supervisor_runtime_run_reason_obj,
116118
(mp_obj_t)&supervisor_runtime_get_run_reason_obj);
117119

120+
//| autoreload: bool
121+
//| """Whether CircuitPython may autoreload based on workflow writes to the filesystem."""
122+
//|
123+
STATIC mp_obj_t supervisor_runtime_get_autoreload(mp_obj_t self) {
124+
return mp_obj_new_bool(autoreload_is_enabled());
125+
}
126+
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_autoreload_obj, supervisor_runtime_get_autoreload);
127+
128+
STATIC mp_obj_t supervisor_runtime_set_autoreload(mp_obj_t self, mp_obj_t state_in) {
129+
if (mp_obj_is_true(state_in)) {
130+
autoreload_enable();
131+
} else {
132+
autoreload_disable();
133+
}
134+
return mp_const_none;
135+
}
136+
MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_autoreload_obj, supervisor_runtime_set_autoreload);
137+
138+
MP_PROPERTY_GETSET(supervisor_runtime_autoreload_obj,
139+
(mp_obj_t)&supervisor_runtime_get_autoreload_obj,
140+
(mp_obj_t)&supervisor_runtime_set_autoreload_obj);
141+
118142
STATIC const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = {
119143
{ MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&supervisor_runtime_usb_connected_obj) },
120144
{ MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) },
121145
{ MP_ROM_QSTR(MP_QSTR_serial_bytes_available), MP_ROM_PTR(&supervisor_runtime_serial_bytes_available_obj) },
122146
{ MP_ROM_QSTR(MP_QSTR_run_reason), MP_ROM_PTR(&supervisor_runtime_run_reason_obj) },
147+
{ MP_ROM_QSTR(MP_QSTR_autoreload), MP_ROM_PTR(&supervisor_runtime_autoreload_obj) },
123148
};
124149

125150
STATIC MP_DEFINE_CONST_DICT(supervisor_runtime_locals_dict, supervisor_runtime_locals_dict_table);

shared-bindings/supervisor/__init__.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,6 @@
5353
//| This object is the sole instance of `supervisor.Runtime`."""
5454
//|
5555

56-
//| def enable_autoreload() -> None:
57-
//| """Enable autoreload based on USB file write activity."""
58-
//| ...
59-
//|
60-
STATIC mp_obj_t supervisor_enable_autoreload(void) {
61-
autoreload_enable();
62-
return mp_const_none;
63-
}
64-
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_enable_autoreload_obj, supervisor_enable_autoreload);
65-
66-
//| def disable_autoreload() -> None:
67-
//| """Disable autoreload based on USB file write activity until
68-
//| `enable_autoreload` is called."""
69-
//| ...
70-
//|
71-
STATIC mp_obj_t supervisor_disable_autoreload(void) {
72-
autoreload_disable();
73-
return mp_const_none;
74-
}
75-
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_disable_autoreload_obj, supervisor_disable_autoreload);
76-
7756
//| def set_rgb_status_brightness(brightness: int) -> None:
7857
//| """Set brightness of status RGB LED from 0-255. This will take effect
7958
//| after the current code finishes and the status LED is used to show
@@ -312,8 +291,6 @@ MP_DEFINE_CONST_FUN_OBJ_2(supervisor_reset_terminal_obj, supervisor_reset_termin
312291

313292
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
314293
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
315-
{ MP_ROM_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
316-
{ MP_ROM_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) },
317294
{ MP_ROM_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
318295
{ MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) },
319296
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },

0 commit comments

Comments
 (0)