pico_stdio improvements (#598)

* add stdio_usb_connected() method
* add PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS to allow waiting for CDC connection during init(* 
* add puts_raw and putchar_raw to skip any CR/LF translation
This commit is contained in:
Graham Sanderson
2021-10-08 09:01:30 -05:00
committed by GitHub
parent 12017a07ab
commit 13be546dc3
5 changed files with 84 additions and 9 deletions

View File

@ -59,6 +59,9 @@ static bool stdout_serialize_begin(void) {
static void stdout_serialize_end(void) {
}
#endif
static void stdio_out_chars_no_crlf(stdio_driver_t *driver, const char *s, int len) {
driver->out_chars(s, len);
}
static void stdio_out_chars_crlf(stdio_driver_t *driver, const char *s, int len) {
#if PICO_STDIO_ENABLE_CRLF_SUPPORT
@ -89,7 +92,7 @@ static void stdio_out_chars_crlf(stdio_driver_t *driver, const char *s, int len)
#endif
}
static bool stdio_put_string(const char *s, int len, bool newline) {
static bool stdio_put_string(const char *s, int len, bool newline, bool no_cr) {
bool serialized = stdout_serialize_begin();
if (!serialized) {
#if PICO_STDIO_IGNORE_NESTED_STDOUT
@ -97,13 +100,14 @@ static bool stdio_put_string(const char *s, int len, bool newline) {
#endif
}
if (len == -1) len = (int)strlen(s);
void (*out_func)(stdio_driver_t *, const char *, int) = no_cr ? stdio_out_chars_no_crlf : stdio_out_chars_crlf;
for (stdio_driver_t *driver = drivers; driver; driver = driver->next) {
if (!driver->out_chars) continue;
if (filter && filter != driver) continue;
stdio_out_chars_crlf(driver, s, len);
out_func(driver, s, len);
if (newline) {
const char c = '\n';
stdio_out_chars_crlf(driver, &c, 1);
out_func(driver, &c, 1);
}
}
if (serialized) {
@ -134,13 +138,26 @@ static int stdio_get_until(char *buf, int len, absolute_time_t until) {
int WRAPPER_FUNC(putchar)(int c) {
char cc = (char)c;
stdio_put_string(&cc, 1, false);
stdio_put_string(&cc, 1, false, false);
return c;
}
int WRAPPER_FUNC(puts)(const char *s) {
int len = (int)strlen(s);
stdio_put_string(s, len, true);
stdio_put_string(s, len, true, false);
stdio_flush();
return len;
}
int putchar_raw(int c) {
char cc = (char)c;
stdio_put_string(&cc, 1, false, true);
return c;
}
int puts_raw(const char *s) {
int len = (int)strlen(s);
stdio_put_string(s, len, true, true);
stdio_flush();
return len;
}
@ -154,7 +171,7 @@ int _read(int handle, char *buffer, int length) {
int _write(int handle, char *buffer, int length) {
if (handle == 1) {
stdio_put_string(buffer, length, false);
stdio_put_string(buffer, length, false, false);
return length;
}
return -1;
@ -243,7 +260,7 @@ int __printflike(1, 0) WRAPPER_FUNC(printf)(const char* format, ...)
return ret;
}
void stdio_init_all() {
void stdio_init_all(void) {
// todo add explicit custom, or registered although you can call stdio_enable_driver explicitly anyway
// These are well known ones
#if LIB_PICO_STDIO_UART