Skip to content

Commit 237fdf6

Browse files
committed
bricks/_common/mphalport: Print exceptions on a new line.
Fixes pybricks/support#870
1 parent 6a07abf commit 237fdf6

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

bricks/_common/micropython.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ bool pbsys_main_stdin_event(uint8_t c) {
7878

7979
// Prints the exception that ended the program.
8080
static void print_final_exception(mp_obj_t exc, int ret) {
81+
82+
// Ensure exception prints on new line.
83+
mp_hal_stdout_tx_flush();
84+
8185
nlr_buf_t nlr;
8286
nlr.ret_val = NULL;
8387

bricks/_common/mphalport.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ int mp_hal_stdin_rx_chr(void) {
5555
return c;
5656
}
5757

58+
static bool ended_on_new_line = true;
59+
5860
// Send string of given length
5961
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
6062
size_t remaining = len;
@@ -63,6 +65,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
6365
uint32_t size = remaining;
6466
pbio_error_t err = pbsys_host_stdout_write((const uint8_t *)str, &size);
6567
if (err == PBIO_SUCCESS) {
68+
ended_on_new_line = str[size - 1] == '\n';
6669
str += size;
6770
remaining -= size;
6871
} else if (err != PBIO_ERROR_AGAIN) {
@@ -85,4 +88,15 @@ void mp_hal_stdout_tx_flush(void) {
8588
while (!pbsys_host_tx_is_idle()) {
8689
MICROPY_VM_HOOK_LOOP;
8790
}
91+
92+
// A program may be interrupted in the middle of a long print, or the user
93+
// may have printed without a newline. Ensure we end on a new line.
94+
if (!ended_on_new_line) {
95+
const char *eol = "\r\n";
96+
uint32_t size = strlen(eol) - 1;
97+
pbsys_host_stdout_write((const uint8_t *)eol, &size);
98+
while (!pbsys_host_tx_is_idle()) {
99+
MICROPY_VM_HOOK_LOOP;
100+
}
101+
}
88102
}

0 commit comments

Comments
 (0)