-
-
Notifications
You must be signed in to change notification settings - Fork 80
Stdout fixes #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stdout fixes #409
Changes from all commits
4b6dbb4
3f634b6
523adde
da615f7
5e61785
4d67eea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,7 @@ | |
| // Contains the MicroPython HAL for STM32-based Pybricks ports. | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include <contiki.h> | ||
| #include <string.h> | ||
|
|
||
| #include <pbdrv/clock.h> | ||
| #include <pbdrv/config.h> | ||
|
|
@@ -55,6 +54,8 @@ int mp_hal_stdin_rx_chr(void) { | |
| return c; | ||
| } | ||
|
|
||
| static bool ended_on_new_line = true; | ||
|
|
||
| // Send string of given length | ||
| mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { | ||
| size_t remaining = len; | ||
|
|
@@ -63,6 +64,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { | |
| uint32_t size = remaining; | ||
| pbio_error_t err = pbsys_host_stdout_write((const uint8_t *)str, &size); | ||
| if (err == PBIO_SUCCESS) { | ||
| ended_on_new_line = str[size - 1] == '\n'; | ||
| str += size; | ||
| remaining -= size; | ||
| } else if (err != PBIO_ERROR_AGAIN) { | ||
|
|
@@ -71,14 +73,29 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { | |
| return len - remaining; | ||
| } | ||
|
|
||
| MICROPY_EVENT_POLL_HOOK | ||
| // Allow long prints to be interrupted. | ||
| if (remaining) { | ||
| MICROPY_EVENT_POLL_HOOK | ||
| } | ||
| } | ||
|
|
||
| return len; | ||
| } | ||
|
|
||
| void mp_hal_stdout_tx_flush(void) { | ||
| // Don't raise, just wait for data to clear. | ||
| while (!pbsys_host_tx_is_idle()) { | ||
| MICROPY_EVENT_POLL_HOOK | ||
| MICROPY_VM_HOOK_LOOP; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really safe? If the host never becomes idle, there is no way to ever interrupt this other than turning off the hub.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It becomes idle when the host disconnects or when nothing more is scheduled. This is why we want this one. The poll hook can raise and schedule more data.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that works as long as we have USB stall after a timeout in case USB is still connected but no app is servicing the endpoints. |
||
| } | ||
|
|
||
| // A program may be interrupted in the middle of a long print, or the user | ||
| // may have printed without a newline. Ensure we end on a new line. | ||
| if (!ended_on_new_line) { | ||
| const char *eol = "\r\n"; | ||
laurensvalk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| uint32_t size = strlen(eol); | ||
| pbsys_host_stdout_write((const uint8_t *)eol, &size); | ||
laurensvalk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| while (!pbsys_host_tx_is_idle()) { | ||
laurensvalk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| MICROPY_VM_HOOK_LOOP; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.