diff --git a/tinyGS/src/Display/Display.cpp b/tinyGS/src/Display/Display.cpp index aaf5755e..edb965d9 100644 --- a/tinyGS/src/Display/Display.cpp +++ b/tinyGS/src/Display/Display.cpp @@ -46,7 +46,7 @@ unsigned long tick_interval; int tick_timing = 100; int graphVal = 1; int delta = 1; -uint8_t oldOledBright = 100; +uint8_t oldOledBright = -1; // to force brightness update on first run void displayInit() { @@ -117,19 +117,6 @@ void msOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) { if (timeinfo->tm_hour < 6 || timeinfo->tm_hour > 18) display->normalDisplay(); else display->invertDisplay(); // change the OLED according to the time. } - - - if (oldOledBright!=ConfigManager::getInstance().getOledBright()) - { - oldOledBright = ConfigManager::getInstance().getOledBright(); - if (ConfigManager::getInstance().getOledBright()==0) { - display->displayOff(); - } - else - { - display->setBrightness(2*ConfigManager::getInstance().getOledBright()); - } - } } void drawRemoteFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y, uint8_t frameNumber) @@ -348,15 +335,39 @@ void displayShowStaMode(bool ap) display->display(); } +/** + * Updates the display brightness based on the configuration. + * If the brightness has changed, it sets the new brightness or turns off the display. + * Also updates the UI if screen is on. + */ void displayUpdate() { - if (ConfigManager::getInstance().getOledBright()) + // Get the current OLED brightness from configuration + uint8_t oledBright = ConfigManager::getInstance().getOledBright(); + + // Check if brightness has changed + if (oldOledBright != oledBright) { + if (oledBright) { + // Set the new brightness + display->setBrightness(2*oledBright); + } else { + // Turn off the display if brightness is 0 + displayTurnOff(); + } + // Save the brightness value + oldOledBright = oledBright; + } + + if (oledBright) { + // Update the UI if screen is on ui->update(); + } } void displayTurnOff() { display->displayOff(); + oldOledBright = 0; } void displayNextFrame() { diff --git a/tinyGS/tinyGS.ino b/tinyGS/tinyGS.ino index 7560ae3b..d293abde 100644 --- a/tinyGS/tinyGS.ino +++ b/tinyGS/tinyGS.ino @@ -322,7 +322,7 @@ void loop() { mqtt.loop(); OTA::loop(); - if (configManager.getOledBright () != 0) displayUpdate (); + displayUpdate (); if (configManager.askedWebLogin () && mqtt.connected ()) {