Skip to content

Commit e5f2f0b

Browse files
committed
simplify suppressing status bar writes
1 parent e045415 commit e5f2f0b

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

supervisor/shared/serial.c

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ byte console_uart_rx_buf[64];
7171
bool tud_vendor_connected(void);
7272
#endif
7373

74+
// Set to true to temporarily discard writes to the console only.
75+
static bool _console_write_disabled;
76+
77+
// Set to true to temporarily discard writes to the display terminal only.
78+
static bool _display_write_disabled;
79+
7480
#if CIRCUITPY_CONSOLE_UART
7581
STATIC void console_uart_print_strn(void *env, const char *str, size_t len) {
7682
(void)env;
@@ -277,22 +283,36 @@ bool serial_bytes_available(void) {
277283
return false;
278284
}
279285

286+
#if CIRCUITPY_STATUS_BAR
287+
// Detect when USB is down when the status bar write starts. If USB comes up in the middle of writing
288+
// the status bar, we want to the skip the rest so so junk doesn't get written out.
289+
static bool ignore_rest_of_status_bar_update = false;
290+
#endif
291+
280292
void serial_write_substring(const char *text, uint32_t length) {
281293
if (length == 0) {
282294
return;
283295
}
284296

285297
#if CIRCUITPY_TERMINALIO
286298
int errcode;
287-
// We might be writing
288299
// If the status bar is disabled for the display, common_hal_terminalio_terminal_write() will not write it.
289300
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode);
290301
#endif
291302

292303
#if CIRCUITPY_STATUS_BAR
293304
// 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)) {
305+
if (supervisor_status_bar_get_update_in_progress(&shared_module_supervisor_status_bar_obj)) {
306+
if (!shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj)) {
307+
// Console status bar disabled, so just return.
308+
return;
309+
}
310+
} else {
311+
// Status bar update is not in progress, so clear this history flag (will get cleared repeatedly).
312+
ignore_rest_of_status_bar_update = false;
313+
}
314+
315+
if (ignore_rest_of_status_bar_update) {
296316
return;
297317
}
298318
#endif
@@ -325,14 +345,23 @@ void serial_write_substring(const char *text, uint32_t length) {
325345

326346
#if CIRCUITPY_USB
327347
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;
348+
if (tud_cdc_connected()) {
349+
while (count < length) {
350+
count += tud_cdc_write(text + count, length - count);
351+
// If we're in an interrupt, then don't wait for more room. Queue up what we can.
352+
if (cpu_interrupt_active()) {
353+
break;
354+
}
355+
usb_background();
333356
}
334-
usb_background();
335357
}
358+
#if CIRCUITPY_STATUS_BAR
359+
else {
360+
// USB was not connected for the first part of the status bar update. Ignore the rest
361+
// so we don't send the remaining part of the OSC sequence if USB comes up later.
362+
ignore_rest_of_status_bar_update = true;
363+
}
364+
#endif
336365
#endif
337366

338367
port_serial_write_substring(text, length);
@@ -341,3 +370,11 @@ void serial_write_substring(const char *text, uint32_t length) {
341370
void serial_write(const char *text) {
342371
serial_write_substring(text, strlen(text));
343372
}
373+
374+
void serial_console_write_disable(bool disabled) {
375+
_serial_console_write_disabled = disabled;
376+
}
377+
378+
void serial_display_write_disable(bool disabled) {
379+
_serial_display_write_disabled = disabled;
380+
}

0 commit comments

Comments
 (0)