Skip to content

Commit 577d53d

Browse files
committed
Add execution status into title bar
1 parent e0cb8ef commit 577d53d

File tree

4 files changed

+74
-37
lines changed

4 files changed

+74
-37
lines changed

main.c

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,32 @@ STATIC void stop_mp(void) {
204204
gc_deinit();
205205
}
206206

207+
STATIC const char *_last_executing_filename = NULL;
208+
STATIC const char *_current_executing_filename = NULL;
209+
210+
STATIC pyexec_result_t _exec_result = {0, MP_OBJ_NULL, 0};
211+
STATIC int _last_return_code = 0;
212+
STATIC int _last_exception_line = 0;
213+
214+
bool supervisor_execution_status_dirty(void) {
215+
return _last_executing_filename != _current_executing_filename ||
216+
_last_return_code != _exec_result.return_code ||
217+
_last_exception_line != _exec_result.exception_line;
218+
}
219+
220+
void supervisor_execution_status(void) {
221+
mp_obj_exception_t *exception = MP_OBJ_TO_PTR(_exec_result.exception);
222+
if ((_exec_result.return_code & PYEXEC_EXCEPTION) != 0 &&
223+
exception != NULL) {
224+
mp_printf(&mp_plat_print, "@%d %q", _exec_result.exception_line, exception->base.type->name);
225+
} else if (_current_executing_filename != NULL) {
226+
serial_write(_current_executing_filename);
227+
}
228+
_last_executing_filename = _current_executing_filename;
229+
_last_return_code = _exec_result.return_code;
230+
_last_exception_line = _exec_result.exception_line;
231+
}
232+
207233
#define STRING_LIST(...) {__VA_ARGS__, ""}
208234

209235
// Look for the first file that exists in the list of filenames, using mp_import_stat().
@@ -218,17 +244,23 @@ STATIC const char *first_existing_file_in_list(const char *const *filenames) {
218244
return NULL;
219245
}
220246

221-
STATIC bool maybe_run_list(const char *const *filenames, pyexec_result_t *exec_result) {
222-
const char *filename = first_existing_file_in_list(filenames);
223-
if (filename == NULL) {
247+
STATIC bool maybe_run_list(const char *const *filenames) {
248+
_exec_result.return_code = 0;
249+
_exec_result.exception = MP_OBJ_NULL;
250+
_exec_result.exception_line = 0;
251+
_current_executing_filename = first_existing_file_in_list(filenames);
252+
if (_current_executing_filename == NULL) {
224253
return false;
225254
}
226-
mp_hal_stdout_tx_str(filename);
255+
mp_hal_stdout_tx_str(_current_executing_filename);
227256
serial_write_compressed(translate(" output:\n"));
228-
pyexec_file(filename, exec_result);
257+
supervisor_title_bar_request_update(false);
258+
pyexec_file(_current_executing_filename, &_exec_result);
229259
#if CIRCUITPY_ATEXIT
230-
shared_module_atexit_execute(exec_result);
260+
shared_module_atexit_execute(&_exec_result);
231261
#endif
262+
_current_executing_filename = "Done";
263+
supervisor_title_bar_request_update(false);
232264
return true;
233265
}
234266

@@ -347,12 +379,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
347379
}
348380
#endif
349381

350-
pyexec_result_t result;
351-
352-
result.return_code = 0;
353-
result.exception = MP_OBJ_NULL;
354-
result.exception_line = 0;
355-
356382
bool skip_repl = false;
357383
bool skip_wait = false;
358384
bool found_main = false;
@@ -391,7 +417,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
391417
if (((next_code_info_t *)next_code_allocation->ptr)->filename[0] != '\0') {
392418
const char *next_list[] = {((next_code_info_t *)next_code_allocation->ptr)->filename, ""};
393419
// This is where the user's python code is actually executed:
394-
found_main = maybe_run_list(next_list, &result);
420+
found_main = maybe_run_list(next_list);
395421
if (!found_main) {
396422
serial_write(((next_code_info_t *)next_code_allocation->ptr)->filename);
397423
serial_write_compressed(translate(" not found.\n"));
@@ -401,11 +427,11 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
401427
// Otherwise, default to the standard list of filenames
402428
if (!found_main) {
403429
// This is where the user's python code is actually executed:
404-
found_main = maybe_run_list(supported_filenames, &result);
430+
found_main = maybe_run_list(supported_filenames);
405431
// If that didn't work, double check the extensions
406432
#if CIRCUITPY_FULL_BUILD
407433
if (!found_main) {
408-
found_main = maybe_run_list(double_extension_filenames, &result);
434+
found_main = maybe_run_list(double_extension_filenames);
409435
if (found_main) {
410436
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
411437
}
@@ -417,15 +443,15 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
417443

418444
// Print done before resetting everything so that we get the message over
419445
// BLE before it is reset and we have a delay before reconnect.
420-
if ((result.return_code & PYEXEC_RELOAD) && supervisor_get_run_reason() == RUN_REASON_AUTO_RELOAD) {
446+
if ((_exec_result.return_code & PYEXEC_RELOAD) && supervisor_get_run_reason() == RUN_REASON_AUTO_RELOAD) {
421447
serial_write_compressed(translate("\nCode stopped by auto-reload. Reloading soon.\n"));
422448
} else {
423449
serial_write_compressed(translate("\nCode done running.\n"));
424450
}
425451

426452

427453
// Finished executing python code. Cleanup includes filesystem flush and a board reset.
428-
cleanup_after_vm(heap, result.exception);
454+
cleanup_after_vm(heap, _exec_result.exception);
429455

430456
// If a new next code file was set, that is a reason to keep it (obviously). Stuff this into
431457
// the options because it can be treated like any other reason-for-stickiness bit. The
@@ -436,15 +462,15 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
436462
next_code_options |= SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
437463
}
438464

439-
if (result.return_code & PYEXEC_RELOAD) {
465+
if (_exec_result.return_code & PYEXEC_RELOAD) {
440466
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
441467
// Reload immediately unless the reload is due to autoreload. In that
442468
// case, we wait below to see if any other writes occur.
443469
if (supervisor_get_run_reason() != RUN_REASON_AUTO_RELOAD) {
444470
skip_repl = true;
445471
skip_wait = true;
446472
}
447-
} else if (result.return_code == 0) {
473+
} else if (_exec_result.return_code == 0) {
448474
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS;
449475
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS) {
450476
skip_repl = true;
@@ -455,12 +481,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
455481
// Deep sleep cannot be skipped
456482
// TODO: settings in deep sleep should persist, using a new sleep memory API
457483
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_ERROR
458-
&& !(result.return_code & PYEXEC_DEEP_SLEEP)) {
484+
&& !(_exec_result.return_code & PYEXEC_DEEP_SLEEP)) {
459485
skip_repl = true;
460486
skip_wait = true;
461487
}
462488
}
463-
if (result.return_code & PYEXEC_FORCED_EXIT) {
489+
if (_exec_result.return_code & PYEXEC_FORCED_EXIT) {
464490
skip_repl = false;
465491
skip_wait = true;
466492
}
@@ -478,12 +504,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
478504
uint8_t blink_count;
479505
bool led_active = false;
480506
#if CIRCUITPY_ALARM
481-
if (result.return_code & PYEXEC_DEEP_SLEEP) {
507+
if (_exec_result.return_code & PYEXEC_DEEP_SLEEP) {
482508
color = BLACK;
483509
blink_count = 0;
484510
} else
485511
#endif
486-
if (result.return_code != PYEXEC_EXCEPTION) {
512+
if (_exec_result.return_code != PYEXEC_EXCEPTION) {
487513
if (safe_mode == NO_SAFE_MODE) {
488514
color = ALL_DONE;
489515
blink_count = ALL_DONE_BLINKS;
@@ -568,7 +594,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
568594

569595
// Sleep until our next interrupt.
570596
#if CIRCUITPY_ALARM
571-
if (result.return_code & PYEXEC_DEEP_SLEEP) {
597+
if (_exec_result.return_code & PYEXEC_DEEP_SLEEP) {
572598
const bool awoke_from_true_deep_sleep =
573599
common_hal_mcu_processor_get_reset_reason() == RESET_REASON_DEEP_SLEEP_ALARM;
574600

@@ -730,8 +756,6 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
730756
usb_set_defaults();
731757
#endif
732758

733-
pyexec_result_t result = {0, MP_OBJ_NULL, 0};
734-
735759
if (ok_to_run) {
736760
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
737761
vstr_t boot_text;
@@ -742,7 +766,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
742766
// Write version info
743767
mp_printf(&mp_plat_print, "%s\nBoard ID:%s\n", MICROPY_FULL_VERSION_INFO, CIRCUITPY_BOARD_ID);
744768

745-
bool found_boot = maybe_run_list(boot_py_filenames, &result);
769+
bool found_boot = maybe_run_list(boot_py_filenames);
746770
(void)found_boot;
747771

748772

@@ -792,7 +816,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
792816
usb_get_boot_py_data(usb_boot_py_data, size);
793817
#endif
794818

795-
cleanup_after_vm(heap, result.exception);
819+
cleanup_after_vm(heap, _exec_result.exception);
796820

797821
#if CIRCUITPY_USB
798822
// Now give back the data we saved from the heap going away.

supervisor/shared/title_bar.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ static void title_bar_background(void *data) {
5252
dirty = dirty || supervisor_web_workflow_status_dirty();
5353
#endif
5454

55+
dirty = dirty || supervisor_execution_status_dirty();
56+
5557
if (!dirty) {
5658
return;
5759
}
@@ -64,8 +66,10 @@ static void title_bar_background(void *data) {
6466
serial_write("🐍 ");
6567
#if CIRCUITPY_WEB_WORKFLOW
6668
supervisor_web_workflow_status();
69+
serial_write(" | ");
6770
#endif
68-
serial_write("|");
71+
supervisor_execution_status();
72+
serial_write(" | ");
6973
serial_write(MICROPY_GIT_TAG);
7074
// Send string terminator
7175
serial_write("\x1b" "\\");

supervisor/shared/title_bar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ void supervisor_title_bar_start(void);
3232
void supervisor_title_bar_suspend(void);
3333
void supervisor_title_bar_resume(void);
3434
void supervisor_title_bar_request_update(bool force_dirty);
35+
36+
// Provided by main.c
37+
bool supervisor_execution_status_dirty(void);
38+
void supervisor_execution_status(void);

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,20 @@ bool supervisor_web_workflow_status_dirty(void) {
195195
}
196196

197197
void supervisor_web_workflow_status(void) {
198-
serial_write_compressed(translate("Wi-Fi: "));
199198
_last_enabled = common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj);
200199
if (_last_enabled) {
201200
uint32_t ipv4_address = wifi_radio_get_ipv4_address(&common_hal_wifi_radio_obj);
201+
if (ipv4_address != 0) {
202+
_update_encoded_ip();
203+
_last_ip = _encoded_ip;
204+
mp_printf(&mp_plat_print, "%s", _our_ip_encoded);
205+
if (web_api_port != 80) {
206+
mp_printf(&mp_plat_print, ":%d", web_api_port);
207+
}
208+
// TODO: Use these unicode to show signal strength: ▂▄▆█
209+
return;
210+
}
211+
serial_write_compressed(translate("Wi-Fi: "));
202212
_last_wifi_status = _wifi_status;
203213
if (_wifi_status == WIFI_RADIO_ERROR_AUTH_EXPIRE ||
204214
_wifi_status == WIFI_RADIO_ERROR_AUTH_FAIL) {
@@ -209,15 +219,10 @@ void supervisor_web_workflow_status(void) {
209219
_last_ip = 0;
210220
serial_write_compressed(translate("No IP"));
211221
} else {
212-
_update_encoded_ip();
213-
_last_ip = _encoded_ip;
214-
mp_printf(&mp_plat_print, "%s", _our_ip_encoded);
215-
if (web_api_port != 80) {
216-
mp_printf(&mp_plat_print, ":%d", web_api_port);
217-
}
218-
// TODO: Use these unicode to show signal strength: ▂▄▆█
219222
}
220223
} else {
224+
// Keep Wi-Fi print separate so its data can be matched with the one above.
225+
serial_write_compressed(translate("Wi-Fi: "));
221226
serial_write_compressed(translate("off"));
222227
}
223228
}

0 commit comments

Comments
 (0)