Refactor EPD code to EPDManager class

This commit is contained in:
Djuri 2025-01-05 22:11:53 +01:00
parent d023643090
commit a6a8b5a071
Signed by: djuri
GPG key ID: 61B9B2DDE5AA3AC1
11 changed files with 532 additions and 638 deletions

View file

@ -181,45 +181,42 @@ void onWebsocketBlockMessage(esp_websocket_event_data_t *event_data)
}
void processNewBlock(uint32_t newBlockHeight) {
if (newBlockHeight < currentBlockHeight)
return;
if (currentBlockHeight == newBlockHeight)
{
return;
}
currentBlockHeight = newBlockHeight;
// Serial.printf("New block found: %d\r\n", block["height"].as<uint>());
preferences.putUInt("blockHeight", currentBlockHeight);
currentBlockHeight = newBlockHeight;
lastBlockUpdate = esp_timer_get_time() / 1000000;
if (workQueue != nullptr)
{
WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0};
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
// xTaskNotifyGive(blockUpdateTaskHandle);
WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0};
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
}
if (ScreenHandler::getCurrentScreen() != SCREEN_BLOCK_HEIGHT &&
preferences.getBool("stealFocus", DEFAULT_STEAL_FOCUS))
{
if (ScreenHandler::getCurrentScreen() != SCREEN_BLOCK_HEIGHT &&
preferences.getBool("stealFocus", DEFAULT_STEAL_FOCUS))
{
uint64_t timerPeriod = 0;
if (isTimerActive())
{
// store timer periode before making inactive to prevent artifacts
timerPeriod = getTimerSeconds();
esp_timer_stop(screenRotateTimer);
timerPeriod = getTimerSeconds();
esp_timer_stop(screenRotateTimer);
}
ScreenHandler::setCurrentScreen(SCREEN_BLOCK_HEIGHT);
if (timerPeriod > 0)
{
esp_timer_start_periodic(screenRotateTimer,
esp_timer_start_periodic(screenRotateTimer,
timerPeriod * usPerSecond);
}
vTaskDelay(pdMS_TO_TICKS(315*NUM_SCREENS)); // Extra delay because of screen switching
}
}
if (preferences.getBool("ledFlashOnUpd", DEFAULT_LED_FLASH_ON_UPD))
{
if (preferences.getBool("ledFlashOnUpd", DEFAULT_LED_FLASH_ON_UPD))
{
vTaskDelay(pdMS_TO_TICKS(250)); // Wait until screens are updated
getLedHandler().queueEffect(LED_FLASH_BLOCK_NOTIFY);
}
}
}

View file

@ -48,7 +48,7 @@ void setup()
setupPreferences();
setupHardware();
setupDisplays();
EPDManager::getInstance().initialize();
if (preferences.getBool("ledTestOnPower", DEFAULT_LED_TEST_ON_POWER))
{
auto& ledHandler = getLedHandler();
@ -115,7 +115,7 @@ void setup()
ButtonHandler::setup();
setupOTA();
waitUntilNoneBusy();
EPDManager::getInstance().waitUntilNoneBusy();
#ifdef HAS_FRONTLIGHT
if (!preferences.getBool("flAlwaysOn", DEFAULT_FL_ALWAYS_ON))
@ -126,7 +126,7 @@ void setup()
}
#endif
forceFullRefresh();
EPDManager::getInstance().forceFullRefresh();
}
void setupWifi()
@ -181,8 +181,8 @@ void setupWifi()
wifiManager->getConfigPortalSSID().c_str(),
softAP_password.c_str());
// delay(6000);
setFgColor(GxEPD_BLACK);
setBgColor(GxEPD_WHITE);
EPDManager::getInstance().setForegroundColor(GxEPD_BLACK);
EPDManager::getInstance().setBackgroundColor(GxEPD_WHITE);
const String qrText = "qrWIFI:S:" + wifiManager->getConfigPortalSSID() +
";T:WPA;P:" + softAP_password.c_str() + ";;";
const String explainText = "*SSID: *\r\n" +
@ -212,58 +212,22 @@ void setupWifi()
#endif
"\r\n\r\n*FW build date:*\r\n" + formattedDate,
qrText};
setEpdContent(epdContent); });
EPDManager::getInstance().setContent(epdContent); });
wm.setSaveConfigCallback([]()
{
preferences.putBool("wifiConfigured", true);
delay(1000);
// just restart after succes
// just restart after success
ESP.restart(); });
bool ac = wm.autoConnect(softAP_SSID.c_str(), softAP_password.c_str());
// waitUntilNoneBusy();
// std::array<String, NUM_SCREENS> epdContent = {"Welcome!",
// "Bienvenidos!", "Use\r\nweb-interface\r\npara configurar", "Use\r\nla
// interfaz web\r\npara configurar", "Or
// restart\r\nwhile\r\nholding\r\n2nd button\r\r\nto start\r\n QR-config",
// "O reinicie\r\nmientras\r\n mantiene presionado\r\nel segundo
// botón\r\r\npara iniciar\r\nQR-config", ""}; setEpdContent(epdContent);
// esp_task_wdt_init(30, false);
// uint count = 0;
// while (WiFi.status() != WL_CONNECTED)
// {
// if (Serial.available() > 0)
// {
// uint8_t b = Serial.read();
// if (parse_improv_serial_byte(x_position, b, x_buffer,
// onImprovCommandCallback, onImprovErrorCallback))
// {
// x_buffer[x_position++] = b;
// }
// else
// {
// x_position = 0;
// }
// }
// count++;
// if (count > 2000000) {
// queueLedEffect(LED_EFFECT_HEARTBEAT);
// count = 0;
// }
// }
// esp_task_wdt_deinit();
// esp_task_wdt_reset();
}
setFgColor(preferences.getUInt("fgColor", isWhiteVersion() ? GxEPD_BLACK : GxEPD_WHITE));
setBgColor(preferences.getUInt("bgColor", isWhiteVersion() ? GxEPD_WHITE : GxEPD_BLACK));
EPDManager::getInstance().setForegroundColor(preferences.getUInt("fgColor", isWhiteVersion() ? GxEPD_BLACK : GxEPD_WHITE));
EPDManager::getInstance().setBackgroundColor(preferences.getUInt("bgColor", isWhiteVersion() ? GxEPD_WHITE : GxEPD_BLACK));
}
// else
// {
@ -299,8 +263,8 @@ void setupPreferences()
{
preferences.begin("btclock", false);
setFgColor(preferences.getUInt("fgColor", DEFAULT_FG_COLOR));
setBgColor(preferences.getUInt("bgColor", DEFAULT_BG_COLOR));
EPDManager::getInstance().setForegroundColor(preferences.getUInt("fgColor", DEFAULT_FG_COLOR));
EPDManager::getInstance().setBackgroundColor(preferences.getUInt("bgColor", DEFAULT_BG_COLOR));
setBlockHeight(preferences.getUInt("blockHeight", INITIAL_BLOCK_HEIGHT));
setPrice(preferences.getUInt("lastPrice", INITIAL_LAST_PRICE), CURRENCY_USD);

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,8 @@
#include <mutex>
#include <native_pin.hpp>
#include <regex>
#include <array>
#include <memory>
#include "fonts/fonts.hpp"
#include "lib/config.hpp"
@ -32,39 +34,102 @@
#include "qrcodegen.h"
#endif
typedef struct {
char dispNum;
} UpdateDisplayTaskItem;
struct UpdateDisplayTaskItem {
char dispNum;
};
void forceFullRefresh();
void setupDisplays();
void loadFonts(const String& fontName);
struct FontFamily {
GFXfont* big;
GFXfont* medium;
GFXfont* small;
};
void splitText(const uint dispNum, const String &top, const String &bottom,
bool partial);
class EPDManager {
public:
static EPDManager& getInstance();
void showDigit(const uint dispNum, char chr, bool partial, const GFXfont *font);
void showChars(const uint dispNum, const String &chars, bool partial,
const GFXfont *font);
// Delete copy constructor and assignment operator
EPDManager(const EPDManager&) = delete;
EPDManager& operator=(const EPDManager&) = delete;
extern "C" void updateDisplay(void *pvParameters) noexcept;
void updateDisplayAlt(int epdIndex);
void prepareDisplayUpdateTask(void *pvParameters);
void initialize();
void forceFullRefresh();
void loadFonts(const String& fontName);
void setContent(const std::array<String, NUM_SCREENS>& newContent, bool forceUpdate = false);
void setContent(const std::array<std::string, NUM_SCREENS>& newContent);
std::array<String, NUM_SCREENS> getCurrentContent() const;
int getBgColor();
int getFgColor();
void setBgColor(int color);
void setFgColor(int color);
int getBackgroundColor() const { return bgColor; }
int getForegroundColor() const { return fgColor; }
void setBackgroundColor(int color) { bgColor = color; }
void setForegroundColor(int color) { fgColor = color; }
void waitUntilNoneBusy();
bool renderIcon(const uint dispNum, const String &text, bool partial);
void renderText(const uint dispNum, const String &text, bool partial);
void renderQr(const uint dispNum, const String &text, bool partial);
private:
EPDManager(); // Private constructor for singleton
~EPDManager(); // Private destructor
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent,
bool forceUpdate);
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent);
void setupDisplay(uint dispNum, const GFXfont* font);
void splitText(uint dispNum, const String& top, const String& bottom, bool partial);
void showDigit(uint dispNum, char chr, bool partial, const GFXfont* font);
void showChars(uint dispNum, const String& chars, bool partial, const GFXfont* font);
bool renderIcon(uint dispNum, const String& text, bool partial);
void renderText(uint dispNum, const String& text, bool partial);
void renderQr(uint dispNum, const String& text, bool partial);
int16_t calculateDescent(const GFXfont* font);
void setEpdContent(std::array<std::string, NUM_SCREENS> newEpdContent);
static void updateDisplayTask(void* pvParameters) noexcept;
static void prepareDisplayUpdateTask(void* pvParameters);
std::array<String, NUM_SCREENS> getCurrentEpdContent();
void waitUntilNoneBusy();
// Member variables
std::array<String, NUM_SCREENS> currentContent;
std::array<String, NUM_SCREENS> content;
std::array<uint32_t, NUM_SCREENS> lastFullRefresh;
std::array<TaskHandle_t, NUM_SCREENS> tasks;
QueueHandle_t updateQueue;
FontFamily antonioFonts;
FontFamily oswaldFonts;
const GFXfont* fontSmall;
const GFXfont* fontBig;
const GFXfont* fontMedium;
const GFXfont* fontSatsymbol;
int bgColor;
int fgColor;
std::mutex updateMutex;
std::array<std::mutex, NUM_SCREENS> displayMutexes;
// Pin configurations based on board version
#ifdef IS_BTCLOCK_REV_B
static Native_Pin EPD_DC;
static std::array<Native_Pin, NUM_SCREENS> EPD_CS;
static std::array<Native_Pin, NUM_SCREENS> EPD_BUSY;
static std::array<MCP23X17_Pin, NUM_SCREENS> EPD_RESET;
#elif defined(IS_BTCLOCK_V8)
static Native_Pin EPD_DC;
static std::array<MCP23X17_Pin, NUM_SCREENS> EPD_BUSY;
static std::array<MCP23X17_Pin, NUM_SCREENS> EPD_CS;
static std::array<MCP23X17_Pin, NUM_SCREENS> EPD_RESET;
#else
static Native_Pin EPD_DC;
static std::array<Native_Pin, NUM_SCREENS> EPD_CS;
static std::array<Native_Pin, NUM_SCREENS> EPD_BUSY;
static std::array<MCP23X17_Pin, NUM_SCREENS> EPD_RESET;
#endif
// Display array
std::array<GxEPD2_BW<EPD_CLASS, EPD_CLASS::HEIGHT>, NUM_SCREENS> displays;
static constexpr size_t UPDATE_QUEUE_SIZE = 14;
static constexpr uint32_t BUSY_TIMEOUT_COUNT = 200;
static constexpr TickType_t BUSY_RETRY_DELAY = pdMS_TO_TICKS(10);
static constexpr size_t EPD_TASK_STACK_SIZE =
#ifdef IS_BTCLOCK_V8
4096
#else
2048
#endif
;
};

View file

@ -283,7 +283,7 @@ void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event)
}
ScreenHandler::setCurrentScreen(SCREEN_CUSTOM);
setEpdContent(textEpdContent);
EPDManager::getInstance().setContent(textEpdContent);
vTaskDelay(pdMS_TO_TICKS(315 * NUM_SCREENS) + pdMS_TO_TICKS(250));
if (preferences.getBool("ledFlashOnZap", DEFAULT_LED_FLASH_ON_ZAP))
{
@ -294,4 +294,20 @@ void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event)
esp_timer_start_periodic(screenRotateTimer,
timerPeriod * usPerSecond);
}
}
}
// void onNostrEvent(const String &subId, const nostr::Event &event) {
// // This is the callback that will be called when a new event is received
// if (event.kind == 9735) {
// // Parse the zap amount from the event
// uint16_t amount = parseZapAmount(event);
// if (amount > 0) {
// std::array<std::string, NUM_SCREENS> zapContent = parseZapNotify(amount, true);
// EPDManager::getInstance().setContent(zapContent);
// if (preferences.getBool("ledFlashOnUpd", DEFAULT_LED_FLASH_ON_UPD)) {
// getLedHandler().queueEffect(LED_FLASH_BLOCK_NOTIFY);
// }
// }
// }
// }

View file

@ -57,10 +57,10 @@ void onOTAProgress(unsigned int progress, unsigned int total)
void onOTAStart()
{
forceFullRefresh();
EPDManager::getInstance().forceFullRefresh();
std::array<String, NUM_SCREENS> epdContent = {"U", "P", "D", "A",
"T", "E", "!"};
setEpdContent(epdContent);
EPDManager::getInstance().setContent(epdContent);
// Stop all timers
esp_timer_stop(screenRotateTimer);
esp_timer_stop(minuteTimer);

View file

@ -203,7 +203,7 @@ void ScreenHandler::showSystemStatusScreen() {
String((int)round(ESP.getFreeHeap() / 1024)) + "/" +
(int)round(ESP.getHeapSize() / 1024);
setCurrentScreen(SCREEN_CUSTOM);
setEpdContent(sysStatusEpdContent);
EPDManager::getInstance().setContent(sysStatusEpdContent);
}
// Keep these as free functions
@ -222,7 +222,7 @@ void workerTask(void *pvParameters) {
taskEpdContent = (currentScreenValue == SCREEN_BITAXE_HASHRATE) ?
parseBitaxeHashRate(BitAxeFetch::getInstance().getHashRate()) :
parseBitaxeBestDiff(BitAxeFetch::getInstance().getBestDiff());
setEpdContent(taskEpdContent);
EPDManager::getInstance().setContent(taskEpdContent);
break;
}
@ -235,7 +235,7 @@ void workerTask(void *pvParameters) {
parseMiningPoolStatsDailyEarnings(MiningPoolStatsFetch::getInstance().getDailyEarnings(),
MiningPoolStatsFetch::getInstance().getPool()->getDailyEarningsLabel(),
*MiningPoolStatsFetch::getInstance().getPool());
setEpdContent(taskEpdContent);
EPDManager::getInstance().setContent(taskEpdContent);
break;
}
@ -256,13 +256,13 @@ void workerTask(void *pvParameters) {
preferences.getBool("mcapBigChar", DEFAULT_MCAP_BIG_CHAR));
}
setEpdContent(taskEpdContent);
EPDManager::getInstance().setContent(taskEpdContent);
break;
}
case TASK_FEE_UPDATE: {
if (currentScreenValue == SCREEN_BLOCK_FEE_RATE) {
taskEpdContent = parseBlockFees(static_cast<std::uint16_t>(getBlockMedianFee()));
setEpdContent(taskEpdContent);
EPDManager::getInstance().setContent(taskEpdContent);
}
break;
}
@ -275,7 +275,7 @@ void workerTask(void *pvParameters) {
if (currentScreenValue == SCREEN_HALVING_COUNTDOWN ||
currentScreenValue == SCREEN_BLOCK_HEIGHT) {
setEpdContent(taskEpdContent);
EPDManager::getInstance().setContent(taskEpdContent);
}
break;
}
@ -302,7 +302,7 @@ void workerTask(void *pvParameters) {
for (uint i = 1; i < NUM_SCREENS; i++) {
taskEpdContent[i] = timeString[i];
}
setEpdContent(taskEpdContent);
EPDManager::getInstance().setContent(taskEpdContent);
}
break;
@ -330,8 +330,6 @@ void setupTasks() {
xTaskCreate(taskScreenRotate, "rotateScreen", 4096, NULL, tskIDLE_PRIORITY,
&taskScreenRotateTaskHandle);
waitUntilNoneBusy();
if (findScreenIndexByValue(preferences.getUInt("currentScreen", DEFAULT_CURRENT_SCREEN)) != -1)
ScreenHandler::setCurrentScreen(preferences.getUInt("currentScreen", DEFAULT_CURRENT_SCREEN));
}

View file

@ -179,4 +179,5 @@ void HttpHelper::end(HTTPClient* http) {
http->end();
delete http;
}
}
}

View file

@ -16,6 +16,8 @@
#include <mutex>
#include <utils.hpp>
#include <array>
#include <string>
#include "defaults.hpp"
@ -118,4 +120,5 @@ private:
static WiFiClientSecure secureClient;
static bool certBundleSet;
static WiFiClient insecureClient;
};
};

View file

@ -106,6 +106,11 @@ namespace V2Notify
JsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, payload, length);
if (error) {
Serial.println(F("Error deserializing message"));
break;
}
V2Notify::handleV2Message(doc);
break;
}

View file

@ -309,7 +309,7 @@ void eventSourceUpdate() {
doc["leds"] = getLedStatusObject()["data"];
// Get current EPD content directly as array
std::array<String, NUM_SCREENS> epdContent = getCurrentEpdContent();
std::array<String, NUM_SCREENS> epdContent = EPDManager::getInstance().getCurrentContent();
// Add EPD content arrays
JsonArray data = doc["data"].to<JsonArray>();
@ -336,7 +336,7 @@ void onApiStatus(AsyncWebServerRequest *request)
JsonDocument root = getStatusObject();
// Get current EPD content directly as array
std::array<String, NUM_SCREENS> epdContent = getCurrentEpdContent();
std::array<String, NUM_SCREENS> epdContent = EPDManager::getInstance().getCurrentContent();
// Add EPD content arrays
JsonArray data = root["data"].to<JsonArray>();
@ -378,11 +378,9 @@ void onApiActionTimerRestart(AsyncWebServerRequest *request)
*/
void onApiFullRefresh(AsyncWebServerRequest *request)
{
forceFullRefresh();
std::array<String, NUM_SCREENS> newEpdContent = getCurrentEpdContent();
setEpdContent(newEpdContent, true);
EPDManager::getInstance().forceFullRefresh();
std::array<String, NUM_SCREENS> newEpdContent = EPDManager::getInstance().getCurrentContent();
EPDManager::getInstance().setContent(newEpdContent, true);
request->send(HTTP_OK);
}
@ -429,7 +427,7 @@ void onApiShowText(AsyncWebServerRequest *request)
textEpdContent[i] = t[i];
}
setEpdContent(textEpdContent);
EPDManager::getInstance().setContent(textEpdContent);
}
ScreenHandler::setCurrentScreen(SCREEN_CUSTOM);
request->send(HTTP_OK);
@ -447,7 +445,7 @@ void onApiShowTextAdvanced(AsyncWebServerRequest *request, JsonVariant &json)
i++;
}
setEpdContent(epdContent);
EPDManager::getInstance().setContent(epdContent);
ScreenHandler::setCurrentScreen(SCREEN_CUSTOM);
request->send(HTTP_OK);
@ -475,13 +473,13 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
if (inverted) {
preferences.putUInt("fgColor", GxEPD_WHITE);
preferences.putUInt("bgColor", GxEPD_BLACK);
setFgColor(GxEPD_WHITE);
setBgColor(GxEPD_BLACK);
EPDManager::getInstance().setForegroundColor(GxEPD_WHITE);
EPDManager::getInstance().setBackgroundColor(GxEPD_BLACK);
} else {
preferences.putUInt("fgColor", GxEPD_BLACK);
preferences.putUInt("bgColor", GxEPD_WHITE);
setFgColor(GxEPD_BLACK);
setBgColor(GxEPD_WHITE);
EPDManager::getInstance().setForegroundColor(GxEPD_BLACK);
EPDManager::getInstance().setBackgroundColor(GxEPD_WHITE);
}
Serial.printf("Setting invertedColor to %d\r\n", inverted);
settingsChanged = true;
@ -680,7 +678,7 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
JsonDocument root;
root["numScreens"] = NUM_SCREENS;
root["invertedColor"] = preferences.getBool("invertedColor", getFgColor() == GxEPD_WHITE);
root["invertedColor"] = preferences.getBool("invertedColor", EPDManager::getInstance().getForegroundColor() == GxEPD_WHITE);
root["timerSeconds"] = getTimerSeconds();
root["timerRunning"] = isTimerActive();
root["minSecPriceUpd"] = preferences.getUInt(
@ -818,7 +816,7 @@ bool processEpdColorSettings(AsyncWebServerRequest *request)
const AsyncWebParameter *fgColor = request->getParam("fgColor", true);
uint32_t color = strtol(fgColor->value().c_str(), NULL, 16);
preferences.putUInt("fgColor", color);
setFgColor(color);
EPDManager::getInstance().setForegroundColor(color);
// Serial.print(F("Setting foreground color to "));
// Serial.println(fgColor->value().c_str());
settingsChanged = true;
@ -829,7 +827,7 @@ bool processEpdColorSettings(AsyncWebServerRequest *request)
uint32_t color = strtol(bgColor->value().c_str(), NULL, 16);
preferences.putUInt("bgColor", color);
setBgColor(color);
EPDManager::getInstance().setBackgroundColor(color);
// Serial.print(F("Setting background color to "));
// Serial.println(bgColor->value().c_str());
settingsChanged = true;
@ -1202,7 +1200,7 @@ void onApiLightsGet(AsyncWebServerRequest *request)
auto& ledHandler = getLedHandler();
auto& pixels = ledHandler.getPixels();
DynamicJsonDocument doc(1024);
JsonDocument doc;
JsonArray lights = doc.createNestedArray("lights");
for (uint i = 0; i < pixels.numPixels(); i++)
@ -1225,7 +1223,7 @@ void onApiLightsPost(AsyncWebServerRequest *request, uint8_t *data, size_t len,
auto& ledHandler = getLedHandler();
auto& pixels = ledHandler.getPixels();
DynamicJsonDocument doc(1024);
JsonDocument doc;
DeserializationError error = deserializeJson(doc, data);
if (error)
{