Skip to content

Instantly share code, notes, and snippets.

@maximevince
Created July 26, 2024 09:23
Show Gist options
  • Save maximevince/f6d3b68ec40c661f2c673ece72b4aec2 to your computer and use it in GitHub Desktop.
Save maximevince/f6d3b68ec40c661f2c673ece72b4aec2 to your computer and use it in GitHub Desktop.
Configure stdin on Zephyr Shell subsystem for a working scanf()
#include <zephyr/shell/shell_uart.h>
static unsigned char shell_stdin_hook(void)
{
const struct shell * sh = shell_backend_uart_get_ptr();
char ch;
size_t count = 0;
while (count == 0) {
(void)sh->iface->api->read(sh->iface, &ch, sizeof(ch), &count);
k_usleep(100); // yield to other threads
}
if (ch == '\r') {
putchar('\r');
putchar('\n');
} else {
putchar(ch);
}
return ch;
}
// Somewhere in your init code:
__stdin_hook_install(shell_stdin_hook); // to get input for scanf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment