Skip to content

Commit 8889ac1

Browse files
committed
Add supervisor.set_next_code() function (prototype).
Part of #1084.
1 parent 97f5d21 commit 8889ac1

File tree

6 files changed

+182
-14
lines changed

6 files changed

+182
-14
lines changed

locale/circuitpython.pot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ msgstr ""
4747
msgid " is of type %q\n"
4848
msgstr ""
4949

50+
#: main.c
51+
msgid " not found.\n"
52+
msgstr ""
53+
5054
#: main.c
5155
msgid " output:\n"
5256
msgstr ""
@@ -2213,7 +2217,7 @@ msgstr ""
22132217
msgid "argsort is not implemented for flattened arrays"
22142218
msgstr ""
22152219

2216-
#: py/runtime.c
2220+
#: py/runtime.c shared-bindings/supervisor/__init__.c
22172221
msgid "argument has wrong type"
22182222
msgstr ""
22192223

main.c

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
272272
result.exception_type = NULL;
273273
result.exception_line = 0;
274274

275+
bool skip_repl;
275276
bool found_main = false;
277+
uint8_t next_code_options = 0;
278+
// Collects stickiness bits that apply in the current situation.
279+
uint8_t next_code_stickiness_situation = SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
276280

277281
if (safe_mode == NO_SAFE_MODE) {
278282
new_status_color(MAIN_RUNNING);
@@ -290,22 +294,62 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
290294
supervisor_allocation* heap = allocate_remaining_memory();
291295
start_mp(heap);
292296

293-
found_main = maybe_run_list(supported_filenames, &result);
294-
#if CIRCUITPY_FULL_BUILD
295-
if (!found_main){
296-
found_main = maybe_run_list(double_extension_filenames, &result);
297-
if (found_main) {
298-
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
297+
if (next_code_allocation) {
298+
((next_code_info_t*)next_code_allocation->ptr)->options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
299+
next_code_options = ((next_code_info_t*)next_code_allocation->ptr)->options;
300+
if (((next_code_info_t*)next_code_allocation->ptr)->filename[0] != '\0') {
301+
const char* next_list[] = {((next_code_info_t*)next_code_allocation->ptr)->filename, ""};
302+
found_main = maybe_run_list(next_list, &result);
303+
if (!found_main) {
304+
serial_write(((next_code_info_t*)next_code_allocation->ptr)->filename);
305+
serial_write_compressed(translate(" not found.\n"));
306+
}
299307
}
300308
}
301-
#endif
309+
if (!found_main) {
310+
found_main = maybe_run_list(supported_filenames, &result);
311+
#if CIRCUITPY_FULL_BUILD
312+
if (!found_main){
313+
found_main = maybe_run_list(double_extension_filenames, &result);
314+
if (found_main) {
315+
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
316+
}
317+
}
318+
#endif
319+
}
302320

303321
// TODO: on deep sleep, make sure display is refreshed before sleeping (for e-ink).
304322

305323
cleanup_after_vm(heap);
306324

325+
// If a new next code file was set, that is a reason to keep it (obviously). Stuff this into
326+
// the options because it can be treated like any other reason-for-stickiness bit. The
327+
// source is different though: it comes from the options that will apply to the next run,
328+
// while the rest of next_code_options is what applied to this run.
329+
if (next_code_allocation != NULL && (((next_code_info_t*)next_code_allocation->ptr)->options & SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET)) {
330+
next_code_options |= SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
331+
}
332+
333+
if (reload_requested) {
334+
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
335+
}
336+
else if (result.return_code == 0) { //TODO mask out PYEXEC_DEEP_SLEEP?
337+
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS;
338+
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS) {
339+
skip_repl = true;
340+
goto done;
341+
}
342+
}
343+
else {
344+
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_ERROR;
345+
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_ERROR) {
346+
skip_repl = true;
347+
goto done;
348+
}
349+
}
307350
if (result.return_code & PYEXEC_FORCED_EXIT) {
308-
return reload_requested;
351+
skip_repl = reload_requested;
352+
goto done;
309353
}
310354

311355
if (reload_requested && result.return_code == PYEXEC_EXCEPTION) {
@@ -333,9 +377,16 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
333377
board_init();
334378
}
335379
#endif
380+
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
381+
// Should the STICKY_ON_SUCCESS and STICKY_ON_ERROR bits be cleared in
382+
// next_code_stickiness_situation? I can see arguments either way, but I'm deciding
383+
// "no" for now, mainly because it's a bit less code. At this point, we have both a
384+
// success or error and a reload, so let's have both of the respective options take
385+
// effect (in OR combination).
336386
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
337387
reload_requested = false;
338-
return true;
388+
skip_repl = true;
389+
goto done;
339390
}
340391

341392
if (serial_connected() && serial_bytes_available()) {
@@ -345,11 +396,11 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
345396
}
346397
#endif
347398
// Skip REPL if reload was requested.
348-
bool ctrl_d = serial_read() == CHAR_CTRL_D;
349-
if (ctrl_d) {
399+
skip_repl = serial_read() == CHAR_CTRL_D;
400+
if (skip_repl) {
350401
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
351402
}
352-
return ctrl_d;
403+
goto done;
353404
}
354405

355406
// Check for a deep sleep alarm and restart the VM. This can happen if
@@ -428,6 +479,13 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
428479
port_idle_until_interrupt();
429480
}
430481
}
482+
483+
done:
484+
if ((next_code_options & next_code_stickiness_situation) == 0) {
485+
free_memory(next_code_allocation);
486+
next_code_allocation = NULL;
487+
}
488+
return skip_repl;
431489
}
432490

433491
FIL* boot_output_file;

shared-bindings/supervisor/__init__.c

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26+
#include <string.h>
27+
2628
#include "py/obj.h"
2729
#include "py/runtime.h"
2830
#include "py/reload.h"
31+
#include "py/objstr.h"
2932

3033
#include "lib/utils/interrupt_char.h"
3134
#include "supervisor/shared/autoreload.h"
@@ -112,6 +115,87 @@ STATIC mp_obj_t supervisor_set_next_stack_limit(mp_obj_t size_obj) {
112115
}
113116
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_next_stack_limit_obj, supervisor_set_next_stack_limit);
114117

118+
//| def set_next_code_file(filename: Optional[str], *, reload_on_success : bool = False, reload_on_error: bool = False, sticky_on_success: bool = False, sticky_on_error: bool = False, sticky_on_reload: bool = False) -> None:
119+
//| """Set what file to run on the next vm run.
120+
//|
121+
//| When not ``None``, the given ``filename`` is inserted at the front of the usual ['code.py',
122+
//| 'main.py'] search sequence.
123+
//|
124+
//| The optional keyword arguments specify what happens after the specified file has run:
125+
//|
126+
//| ``sticky_on_…`` determine whether the newly set filename and options stay in effect: If
127+
//| True, further runs will continue to run that file (unless it says otherwise by calling
128+
//| ``set_next_code_filename()`` itself). If False, the settings will only affect one run and
129+
//| revert to the standard code.py/main.py afterwards.
130+
//|
131+
//| ``reload_on_…`` determine how to continue: If False, wait in the usual "Code done running.
132+
//| Waiting for reload. / Press any key to enter the REPL. Use CTRL-D to reload." state. If
133+
//| True, reload immediately as if CTRL-D was pressed.
134+
//|
135+
//| ``…_on_success`` take effect when the program runs to completion or calls ``sys.exit()``.
136+
//|
137+
//| ``…_on_error`` take effect when the program exits with an exception, including the
138+
//| KeyboardInterrupt caused by CTRL-C.
139+
//|
140+
//| ``…_on_reload`` take effect when the program is interrupted by files being written to the USB
141+
//| drive (auto-reload) or when it calls ``supervisor.reload()``.
142+
//|
143+
//| These settings are stored in RAM, not in persistent memory, and will therefore only affect
144+
//| soft reloads. Powering off or resetting the device will always revert to standard settings.
145+
//|
146+
//| When called multiple times in the same run, only the last call takes effect, replacing any
147+
//| settings made by previous ones. This is the main use of passing ``None`` as a filename: to
148+
//| reset to the standard search sequence."""
149+
//| ...
150+
//|
151+
STATIC mp_obj_t supervisor_set_next_code_file(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
152+
static const mp_arg_t allowed_args[] = {
153+
{ MP_QSTR_filename, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
154+
{ MP_QSTR_reload_on_success, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
155+
{ MP_QSTR_reload_on_error, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
156+
{ MP_QSTR_sticky_on_success, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
157+
{ MP_QSTR_sticky_on_error, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
158+
{ MP_QSTR_sticky_on_reload, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
159+
};
160+
struct {
161+
mp_arg_val_t filename;
162+
mp_arg_val_t reload_on_success;
163+
mp_arg_val_t reload_on_error;
164+
mp_arg_val_t sticky_on_success;
165+
mp_arg_val_t sticky_on_error;
166+
mp_arg_val_t sticky_on_reload;
167+
} args;
168+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
169+
if (!MP_OBJ_IS_STR_OR_BYTES(args.filename.u_obj) && args.filename.u_obj != mp_const_none) {
170+
mp_raise_TypeError(translate("argument has wrong type"));
171+
}
172+
if (args.filename.u_obj == mp_const_none) args.filename.u_obj = mp_const_empty_bytes;
173+
uint8_t options = 0;
174+
if (args.reload_on_success.u_bool) options |= SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS;
175+
if (args.reload_on_error.u_bool) options |= SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_ERROR;
176+
if (args.sticky_on_success.u_bool) options |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS;
177+
if (args.sticky_on_error.u_bool) options |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_ERROR;
178+
if (args.sticky_on_reload.u_bool) options |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
179+
size_t len;
180+
const char* filename = mp_obj_str_get_data(args.filename.u_obj, &len);
181+
free_memory(next_code_allocation);
182+
if (options != 0 || len != 0) {
183+
next_code_allocation = allocate_memory(align32_size(sizeof(next_code_info_t) + len + 1), false, true);
184+
if (next_code_allocation == NULL) {
185+
m_malloc_fail(sizeof(next_code_info_t) + len + 1);
186+
}
187+
next_code_info_t* next_code = (next_code_info_t*)next_code_allocation->ptr;
188+
next_code->options = options | SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
189+
memcpy(&next_code->filename, filename, len);
190+
next_code->filename[len] = '\0';
191+
}
192+
else {
193+
next_code_allocation = NULL;
194+
}
195+
return mp_const_none;
196+
}
197+
MP_DEFINE_CONST_FUN_OBJ_KW(supervisor_set_next_code_file_obj, 0, supervisor_set_next_code_file);
198+
115199
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
116200
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
117201
{ MP_ROM_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
@@ -121,7 +205,7 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
121205
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },
122206
{ MP_ROM_QSTR(MP_QSTR_RunReason), MP_ROM_PTR(&supervisor_run_reason_type) },
123207
{ MP_ROM_QSTR(MP_QSTR_set_next_stack_limit), MP_ROM_PTR(&supervisor_set_next_stack_limit_obj) },
124-
208+
{ MP_ROM_QSTR(MP_QSTR_set_next_code_file), MP_ROM_PTR(&supervisor_set_next_code_file_obj) },
125209
};
126210

127211
STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table);

supervisor/shared/autoreload.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "py/reload.h"
3131
#include "supervisor/shared/tick.h"
3232

33+
supervisor_allocation* next_code_allocation;
34+
3335
static volatile uint32_t autoreload_delay_ms = 0;
3436
static bool autoreload_enabled = false;
3537
static bool autoreload_suspended = false;

supervisor/shared/autoreload.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929

3030
#include <stdbool.h>
3131

32+
#include "supervisor/memory.h"
33+
34+
enum {
35+
SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS = 0x1,
36+
SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_ERROR = 0x2,
37+
SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS = 0x4,
38+
SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_ERROR = 0x8,
39+
SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD = 0x10,
40+
SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET = 0x20,
41+
};
42+
43+
typedef struct {
44+
uint8_t options;
45+
char filename[];
46+
} next_code_info_t;
47+
48+
extern supervisor_allocation* next_code_allocation;
49+
3250
extern volatile bool reload_requested;
3351

3452
void autoreload_tick(void);

supervisor/shared/memory.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ enum {
3636
CIRCUITPY_SUPERVISOR_IMMOVABLE_ALLOC_COUNT =
3737
// stack + heap
3838
2
39+
// next_code_allocation
40+
+ 1
3941
#ifdef EXTERNAL_FLASH_DEVICES
4042
+ 1
4143
#endif

0 commit comments

Comments
 (0)