/* * Simple USB Serial Echo Program with Timeout, Reconnect, and Backspace Logic. * Converted to C++ so the project can use the `pico-ssd1306` C++ library. */ #include "pico/stdlib.h" #include "pico/time.h" // Needed for time_us_64() #include #include #include #include "display.h" // Holds last echoed line for display static char g_last_echo[128] = ""; // Define the maximum size for the input string buffer #define MAX_INPUT_LEN 64 // Define the timeout period: 5 seconds in microseconds #define TIMEOUT_US 5000000 // ASCII code for Backspace (often sent as 0x08) #define ASCII_BACKSPACE 8 // Helper function to handle the echoing and resetting of the buffer void echo_and_reset(char* buffer, int* index_ptr, DisplayManager &display) { if (*index_ptr > 0) { buffer[*index_ptr] = '\0'; // Print to USB serial (ALL CAPS) printf("\nEchoed (ALL CAPS): "); for (int i = 0; buffer[i] != '\0'; i++) { char ch = toupper(buffer[i]); printf("%c", ch); } printf("\n"); // Save last echoed (all caps) for display size_t i = 0; for (; buffer[i] != '\0' && i < sizeof(g_last_echo) - 1; ++i) { g_last_echo[i] = (char)toupper(buffer[i]); } g_last_echo[i] = '\0'; } *index_ptr = 0; printf("> "); // Update OLED to show cleared input and last echo display.set_last_echo(g_last_echo); display.refresh("", g_last_echo); } void wait_for_usb_connection() { printf("Waiting for USB host to connect...\n"); while (!stdio_usb_connected()) { sleep_ms(100); } printf("\nConnection Established! Starting Echo Session...\n"); } void run_echo_session(DisplayManager &display) { char input_buffer[MAX_INPUT_LEN]; int buffer_index = 0; uint64_t last_char_time = time_us_64(); printf("--- Pico USB String Echo Program Started ---\n"); printf("Type a sentence (up to %d chars). It will echo on Enter/Timeout.\n", MAX_INPUT_LEN - 2); printf("--------------------------------------------\n"); printf("> "); while (stdio_usb_connected()) { int c = getchar_timeout_us(0); if (c != PICO_ERROR_TIMEOUT) { last_char_time = time_us_64(); char input_char = (char)c; if (input_char == ASCII_BACKSPACE || input_char == 127) { if (buffer_index > 0) { buffer_index--; printf("\b \b"); // update display to reflect removed char input_buffer[buffer_index] = '\0'; display.refresh(input_buffer, g_last_echo); } } else if (input_char == '\r' || input_char == '\n') { echo_and_reset(input_buffer, &buffer_index, display); // after echo_and_reset the display is refreshed inside it } else if (buffer_index < (MAX_INPUT_LEN - 1) && isprint(input_char)) { printf("%c", input_char); input_buffer[buffer_index] = input_char; buffer_index++; // update OLED with current input input_buffer[buffer_index] = '\0'; display.refresh(input_buffer, g_last_echo); } } if (buffer_index > 0 && (time_us_64() - last_char_time) > TIMEOUT_US) { printf("\n--- Timeout Reached (5.0s silence) ---\n"); echo_and_reset(input_buffer, &buffer_index, display); last_char_time = time_us_64(); } // Advance marquee for last echoed text if needed size_t last_len = strlen(g_last_echo); uint64_t now_ms = time_us_64() / 1000; sleep_us(100); } printf("\nHost disconnected. Ending Echo Session.\n"); } int main() { stdio_init_all(); DisplayManager display; display.init(); while (true) { wait_for_usb_connection(); run_echo_session(display); } return 0; }