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

@@ -26,14 +26,15 @@ static char g_last_echo[128] = "";
#include "epaper_manager.h"
#include "wifi_manager.h"
#include "tcp_debug.h"
#include "keyboard_stdio.h"
// Global display manager instance
DisplayManager* g_display_manager = nullptr;
// Keyboard buffer
// Command line buffer
#define MAX_INPUT_LEN 64
static char g_input_buffer[MAX_INPUT_LEN];
static int g_buffer_index = 0;
static char g_cmd_buffer[MAX_INPUT_LEN];
static int g_cmd_index = 0;
static bool execute_command(CommandAction action, const char* input) {
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) {
KeyEvent event;
if (parse_keyboard_report(report, &event)) {
if (event.is_backspace) {
if (g_buffer_index > 0) {
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);
}
if (event.ascii) {
keyboard_stdio_push_char(event.ascii);
}
}
}
@@ -206,8 +153,8 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
g_keyboard_mounted = false;
// Reset input buffer
g_buffer_index = 0;
g_input_buffer[0] = '\0';
g_cmd_index = 0;
g_cmd_buffer[0] = '\0';
if (g_display_manager) {
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() {
stdio_init_all();
keyboard_stdio_init();
sleep_ms(3000); // Give time for power to settle and serial to connect
printf("System Booting...\n");
@@ -247,6 +255,11 @@ int main() {
while (true) {
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());
if (now - last_print > 5000) {
printf("Heartbeat: %u, Mounted: %d\n", now, g_keyboard_mounted);