Skip to content

Commit d3449bd

Browse files
authored
Merge pull request #6952 from dhalbert/simpler-status-bar-code
Simpler status bar code
2 parents 39492b3 + 0cd3737 commit d3449bd

File tree

6 files changed

+78
-49
lines changed

6 files changed

+78
-49
lines changed

shared-module/supervisor/StatusBar.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void shared_module_supervisor_status_bar_set_console(supervisor_status_bar_obj_t
4545
return;
4646
}
4747

48-
if (self->written) {
48+
if (self->updated) {
4949
// Clear before changing state. If disabling, will remain cleared.
5050
supervisor_status_bar_clear();
5151
}
@@ -67,7 +67,7 @@ void shared_module_supervisor_status_bar_set_display(supervisor_status_bar_obj_t
6767
return;
6868
}
6969

70-
if (self->written) {
70+
if (self->updated) {
7171
// Clear before changing state. If disabling, will remain cleared.
7272
terminalio_terminal_clear_status_bar(&supervisor_terminal);
7373
}
@@ -79,11 +79,12 @@ void shared_module_supervisor_status_bar_set_display(supervisor_status_bar_obj_t
7979
}
8080
#endif
8181

82-
bool supervisor_status_bar_get_update_in_progress(supervisor_status_bar_obj_t *self) {
83-
return self->update_in_progress;
82+
void shared_module_supervisor_status_bar_init(supervisor_status_bar_obj_t *self) {
83+
self->console = true;
84+
self->display = true;
85+
self->updated = false;
8486
}
8587

86-
void supervisor_status_bar_set_update_in_progress(supervisor_status_bar_obj_t *self, bool update_in_progress) {
87-
self->written = true;
88-
self->update_in_progress = update_in_progress;
88+
void shared_module_supervisor_status_bar_updated(supervisor_status_bar_obj_t *self) {
89+
self->updated = true;
8990
}

shared-module/supervisor/StatusBar.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ typedef struct {
3333
mp_obj_base_t base;
3434
bool console;
3535
bool display;
36-
bool update_in_progress;
37-
bool written;
36+
bool updated;
3837
} supervisor_status_bar_obj_t;
3938

40-
extern bool supervisor_status_bar_get_update_in_progress(supervisor_status_bar_obj_t *self);
41-
extern void supervisor_status_bar_set_update_in_progress(supervisor_status_bar_obj_t *self, bool in_progress);
39+
extern void shared_module_supervisor_status_bar_init(supervisor_status_bar_obj_t *self);
40+
extern void shared_module_supervisor_status_bar_updated(supervisor_status_bar_obj_t *self);
4241

4342
#endif // MICROPY_INCLUDED_SHARED_MODULE_SUPERVISOR_STATUS_BAR_H

shared-module/terminalio/Terminal.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
6666
return len;
6767
}
6868

69-
#if CIRCUITPY_STATUS_BAR
70-
// Skip the status bar OSC sequence if it's disabled for the display.
71-
const bool status_bar_write_ok =
72-
shared_module_supervisor_status_bar_get_display(&shared_module_supervisor_status_bar_obj) ||
73-
!supervisor_status_bar_get_update_in_progress(&shared_module_supervisor_status_bar_obj);
74-
#endif
75-
7669
const byte *i = data;
7770
uint16_t start_y = self->cursor_y;
7871
while (i < data + len) {
@@ -85,9 +78,6 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
8578
self->status_y = 0;
8679
i += 1;
8780
} else if (
88-
#if CIRCUITPY_STATUS_BAR
89-
status_bar_write_ok &&
90-
#endif
9181
self->osc_command == 0 &&
9282
self->status_bar != NULL &&
9383
self->status_y < self->status_bar->height_in_tiles) {

supervisor/serial.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ char serial_read(void);
4949
bool serial_bytes_available(void);
5050
bool serial_connected(void);
5151

52+
// Used for temporarily suppressing output to the console or display.
53+
bool serial_console_write_disable(bool disabled);
54+
bool serial_display_write_disable(bool disabled);
55+
5256
// These have no-op versions that are weak and the port can override. They work
5357
// in tandem with the cross-port mechanics like USB and BLE.
5458
void port_serial_early_init(void);

supervisor/shared/serial.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
#include "supervisor/shared/bluetooth/serial.h"
4242
#endif
4343

44-
#if CIRCUITPY_STATUS_BAR
45-
#include "shared-bindings/supervisor/__init__.h"
46-
#include "shared-bindings/supervisor/StatusBar.h"
47-
#endif
48-
4944
#if CIRCUITPY_USB
5045
#include "tusb.h"
5146
#endif
@@ -71,6 +66,12 @@ byte console_uart_rx_buf[64];
7166
bool tud_vendor_connected(void);
7267
#endif
7368

69+
// Set to true to temporarily discard writes to the console only.
70+
static bool _serial_console_write_disabled;
71+
72+
// Set to true to temporarily discard writes to the display terminal only.
73+
static bool _serial_display_write_disabled;
74+
7475
#if CIRCUITPY_CONSOLE_UART
7576
STATIC void console_uart_print_strn(void *env, const char *str, size_t len) {
7677
(void)env;
@@ -284,18 +285,14 @@ void serial_write_substring(const char *text, uint32_t length) {
284285

285286
#if CIRCUITPY_TERMINALIO
286287
int errcode;
287-
// We might be writing
288-
// If the status bar is disabled for the display, common_hal_terminalio_terminal_write() will not write it.
289-
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode);
288+
if (!_serial_display_write_disabled) {
289+
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode);
290+
}
290291
#endif
291292

292-
#if CIRCUITPY_STATUS_BAR
293-
// If the status bar is disabled for the console, skip writing out the OSC sequence.
294-
if (supervisor_status_bar_get_update_in_progress(&shared_module_supervisor_status_bar_obj) &&
295-
!shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj)) {
293+
if (_serial_console_write_disabled) {
296294
return;
297295
}
298-
#endif
299296

300297
#if CIRCUITPY_USB_VENDOR
301298
if (tud_vendor_connected()) {
@@ -325,13 +322,15 @@ void serial_write_substring(const char *text, uint32_t length) {
325322

326323
#if CIRCUITPY_USB
327324
uint32_t count = 0;
328-
while (count < length && tud_cdc_connected()) {
329-
count += tud_cdc_write(text + count, length - count);
330-
// If we're in an interrupt, then don't wait for more room. Queue up what we can.
331-
if (cpu_interrupt_active()) {
332-
break;
325+
if (tud_cdc_connected()) {
326+
while (count < length) {
327+
count += tud_cdc_write(text + count, length - count);
328+
// If we're in an interrupt, then don't wait for more room. Queue up what we can.
329+
if (cpu_interrupt_active()) {
330+
break;
331+
}
332+
usb_background();
333333
}
334-
usb_background();
335334
}
336335
#endif
337336

@@ -341,3 +340,15 @@ void serial_write_substring(const char *text, uint32_t length) {
341340
void serial_write(const char *text) {
342341
serial_write_substring(text, strlen(text));
343342
}
343+
344+
bool serial_console_write_disable(bool disabled) {
345+
bool now = _serial_console_write_disabled;
346+
_serial_console_write_disabled = disabled;
347+
return now;
348+
}
349+
350+
bool serial_display_write_disable(bool disabled) {
351+
bool now = _serial_display_write_disabled;
352+
_serial_display_write_disabled = disabled;
353+
return now;
354+
}

supervisor/shared/status_bar.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ static bool _suspended = false;
5353
// Clear if possible, but give up if we can't do it now.
5454
void supervisor_status_bar_clear(void) {
5555
if (!_suspended) {
56-
supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, true);
5756
serial_write("\x1b" "]0;" "\x1b" "\\");
58-
supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, false);
5957
}
6058
}
6159

@@ -66,9 +64,31 @@ void supervisor_status_bar_update(void) {
6664
}
6765
_forced_dirty = false;
6866

69-
supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, true);
67+
shared_module_supervisor_status_bar_updated(&shared_module_supervisor_status_bar_obj);
7068

71-
// Neighboring "" "" are concatenated by the compiler. Without this separation, the hex code
69+
// Disable status bar console writes if supervisor.status_bar.console is False.
70+
// Also disable if there is no serial connection now. This avoids sending part
71+
// of the status bar update if the serial connection comes up during the update.
72+
bool disable_console_writes =
73+
!shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj) ||
74+
!serial_connected();
75+
76+
// Disable status bar display writes if supervisor.status_bar.display is False.
77+
bool disable_display_writes =
78+
!shared_module_supervisor_status_bar_get_display(&shared_module_supervisor_status_bar_obj);
79+
80+
// Suppress writes to console and/or display if status bar is not enabled for either or both.
81+
bool prev_console_disable;
82+
bool prev_display_disable;
83+
84+
if (disable_console_writes) {
85+
prev_console_disable = serial_console_write_disable(true);
86+
}
87+
if (disable_display_writes) {
88+
prev_display_disable = serial_display_write_disable(true);
89+
}
90+
91+
// Neighboring "..." "..." are concatenated by the compiler. Without this separation, the hex code
7292
// doesn't get terminated after two following characters and the value is invalid.
7393
// This is the OSC command to set the title and the icon text. It can be up to 255 characters
7494
// but some may be cut off.
@@ -91,7 +111,14 @@ void supervisor_status_bar_update(void) {
91111
// Send string terminator
92112
serial_write("\x1b" "\\");
93113

94-
supervisor_status_bar_set_update_in_progress(&shared_module_supervisor_status_bar_obj, false);
114+
// Restore writes to console and/or display.
115+
if (disable_console_writes) {
116+
serial_console_write_disable(prev_console_disable);
117+
}
118+
if (disable_display_writes) {
119+
serial_display_write_disable(prev_display_disable);
120+
}
121+
95122
}
96123

97124
static void status_bar_background(void *data) {
@@ -137,8 +164,5 @@ void supervisor_status_bar_init(void) {
137164
status_bar_background_cb.fun = status_bar_background;
138165
status_bar_background_cb.data = NULL;
139166

140-
shared_module_supervisor_status_bar_obj.console = true;
141-
shared_module_supervisor_status_bar_obj.display = true;
142-
shared_module_supervisor_status_bar_obj.update_in_progress = false;
143-
shared_module_supervisor_status_bar_obj.written = false;
167+
shared_module_supervisor_status_bar_init(&shared_module_supervisor_status_bar_obj);
144168
}

0 commit comments

Comments
 (0)