2026-08-03 10:46:23 -04:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <M5Unified.h>
|
2026-08-03 11:00:02 -04:00
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
|
#include <freertos/task.h>
|
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
|
|
2026-08-03 10:46:23 -04:00
|
|
|
#include "config.h"
|
2026-08-03 10:49:13 -04:00
|
|
|
#include "settings.h"
|
2026-08-03 10:49:58 -04:00
|
|
|
#include "wifi_manager.h"
|
2026-08-03 10:50:32 -04:00
|
|
|
#include "power_manager.h"
|
2026-08-03 10:51:29 -04:00
|
|
|
#include "button_handler.h"
|
2026-08-03 11:00:02 -04:00
|
|
|
#include "immich_client.h"
|
|
|
|
|
#include "photo_queue.h"
|
|
|
|
|
#include "image_pipeline.h"
|
|
|
|
|
#include "display_manager.h"
|
|
|
|
|
#include "web_server.h"
|
2026-08-03 10:49:13 -04:00
|
|
|
|
2026-08-03 11:00:02 -04:00
|
|
|
// Global instances
|
2026-08-03 10:49:13 -04:00
|
|
|
SettingsManager settingsManager;
|
2026-08-03 10:49:58 -04:00
|
|
|
WiFiManager wifiManager;
|
2026-08-03 10:50:32 -04:00
|
|
|
PowerManager powerManager;
|
2026-08-03 10:51:29 -04:00
|
|
|
ButtonHandler buttonHandler;
|
2026-08-03 11:00:02 -04:00
|
|
|
ImmichClient immichClient;
|
|
|
|
|
PhotoQueue photoQueue;
|
|
|
|
|
ImagePipeline imagePipeline;
|
|
|
|
|
DisplayManager displayManager;
|
|
|
|
|
AppWebServer webServer;
|
|
|
|
|
|
|
|
|
|
// Shared state
|
|
|
|
|
SemaphoreHandle_t stateMutex;
|
|
|
|
|
volatile bool slideshowPlaying = true;
|
|
|
|
|
volatile bool refreshRequested = false;
|
|
|
|
|
volatile bool randomRequested = false;
|
|
|
|
|
volatile unsigned long lastRefreshTime = 0;
|
|
|
|
|
|
|
|
|
|
// Task handles
|
|
|
|
|
TaskHandle_t displayTaskHandle = nullptr;
|
|
|
|
|
|
|
|
|
|
void displayTask(void* param) {
|
|
|
|
|
Serial.println("[display_task] Started on Core 1");
|
|
|
|
|
|
|
|
|
|
// Initial sync
|
|
|
|
|
if (wifiManager.isConnected()) {
|
|
|
|
|
Settings s = settingsManager.get();
|
|
|
|
|
immichClient.begin(s.immich_url, s.immich_key);
|
|
|
|
|
photoQueue.begin(immichClient, settingsManager);
|
|
|
|
|
|
|
|
|
|
if (photoQueue.sync()) {
|
|
|
|
|
Serial.printf("[display_task] Queue ready: %d photos\n", photoQueue.size());
|
|
|
|
|
} else {
|
|
|
|
|
displayManager.showMessage("No Photos", "Select albums in web UI");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
unsigned long now = millis();
|
|
|
|
|
Settings s = settingsManager.get();
|
|
|
|
|
unsigned long intervalMs = s.interval_min * 60000UL;
|
|
|
|
|
|
|
|
|
|
bool shouldRefresh = false;
|
|
|
|
|
bool wantsRandom = false;
|
|
|
|
|
|
|
|
|
|
if (xSemaphoreTake(stateMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
|
|
|
if (refreshRequested) {
|
|
|
|
|
shouldRefresh = true;
|
|
|
|
|
refreshRequested = false;
|
|
|
|
|
} else if (randomRequested) {
|
|
|
|
|
shouldRefresh = true;
|
|
|
|
|
wantsRandom = true;
|
|
|
|
|
randomRequested = false;
|
|
|
|
|
} else if (slideshowPlaying && (now - lastRefreshTime >= intervalMs)) {
|
|
|
|
|
shouldRefresh = true;
|
|
|
|
|
}
|
|
|
|
|
xSemaphoreGive(stateMutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Periodic re-sync
|
|
|
|
|
if (photoQueue.needsResync() && wifiManager.isConnected()) {
|
|
|
|
|
photoQueue.sync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldRefresh && photoQueue.size() > 0 && wifiManager.isConnected()) {
|
|
|
|
|
// Get next photo ID
|
|
|
|
|
String assetId;
|
|
|
|
|
if (wantsRandom) {
|
|
|
|
|
assetId = photoQueue.random();
|
|
|
|
|
} else {
|
|
|
|
|
assetId = photoQueue.next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (assetId.length() > 0) {
|
|
|
|
|
Serial.printf("[display_task] Loading asset: %s\n", assetId.c_str());
|
|
|
|
|
|
|
|
|
|
// Fetch asset info for portrait detection and metadata
|
|
|
|
|
AssetInfo info = immichClient.fetchAssetInfo(assetId);
|
|
|
|
|
|
|
|
|
|
// Download photo
|
|
|
|
|
uint8_t* jpegBuf = nullptr;
|
|
|
|
|
size_t jpegSize = 0;
|
|
|
|
|
bool downloaded = immichClient.downloadAsset(
|
|
|
|
|
assetId, s.img_quality, &jpegBuf, &jpegSize);
|
|
|
|
|
|
|
|
|
|
if (downloaded && jpegBuf != nullptr) {
|
|
|
|
|
ProcessedImage img;
|
|
|
|
|
|
|
|
|
|
if (info.isPortrait) {
|
|
|
|
|
// Try to find a portrait pair
|
|
|
|
|
String pairId = photoQueue.findPortraitPair(
|
|
|
|
|
settingsManager.get().queue_cursor);
|
|
|
|
|
if (pairId.length() > 0) {
|
|
|
|
|
AssetInfo pairInfo = immichClient.fetchAssetInfo(pairId);
|
|
|
|
|
if (pairInfo.isPortrait) {
|
|
|
|
|
uint8_t* jpeg2Buf = nullptr;
|
|
|
|
|
size_t jpeg2Size = 0;
|
|
|
|
|
if (immichClient.downloadAsset(pairId, s.img_quality,
|
|
|
|
|
&jpeg2Buf, &jpeg2Size)) {
|
|
|
|
|
img = imagePipeline.processPortraitPair(
|
|
|
|
|
jpegBuf, jpegSize, jpeg2Buf, jpeg2Size);
|
|
|
|
|
free(jpeg2Buf);
|
|
|
|
|
} else {
|
|
|
|
|
img = imagePipeline.process(jpegBuf, jpegSize);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
img = imagePipeline.process(jpegBuf, jpegSize);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
img = imagePipeline.process(jpegBuf, jpegSize);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
img = imagePipeline.process(jpegBuf, jpegSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(jpegBuf);
|
|
|
|
|
|
|
|
|
|
if (img.valid) {
|
|
|
|
|
displayManager.showImage(img);
|
|
|
|
|
// Show metadata overlay if enabled
|
|
|
|
|
if (s.meta_flags != 0) {
|
|
|
|
|
displayManager.showMetadata(info, s.meta_flags, s.meta_pos);
|
|
|
|
|
}
|
|
|
|
|
imagePipeline.freeImage(img);
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("[display_task] Image processing failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (xSemaphoreTake(stateMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
|
|
|
lastRefreshTime = millis();
|
|
|
|
|
xSemaphoreGive(stateMutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Yield — check every second
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void webActionCallback(const String& action) {
|
|
|
|
|
if (xSemaphoreTake(stateMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
|
|
|
|
if (action == "next") {
|
|
|
|
|
refreshRequested = true;
|
|
|
|
|
} else if (action == "random") {
|
|
|
|
|
randomRequested = true;
|
|
|
|
|
} else if (action == "pause") {
|
|
|
|
|
slideshowPlaying = false;
|
|
|
|
|
} else if (action == "play") {
|
|
|
|
|
slideshowPlaying = true;
|
|
|
|
|
}
|
|
|
|
|
xSemaphoreGive(stateMutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-03 10:46:23 -04:00
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
auto cfg = M5.config();
|
2026-08-03 11:31:36 -04:00
|
|
|
cfg.clear_display = false; // Don't clear/refresh e-ink on init
|
2026-08-03 10:46:23 -04:00
|
|
|
M5.begin(cfg);
|
|
|
|
|
|
2026-08-03 11:31:36 -04:00
|
|
|
// Prevent any automatic e-ink refreshes until we explicitly call display()
|
|
|
|
|
M5.Display.setAutoDisplay(false);
|
|
|
|
|
|
2026-08-03 10:46:23 -04:00
|
|
|
Serial.begin(115200);
|
2026-08-03 11:00:02 -04:00
|
|
|
Serial.println("[main] Immich Frame v1.0 booting...");
|
|
|
|
|
|
|
|
|
|
// Create state mutex
|
|
|
|
|
stateMutex = xSemaphoreCreateMutex();
|
2026-08-03 10:46:23 -04:00
|
|
|
|
2026-08-03 11:00:02 -04:00
|
|
|
// Initialize subsystems
|
2026-08-03 10:50:32 -04:00
|
|
|
powerManager.begin();
|
2026-08-03 10:49:13 -04:00
|
|
|
settingsManager.begin();
|
2026-08-03 10:49:58 -04:00
|
|
|
wifiManager.begin(settingsManager);
|
2026-08-03 10:51:29 -04:00
|
|
|
buttonHandler.begin(powerManager, settingsManager);
|
2026-08-03 11:00:02 -04:00
|
|
|
displayManager.begin(powerManager);
|
|
|
|
|
|
|
|
|
|
if (!wifiManager.isAPMode()) {
|
|
|
|
|
// Station mode — set up Immich and web server
|
|
|
|
|
Settings s = settingsManager.get();
|
|
|
|
|
immichClient.begin(s.immich_url, s.immich_key);
|
|
|
|
|
photoQueue.begin(immichClient, settingsManager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start web server (works in both AP and station modes)
|
|
|
|
|
webServer.begin(settingsManager, immichClient, powerManager, wifiManager);
|
|
|
|
|
webServer.setActionCallback(webActionCallback);
|
|
|
|
|
|
2026-08-03 11:25:41 -04:00
|
|
|
// Single display update after all init is complete
|
2026-08-03 11:00:02 -04:00
|
|
|
if (wifiManager.isAPMode()) {
|
2026-08-03 11:25:41 -04:00
|
|
|
displayManager.showSetupScreen();
|
2026-08-03 11:00:02 -04:00
|
|
|
} else {
|
|
|
|
|
displayManager.showMessage("Immich Frame", "Loading photos...");
|
|
|
|
|
|
|
|
|
|
// Create display task on Core 1
|
|
|
|
|
xTaskCreatePinnedToCore(displayTask, "display", 32768, nullptr, 1,
|
|
|
|
|
&displayTaskHandle, 1);
|
|
|
|
|
|
2026-08-03 11:25:41 -04:00
|
|
|
// Enable light sleep only in station mode (not supported in AP mode)
|
|
|
|
|
powerManager.enableLightSleep();
|
|
|
|
|
}
|
2026-08-03 10:49:13 -04:00
|
|
|
|
|
|
|
|
Serial.println("[main] Boot complete");
|
2026-08-03 10:46:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
M5.update();
|
2026-08-03 10:50:32 -04:00
|
|
|
powerManager.updateBatteryLED();
|
2026-08-03 10:51:29 -04:00
|
|
|
|
2026-08-03 11:00:02 -04:00
|
|
|
// Handle button events
|
2026-08-03 10:51:29 -04:00
|
|
|
ButtonEvent event = buttonHandler.poll();
|
|
|
|
|
switch (event) {
|
|
|
|
|
case ButtonEvent::NextPhoto:
|
2026-08-03 11:00:02 -04:00
|
|
|
if (xSemaphoreTake(stateMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
|
|
|
|
refreshRequested = true;
|
|
|
|
|
xSemaphoreGive(stateMutex);
|
|
|
|
|
}
|
2026-08-03 10:51:29 -04:00
|
|
|
break;
|
|
|
|
|
case ButtonEvent::RandomPhoto:
|
2026-08-03 11:00:02 -04:00
|
|
|
if (xSemaphoreTake(stateMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
|
|
|
|
randomRequested = true;
|
|
|
|
|
xSemaphoreGive(stateMutex);
|
|
|
|
|
}
|
2026-08-03 10:51:29 -04:00
|
|
|
break;
|
|
|
|
|
case ButtonEvent::PlayPause:
|
2026-08-03 11:00:02 -04:00
|
|
|
if (xSemaphoreTake(stateMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
|
|
|
|
slideshowPlaying = !slideshowPlaying;
|
|
|
|
|
Serial.printf("[main] Slideshow: %s\n", slideshowPlaying ? "playing" : "paused");
|
|
|
|
|
xSemaphoreGive(stateMutex);
|
|
|
|
|
}
|
2026-08-03 10:51:29 -04:00
|
|
|
break;
|
|
|
|
|
case ButtonEvent::DeepSleep:
|
|
|
|
|
case ButtonEvent::FactoryReset:
|
2026-08-03 11:00:02 -04:00
|
|
|
break;
|
2026-08-03 10:51:29 -04:00
|
|
|
case ButtonEvent::None:
|
|
|
|
|
break;
|
|
|
|
|
default: {
|
|
|
|
|
ButtonEvent unreachable = event;
|
|
|
|
|
(void)unreachable;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-03 11:00:02 -04:00
|
|
|
delay(10);
|
2026-08-03 10:46:23 -04:00
|
|
|
}
|