Files
immich-frame/src/display_manager.cpp
cottongin 0399573edf feat: blurred letterbox, portrait pairs, overlay metadata, button fixes
- Letterbox: extend edge pixels outward with blur + fade to solid color
- Portrait: detect and pair portraits side-by-side, fallback to solo
- Metadata: add overlay mode (corner badge), time field, custom date/time
  format strings (strftime), font size 1.5x, region-aware for pairs
- Buttons: swap G9/G10 to match physical layout, double-press random
  button for deep sleep, SLP indicator drawn before sleep
- Boot: remove redundant 'Loading photos' screen (saves 17s refresh)

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 15:53:06 -04:00

296 lines
10 KiB
C++

#include "display_manager.h"
#include "power_manager.h"
#include <M5Unified.h>
// Map 6-color palette indices to RGB565 for M5GFX
static const uint16_t PALETTE_565[6] = {
0x0000, // Black
0xFFFF, // White
0xF800, // Red (approximate)
0x07E0, // Green (approximate)
0x001F, // Blue (approximate)
0xFFE0 // Yellow (approximate)
};
void DisplayManager::begin(PowerManager& power) {
_power = &power;
_display = &M5.Display;
// Ensure no automatic e-ink refreshes happen
_display->setAutoDisplay(false);
// Set rotation for landscape (device physically rotated)
_display->setRotation(3);
Serial.printf("[display] Initialized: %dx%d, rotation=%d\n",
_display->width(), _display->height(), _display->getRotation());
}
void DisplayManager::showSetupScreen() {
_power->enableEPDPower();
// Batch all drawing — nothing hits the panel until display() at the end
_display->startWrite();
_display->fillScreen(TFT_WHITE);
// Generate QR code for WiFi auto-connect
const char* qrData = "WIFI:T:nopass;S:PaperColor-Setup;;";
QRCode qrcode;
uint8_t qrcodeData[qrcode_getBufferSize(6)];
qrcode_initText(&qrcode, qrcodeData, 6, ECC_LOW, qrData);
// Draw QR code centered vertically, left-of-center horizontally
int qrSize = qrcode.size;
int scale = 4;
int qrPixels = qrSize * scale;
int qrX = (DISPLAY_WIDTH / 3) - (qrPixels / 2);
int qrY = (DISPLAY_HEIGHT - qrPixels) / 2;
for (int y = 0; y < qrSize; y++) {
for (int x = 0; x < qrSize; x++) {
uint16_t color = qrcode_getModule(&qrcode, x, y) ? TFT_BLACK : TFT_WHITE;
_display->fillRect(qrX + x * scale, qrY + y * scale, scale, scale, color);
}
}
// Text instructions to the right of QR code
int textX = DISPLAY_WIDTH / 2 + 40;
_display->setTextColor(TFT_BLACK);
_display->setTextDatum(middle_left);
_display->setTextSize(2);
_display->drawString("PaperColor", textX, DISPLAY_HEIGHT / 2 - 60);
_display->setTextSize(1);
_display->drawString("Scan QR to connect", textX, DISPLAY_HEIGHT / 2 - 20);
_display->drawString("or join WiFi:", textX, DISPLAY_HEIGHT / 2 + 10);
_display->setTextSize(1.5);
_display->drawString("PaperColor-Setup", textX, DISPLAY_HEIGHT / 2 + 40);
_display->setTextSize(1);
_display->drawString("Then open:", textX, DISPLAY_HEIGHT / 2 + 70);
_display->drawString("192.168.4.1", textX, DISPLAY_HEIGHT / 2 + 95);
_display->endWrite();
// Single e-ink refresh — the only panel update during boot
triggerRefresh();
_power->disableEPDPower();
}
void DisplayManager::showImage(const ProcessedImage& img) {
if (!img.valid || img.framebuffer == nullptr) {
Serial.println("[display] Invalid image — skipping");
return;
}
_power->enableEPDPower();
Serial.println("[display] Writing framebuffer to e-ink...");
// Write pixel by pixel using palette-mapped colors
_display->startWrite();
for (uint16_t y = 0; y < img.height; y++) {
for (uint16_t x = 0; x < img.width; x++) {
uint8_t colorIdx = img.framebuffer[y * img.width + x];
_display->writePixel(x, y, PALETTE_565[colorIdx]);
}
}
_display->endWrite();
// Do NOT refresh here — caller should add metadata overlay then call refresh()
}
void DisplayManager::showMessage(const char* title, const char* body) {
_power->enableEPDPower();
_display->startWrite();
_display->fillScreen(TFT_WHITE);
_display->setTextColor(TFT_BLACK);
_display->setTextDatum(middle_center);
_display->setTextSize(2);
_display->drawString(title, DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2 - 30);
_display->setTextSize(1);
_display->drawString(body, DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2 + 20);
_display->endWrite();
triggerRefresh();
_power->disableEPDPower();
}
void DisplayManager::showMetadata(const AssetInfo& info, uint8_t metaFlags, MetaPosition pos,
const String& dateFmt, const String& timeFmt) {
showMetadataInRegion(info, metaFlags, pos, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, dateFmt, timeFmt);
}
void DisplayManager::showMetadataInRegion(const AssetInfo& info, uint8_t metaFlags, MetaPosition pos,
uint16_t regionX, uint16_t regionY,
uint16_t regionW, uint16_t regionH,
const String& dateFmt, const String& timeFmt) {
if (metaFlags == 0) return;
// Build metadata string
String metaText = "";
// Parse ISO datetime (YYYY-MM-DDTHH:MM:SS) into struct tm for strftime
if (((metaFlags & META_DATE) || (metaFlags & META_TIME)) && info.dateTime.length() >= 19) {
struct tm t = {};
sscanf(info.dateTime.c_str(), "%d-%d-%dT%d:%d:%d",
&t.tm_year, &t.tm_mon, &t.tm_mday,
&t.tm_hour, &t.tm_min, &t.tm_sec);
t.tm_year -= 1900;
t.tm_mon -= 1;
char buf[64];
if ((metaFlags & META_DATE) && (metaFlags & META_TIME)) {
String combinedFmt = dateFmt + " " + timeFmt;
strftime(buf, sizeof(buf), combinedFmt.c_str(), &t);
metaText += buf;
} else if (metaFlags & META_DATE) {
strftime(buf, sizeof(buf), dateFmt.c_str(), &t);
metaText += buf;
} else {
strftime(buf, sizeof(buf), timeFmt.c_str(), &t);
metaText += buf;
}
} else if ((metaFlags & META_DATE) && info.dateTime.length() >= 10) {
// Fallback if datetime is too short for full parse
metaText += info.dateTime.substring(0, 10);
}
if ((metaFlags & META_LOCATION) && info.city.length() > 0) {
if (metaText.length() > 0) metaText += " | ";
metaText += info.city;
}
if ((metaFlags & META_PEOPLE) && !info.people.empty()) {
if (metaText.length() > 0) metaText += " | ";
for (size_t i = 0; i < info.people.size(); i++) {
if (i > 0) metaText += ", ";
metaText += info.people[i];
}
}
if ((metaFlags & META_ALBUM)) {
// Album name would need to be passed separately
}
if ((metaFlags & META_CAMERA) && info.camera.length() > 0) {
if (metaText.length() > 0) metaText += " | ";
metaText += info.camera;
}
if (metaText.length() == 0) return;
_display->setTextSize(1.5f);
_display->setTextColor(TFT_WHITE);
switch (pos) {
case MetaPosition::CaptionBottom:
case MetaPosition::CaptionTop: {
// Bar spanning the region width, positioned at region top or bottom
uint16_t barH = 36;
uint16_t barY = (pos == MetaPosition::CaptionTop)
? regionY
: regionY + regionH - barH;
_display->fillRect(regionX, barY, regionW, barH, TFT_BLACK);
_display->setTextDatum(middle_center);
_display->drawString(metaText.c_str(), regionX + regionW / 2, barY + barH / 2);
break;
}
case MetaPosition::OverlayTopLeft:
case MetaPosition::OverlayTopRight:
case MetaPosition::OverlayBottomLeft:
case MetaPosition::OverlayBottomRight: {
static constexpr uint16_t MARGIN = 8;
static constexpr uint16_t PAD_X = 8;
static constexpr uint16_t PAD_Y = 5;
static constexpr uint16_t TEXT_H = 20;
uint16_t textW = _display->textWidth(metaText.c_str());
uint16_t badgeW = min((uint16_t)(textW + PAD_X * 2), regionW);
uint16_t badgeH = TEXT_H + PAD_Y * 2;
uint16_t bx, by;
switch (pos) {
case MetaPosition::OverlayTopLeft:
bx = regionX + MARGIN;
by = regionY + MARGIN;
break;
case MetaPosition::OverlayTopRight:
bx = regionX + regionW - badgeW - MARGIN;
by = regionY + MARGIN;
break;
case MetaPosition::OverlayBottomLeft:
bx = regionX + MARGIN;
by = regionY + regionH - badgeH - MARGIN;
break;
case MetaPosition::OverlayBottomRight:
bx = regionX + regionW - badgeW - MARGIN;
by = regionY + regionH - badgeH - MARGIN;
break;
default:
bx = regionX + MARGIN;
by = regionY + MARGIN;
break;
}
_display->fillRect(bx, by, badgeW, badgeH, TFT_BLACK);
_display->setTextDatum(middle_left);
_display->drawString(metaText.c_str(), bx + PAD_X, by + badgeH / 2);
break;
}
}
}
void DisplayManager::showSleepIndicator(MetaPosition metaPos) {
_power->enableEPDPower();
// Pick a corner opposite to metadata position
static constexpr uint16_t MARGIN = 6;
uint16_t tx, ty;
lgfx::textdatum_t datum;
switch (metaPos) {
case MetaPosition::OverlayTopLeft:
case MetaPosition::CaptionTop:
tx = DISPLAY_WIDTH - MARGIN;
ty = DISPLAY_HEIGHT - MARGIN;
datum = bottom_right;
break;
case MetaPosition::OverlayTopRight:
tx = MARGIN;
ty = DISPLAY_HEIGHT - MARGIN;
datum = bottom_left;
break;
case MetaPosition::OverlayBottomLeft:
tx = DISPLAY_WIDTH - MARGIN;
ty = MARGIN;
datum = top_right;
break;
case MetaPosition::OverlayBottomRight:
case MetaPosition::CaptionBottom:
default:
tx = MARGIN;
ty = MARGIN;
datum = top_left;
break;
}
_display->setTextSize(1);
_display->setTextColor(TFT_BLACK);
_display->setTextDatum(datum);
_display->drawString("SLP", tx, ty);
}
void DisplayManager::refresh() {
unsigned long start = millis();
triggerRefresh();
unsigned long elapsed = millis() - start;
Serial.printf("[display] Refresh complete in %lu ms\n", elapsed);
_power->disableEPDPower();
}
void DisplayManager::triggerRefresh() {
// M5GFX handles e-ink refresh internally when using the EPD panel driver
// The display() call triggers the actual e-ink refresh cycle (10-20s)
_display->display();
}
uint16_t DisplayManager::paletteToColor565(uint8_t index) {
if (index >= DISPLAY_COLORS) return 0;
return PALETTE_565[index];
}