diff --git a/hello_usb.cpp b/hello_usb.cpp index 9aa44c6..d2acac5 100644 --- a/hello_usb.cpp +++ b/hello_usb.cpp @@ -11,9 +11,28 @@ #include "hardware/i2c.h" #include "hardware/gpio.h" #include "pico-ssd1306/ssd1306.h" +#include "pico-ssd1306/textRenderer/TextRenderer.h" using namespace pico_ssd1306; +// Global display pointer so helper functions can update the OLED +static SSD1306 *g_display = nullptr; + +// Holds last echoed line for display +static char g_last_echo[128] = ""; + +// Refresh OLED with current input (line 0) and last echoed text (line 2) +static void refresh_display(const char *current_input, const char *last_echo) { + if (!g_display) return; + g_display->clear(); + // Use next bigger font (12x16) to improve readability + // First line: current input + pico_ssd1306::drawText(g_display, font_12x16, current_input, 0, 0); + // Second line: last echoed text (start lower to avoid overlap) + pico_ssd1306::drawText(g_display, font_12x16, last_echo, 0, 20); + g_display->sendBuffer(); +} + // Define the maximum size for the input string buffer #define MAX_INPUT_LEN 64 // Define the timeout period: 5 seconds in microseconds @@ -25,14 +44,26 @@ using namespace pico_ssd1306; void echo_and_reset(char* buffer, int* index_ptr) { 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++) { - printf("%c", toupper(buffer[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 + refresh_display("", g_last_echo); } void wait_for_usb_connection() { @@ -62,13 +93,20 @@ void run_echo_session() { if (buffer_index > 0) { buffer_index--; printf("\b \b"); + // update display to reflect removed char + input_buffer[buffer_index] = '\0'; + refresh_display(input_buffer, g_last_echo); } } else if (input_char == '\r' || input_char == '\n') { echo_and_reset(input_buffer, &buffer_index); + // 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'; + refresh_display(input_buffer, g_last_echo); } } @@ -96,12 +134,15 @@ int main() { // Create display object (address 0x3C is common). Adjust size/address if needed. SSD1306 display(i2c0, 0x3C, Size::W128xH64); + // rotate display 180 degrees (flip). setOrientation(1) flips screen orientation + display.setOrientation(false); display.clear(); display.sendBuffer(); - for (int y = 0; y < 64; y++){ - display.setPixel(64, y); - } - display.sendBuffer(); //Send buffer to device and show on screen + + // set global pointer and show initial prompt using larger font + g_display = &display; + g_last_echo[0] = '\0'; + refresh_display("> ", g_last_echo); while (true) {