refactoring into multiple files, not tested on hardware

This commit is contained in:
Adolfo Reyna
2025-12-09 18:41:03 -05:00
parent bc0e082b89
commit 47956bf64c
8 changed files with 552 additions and 439 deletions

247
epaper_manager.cpp Normal file
View File

@@ -0,0 +1,247 @@
#include "epaper_manager.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// e-Paper library includes
extern "C" {
#include "DEV_Config.h"
#include "EPD_7in5b_V2.h"
#include "GUI_Paint.h"
}
// 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 15
#define ENTRY_LENGTH 64
typedef struct {
char entries[MAX_ENTRIES][ENTRY_LENGTH];
int count;
} EntryList;
static EntryList g_entry_list = {{}, 0};
static bool g_force_full_refresh = false; // Flag to force full refresh when list is full
// 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;
static 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");
// 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
EPD_7IN5B_V2_Init_Part();
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
Paint_SelectImage(g_epd_red);
printf("Drawing header\r\n");
Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, RED);
// Display the image with both black and red buffers
EPD_7IN5B_V2_Display_Partial(g_epd_red, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
printf("e-Paper display ready!\r\n");
}
/**
* Core 1 function: Runs display initialization and update handling
*/
static 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;
// Drain FIFO to get the latest message
while (multicore_fifo_rvalid()) {
printf("[Core 1] Skipping intermediate update\n");
free(msg); // Free the stale message
msg_addr = multicore_fifo_pop_blocking();
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) {
// For partial refresh, only clear the text area
UWORD y_start = msg->ystart;
UWORD y_end = msg->yend;
Paint_SelectImage(g_epd_image);
Paint_Clear(WHITE);
// Calculate offset for partial refresh
UBYTE *image_ptr = g_epd_image + (msg->ystart * (EPD_7IN5B_V2_WIDTH / 8));
if(!msg->use_partial){
// for loop 5 times display white partial
int i = 0;
for(i = 0; i < 1; i++){
EPD_7IN5B_V2_Display_Partial(image_ptr, msg->xstart, msg->ystart, msg->xend, msg->yend);
}
}
// Draw all entries starting below header
UWORD y_pos = 0;
// paint only the last 10 entries
int start_index = msg->entries.count > 10 ? msg->entries.count - 10 : 0;
for (int i = start_index; i < msg->entries.count; i++) {
if (y_pos + 25 < 480) { //Don't draw beyond screen
Paint_DrawString_EN(20, 50 + i*25, msg->entries.entries[i], &Font16, WHITE, BLACK);
y_pos += 25; // Space between entries
} else {
printf("Skipping entry to avoid overflow\n");
}
}
// Use partial or full refresh
printf("[Core 1] Using partial refresh\n");
EPD_7IN5B_V2_Display_Partial(image_ptr, msg->xstart, msg->ystart, msg->xend, msg->yend);
}
// Free the message (it was allocated by core 0)
free(msg);
}
sleep_ms(10);
}
}
void epaper_start_background_thread() {
printf("Launching e-Paper display init on core 1...\n");
multicore_launch_core1(core1_display_init);
}
void epaper_send_update(const char *entry, bool finish_line) {
if (!g_display_ready) return; // Don't send if display isn't ready
// Check if list is at capacity
if (g_entry_list.count >= MAX_ENTRIES) {
// List is full - clear all entries and start fresh
printf("[Core 0] List is FULL - clearing all entries and starting fresh\n");
g_entry_list.count = 0;
g_force_full_refresh = true;
}
// Update the current entry (at g_entry_list.count)
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';
// 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;
// We want to show the current line being edited, so we treat count as count + 1 for display purposes
msg->entries.count = g_entry_list.count + 1;
// Decide whether to use partial or full refresh
msg->use_partial = !g_force_full_refresh;
if (g_force_full_refresh) {
printf("[Core 0] Forcing full refresh this update\n");
g_force_full_refresh = false; // Reset flag after setting it in message
// Full text area refresh
msg->xstart = 0;
msg->ystart = 50; // Start below the header
msg->xend = 800; // Full width
msg->yend = 480; // Full height from header onwards
} else {
// Partial refresh of ONLY the current line
msg->xstart = 0;
msg->ystart = 50 + (g_entry_list.count * 25);
msg->xend = 800;
msg->yend = msg->ystart + 25;
}
// Send message pointer to core 1 if FIFO has space
if (multicore_fifo_wready()) {
multicore_fifo_push_blocking((uint32_t)msg);
} else {
printf("[Core 0] FIFO full, skipping display update\n");
free(msg);
}
// If finishing the line, increment count for next time
if (finish_line) {
g_entry_list.count++;
}
}
void epaper_clear() {
g_entry_list.count = 0;
memset(g_entry_list.entries, 0, sizeof(g_entry_list.entries));
g_force_full_refresh = true;
epaper_send_update("", false);
}
void epaper_force_refresh() {
g_force_full_refresh = true;
epaper_send_update("", false);
}