Implement smooth non-blocking backlight fade for day/night transitions

This commit is contained in:
Sebastián Nieto 2026-09-07 21:27:30 -03:00
parent 449a460f9c
commit 70d3bcfd2a
2 changed files with 42 additions and 15 deletions

View file

@ -801,37 +801,62 @@ void updateMeter(uint8_t pidNo, String response) { // update parameter on screen
}
/*-------------------------------*/
// Target and current brightness variables for smooth fade
uint8_t targetBacklight = 255;
float currentBacklight = 255.0f;
uint32_t lastBacklightStepTime = 0;
void updateBacklightFade() {
uint32_t now = millis();
if (now - lastBacklightStepTime >= 15) { // every 15ms step
lastBacklightStepTime = now;
if (abs(currentBacklight - (float)targetBacklight) > 0.5f) {
if (currentBacklight < targetBacklight) {
currentBacklight += 3.0f;
if (currentBacklight > targetBacklight)
currentBacklight = targetBacklight;
} else {
currentBacklight -= 3.0f;
if (currentBacklight < targetBacklight)
currentBacklight = targetBacklight;
}
ledcWrite(backlightChannel, (uint32_t)currentBacklight);
}
}
}
// auto dim backlight function
void autoDim() {
int light = analogRead(LDR_PIN); // read light
if (light > LIGHT_LEVEL) { // low light
if (light > LIGHT_LEVEL) { // low light (dark)
low_count++;
if (low_count > 10) {
low_count = 10; // low amb light 10 times count
if (low_count > 6) {
low_count = 6;
if (!dim) {
ledcWrite(backlightChannel, 50); // dim light
targetBacklight = 50; // smooth fade target: dim
dim = true;
#ifdef SERIAL_DEBUG
Serial.println(F("Backlight -> dim"));
Serial.println(F("Backlight -> target dim (50)"));
#endif
} // if !dim
} // if low_count
}
}
high_count = 0;
} else {
} else { // bright light (day)
high_count++;
if (high_count > 10) {
high_count = 10; // high amb light 10 times count
if (high_count > 6) {
high_count = 6;
if (dim) {
ledcWrite(backlightChannel, 255); // brightest
targetBacklight = 255; // smooth fade target: bright
dim = false;
#ifdef SERIAL_DEBUG
Serial.println(F("Backlight -> bright"));
Serial.println(F("Backlight -> target bright (255)"));
#endif
} // if dim
} // if high_count
}
}
low_count = 0;
} // if light > lightlevel
}
updateBacklightFade();
} // autodim
//---------------------------
/*############################################*/

View file

@ -413,6 +413,8 @@ recent_client_addr : {0x00,0x00,0x00,0x00,0x00,0x00} array of bytes[6]
/*###################################*/
void loop() {
updateBacklightFade(); // non-blocking smooth backlight fade handler
// SCAN TOUCHSCREEN & BUTTON
// - Single click on a quadrant: toggles between clock (arc) and number
// - Double click on a quadrant: puts that instrument in full screen (or exits full screen)