Files
eink-dairy/hello_usb.cpp
2025-11-26 13:03:43 -05:00

277 lines
9.0 KiB
C++

/*
* 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 <stdio.h>
#include <ctype.h>
#include <cstring>
#include <stdlib.h>
#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
// 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");
// 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 "Hello World" on the display
printf("Drawing\r\n");
Paint_DrawString_EN(10, 10, "Hello World!", &Font24, WHITE, BLACK);
Paint_DrawString_EN(10, 60, "7.5\" e-Paper Initialized", &Font16, WHITE, BLACK);
// 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(2000);
printf("e-Paper display ready!\r\n");
// EPD_7IN5B_V2_Sleep();
}
void update_epaper_display(const char *line1, const char *line2) {
if (g_epd_image == NULL || g_epd_red == NULL) return;
// Clear and prepare buffers
UWORD imagesize = (EPD_7IN5B_V2_WIDTH / 8) * EPD_7IN5B_V2_HEIGHT;
for (UWORD i = 0; i < imagesize; i++) {
g_epd_red[i] = 0xFF; // No red pixels
}
Paint_SelectImage(g_epd_image);
Paint_Clear(WHITE);
if (line1) {
Paint_DrawString_EN(50, 100, (char *)line1, &Font16, BLACK, WHITE);
}
if (line2) {
Paint_DrawString_EN(50, 150, (char *)line2, &Font16, BLACK, WHITE);
}
EPD_7IN5B_V2_Display(g_epd_image, g_epd_red);
}
// 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
/**
* 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()) {
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;
}