diff --git a/src/lib/block_notify.cpp b/src/lib/block_notify.cpp index a7a6751..05e0417 100644 --- a/src/lib/block_notify.cpp +++ b/src/lib/block_notify.cpp @@ -119,7 +119,6 @@ void onWebsocketEvent(void *handler_args, esp_event_base_t base, int32_t event_i break; case WEBSOCKET_EVENT_DATA: onWebsocketMessage(data); - // Handle the received WebSocket message (block notifications) here break; case WEBSOCKET_EVENT_ERROR: Serial.println(F("Mempool.space WS Connnection error")); @@ -143,8 +142,7 @@ void onWebsocketMessage(esp_websocket_event_data_t *event_data) currentBlockHeight = block["height"].as(); Serial.printf("New block found: %d\r\n", block["height"].as()); - size_t prefWrite = preferences.putUInt("blockHeight", currentBlockHeight); - Serial.printf("Wrote %d for block\r\n", prefWrite); + preferences.putUInt("blockHeight", currentBlockHeight); if (workQueue != nullptr) { @@ -154,7 +152,16 @@ void onWebsocketMessage(esp_websocket_event_data_t *event_data) if (getCurrentScreen() != SCREEN_BLOCK_HEIGHT && preferences.getBool("stealFocus", true)) { + uint64_t timerPeriod = 0; + if (isTimerActive()) { + // store timer periode before making inactive to prevent artifacts + timerPeriod = getTimerSeconds(); + esp_timer_stop(screenRotateTimer); + } setCurrentScreen(SCREEN_BLOCK_HEIGHT); + if (timerPeriod > 0) { + esp_timer_start_periodic(screenRotateTimer, timerPeriod * usPerSecond); + } } if (getCurrentScreen() == SCREEN_BLOCK_HEIGHT && preferences.getBool("ledFlashOnUpd", false)) diff --git a/src/lib/block_notify.hpp b/src/lib/block_notify.hpp index 2783c2b..46012d5 100644 --- a/src/lib/block_notify.hpp +++ b/src/lib/block_notify.hpp @@ -6,7 +6,7 @@ #include #include #include "shared.hpp" - +#include "esp_timer.h" #include "esp_websocket_client.h" #include "screen_handler.hpp" #include "led_handler.hpp" diff --git a/src/lib/epd.cpp b/src/lib/epd.cpp index 560bfef..7475aea 100644 --- a/src/lib/epd.cpp +++ b/src/lib/epd.cpp @@ -58,7 +58,7 @@ TaskHandle_t tasks[NUM_SCREENS]; #define UPDATE_QUEUE_SIZE 14 QueueHandle_t updateQueue; -SemaphoreHandle_t epdUpdateSemaphore[NUM_SCREENS]; +//SemaphoreHandle_t epdUpdateSemaphore[NUM_SCREENS]; int fgColor = GxEPD_WHITE; int bgColor = GxEPD_BLACK; @@ -66,6 +66,8 @@ int bgColor = GxEPD_BLACK; #define FONT_SMALL Antonio_SemiBold20pt7b #define FONT_BIG Antonio_SemiBold90pt7b +std::mutex epdUpdateMutex; + uint8_t qrcode[800]; void forceFullRefresh() @@ -89,8 +91,8 @@ void setupDisplays() for (uint i = 0; i < NUM_SCREENS; i++) { - epdUpdateSemaphore[i] = xSemaphoreCreateBinary(); - xSemaphoreGive(epdUpdateSemaphore[i]); + // epdUpdateSemaphore[i] = xSemaphoreCreateBinary(); + // xSemaphoreGive(epdUpdateSemaphore[i]); int *taskParam = new int; *taskParam = i; @@ -116,6 +118,8 @@ void setEpdContent(std::array newEpdContent) void setEpdContent(std::array newEpdContent, bool forceUpdate) { + std::lock_guard lock(epdUpdateMutex); + for (uint i = 0; i < NUM_SCREENS; i++) { if (newEpdContent[i].compareTo(currentEpdContent[i]) != 0 || forceUpdate) @@ -123,15 +127,8 @@ void setEpdContent(std::array newEpdContent, bool forceUpda epdContent[i] = newEpdContent[i]; UpdateDisplayTaskItem dispUpdate = {i}; xQueueSend(updateQueue, &dispUpdate, portMAX_DELAY); - // if (xSemaphoreTake(epdUpdateSemaphore[i], pdMS_TO_TICKS(5000)) == pdTRUE) - // { - // xTaskNotifyGive(tasks[i]); - // } } } - - if (eventSourceTaskHandle != NULL) - xTaskNotifyGive(eventSourceTaskHandle); } void prepareDisplayUpdateTask(void *pvParameters) @@ -191,22 +188,17 @@ extern "C" void updateDisplay(void *pvParameters) noexcept // Wait for the task notification ulTaskNotifyTake(pdTRUE, portMAX_DELAY); - // if (xSemaphoreTake(epdUpdateSemaphore[epdIndex], pdMS_TO_TICKS(5000)) == pdTRUE) - // { + uint count = 0; while (EPD_BUSY[epdIndex].digitalRead() == HIGH || count < 10) { vTaskDelay(pdMS_TO_TICKS(100)); - // if (count >= 9) - // { - // displays[epdIndex].init(0, false); - // } count++; } bool updatePartial = true; - // Full Refresh every half hour + // Full Refresh every x minutes if (!lastFullRefresh[epdIndex] || (millis() - lastFullRefresh[epdIndex]) > (preferences.getUInt("fullRefreshMin", 30) * 60 * 1000)) { updatePartial = false; @@ -222,14 +214,15 @@ extern "C" void updateDisplay(void *pvParameters) noexcept if (!updatePartial) lastFullRefresh[epdIndex] = millis(); break; + + + if (eventSourceTaskHandle != NULL) + xTaskNotifyGive(eventSourceTaskHandle); } vTaskDelay(pdMS_TO_TICKS(100)); tries++; } - - // xSemaphoreGive(epdUpdateSemaphore[epdIndex]); - // } } } @@ -334,9 +327,6 @@ void renderText(const uint dispNum, const String &text, bool partial) displays[dispNum].fillScreen(GxEPD_WHITE); displays[dispNum].setTextColor(GxEPD_BLACK); displays[dispNum].setCursor(0, 50); - // displays[dispNum].setFont(&FreeSans9pt7b); - - // std::regex pattern("/\*(.*)\*/"); std::stringstream ss; ss.str(text.c_str()); @@ -358,8 +348,6 @@ void renderText(const uint dispNum, const String &text, bool partial) displays[dispNum].println(line.c_str()); } } - - // displays[dispNum].display(partial); } void renderQr(const uint dispNum, const String &text, bool partial) @@ -387,10 +375,6 @@ void renderQr(const uint dispNum, const String &text, bool partial) displays[dispNum].drawPixel(padding + x, paddingY + y, qrcodegen_getModule(qrcode, floor(float(x) / 4), floor(float(y) / 4)) ? GxEPD_BLACK : GxEPD_WHITE); } } -// displays[dispNum].display(partial); - -// free(tempBuffer); -// free(qrcode); #endif } @@ -403,8 +387,11 @@ void waitUntilNoneBusy() { count++; vTaskDelay(10); - if (count > 200) { + if (count == 200) { displays[i].init(0, false); + vTaskDelay(100); + } else if (count > 205) { + break; } } } diff --git a/src/lib/epd.hpp b/src/lib/epd.hpp index 5f34f06..1194e2d 100644 --- a/src/lib/epd.hpp +++ b/src/lib/epd.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef USE_QR #include "qrcodegen.h" diff --git a/src/lib/screen_handler.cpp b/src/lib/screen_handler.cpp index e92040b..04f1ed3 100644 --- a/src/lib/screen_handler.cpp +++ b/src/lib/screen_handler.cpp @@ -10,8 +10,7 @@ esp_timer_handle_t minuteTimer; std::array taskEpdContent = {"", "", "", "", "", "", ""}; std::string priceString; -const int usPerSecond = 1000000; -const int usPerMinute = 60 * usPerSecond; + // typedef enum // { diff --git a/src/lib/shared.hpp b/src/lib/shared.hpp index f24632b..73868a2 100644 --- a/src/lib/shared.hpp +++ b/src/lib/shared.hpp @@ -21,6 +21,8 @@ const PROGMEM int SCREEN_COUNTDOWN = 98; const PROGMEM int SCREEN_CUSTOM = 99; const int SCREEN_COUNT = 6; const PROGMEM int screens[SCREEN_COUNT] = { SCREEN_BLOCK_HEIGHT, SCREEN_MSCW_TIME, SCREEN_BTC_TICKER, SCREEN_TIME, SCREEN_HALVING_COUNTDOWN, SCREEN_MARKET_CAP }; +const int usPerSecond = 1000000; +const int usPerMinute = 60 * usPerSecond; struct SpiRamAllocator { void* allocate(size_t size) { diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index 2c4c958..bda8a3c 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -12,6 +12,7 @@ void setupWebserver() return; } + events.onConnect([](AsyncEventSourceClient *client) { client->send("welcome", NULL, millis(), 1000); }); server.addHandler(&events); @@ -280,8 +281,8 @@ bool processEpdColorSettings(AsyncWebServerRequest *request) AsyncWebParameter *fgColor = request->getParam("fgColor", true); preferences.putUInt("fgColor", strtol(fgColor->value().c_str(), NULL, 16)); setFgColor(int(strtol(fgColor->value().c_str(), NULL, 16))); - Serial.print(F("Setting foreground color to ")); - Serial.println(fgColor->value().c_str()); + // Serial.print(F("Setting foreground color to ")); + // Serial.println(fgColor->value().c_str()); settingsChanged = true; } if (request->hasParam("bgColor", true)) @@ -290,8 +291,8 @@ bool processEpdColorSettings(AsyncWebServerRequest *request) preferences.putUInt("bgColor", strtol(bgColor->value().c_str(), NULL, 16)); setBgColor(int(strtol(bgColor->value().c_str(), NULL, 16))); - Serial.print(F("Setting background color to ")); - Serial.println(bgColor->value().c_str()); + // Serial.print(F("Setting background color to ")); + // Serial.println(bgColor->value().c_str()); settingsChanged = true; } diff --git a/src/main.cpp b/src/main.cpp index 9c009a7..9d4b5e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "lib/config.hpp" #define USE_QR + //char ptrTaskList[400]; extern "C" void app_main()