Skip to content

Commit e5541f7

Browse files
committed
bricks/_common/mphalport: Fix flush to new line.
- Fixes doing this only once by setting ended_on_new_line. - Restores original mp_hal_stdout_tx_flush without the new line for compatibility with sys_stdio_mphal.
1 parent 9b10fc1 commit e5541f7

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

bricks/_common/micropython.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ bool pbsys_main_stdin_event(uint8_t c) {
6969
static void print_final_exception(mp_obj_t exc, int ret) {
7070

7171
// Ensure exception prints on new line.
72-
mp_hal_stdout_tx_flush();
72+
pb_stdout_flush_to_new_line();
7373

7474
nlr_buf_t nlr;
7575
nlr.ret_val = NULL;
@@ -402,7 +402,7 @@ void pbsys_main_run_program(pbsys_main_program_t *program) {
402402

403403
// Ensure everything is written before the user application is considered
404404
// done, so that the host does not receive stdout after receiving stop.
405-
mp_hal_stdout_tx_flush();
405+
pb_stdout_flush_to_new_line();
406406
}
407407

408408
void pbsys_main_run_program_cleanup(void) {

bricks/_common/mphalport.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,28 @@ void mp_hal_stdout_tx_flush(void) {
8787
while (!pbsys_host_tx_is_idle()) {
8888
MICROPY_VM_HOOK_LOOP;
8989
}
90+
}
91+
92+
/**
93+
* Flushes stdout and adds a newline if the last printed character was not a
94+
* new line.
95+
*/
96+
void pb_stdout_flush_to_new_line(void) {
97+
98+
mp_hal_stdout_tx_flush();
9099

91100
// A program may be interrupted in the middle of a long print, or the user
92101
// may have printed without a newline. Ensure we end on a new line.
93102
if (!ended_on_new_line) {
103+
104+
// We have just flushed, so we should be able to easily buffer two more
105+
// characters. It is only for aesthetics, so it is not critical if this
106+
// fails. We flush again below just in case.
94107
const char *eol = "\r\n";
95108
uint32_t size = strlen(eol);
96109
pbsys_host_stdout_write((const uint8_t *)eol, &size);
97-
while (!pbsys_host_tx_is_idle()) {
98-
MICROPY_VM_HOOK_LOOP;
99-
}
110+
ended_on_new_line = true;
111+
112+
mp_hal_stdout_tx_flush();
100113
}
101114
}

bricks/_common/mphalport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ void mp_hal_set_interrupt_char(int c);
99
#define mp_hal_ticks_cpu() 0
1010
#define mp_hal_delay_us pbdrv_clock_busy_delay_us
1111

12-
// Platform-specific code to run on completing the poll hook.
13-
void pb_event_poll_hook_leave(void);
12+
void pb_stdout_flush_to_new_line(void);

bricks/nxt/mphalport.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
9292
void mp_hal_stdout_tx_flush(void) {
9393
// currently not buffered
9494
}
95+
96+
void pb_stdout_flush_to_new_line(void) {
97+
// currently not buffered
98+
}

0 commit comments

Comments
 (0)