diff --git a/CMakeLists.txt b/CMakeLists.txt index 92543c7..21b19eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,7 @@ if (TARGET tinyusb_device) ) # pull in common dependencies - target_link_libraries(hello_usb pico_stdlib pico_ssd1306 hardware_i2c Config ePaper GUI Fonts hardware_spi) + target_link_libraries(hello_usb pico_stdlib pico_ssd1306 hardware_i2c Config ePaper GUI Fonts hardware_spi pico_multicore) # enable usb output, disable uart output pico_enable_stdio_usb(hello_usb 1) diff --git a/hello_usb.cpp b/hello_usb.cpp index b6d76f6..4ef2a71 100644 --- a/hello_usb.cpp +++ b/hello_usb.cpp @@ -6,6 +6,7 @@ #include "pico/stdlib.h" #include "pico/time.h" // Needed for time_us_64() +#include "pico/multicore.h" #include #include #include @@ -28,6 +29,15 @@ static char g_last_echo[128] = ""; static UBYTE *g_epd_image = NULL; // Black image buffer static UBYTE *g_epd_red = NULL; // Red image buffer +// Synchronization flag for multicore initialization +static volatile bool g_display_ready = false; + +// Display update message structure for inter-core communication +typedef struct { + char line1[64]; + char line2[64]; +} DisplayMessage; + void init_epaper_display() { printf("Initializing 7.5\" e-Paper display (B V2)...\r\n"); @@ -111,6 +121,83 @@ void update_epaper_display(const char *line1, const char *line2) { // ASCII code for Backspace (often sent as 0x08) #define ASCII_BACKSPACE 8 +/** + * Send a display update message to core 1 via FIFO + */ +void send_display_update(const char *line1, const char *line2) { + if (!g_display_ready) return; // Don't send if display isn't ready + + // Allocate message structure + DisplayMessage *msg = (DisplayMessage *)malloc(sizeof(DisplayMessage)); + if (msg == NULL) { + printf("Failed to allocate display message!\n"); + return; + } + + // Copy strings into message + strncpy(msg->line1, line1 ? line1 : "", sizeof(msg->line1) - 1); + msg->line1[sizeof(msg->line1) - 1] = '\0'; + + strncpy(msg->line2, line2 ? line2 : "", sizeof(msg->line2) - 1); + msg->line2[sizeof(msg->line2) - 1] = '\0'; + + // 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"); + + // 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: '%s' / '%s'\n", msg->line1, msg->line2); + + // 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 (UWORD i = 0; i < imagesize; i++) { + g_epd_red[i] = 0xFF; + } + + // Paint_SelectImage(g_epd_image); + // Paint_Clear(WHITE); + + if (msg->line1[0] != '\0') { + Paint_DrawString_EN(50, 100, msg->line1, &Font16, BLACK, WHITE); + } + if (msg->line2[0] != '\0') { + Paint_DrawString_EN(50, 150, msg->line2, &Font16, BLACK, WHITE); + } + + 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()) { @@ -118,9 +205,6 @@ void wait_for_usb_connection(DisplayManager &display) { } printf("\nConnection Established! Starting Echo Session...\n"); display.refresh(">", nullptr); - - // Initialize e-Paper display - init_epaper_display(); } void run_echo_session(DisplayManager &display) { @@ -140,9 +224,10 @@ void run_echo_session(DisplayManager &display) { if (buffer_index > 0) { buffer_index--; printf("\b \b"); - // update display to reflect removed char + // 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); @@ -152,16 +237,18 @@ void run_echo_session(DisplayManager &display) { g_last_echo[i] = input_buffer[i]; } g_last_echo[i] = '\0'; - // Update OLED to show cleared input and last echo + // 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 OLED with current input + // 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); @@ -172,7 +259,9 @@ void run_echo_session(DisplayManager &display) { 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();