Files
eink-dairy/hello_usb.cpp
2025-11-26 12:29:21 -05:00

188 lines
5.8 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 <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
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
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);
// Initialize e-Paper display
init_epaper_display();
}
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 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);
// 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 OLED to show cleared input and last echo
display.set_last_echo(g_last_echo);
display.refresh("", 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
input_buffer[buffer_index] = '\0';
display.refresh(input_buffer, g_last_echo);
}
}
sleep_us(100);
}
printf("\nHost disconnected. Ending Echo Session.\n");
}
int main() {
stdio_init_all();
DisplayManager display;
display.init();
display.refresh("Waiting for USB/Serial", nullptr);
while (true) {
wait_for_usb_connection(display);
run_echo_session(display);
}
return 0;
}