Refactor input to use stdio drivers for Keyboard and TCP

This commit is contained in:
Adolfo Reyna
2025-12-23 23:50:58 -05:00
parent 9f53148583
commit 9bc57f7ee2
5 changed files with 182 additions and 64 deletions

View File

@@ -45,6 +45,7 @@ if (TARGET tinyusb_host)
epaper_manager.cpp epaper_manager.cpp
wifi_manager.cpp wifi_manager.cpp
tcp_debug.cpp tcp_debug.cpp
keyboard_stdio.cpp
) )
add_subdirectory(pico-ssd1306 commands) add_subdirectory(pico-ssd1306 commands)

View File

@@ -26,14 +26,15 @@ static char g_last_echo[128] = "";
#include "epaper_manager.h" #include "epaper_manager.h"
#include "wifi_manager.h" #include "wifi_manager.h"
#include "tcp_debug.h" #include "tcp_debug.h"
#include "keyboard_stdio.h"
// Global display manager instance // Global display manager instance
DisplayManager* g_display_manager = nullptr; DisplayManager* g_display_manager = nullptr;
// Keyboard buffer // Command line buffer
#define MAX_INPUT_LEN 64 #define MAX_INPUT_LEN 64
static char g_input_buffer[MAX_INPUT_LEN]; static char g_cmd_buffer[MAX_INPUT_LEN];
static int g_buffer_index = 0; static int g_cmd_index = 0;
static bool execute_command(CommandAction action, const char* input) { static bool execute_command(CommandAction action, const char* input) {
switch (action) { switch (action) {
@@ -109,62 +110,8 @@ static bool execute_command(CommandAction action, const char* input) {
static void process_kbd_report(hid_keyboard_report_t const *report) { static void process_kbd_report(hid_keyboard_report_t const *report) {
KeyEvent event; KeyEvent event;
if (parse_keyboard_report(report, &event)) { if (parse_keyboard_report(report, &event)) {
if (event.is_backspace) { if (event.ascii) {
if (g_buffer_index > 0) { keyboard_stdio_push_char(event.ascii);
printf("\b \b");
g_buffer_index--;
g_input_buffer[g_buffer_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_input_buffer, g_last_echo);
// Update e-Paper (in-place)
epaper_send_update(g_input_buffer, false);
}
} else if (event.is_enter) {
printf("\n");
// Always commit the input line to e-Paper so commands are visible
if (g_buffer_index > 0) {
epaper_send_update(g_input_buffer, true);
}
CommandAction action = parse_command(g_input_buffer);
if (execute_command(action, g_input_buffer)) {
// Command handled
if (g_display_manager) {
g_display_manager->set_last_echo("Command Executed");
g_display_manager->refresh("", "Command Executed");
}
} else {
// Not a command, just text (already committed to e-Paper above)
// Save to last echo
strncpy(g_last_echo, g_input_buffer, sizeof(g_last_echo) - 1);
g_last_echo[sizeof(g_last_echo) - 1] = '\0';
// Update OLED
if (g_display_manager) {
g_display_manager->set_last_echo(g_last_echo);
g_display_manager->refresh("", g_last_echo);
}
}
// Clear buffer
g_buffer_index = 0;
g_input_buffer[0] = '\0';
} else if (event.is_printable) {
// printf("%c", event.ascii);
if (g_buffer_index < MAX_INPUT_LEN - 1) {
g_input_buffer[g_buffer_index++] = event.ascii;
g_input_buffer[g_buffer_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_input_buffer, g_last_echo);
// Update e-Paper on every keystroke
epaper_send_update(g_input_buffer, false);
}
} }
} }
} }
@@ -206,8 +153,8 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
g_keyboard_mounted = false; g_keyboard_mounted = false;
// Reset input buffer // Reset input buffer
g_buffer_index = 0; g_cmd_index = 0;
g_input_buffer[0] = '\0'; g_cmd_buffer[0] = '\0';
if (g_display_manager) { if (g_display_manager) {
g_display_manager->refresh("Waiting for Keyboard", nullptr); g_display_manager->refresh("Waiting for Keyboard", nullptr);
@@ -227,8 +174,69 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
} }
} }
static void process_input_char(char c) {
if (c == '\b' || c == 127) {
if (g_cmd_index > 0) {
printf("\b \b");
g_cmd_index--;
g_cmd_buffer[g_cmd_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_cmd_buffer, g_last_echo);
// Update e-Paper (in-place)
epaper_send_update(g_cmd_buffer, false);
}
} else if (c == '\r' || c == '\n') {
printf("\n");
// Always commit the input line to e-Paper so commands are visible
if (g_cmd_index > 0) {
epaper_send_update(g_cmd_buffer, true);
}
CommandAction action = parse_command(g_cmd_buffer);
if (execute_command(action, g_cmd_buffer)) {
// Command handled
if (g_display_manager) {
g_display_manager->set_last_echo("Command Executed");
g_display_manager->refresh("", "Command Executed");
}
} else {
// Not a command, just text (already committed to e-Paper above)
// Save to last echo
strncpy(g_last_echo, g_cmd_buffer, sizeof(g_last_echo) - 1);
g_last_echo[sizeof(g_last_echo) - 1] = '\0';
// Update OLED
if (g_display_manager) {
g_display_manager->set_last_echo(g_last_echo);
g_display_manager->refresh("", g_last_echo);
}
}
// Clear buffer
g_cmd_index = 0;
g_cmd_buffer[0] = '\0';
} else {
printf("%c", c);
if (g_cmd_index < MAX_INPUT_LEN - 1) {
g_cmd_buffer[g_cmd_index++] = c;
g_cmd_buffer[g_cmd_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_cmd_buffer, g_last_echo);
// Update e-Paper on every keystroke
epaper_send_update(g_cmd_buffer, false);
}
}
}
int main() { int main() {
stdio_init_all(); stdio_init_all();
keyboard_stdio_init();
sleep_ms(3000); // Give time for power to settle and serial to connect sleep_ms(3000); // Give time for power to settle and serial to connect
printf("System Booting...\n"); printf("System Booting...\n");
@@ -247,6 +255,11 @@ int main() {
while (true) { while (true) {
tuh_task(); tuh_task();
int c = getchar_timeout_us(0);
if (c != PICO_ERROR_TIMEOUT) {
process_input_char((char)c);
}
uint32_t now = to_ms_since_boot(get_absolute_time()); uint32_t now = to_ms_since_boot(get_absolute_time());
if (now - last_print > 5000) { if (now - last_print > 5000) {
printf("Heartbeat: %u, Mounted: %d\n", now, g_keyboard_mounted); printf("Heartbeat: %u, Mounted: %d\n", now, g_keyboard_mounted);

46
keyboard_stdio.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "keyboard_stdio.h"
#include "pico/stdio/driver.h"
#include "pico/sync.h"
#define KBD_BUF_SIZE 128
// Circular buffer for keyboard input
static char kbd_buf[KBD_BUF_SIZE];
static volatile int write_idx = 0;
static volatile int read_idx = 0;
static critical_section_t kbd_crit;
void keyboard_stdio_push_char(char c) {
critical_section_enter_blocking(&kbd_crit);
int next_write = (write_idx + 1) % KBD_BUF_SIZE;
if (next_write != read_idx) { // Only write if not full
kbd_buf[write_idx] = c;
write_idx = next_write;
}
critical_section_exit(&kbd_crit);
}
static int keyboard_in_chars(char *buf, int len) {
int count = 0;
critical_section_enter_blocking(&kbd_crit);
while (count < len && read_idx != write_idx) {
buf[count++] = kbd_buf[read_idx];
read_idx = (read_idx + 1) % KBD_BUF_SIZE;
}
critical_section_exit(&kbd_crit);
return count > 0 ? count : PICO_ERROR_NO_DATA;
}
static stdio_driver_t kbd_driver = {
.out_chars = NULL,
.out_flush = NULL,
.in_chars = keyboard_in_chars,
#if PICO_STDIO_ENABLE_CRLF_SUPPORT
.crlf_enabled = false
#endif
};
void keyboard_stdio_init(void) {
critical_section_init(&kbd_crit);
stdio_set_driver_enabled(&kbd_driver, true);
}

18
keyboard_stdio.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef KEYBOARD_STDIO_H
#define KEYBOARD_STDIO_H
#include <stdbool.h>
/**
* Initializes the keyboard stdio driver.
* This allows standard C input functions (getchar, scanf, etc.) to read from the USB keyboard.
*/
void keyboard_stdio_init(void);
/**
* Pushes a character into the keyboard input buffer.
* This should be called by the USB HID report handler.
*/
void keyboard_stdio_push_char(char c);
#endif

View File

@@ -2,16 +2,25 @@
#include "pico/cyw43_arch.h" #include "pico/cyw43_arch.h"
#include "lwip/tcp.h" #include "lwip/tcp.h"
#include "pico/stdio/driver.h" #include "pico/stdio/driver.h"
#include "pico/sync.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#define TCP_PORT 4242 #define TCP_PORT 4242
#define DEBUG_BUF_SIZE 2048 #define DEBUG_BUF_SIZE 2048
#define TCP_IN_BUF_SIZE 128
// Circular buffer for debug output // Circular buffer for debug output
static char debug_buf[DEBUG_BUF_SIZE]; static char debug_buf[DEBUG_BUF_SIZE];
static volatile int write_idx = 0; static volatile int write_idx = 0;
static volatile int read_idx = 0; static volatile int read_idx = 0;
// Circular buffer for TCP input
static char tcp_in_buf[TCP_IN_BUF_SIZE];
static volatile int in_write_idx = 0;
static volatile int in_read_idx = 0;
static critical_section_t tcp_in_crit;
static struct tcp_pcb *server_pcb = NULL; static struct tcp_pcb *server_pcb = NULL;
static struct tcp_pcb *client_pcb = NULL; static struct tcp_pcb *client_pcb = NULL;
@@ -39,10 +48,21 @@ static void tcp_out_chars(const char *buf, int len) {
} }
} }
static int tcp_in_chars(char *buf, int len) {
int count = 0;
critical_section_enter_blocking(&tcp_in_crit);
while (count < len && in_read_idx != in_write_idx) {
buf[count++] = tcp_in_buf[in_read_idx];
in_read_idx = (in_read_idx + 1) % TCP_IN_BUF_SIZE;
}
critical_section_exit(&tcp_in_crit);
return count > 0 ? count : PICO_ERROR_NO_DATA;
}
static stdio_driver_t tcp_driver = { static stdio_driver_t tcp_driver = {
.out_chars = tcp_out_chars, .out_chars = tcp_out_chars,
.out_flush = NULL, .out_flush = NULL,
.in_chars = NULL, .in_chars = tcp_in_chars,
#if PICO_STDIO_ENABLE_CRLF_SUPPORT #if PICO_STDIO_ENABLE_CRLF_SUPPORT
.crlf_enabled = false .crlf_enabled = false
#endif #endif
@@ -84,8 +104,26 @@ static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, er
printf("TCP debug client disconnected\n"); printf("TCP debug client disconnected\n");
return ERR_OK; return ERR_OK;
} }
// We don't expect input, just discard it
// Process received data into input buffer
if (p->tot_len > 0) {
critical_section_enter_blocking(&tcp_in_crit);
struct pbuf *q = p;
while (q != NULL) {
char *data = (char *)q->payload;
for (int i = 0; i < q->len; i++) {
int next_write = (in_write_idx + 1) % TCP_IN_BUF_SIZE;
if (next_write != in_read_idx) {
tcp_in_buf[in_write_idx] = data[i];
in_write_idx = next_write;
}
}
q = q->next;
}
critical_section_exit(&tcp_in_crit);
tcp_recved(tpcb, p->tot_len); tcp_recved(tpcb, p->tot_len);
}
pbuf_free(p); pbuf_free(p);
return ERR_OK; return ERR_OK;
} }
@@ -112,6 +150,8 @@ static err_t tcp_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
bool tcp_debug_init(void) { bool tcp_debug_init(void) {
if (server_pcb) return true; if (server_pcb) return true;
critical_section_init(&tcp_in_crit);
// Register stdio driver // Register stdio driver
stdio_set_driver_enabled(&tcp_driver, true); stdio_set_driver_enabled(&tcp_driver, true);