Implement smooth non-blocking backlight fade for day/night transitions
This commit is contained in:
parent
449a460f9c
commit
70d3bcfd2a
2 changed files with 42 additions and 15 deletions
|
|
@ -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
|
// auto dim backlight function
|
||||||
void autoDim() {
|
void autoDim() {
|
||||||
int light = analogRead(LDR_PIN); // read light
|
int light = analogRead(LDR_PIN); // read light
|
||||||
if (light > LIGHT_LEVEL) { // low light
|
if (light > LIGHT_LEVEL) { // low light (dark)
|
||||||
low_count++;
|
low_count++;
|
||||||
if (low_count > 10) {
|
if (low_count > 6) {
|
||||||
low_count = 10; // low amb light 10 times count
|
low_count = 6;
|
||||||
if (!dim) {
|
if (!dim) {
|
||||||
ledcWrite(backlightChannel, 50); // dim light
|
targetBacklight = 50; // smooth fade target: dim
|
||||||
dim = true;
|
dim = true;
|
||||||
#ifdef SERIAL_DEBUG
|
#ifdef SERIAL_DEBUG
|
||||||
Serial.println(F("Backlight -> dim"));
|
Serial.println(F("Backlight -> target dim (50)"));
|
||||||
#endif
|
#endif
|
||||||
} // if !dim
|
}
|
||||||
} // if low_count
|
}
|
||||||
high_count = 0;
|
high_count = 0;
|
||||||
|
|
||||||
} else {
|
} else { // bright light (day)
|
||||||
high_count++;
|
high_count++;
|
||||||
if (high_count > 10) {
|
if (high_count > 6) {
|
||||||
high_count = 10; // high amb light 10 times count
|
high_count = 6;
|
||||||
if (dim) {
|
if (dim) {
|
||||||
ledcWrite(backlightChannel, 255); // brightest
|
targetBacklight = 255; // smooth fade target: bright
|
||||||
dim = false;
|
dim = false;
|
||||||
#ifdef SERIAL_DEBUG
|
#ifdef SERIAL_DEBUG
|
||||||
Serial.println(F("Backlight -> bright"));
|
Serial.println(F("Backlight -> target bright (255)"));
|
||||||
#endif
|
#endif
|
||||||
} // if dim
|
}
|
||||||
} // if high_count
|
}
|
||||||
low_count = 0;
|
low_count = 0;
|
||||||
} // if light > lightlevel
|
}
|
||||||
|
updateBacklightFade();
|
||||||
} // autodim
|
} // autodim
|
||||||
//---------------------------
|
//---------------------------
|
||||||
/*############################################*/
|
/*############################################*/
|
||||||
|
|
|
||||||
|
|
@ -413,6 +413,8 @@ recent_client_addr : {0x00,0x00,0x00,0x00,0x00,0x00} array of bytes[6]
|
||||||
|
|
||||||
/*###################################*/
|
/*###################################*/
|
||||||
void loop() {
|
void loop() {
|
||||||
|
updateBacklightFade(); // non-blocking smooth backlight fade handler
|
||||||
|
|
||||||
// SCAN TOUCHSCREEN & BUTTON
|
// SCAN TOUCHSCREEN & BUTTON
|
||||||
// - Single click on a quadrant: toggles between clock (arc) and number
|
// - 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)
|
// - Double click on a quadrant: puts that instrument in full screen (or exits full screen)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue