Skip to content

Commit 1c2d2c9

Browse files
authored
Merge pull request #6980 from MicroDev1/supervisor
Implement certain supervisor functions as properties
2 parents 1fd09cb + 4fd15ac commit 1c2d2c9

File tree

8 files changed

+106
-52
lines changed

8 files changed

+106
-52
lines changed

shared-bindings/supervisor/Runtime.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#include "shared-bindings/supervisor/Runtime.h"
3535

3636
#include "supervisor/shared/reload.h"
37+
#include "supervisor/shared/stack.h"
38+
#include "supervisor/shared/status_leds.h"
39+
#include "supervisor/shared/bluetooth/bluetooth.h"
3740

3841
#if (CIRCUITPY_USB)
3942
#include "tusb.h"
@@ -134,12 +137,91 @@ MP_PROPERTY_GETSET(supervisor_runtime_autoreload_obj,
134137
(mp_obj_t)&supervisor_runtime_get_autoreload_obj,
135138
(mp_obj_t)&supervisor_runtime_set_autoreload_obj);
136139

140+
//| ble_workflow: bool
141+
//| """Enable/Disable ble workflow until a reset. This prevents BLE advertising outside of the VM and
142+
//| the services used for it."""
143+
//|
144+
STATIC mp_obj_t supervisor_runtime_get_ble_workflow(mp_obj_t self) {
145+
#if CIRCUITPY_BLE_FILE_SERVICE && CIRCUITPY_SERIAL_BLE
146+
return mp_obj_new_bool(supervisor_bluetooth_workflow_is_enabled());
147+
#else
148+
return mp_const_false;
149+
#endif
150+
}
151+
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_ble_workflow_obj, supervisor_runtime_get_ble_workflow);
152+
153+
STATIC mp_obj_t supervisor_runtime_set_ble_workflow(mp_obj_t self, mp_obj_t state_in) {
154+
#if CIRCUITPY_BLE_FILE_SERVICE && CIRCUITPY_SERIAL_BLE
155+
if (mp_obj_is_true(state_in)) {
156+
supervisor_bluetooth_enable_workflow();
157+
} else {
158+
supervisor_bluetooth_disable_workflow();
159+
}
160+
#else
161+
mp_raise_NotImplementedError(NULL);
162+
#endif
163+
return mp_const_none;
164+
}
165+
MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_ble_workflow_obj, supervisor_runtime_set_ble_workflow);
166+
167+
MP_PROPERTY_GETSET(supervisor_runtime_ble_workflow_obj,
168+
(mp_obj_t)&supervisor_runtime_get_ble_workflow_obj,
169+
(mp_obj_t)&supervisor_runtime_set_ble_workflow_obj);
170+
171+
//| next_stack_limit: int
172+
//| """The size of the stack for the next vm run. If its too large, the default will be used."""
173+
//|
174+
STATIC mp_obj_t supervisor_runtime_get_next_stack_limit(mp_obj_t self) {
175+
return mp_obj_new_int(get_next_stack_size());
176+
}
177+
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_next_stack_limit_obj, supervisor_runtime_get_next_stack_limit);
178+
179+
STATIC mp_obj_t supervisor_runtime_set_next_stack_limit(mp_obj_t self, mp_obj_t size_obj) {
180+
mp_int_t size = mp_obj_get_int(size_obj);
181+
mp_arg_validate_int_min(size, 256, MP_QSTR_size);
182+
set_next_stack_size(size);
183+
return mp_const_none;
184+
}
185+
MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_next_stack_limit_obj, supervisor_runtime_set_next_stack_limit);
186+
187+
MP_PROPERTY_GETSET(supervisor_runtime_next_stack_limit_obj,
188+
(mp_obj_t)&supervisor_runtime_get_next_stack_limit_obj,
189+
(mp_obj_t)&supervisor_runtime_set_next_stack_limit_obj);
190+
191+
//| rgb_status_brightness: int
192+
//| """Set brightness of status RGB LED from 0-255. This will take effect
193+
//| after the current code finishes and the status LED is used to show
194+
//| the finish state."""
195+
//|
196+
STATIC mp_obj_t supervisor_runtime_get_rgb_status_brightness(mp_obj_t self) {
197+
return MP_OBJ_NEW_SMALL_INT(get_status_brightness());
198+
}
199+
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_rgb_status_brightness_obj, supervisor_runtime_get_rgb_status_brightness);
200+
201+
STATIC mp_obj_t supervisor_runtime_set_rgb_status_brightness(mp_obj_t self, mp_obj_t lvl) {
202+
#if CIRCUITPY_STATUS_LED
203+
// This must be int. If cast to uint8_t first, will never raise a ValueError.
204+
set_status_brightness((uint8_t)mp_arg_validate_int_range(mp_obj_get_int(lvl), 0, 255, MP_QSTR_brightness));
205+
#else
206+
mp_raise_NotImplementedError(NULL);
207+
#endif
208+
return mp_const_none;
209+
}
210+
MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_rgb_status_brightness_obj, supervisor_runtime_set_rgb_status_brightness);
211+
212+
MP_PROPERTY_GETSET(supervisor_runtime_rgb_status_brightness_obj,
213+
(mp_obj_t)&supervisor_runtime_get_rgb_status_brightness_obj,
214+
(mp_obj_t)&supervisor_runtime_set_rgb_status_brightness_obj);
215+
137216
STATIC const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = {
138217
{ MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&supervisor_runtime_usb_connected_obj) },
139218
{ MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) },
140219
{ MP_ROM_QSTR(MP_QSTR_serial_bytes_available), MP_ROM_PTR(&supervisor_runtime_serial_bytes_available_obj) },
141220
{ MP_ROM_QSTR(MP_QSTR_run_reason), MP_ROM_PTR(&supervisor_runtime_run_reason_obj) },
142221
{ MP_ROM_QSTR(MP_QSTR_autoreload), MP_ROM_PTR(&supervisor_runtime_autoreload_obj) },
222+
{ MP_ROM_QSTR(MP_QSTR_ble_workflow), MP_ROM_PTR(&supervisor_runtime_ble_workflow_obj) },
223+
{ MP_ROM_QSTR(MP_QSTR_next_stack_limit), MP_ROM_PTR(&supervisor_runtime_next_stack_limit_obj) },
224+
{ MP_ROM_QSTR(MP_QSTR_rgb_status_brightness), MP_ROM_PTR(&supervisor_runtime_rgb_status_brightness_obj) },
143225
};
144226

145227
STATIC MP_DEFINE_CONST_DICT(supervisor_runtime_locals_dict, supervisor_runtime_locals_dict_table);

shared-bindings/supervisor/__init__.c

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
#include "py/objstr.h"
3131

3232
#include "shared/runtime/interrupt_char.h"
33-
#include "supervisor/shared/bluetooth/bluetooth.h"
3433
#include "supervisor/shared/display.h"
35-
#include "supervisor/shared/status_leds.h"
3634
#include "supervisor/shared/reload.h"
37-
#include "supervisor/shared/stack.h"
3835
#include "supervisor/shared/traceback.h"
3936
#include "supervisor/shared/translate/translate.h"
4037
#include "supervisor/shared/workflow.h"
@@ -63,21 +60,6 @@
6360
//| the last exception name and location, and firmware version information.
6461
//| This object is the sole instance of `supervisor.StatusBar`."""
6562

66-
//| def set_rgb_status_brightness(brightness: int) -> None:
67-
//| """Set brightness of status RGB LED from 0-255. This will take effect
68-
//| after the current code finishes and the status LED is used to show
69-
//| the finish state."""
70-
//| ...
71-
//|
72-
STATIC mp_obj_t supervisor_set_rgb_status_brightness(mp_obj_t lvl) {
73-
// This must be int. If cast to uint8_t first, will never raise a ValueError.
74-
int brightness_int = mp_obj_get_int(lvl);
75-
mp_arg_validate_int_range(brightness_int, 0, 255, MP_QSTR_brightness);
76-
set_status_brightness((uint8_t)brightness_int);
77-
return mp_const_none;
78-
}
79-
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_set_rgb_status_brightness);
80-
8163
//| def reload() -> None:
8264
//| """Reload the main Python code and run it (equivalent to hitting Ctrl-D at the REPL)."""
8365
//| ...
@@ -88,21 +70,6 @@ STATIC mp_obj_t supervisor_reload(void) {
8870
}
8971
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload);
9072

91-
//| def set_next_stack_limit(size: int) -> None:
92-
//| """Set the size of the stack for the next vm run. If its too large, the default will be used."""
93-
//| ...
94-
//|
95-
STATIC mp_obj_t supervisor_set_next_stack_limit(mp_obj_t size_obj) {
96-
mp_int_t size = mp_obj_get_int(size_obj);
97-
98-
mp_arg_validate_int_min(size, 256, MP_QSTR_size);
99-
100-
set_next_stack_size(size);
101-
102-
return mp_const_none;
103-
}
104-
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_next_stack_limit_obj, supervisor_set_next_stack_limit);
105-
10673
//| def set_next_code_file(
10774
//| filename: Optional[str],
10875
//| *,
@@ -278,21 +245,6 @@ STATIC mp_obj_t supervisor_get_previous_traceback(void) {
278245
}
279246
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_get_previous_traceback_obj, supervisor_get_previous_traceback);
280247

281-
//| def disable_ble_workflow() -> None:
282-
//| """Disable ble workflow until a reset. This prevents BLE advertising outside of the VM and
283-
//| the services used for it."""
284-
//| ...
285-
//|
286-
STATIC mp_obj_t supervisor_disable_ble_workflow(void) {
287-
#if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
288-
mp_raise_NotImplementedError(NULL);
289-
#else
290-
supervisor_bluetooth_disable_workflow();
291-
#endif
292-
return mp_const_none;
293-
}
294-
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_disable_ble_workflow_obj, supervisor_disable_ble_workflow);
295-
296248
//| def reset_terminal(x_pixels: int, y_pixels: int) -> None:
297249
//| """Reset the CircuitPython serial terminal with new dimensions."""
298250
//| ...
@@ -380,15 +332,12 @@ MP_DEFINE_CONST_FUN_OBJ_KW(supervisor_set_usb_identification_obj, 0, supervisor_
380332

381333
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
382334
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
383-
{ MP_ROM_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
384335
{ MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) },
385336
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },
386337
{ MP_ROM_QSTR(MP_QSTR_RunReason), MP_ROM_PTR(&supervisor_run_reason_type) },
387-
{ MP_ROM_QSTR(MP_QSTR_set_next_stack_limit), MP_ROM_PTR(&supervisor_set_next_stack_limit_obj) },
388338
{ MP_ROM_QSTR(MP_QSTR_set_next_code_file), MP_ROM_PTR(&supervisor_set_next_code_file_obj) },
389339
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&supervisor_ticks_ms_obj) },
390340
{ MP_ROM_QSTR(MP_QSTR_get_previous_traceback), MP_ROM_PTR(&supervisor_get_previous_traceback_obj) },
391-
{ MP_ROM_QSTR(MP_QSTR_disable_ble_workflow), MP_ROM_PTR(&supervisor_disable_ble_workflow_obj) },
392341
{ MP_ROM_QSTR(MP_QSTR_reset_terminal), MP_ROM_PTR(&supervisor_reset_terminal_obj) },
393342
{ MP_ROM_QSTR(MP_QSTR_set_usb_identification), MP_ROM_PTR(&supervisor_set_usb_identification_obj) },
394343
{ MP_ROM_QSTR(MP_QSTR_status_bar), MP_ROM_PTR(&shared_module_supervisor_status_bar_obj) },

supervisor/shared/bluetooth/bluetooth.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ void supervisor_bluetooth_enable_workflow(void) {
354354
if (workflow_state == WORKFLOW_DISABLED) {
355355
return;
356356
}
357-
358357
workflow_state = WORKFLOW_ENABLED;
359358
#endif
360359
}
@@ -364,3 +363,12 @@ void supervisor_bluetooth_disable_workflow(void) {
364363
workflow_state = WORKFLOW_DISABLED;
365364
#endif
366365
}
366+
367+
bool supervisor_bluetooth_workflow_is_enabled(void) {
368+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
369+
if (workflow_state == 1) {
370+
return true;
371+
}
372+
#endif
373+
return false;
374+
}

supervisor/shared/bluetooth/bluetooth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void supervisor_stop_bluetooth(void);
3737
// Enable only works if it hasn't been set yet.
3838
void supervisor_bluetooth_enable_workflow(void);
3939
void supervisor_bluetooth_disable_workflow(void);
40+
bool supervisor_bluetooth_workflow_is_enabled(void);
4041

4142
// Title bar status
4243
bool supervisor_bluetooth_status_dirty(void);

supervisor/shared/stack.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ void set_next_stack_size(uint32_t size) {
107107
next_stack_size = size;
108108
}
109109

110+
uint32_t get_next_stack_size(void) {
111+
return next_stack_size;
112+
}
113+
110114
uint32_t get_current_stack_size(void) {
111115
return current_stack_size;
112116
}

supervisor/shared/stack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ uint32_t *stack_get_bottom(void);
3838
size_t stack_get_length(void);
3939
// Next/current requested stack size.
4040
void set_next_stack_size(uint32_t size);
41+
uint32_t get_next_stack_size(void);
4142
uint32_t get_current_stack_size(void);
4243
bool stack_ok(void);
4344

supervisor/shared/status_leds.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ void set_status_brightness(uint8_t level) {
330330
#endif
331331
}
332332

333+
uint8_t get_status_brightness(void) {
334+
#if CIRCUITPY_STATUS_LED
335+
return rgb_status_brightness;
336+
#else
337+
return 0;
338+
#endif
339+
}
340+
333341
void init_rxtx_leds(void) {
334342
#if CIRCUITPY_DIGITALIO && defined(MICROPY_HW_LED_RX)
335343
common_hal_digitalio_digitalinout_construct(&rx_led, MICROPY_HW_LED_RX);

supervisor/shared/status_leds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void new_status_color(uint32_t rgb);
5353

5454
uint32_t color_brightness(uint32_t color, uint8_t brightness);
5555
void set_status_brightness(uint8_t level);
56+
uint8_t get_status_brightness(void);
5657

5758
void init_rxtx_leds(void);
5859
void deinit_rxtx_leds(void);

0 commit comments

Comments
 (0)