Merge pull request #6952 from dhalbert/simpler-status-bar-code

Simpler status bar code
This commit is contained in:
Dan Halbert 2022-09-28 12:40:01 -04:00 committed by GitHub
commit d3449bdabb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 51 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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) {

View File

@ -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);

View File

@ -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
@ -71,6 +66,12 @@ byte console_uart_rx_buf[64];
bool tud_vendor_connected(void);
#endif
// Set to true to temporarily discard writes to the console only.
static bool _serial_console_write_disabled;
// Set to true to temporarily discard writes to the display terminal only.
static bool _serial_display_write_disabled;
#if CIRCUITPY_CONSOLE_UART
STATIC void console_uart_print_strn(void *env, const char *str, size_t len) {
(void)env;
@ -284,19 +285,15 @@ void serial_write_substring(const char *text, uint32_t length) {
#if CIRCUITPY_TERMINALIO
int errcode;
// We might be writing
// 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);
#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) &&
!shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj)) {
return;
if (!_serial_display_write_disabled) {
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode);
}
#endif
if (_serial_console_write_disabled) {
return;
}
#if CIRCUITPY_USB_VENDOR
if (tud_vendor_connected()) {
tud_vendor_write(text, length);
@ -325,13 +322,15 @@ void serial_write_substring(const char *text, uint32_t length) {
#if CIRCUITPY_USB
uint32_t count = 0;
while (count < length && tud_cdc_connected()) {
count += tud_cdc_write(text + count, length - count);
// If we're in an interrupt, then don't wait for more room. Queue up what we can.
if (cpu_interrupt_active()) {
break;
if (tud_cdc_connected()) {
while (count < length) {
count += tud_cdc_write(text + count, length - count);
// If we're in an interrupt, then don't wait for more room. Queue up what we can.
if (cpu_interrupt_active()) {
break;
}
usb_background();
}
usb_background();
}
#endif
@ -341,3 +340,15 @@ void serial_write_substring(const char *text, uint32_t length) {
void serial_write(const char *text) {
serial_write_substring(text, strlen(text));
}
bool serial_console_write_disable(bool disabled) {
bool now = _serial_console_write_disabled;
_serial_console_write_disabled = disabled;
return now;
}
bool serial_display_write_disable(bool disabled) {
bool now = _serial_display_write_disabled;
_serial_display_write_disabled = disabled;
return now;
}

View File

@ -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);
}