From 0cd37376a09860976f55ef59e40d073f4f6a2593 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 27 Sep 2022 15:14:40 -0400 Subject: [PATCH] finish status bar internal simplification --- shared-module/supervisor/StatusBar.c | 15 +++++---- shared-module/supervisor/StatusBar.h | 7 ++-- shared-module/terminalio/Terminal.c | 10 ------ supervisor/serial.h | 4 +++ supervisor/shared/serial.c | 50 +++++++--------------------- supervisor/shared/status_bar.c | 42 ++++++++++++++++++----- 6 files changed, 60 insertions(+), 68 deletions(-) diff --git a/shared-module/supervisor/StatusBar.c b/shared-module/supervisor/StatusBar.c index 0cc0552fcd..9b6fe9b849 100644 --- a/shared-module/supervisor/StatusBar.c +++ b/shared-module/supervisor/StatusBar.c @@ -45,7 +45,7 @@ void shared_module_supervisor_status_bar_set_console(supervisor_status_bar_obj_t return; } - if (self->written) { + if (self->updated) { // Clear before changing state. If disabling, will remain cleared. supervisor_status_bar_clear(); } @@ -67,7 +67,7 @@ void shared_module_supervisor_status_bar_set_display(supervisor_status_bar_obj_t return; } - if (self->written) { + if (self->updated) { // Clear before changing state. If disabling, will remain cleared. terminalio_terminal_clear_status_bar(&supervisor_terminal); } @@ -79,11 +79,12 @@ void shared_module_supervisor_status_bar_set_display(supervisor_status_bar_obj_t } #endif -bool supervisor_status_bar_get_update_in_progress(supervisor_status_bar_obj_t *self) { - return self->update_in_progress; +void shared_module_supervisor_status_bar_init(supervisor_status_bar_obj_t *self) { + self->console = true; + self->display = true; + self->updated = false; } -void supervisor_status_bar_set_update_in_progress(supervisor_status_bar_obj_t *self, bool update_in_progress) { - self->written = true; - self->update_in_progress = update_in_progress; +void shared_module_supervisor_status_bar_updated(supervisor_status_bar_obj_t *self) { + self->updated = true; } diff --git a/shared-module/supervisor/StatusBar.h b/shared-module/supervisor/StatusBar.h index bc80d7825f..a1254addc4 100644 --- a/shared-module/supervisor/StatusBar.h +++ b/shared-module/supervisor/StatusBar.h @@ -33,11 +33,10 @@ typedef struct { mp_obj_base_t base; bool console; bool display; - bool update_in_progress; - bool written; + bool updated; } supervisor_status_bar_obj_t; -extern bool supervisor_status_bar_get_update_in_progress(supervisor_status_bar_obj_t *self); -extern void supervisor_status_bar_set_update_in_progress(supervisor_status_bar_obj_t *self, bool in_progress); +extern void shared_module_supervisor_status_bar_init(supervisor_status_bar_obj_t *self); +extern void shared_module_supervisor_status_bar_updated(supervisor_status_bar_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_MODULE_SUPERVISOR_STATUS_BAR_H diff --git a/shared-module/terminalio/Terminal.c b/shared-module/terminalio/Terminal.c index b49f48c4e6..a19768a622 100644 --- a/shared-module/terminalio/Terminal.c +++ b/shared-module/terminalio/Terminal.c @@ -66,13 +66,6 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con return len; } - #if CIRCUITPY_STATUS_BAR - // Skip the status bar OSC sequence if it's disabled for the display. - const bool status_bar_write_ok = - shared_module_supervisor_status_bar_get_display(&shared_module_supervisor_status_bar_obj) || - !supervisor_status_bar_get_update_in_progress(&shared_module_supervisor_status_bar_obj); - #endif - const byte *i = data; uint16_t start_y = self->cursor_y; while (i < data + len) { @@ -85,9 +78,6 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con self->status_y = 0; i += 1; } else if ( - #if CIRCUITPY_STATUS_BAR - status_bar_write_ok && - #endif self->osc_command == 0 && self->status_bar != NULL && self->status_y < self->status_bar->height_in_tiles) { diff --git a/supervisor/serial.h b/supervisor/serial.h index 997a2bc38f..2e30998f3b 100644 --- a/supervisor/serial.h +++ b/supervisor/serial.h @@ -49,6 +49,10 @@ char serial_read(void); bool serial_bytes_available(void); bool serial_connected(void); +// Used for temporarily suppressing output to the console or display. +bool serial_console_write_disable(bool disabled); +bool serial_display_write_disable(bool disabled); + // These have no-op versions that are weak and the port can override. They work // in tandem with the cross-port mechanics like USB and BLE. void port_serial_early_init(void); diff --git a/supervisor/shared/serial.c b/supervisor/shared/serial.c index 6a336c5826..0326f15437 100644 --- a/supervisor/shared/serial.c +++ b/supervisor/shared/serial.c @@ -41,11 +41,6 @@ #include "supervisor/shared/bluetooth/serial.h" #endif -#if CIRCUITPY_STATUS_BAR -#include "shared-bindings/supervisor/__init__.h" -#include "shared-bindings/supervisor/StatusBar.h" -#endif - #if CIRCUITPY_USB #include "tusb.h" #endif @@ -72,10 +67,10 @@ bool tud_vendor_connected(void); #endif // Set to true to temporarily discard writes to the console only. -static bool _console_write_disabled; +static bool _serial_console_write_disabled; // Set to true to temporarily discard writes to the display terminal only. -static bool _display_write_disabled; +static bool _serial_display_write_disabled; #if CIRCUITPY_CONSOLE_UART STATIC void console_uart_print_strn(void *env, const char *str, size_t len) { @@ -283,12 +278,6 @@ bool serial_bytes_available(void) { return false; } -#if CIRCUITPY_STATUS_BAR -// Detect when USB is down when the status bar write starts. If USB comes up in the middle of writing -// the status bar, we want to the skip the rest so so junk doesn't get written out. -static bool ignore_rest_of_status_bar_update = false; -#endif - void serial_write_substring(const char *text, uint32_t length) { if (length == 0) { return; @@ -296,26 +285,14 @@ void serial_write_substring(const char *text, uint32_t length) { #if CIRCUITPY_TERMINALIO int errcode; - // If the status bar is disabled for the display, common_hal_terminalio_terminal_write() will not write it. - common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode); + if (!_serial_display_write_disabled) { + common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode); + } #endif - #if CIRCUITPY_STATUS_BAR - // If the status bar is disabled for the console, skip writing out the OSC sequence. - if (supervisor_status_bar_get_update_in_progress(&shared_module_supervisor_status_bar_obj)) { - if (!shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj)) { - // Console status bar disabled, so just return. - return; - } - } else { - // Status bar update is not in progress, so clear this history flag (will get cleared repeatedly). - ignore_rest_of_status_bar_update = false; - } - - if (ignore_rest_of_status_bar_update) { + if (_serial_console_write_disabled) { return; } - #endif #if CIRCUITPY_USB_VENDOR if (tud_vendor_connected()) { @@ -355,13 +332,6 @@ void serial_write_substring(const char *text, uint32_t length) { usb_background(); } } - #if CIRCUITPY_STATUS_BAR - else { - // USB was not connected for the first part of the status bar update. Ignore the rest - // so we don't send the remaining part of the OSC sequence if USB comes up later. - ignore_rest_of_status_bar_update = true; - } - #endif #endif port_serial_write_substring(text, length); @@ -371,10 +341,14 @@ void serial_write(const char *text) { serial_write_substring(text, strlen(text)); } -void serial_console_write_disable(bool disabled) { +bool serial_console_write_disable(bool disabled) { + bool now = _serial_console_write_disabled; _serial_console_write_disabled = disabled; + return now; } -void serial_display_write_disable(bool disabled) { +bool serial_display_write_disable(bool disabled) { + bool now = _serial_display_write_disabled; _serial_display_write_disabled = disabled; + return now; } diff --git a/supervisor/shared/status_bar.c b/supervisor/shared/status_bar.c index 066a8d3d10..4f5fa06464 100644 --- a/supervisor/shared/status_bar.c +++ b/supervisor/shared/status_bar.c @@ -53,9 +53,7 @@ static bool _suspended = false; // Clear if possible, but give up if we can't do it now. void supervisor_status_bar_clear(void) { if (!_suspended) { - supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, true); serial_write("\x1b" "]0;" "\x1b" "\\"); - supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, false); } } @@ -66,9 +64,31 @@ void supervisor_status_bar_update(void) { } _forced_dirty = false; - supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, true); + shared_module_supervisor_status_bar_updated(&shared_module_supervisor_status_bar_obj); - // Neighboring "" "" are concatenated by the compiler. Without this separation, the hex code + // Disable status bar console writes if supervisor.status_bar.console is False. + // Also disable if there is no serial connection now. This avoids sending part + // of the status bar update if the serial connection comes up during the update. + bool disable_console_writes = + !shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj) || + !serial_connected(); + + // Disable status bar display writes if supervisor.status_bar.display is False. + bool disable_display_writes = + !shared_module_supervisor_status_bar_get_display(&shared_module_supervisor_status_bar_obj); + + // Suppress writes to console and/or display if status bar is not enabled for either or both. + bool prev_console_disable; + bool prev_display_disable; + + if (disable_console_writes) { + prev_console_disable = serial_console_write_disable(true); + } + if (disable_display_writes) { + prev_display_disable = serial_display_write_disable(true); + } + + // Neighboring "..." "..." are concatenated by the compiler. Without this separation, the hex code // doesn't get terminated after two following characters and the value is invalid. // This is the OSC command to set the title and the icon text. It can be up to 255 characters // but some may be cut off. @@ -91,7 +111,14 @@ void supervisor_status_bar_update(void) { // Send string terminator serial_write("\x1b" "\\"); - supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, false); + // Restore writes to console and/or display. + if (disable_console_writes) { + serial_console_write_disable(prev_console_disable); + } + if (disable_display_writes) { + serial_display_write_disable(prev_display_disable); + } + } static void status_bar_background(void *data) { @@ -137,8 +164,5 @@ void supervisor_status_bar_init(void) { status_bar_background_cb.fun = status_bar_background; status_bar_background_cb.data = NULL; - shared_module_supervisor_status_bar_obj.console = true; - shared_module_supervisor_status_bar_obj.display = true; - shared_module_supervisor_status_bar_obj.update_in_progress = false; - shared_module_supervisor_status_bar_obj.written = false; + shared_module_supervisor_status_bar_init(&shared_module_supervisor_status_bar_obj); }