Nightlight working!

This commit is contained in:
cschwinne 2016-11-27 16:45:54 +01:00
parent 25e9967dae
commit 123ab66b31
6 changed files with 70 additions and 51 deletions

View File

@ -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<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> 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();
}

View File

@ -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)
{

View File

@ -4,7 +4,13 @@ void XML_response()
resp = resp + "<?xml version = \"1.0\" ?>";
resp = resp + "<vs>";
resp = resp + "<act>";
if (nightlightActive && nightlightFade)
{
resp = resp + bri_t;
} else
{
resp = resp + bri;
}
resp = resp + "</act>";
for (int i = 0; i < 3; i++)

View File

@ -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"))
{

View File

@ -22,7 +22,6 @@ void wledInit()
Serial.println("Init EEPROM");
EEPROM.begin(1024);
loadSettingsFromEEPROM();
nightlightFade_old = nightlightFade;
Serial.print("CC: SSID: ");
Serial.print(clientssid);

View File

@ -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);
}
}
}