/* * Simple USB Serial Echo Program with Timeout, Reconnect, and Backspace Logic. * Converted to C++ so the project can use the `pico-ssd1306` C++ library. * Integrated with e-ink display support via Pico_ePaper library. */ #include "pico/stdlib.h" #include "pico/time.h" // Needed for time_us_64() #include "pico/multicore.h" #include #include #include #include #include "display.h" #include "commands/echo.h" // e-Paper library includes extern "C" { #include "DEV_Config.h" #include "EPD_7in5b_V2.h" #include "GUI_Paint.h" } // Holds last echoed line for display static char g_last_echo[128] = ""; // e-Paper display buffers static UBYTE *g_epd_image = NULL; // Black image buffer static UBYTE *g_epd_red = NULL; // Red image buffer // Entry list for display #define MAX_ENTRIES 10 #define ENTRY_LENGTH 64 typedef struct { char entries[MAX_ENTRIES][ENTRY_LENGTH]; int count; } EntryList; static EntryList g_entry_list = {{}, 0}; // Synchronization flag for multicore initialization static volatile bool g_display_ready = false; // Display update message structure for inter-core communication typedef struct { EntryList entries; bool use_partial; // Use partial refresh instead of full refresh UWORD xstart; // Partial refresh region UWORD ystart; UWORD xend; UWORD yend; } DisplayMessage; void init_epaper_display() { printf("Initializing 7.5\" e-Paper display (B V2)...\r\n"); // Initialize the hardware if (DEV_Module_Init() != 0) { printf("Failed to initialize e-Paper hardware!\r\n"); return; } // Initialize the display printf("EPD_7IN5B_V2_Init()\r\n"); EPD_7IN5B_V2_Init_Fast(); printf("EPD_7IN5B_V2_Clear()\r\n"); EPD_7IN5B_V2_ClearBlack(); DEV_Delay_ms(500); // Create image buffers for black and red content // 7.5" display: 800x480, 8 pixels per byte, so (800/8) * 480 = 48000 bytes each UWORD imagesize = (EPD_7IN5B_V2_WIDTH / 8) * EPD_7IN5B_V2_HEIGHT; g_epd_image = (UBYTE *)malloc(imagesize); g_epd_red = (UBYTE *)malloc(imagesize); if (g_epd_image == NULL || g_epd_red == NULL) { printf("Failed to allocate memory for e-Paper image buffers!\r\n"); return; } // Initialize red buffer to all white (0xFF = no red pixels) for (UWORD i = 0; i < imagesize; i++) { g_epd_red[i] = 0xFF; } // Setup paint buffer for black content printf("White background\r\n"); Paint_NewImage(g_epd_image, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT, 0, WHITE); Paint_SelectImage(g_epd_image); Paint_Clear(WHITE); // Draw header // printf("Drawing header\r\n"); // Paint_DrawString_EN(10, 10, "What's new today:", &Font24, BLACK, WHITE); // Display the image with both black and red buffers // printf("display text\r\n"); EPD_7IN5B_V2_Display(g_epd_image, g_epd_red); // printf("delay\r\n"); // DEV_Delay_ms(1000); printf("e-Paper display ready!\r\n"); // EPD_7IN5B_V2_Sleep(); } // 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 /** * Add an entry to the display list and send update to core 1 */ void send_display_update(const char *entry) { if (!g_display_ready) return; // Don't send if display isn't ready // Add entry to global list if (g_entry_list.count < MAX_ENTRIES) { strncpy(g_entry_list.entries[g_entry_list.count], entry, ENTRY_LENGTH - 1); g_entry_list.entries[g_entry_list.count][ENTRY_LENGTH - 1] = '\0'; g_entry_list.count++; } else { // Shift entries up and add new one at bottom for (int i = 0; i < MAX_ENTRIES - 1; i++) { strcpy(g_entry_list.entries[i], g_entry_list.entries[i + 1]); } strncpy(g_entry_list.entries[MAX_ENTRIES - 1], entry, ENTRY_LENGTH - 1); g_entry_list.entries[MAX_ENTRIES - 1][ENTRY_LENGTH - 1] = '\0'; } // Allocate message structure DisplayMessage *msg = (DisplayMessage *)malloc(sizeof(DisplayMessage)); if (msg == NULL) { printf("Failed to allocate display message!\n"); return; } // Copy entry list to message msg->entries = g_entry_list; // Setup partial refresh region (text area at top of display) msg->use_partial = true; msg->xstart = 0; msg->ystart = 50; // Start below the header msg->xend = 800; // Full width msg->yend = 480; // Full height from header onwards // Send message pointer to core 1 multicore_fifo_push_blocking((uint32_t)msg); // Wait for core 1 to complete the update multicore_fifo_pop_blocking(); } /** * Core 1 function: Runs display initialization and update handling */ void core1_display_init() { init_epaper_display(); g_display_ready = true; printf("[Core 1] Display ready, waiting for update messages...\n"); // Initialize for partial refresh on second call EPD_7IN5B_V2_Init_Part(); // Core 1 main loop: handle display updates via FIFO while (true) { // Check if there's a message from core 0 if (multicore_fifo_rvalid()) { // Read the message pointer uint32_t msg_addr = multicore_fifo_pop_blocking(); DisplayMessage *msg = (DisplayMessage *)msg_addr; printf("[Core 1] Updating display with %d entries\n", msg->entries.count); // Update the e-Paper display if (g_epd_image != NULL && g_epd_red != NULL) { UWORD imagesize = (EPD_7IN5B_V2_WIDTH / 8) * EPD_7IN5B_V2_HEIGHT; // For partial refresh, only clear the text area UWORD width = (EPD_7IN5B_V2_WIDTH / 8); UWORD y_start = msg->ystart; UWORD y_end = msg->yend; if (msg->use_partial) { // Clear only the affected region in the buffers for (UWORD y = y_start; y < y_end; y++) { for (UWORD x = 0; x < width; x++) { g_epd_image[x + y * width] = 0xFF; // White g_epd_red[x + y * width] = 0xFF; // No red } } } else { // Full clear for (UWORD i = 0; i < imagesize; i++) { g_epd_image[i] = 0xFF; g_epd_red[i] = 0xFF; } } Paint_SelectImage(g_epd_image); // Draw header Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, BLACK); // Draw all entries starting below header UWORD y_pos = 50; for (int i = 0; i < msg->entries.count; i++) { if (y_pos + 20 < 480) { // Don't draw beyond screen Paint_DrawString_EN(20, y_pos, msg->entries.entries[i], &Font16, WHITE, BLACK); y_pos += 25; // Space between entries } } // Use partial or full refresh if (msg->use_partial) { printf("[Core 1] Using partial refresh\n"); EPD_7IN5B_V2_Display_Partial(g_epd_image, msg->xstart, msg->ystart, msg->xend, msg->yend); } else { printf("[Core 1] Using full refresh\n"); EPD_7IN5B_V2_Display(g_epd_image, g_epd_red); } } // Free the message (it was allocated by core 0) free(msg); // Signal back to core 0 that update is complete multicore_fifo_push_blocking(1); } sleep_ms(10); } } void wait_for_usb_connection(DisplayManager &display) { printf("Waiting for USB host to connect...\n"); while (!stdio_usb_connected()) { sleep_ms(100); } printf("\nConnection Established! Starting Echo Session...\n"); display.refresh(">", nullptr); } void run_echo_session(DisplayManager &display) { char input_buffer[MAX_INPUT_LEN]; int buffer_index = 0; printf("--- Welcome User! ---\n"); printf("Type a command (or write help):\n"); printf("--------------------------------------------\n"); printf("> "); while (stdio_usb_connected()) { int c = getchar_timeout_us(0); if (c != PICO_ERROR_TIMEOUT) { char input_char = (char)c; if (input_char == ASCII_BACKSPACE || input_char == 127) { if (buffer_index > 0) { buffer_index--; printf("\b \b"); // update displays to reflect removed char input_buffer[buffer_index] = '\0'; display.refresh(input_buffer, g_last_echo); // send_display_update(input_buffer, g_last_echo); } } else if (input_char == '\r' || input_char == '\n') { echo_and_reset(input_buffer, &buffer_index); // Save last echoed (all caps) for display size_t i = 0; for (; input_buffer[i] != '\0' && i < sizeof(g_last_echo) - 1; ++i) { g_last_echo[i] = input_buffer[i]; } g_last_echo[i] = '\0'; // Update both displays to show cleared input and last echo display.set_last_echo(g_last_echo); display.refresh("", g_last_echo); send_display_update(g_last_echo); } else if (buffer_index < (MAX_INPUT_LEN - 1) && isprint(input_char)) { printf("%c", input_char); input_buffer[buffer_index] = input_char; buffer_index++; // update both displays with current input input_buffer[buffer_index] = '\0'; display.refresh(input_buffer, g_last_echo); // send_display_update(input_buffer, g_last_echo); } } sleep_us(100); } printf("\nHost disconnected. Ending Echo Session.\n"); } int main() { stdio_init_all(); // Launch display initialization on core 1 printf("Launching e-Paper display init on core 1...\n"); multicore_launch_core1(core1_display_init); DisplayManager display; display.init(); display.refresh("Waiting for USB/Serial", nullptr); while (true) { wait_for_usb_connection(display); run_echo_session(display); } return 0; }