From 698c3a3a437d9e97e9855b60175975639813d3c9 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 26 Dec 2024 22:20:30 +0100 Subject: [PATCH] More code optimizations, remove unnecessary checks --- src/lib/led_handler.cpp | 21 +++++++++------------ src/lib/led_handler.hpp | 2 +- src/lib/nostr_notify.cpp | 4 +--- src/lib/v2_notify.cpp | 8 +++++--- src/main.cpp | 9 ++++++++- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/lib/led_handler.cpp b/src/lib/led_handler.cpp index 338c3c3..c2c56f4 100644 --- a/src/lib/led_handler.cpp +++ b/src/lib/led_handler.cpp @@ -6,7 +6,7 @@ Adafruit_NeoPixel pixels(NEOPIXEL_COUNT, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); uint ledTaskParams; #ifdef HAS_FRONTLIGHT -#define FL_FADE_STEP 25 +constexpr uint16_t FL_FADE_STEP = 25; bool frontlightOn = false; bool flInTransition = false; @@ -68,18 +68,15 @@ void frontlightFadeInAll(int flDelayTime) void frontlightFadeInAll(int flDelayTime, bool staggered) { - if (preferences.getBool("flDisable")) - return; - if (frontlightIsOn()) - return; - if (flInTransition) + if (preferences.getBool("flDisable") || frontlightIsOn() || flInTransition) return; flInTransition = true; + const int maxBrightness = preferences.getUInt("flMaxBrightness"); + if (staggered) { - int maxBrightness = preferences.getUInt("flMaxBrightness"); int step = FL_FADE_STEP; int staggerDelay = flDelayTime / NUM_SCREENS; @@ -100,7 +97,7 @@ void frontlightFadeInAll(int flDelayTime, bool staggered) } else { - for (int dutyCycle = 0; dutyCycle <= preferences.getUInt("flMaxBrightness"); dutyCycle += FL_FADE_STEP) + for (int dutyCycle = 0; dutyCycle <= maxBrightness; dutyCycle += FL_FADE_STEP) { for (int ledPin = 0; ledPin <= NUM_SCREENS; ledPin++) { @@ -179,7 +176,7 @@ std::vector frontlightGetStatus() return statuses; } -bool frontlightIsOn() +inline bool frontlightIsOn() { return frontlightOn; } @@ -225,7 +222,7 @@ void ledTask(void *parameter) continue; } - uint32_t oldLights[NEOPIXEL_COUNT]; + std::array oldLights; // get current state for (int i = 0; i < NEOPIXEL_COUNT; i++) @@ -498,7 +495,7 @@ void blinkDelay(int d, int times) pixels.show(); } -void blinkDelayColor(int d, int times, uint r, uint g, uint b) +void blinkDelayColor(int d, int times, uint r, uint g, uint b) { for (int j = 0; j < times; j++) { @@ -518,7 +515,7 @@ void blinkDelayColor(int d, int times, uint r, uint g, uint b) pixels.show(); } -void blinkDelayTwoColor(int d, int times, uint32_t c1, uint32_t c2) +void blinkDelayTwoColor(int d, int times, const uint32_t& c1, const uint32_t& c2) { for (int j = 0; j < times; j++) { diff --git a/src/lib/led_handler.hpp b/src/lib/led_handler.hpp index b86475e..98d8834 100644 --- a/src/lib/led_handler.hpp +++ b/src/lib/led_handler.hpp @@ -49,7 +49,7 @@ void setupLeds(); void setupLedTask(); void blinkDelay(int d, int times); void blinkDelayColor(int d, int times, uint r, uint g, uint b); -void blinkDelayTwoColor(int d, int times, uint32_t c1, uint32_t c2); +void blinkDelayTwoColor(int d, int times, const uint32_t& c1, const uint32_t& c2); void clearLeds(); void saveLedState(); void restoreLedState(); diff --git a/src/lib/nostr_notify.cpp b/src/lib/nostr_notify.cpp index d19002c..a919e0e 100644 --- a/src/lib/nostr_notify.cpp +++ b/src/lib/nostr_notify.cpp @@ -63,9 +63,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify) nostrIsConnected = (status == nostr::ConnectionStatus::CONNECTED); if (!nostrIsConnected) { nostrIsSubscribed = false; - } - - Serial.println("[ Nostr ] Connection status changed: " + String(STATUS_STRINGS[statusIndex])); + } }); } diff --git a/src/lib/v2_notify.cpp b/src/lib/v2_notify.cpp index 33104a5..e0b5966 100644 --- a/src/lib/v2_notify.cpp +++ b/src/lib/v2_notify.cpp @@ -34,11 +34,12 @@ namespace V2Notify switch (type) { case WStype_DISCONNECTED: - Serial.printf("[WSc] Disconnected!\n"); + Serial.print(F("[WSc] Disconnected!\n")); break; case WStype_CONNECTED: { - Serial.printf("[WSc] Connected to url: %s\n", payload); + Serial.print(F("[WSc] Connected to url:")); + Serial.println((char *)payload); JsonDocument response; @@ -81,7 +82,8 @@ namespace V2Notify break; } case WStype_TEXT: - Serial.printf("[WSc] get text: %s\n", payload); + Serial.print(F("[WSc] get text: ")); + Serial.println((char *)payload); // send message to server // webSocket.sendTXT("message here"); diff --git a/src/main.cpp b/src/main.cpp index 845de01..ea24213 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,6 +102,7 @@ void checkMissedBlocks() { void monitorDataConnections() { + // Price notification monitoring if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected()) { handlePriceNotifyDisconnection(); @@ -134,6 +135,9 @@ extern "C" void app_main() { Serial.begin(115200); setup(); + bool ownDataSource = preferences.getBool("ownDataSource", DEFAULT_OWN_DATA_SOURCE); + + while (true) { if (eventSourceTaskHandle != NULL) { xTaskNotifyGive(eventSourceTaskHandle); @@ -142,7 +146,10 @@ extern "C" void app_main() { if (!getIsOTAUpdating()) { handleFrontlight(); checkWiFiConnection(); - monitorDataConnections(); + + if (!ownDataSource) { + monitorDataConnections(); + } if (getUptime() - getLastTimeSync() > 24 * 60 * 60) { Serial.println(F("Last time update is longer than 24 hours ago, sync again"));