diff --git a/wled00/wled00.ino b/wled00/wled00.ino index 7bf168fd..a296ee8c 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -43,7 +43,7 @@ uint8_t bri_n = 100; uint8_t nightlightDelayMins = 60; boolean nightlightFade = true; -double transitionResolution = 0.015; +double transitionResolution = 0.011; //Internal vars byte col_old[]{0, 0, 0}; @@ -62,10 +62,10 @@ boolean buttonPressedBefore = false; int notifier_ips_count = 1; String notifier_ips_raw = ""; boolean nightlightActive = false; -boolean nightlightFade_old = false; boolean nightlightActive_old = false; int transitionDelay_old; - +long nightlightPassedTime = 0; +int nightlightDelayMs; NeoPixelBus strip(led_amount, 1); @@ -96,42 +96,6 @@ uint8_t bool2int(boolean value) return 0; } -void handleNightlight() -{ - if (nightlightActive) - { - if (!nightlightActive_old) //init - { - nightlightActive_old = true; - if (nightlightFade) - { - transitionDelay_old = transitionDelay; - transitionDelay = (int)(nightlightDelayMins*60000); - transitionStartTime = nightlightStartTime; - transitionActive = true; - } - } - float nper = (millis() - nightlightStartTime)/(float)transitionDelay; - if (nper >= 1) - { - nightlightActive = false; - } - } - } else if (nightlightActive_old) //de-init - { - nightlightActive_old = false; - if (nightlightFade) - { - transitionDelay = transitionDelay_old; - transitionActive = false; - } else - { - bri = 0; - colorUpdated(4); - } - } -} - void setup() { wledInit(); } @@ -140,7 +104,6 @@ void loop() { server.handleClient(); handleTransitions(); handleNightlight(); - handleAnimations(); handleButton(); } diff --git a/wled00/wled01_eeprom.ino b/wled00/wled01_eeprom.ino index 1e6314cb..ae62a0d1 100644 --- a/wled00/wled01_eeprom.ino +++ b/wled00/wled01_eeprom.ino @@ -141,6 +141,7 @@ void loadSettingsFromEEPROM() receiveNotifications = EEPROM.read(250); fadeTransition = EEPROM.read(251); transitionDelay = ((EEPROM.read(253) << 0) & 0xFF) + ((EEPROM.read(254) << 8) & 0xFF00); + transitionDelay_old = transitionDelay; bri_n = EEPROM.read(255); for (int i = 256; i < 288; ++i) { diff --git a/wled00/wled02_xml.ino b/wled00/wled02_xml.ino index 373b4a0b..55600a3b 100644 --- a/wled00/wled02_xml.ino +++ b/wled00/wled02_xml.ino @@ -4,7 +4,13 @@ void XML_response() resp = resp + ""; resp = resp + ""; resp = resp + ""; - resp = resp + bri; + if (nightlightActive && nightlightFade) + { + resp = resp + bri_t; + } else + { + resp = resp + bri; + } resp = resp + ""; for (int i = 0; i < 3; i++) diff --git a/wled00/wled03_set.ino b/wled00/wled03_set.ino index 91d9c1b8..91c51b1d 100644 --- a/wled00/wled03_set.ino +++ b/wled00/wled03_set.ino @@ -103,7 +103,10 @@ void handleSettingsSet() if (server.hasArg("TDLAY")) { int i = server.arg("TDLAY").toInt(); - if (i > 0) transitionDelay = i; + if (i > 0){ + transitionDelay = i; + transitionDelay_old = transitionDelay; + } } if (server.hasArg("TLDUR")) { @@ -111,7 +114,6 @@ void handleSettingsSet() if (i > 0) nightlightDelayMins = i; } nightlightFade = server.hasArg("TLFDE"); - nightlightFade_old = nightlightFade; receiveNotifications = server.hasArg("NRCVE"); if (server.hasArg("NRBRI")) { diff --git a/wled00/wled05_init.ino b/wled00/wled05_init.ino index 5a8f2813..e6bcfe50 100644 --- a/wled00/wled05_init.ino +++ b/wled00/wled05_init.ino @@ -22,7 +22,6 @@ void wledInit() Serial.println("Init EEPROM"); EEPROM.begin(1024); loadSettingsFromEEPROM(); - nightlightFade_old = nightlightFade; Serial.print("CC: SSID: "); Serial.print(clientssid); diff --git a/wled00/wled08_led.ino b/wled00/wled08_led.ino index 720547a9..1d751132 100644 --- a/wled00/wled08_led.ino +++ b/wled00/wled08_led.ino @@ -47,11 +47,7 @@ void colorUpdated(int callMode) } transitionActive = true; transitionStartTime = millis(); - if (nightlightActive) - { - nightlightFade_old = nightlightFade; - nightlightFade = false; - } + transitionDelay = transitionDelay_old; } else { setLedsStandard(); @@ -67,8 +63,11 @@ void handleTransitions() { transitionActive = false; tper_last = 0; - nightlightFade = nightlightFade_old; setLedsStandard(); + if (nightlightActive && nightlightFade) + { + initNightlightFade(); + } return; } if (tper - tper_last < transitionResolution) @@ -90,4 +89,53 @@ void handleTransitions() } } -void handleAnimations(){}; +void initNightlightFade() +{ + float nper = (millis() - nightlightStartTime)/((float)nightlightDelayMs); + nightlightDelayMs = nightlightDelayMs*(1-nper); + if (nper >= 1) + { + return; + } + bri = 0; + bri_it = 0; + transitionDelay = (int)(nightlightDelayMins*60000); + transitionStartTime = nightlightStartTime; + transitionActive = true; + nightlightStartTime = millis(); +} + +void handleNightlight() +{ + if (nightlightActive) + { + if (!nightlightActive_old) //init + { + nightlightDelayMs = (int)(nightlightDelayMins*60000); + nightlightActive_old = true; + if (nightlightFade) + { + initNightlightFade(); + } + } + float nper = (millis() - nightlightStartTime)/((float)nightlightDelayMs); + Serial.println(nper); + if (nper >= 1) + { + nightlightActive = false; + } + } else if (nightlightActive_old) //de-init + { + nightlightPassedTime = 0; + nightlightActive_old = false; + if (nightlightFade) + { + transitionDelay = transitionDelay_old; + transitionActive = false; + } else + { + bri = 0; + colorUpdated(4); + } + } +}