diff --git a/src/lib/bitaxe_fetch.cpp b/src/lib/bitaxe_fetch.cpp index d2dae13..8fa1db0 100644 --- a/src/lib/bitaxe_fetch.cpp +++ b/src/lib/bitaxe_fetch.cpp @@ -1,24 +1,19 @@ #include "bitaxe_fetch.hpp" -TaskHandle_t bitaxeFetchTaskHandle; - -uint64_t bitaxeHashrate; -uint64_t bitaxeBestDiff; - -uint64_t getBitAxeHashRate() -{ - return bitaxeHashrate; +void BitAxeFetch::taskWrapper(void* pvParameters) { + BitAxeFetch::getInstance().task(); } -uint64_t getBitaxeBestDiff() -{ - return bitaxeBestDiff; +uint64_t BitAxeFetch::getHashRate() const { + return hashrate; } -void taskBitaxeFetch(void *pvParameters) -{ - for (;;) - { +uint64_t BitAxeFetch::getBestDiff() const { + return bestDiff; +} + +void BitAxeFetch::task() { + for (;;) { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); HTTPClient http; @@ -28,46 +23,38 @@ void taskBitaxeFetch(void *pvParameters) int httpCode = http.GET(); - if (httpCode == 200) - { + if (httpCode == 200) { String payload = http.getString(); JsonDocument doc; deserializeJson(doc, payload); // Convert GH/s to H/s (multiply by 10^9) float hashRateGH = doc["hashRate"].as(); - bitaxeHashrate = static_cast(std::round(hashRateGH * std::pow(10, getHashrateMultiplier('G')))); + hashrate = static_cast(std::round(hashRateGH * std::pow(10, getHashrateMultiplier('G')))); // Parse difficulty string and convert to uint64_t std::string diffStr = doc["bestDiff"].as(); char diffUnit = diffStr[diffStr.length() - 1]; if (std::isalpha(diffUnit)) { float diffValue = std::stof(diffStr.substr(0, diffStr.length() - 1)); - bitaxeBestDiff = static_cast(std::round(diffValue * std::pow(10, getDifficultyMultiplier(diffUnit)))); + bestDiff = static_cast(std::round(diffValue * std::pow(10, getDifficultyMultiplier(diffUnit)))); } else { - bitaxeBestDiff = std::stoull(diffStr); + bestDiff = std::stoull(diffStr); } - if (workQueue != nullptr && (ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_HASHRATE || ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_BESTDIFF)) - { + if (workQueue != nullptr && (ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_HASHRATE || ScreenHandler::getCurrentScreen() == SCREEN_BITAXE_BESTDIFF)) { WorkItem priceUpdate = {TASK_BITAXE_UPDATE, 0}; xQueueSend(workQueue, &priceUpdate, portMAX_DELAY); } - } - else - { - Serial.print( - F("Error retrieving BitAxe data. HTTP status code: ")); + } else { + Serial.print(F("Error retrieving BitAxe data. HTTP status code: ")); Serial.println(httpCode); Serial.println(bitaxeApiUrl); } } } -void setupBitaxeFetchTask() -{ - xTaskCreate(taskBitaxeFetch, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY, - &bitaxeFetchTaskHandle); - - xTaskNotifyGive(bitaxeFetchTaskHandle); +void BitAxeFetch::setup() { + xTaskCreate(taskWrapper, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY, &taskHandle); + xTaskNotifyGive(taskHandle); } \ No newline at end of file diff --git a/src/lib/bitaxe_fetch.hpp b/src/lib/bitaxe_fetch.hpp index 8e1da37..0dd98d9 100644 --- a/src/lib/bitaxe_fetch.hpp +++ b/src/lib/bitaxe_fetch.hpp @@ -7,10 +7,28 @@ #include "lib/config.hpp" #include "lib/shared.hpp" -extern TaskHandle_t bitaxeFetchTaskHandle; +class BitAxeFetch { +public: + static BitAxeFetch& getInstance() { + static BitAxeFetch instance; + return instance; + } -void setupBitaxeFetchTask(); -void taskBitaxeFetch(void *pvParameters); + void setup(); + uint64_t getHashRate() const; + uint64_t getBestDiff() const; + static void taskWrapper(void* pvParameters); + TaskHandle_t getTaskHandle() const { return taskHandle; } -uint64_t getBitAxeHashRate(); -uint64_t getBitaxeBestDiff(); \ No newline at end of file +private: + BitAxeFetch() = default; + ~BitAxeFetch() = default; + BitAxeFetch(const BitAxeFetch&) = delete; + BitAxeFetch& operator=(const BitAxeFetch&) = delete; + + void task(); + + TaskHandle_t taskHandle = nullptr; + uint64_t hashrate = 0; + uint64_t bestDiff = 0; +}; \ No newline at end of file diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 765bedd..2dce75e 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -100,7 +100,7 @@ void setup() if (preferences.getBool("bitaxeEnabled", DEFAULT_BITAXE_ENABLED)) { - setupBitaxeFetchTask(); + BitAxeFetch::getInstance().setup(); } if (preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED)) @@ -221,7 +221,7 @@ void setupWifi() // waitUntilNoneBusy(); // std::array epdContent = {"Welcome!", - // "Bienvenidos!", "Use\r\nweb-interface\r\nto configure", "Use\r\nla + // "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 diff --git a/src/lib/screen_handler.cpp b/src/lib/screen_handler.cpp index 4d8d1b9..27d83b9 100644 --- a/src/lib/screen_handler.cpp +++ b/src/lib/screen_handler.cpp @@ -220,8 +220,8 @@ void workerTask(void *pvParameters) { currentScreenValue != SCREEN_BITAXE_BESTDIFF) break; taskEpdContent = (currentScreenValue == SCREEN_BITAXE_HASHRATE) ? - parseBitaxeHashRate(getBitAxeHashRate()) : - parseBitaxeBestDiff(getBitaxeBestDiff()); + parseBitaxeHashRate(BitAxeFetch::getInstance().getHashRate()) : + parseBitaxeBestDiff(BitAxeFetch::getInstance().getBestDiff()); setEpdContent(taskEpdContent); break; } diff --git a/src/lib/timers.cpp b/src/lib/timers.cpp index 2fdb71c..3779c2d 100644 --- a/src/lib/timers.cpp +++ b/src/lib/timers.cpp @@ -68,8 +68,9 @@ void IRAM_ATTR minuteTimerISR(void *arg) { WorkItem timeUpdate = {TASK_TIME_UPDATE, 0}; xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken); - if (bitaxeFetchTaskHandle != NULL) { - vTaskNotifyGiveFromISR(bitaxeFetchTaskHandle, &xHigherPriorityTaskWoken); + TaskHandle_t bitaxeHandle = BitAxeFetch::getInstance().getTaskHandle(); + if (bitaxeHandle != NULL) { + vTaskNotifyGiveFromISR(bitaxeHandle, &xHigherPriorityTaskWoken); } if (miningPoolStatsFetchTaskHandle != NULL) {