Fixed some webinterface missbehavior. transition time should be better now.

This commit is contained in:
Kai Lauterbach 2023-04-29 12:40:14 +02:00
parent ebd742bd21
commit 2d635ba0c4

View file

@ -49,6 +49,8 @@ uint8_t tc_enabled;
bool light_state[LIGHTS_COUNT];
bool in_transition;
int default_transitiontime = 4; // 4 seconds
int transitiontime[LIGHTS_COUNT];
int bri[LIGHTS_COUNT];
uint16_t current_pwm[LIGHTS_COUNT];
@ -79,8 +81,10 @@ void apply_scene(uint8_t new_scene, uint8_t light) {
//********************************//
void process_lightdata(uint8_t light, float tt) {
if (light_state[light]) {
void process_lightdata(uint8_t light, float tt)
{
if (light_state[light])
{
step_level[light] = (bri[light] - current_bri[light]) / tt;
} else {
@ -90,7 +94,8 @@ void process_lightdata(uint8_t light, float tt) {
//********************************//
void lightEngine() {
void lightEngine()
{
if (millis() < (last_lightengine_activity + TIME_LIGHTENGINE_INTERVAL_MS)) {
// abort processing, the transition setting is a delay of seconds
@ -108,7 +113,8 @@ void lightEngine() {
{
in_transition = true;
current_bri[i] += step_level[i] / BRI_MOD_STEPS_PER_SEC;
if ((step_level[i] > 0.0 && current_bri[i] > bri[i]) || (step_level[i] < 0.0 && current_bri[i] < bri[i])) {
if ((step_level[i] > 0.0 && current_bri[i] > bri[i]) || (step_level[i] < 0.0 && current_bri[i] < bri[i]))
{
current_bri[i] = bri[i];
//Serial.println("Reached target bri[" + (String)i + "] = " + (String)bri[i]);
}
@ -326,7 +332,6 @@ void init_webserver()
const char* key = state.key().c_str();
int light = atoi(key) - 1;
JsonObject values = state.value();
int transitiontime = 4;
uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS);
if (values.containsKey("on"))
@ -361,9 +366,17 @@ void init_webserver()
if (values.containsKey("transitiontime"))
{
transitiontime = values["transitiontime"];
default_transitiontime = values["transitiontime"];
if (tc_enabled == TIMING_CONTROL_DISABLED)
{
for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++)
{
// set the default transition time for all lights
process_lightdata(i, default_transitiontime);
}
}
}
process_lightdata(light, transitiontime);
}
String output;
serializeJson(root, output);
@ -414,11 +427,18 @@ void init_webserver()
{
#ifndef DISABLE_WEB_CONTROL
static float transitiontime = 4.0;
if (server.hasArg("transition"))
{
transitiontime = server.arg("transition").toFloat();
default_transitiontime = server.arg("transition").toFloat();
if (tc_enabled == TIMING_CONTROL_DISABLED)
{
for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++)
{
// set the default transition time for all lights
process_lightdata(i, default_transitiontime);
}
}
}
// startup behavior switch handling
@ -458,6 +478,7 @@ void init_webserver()
EEPROM.commit();
Serial.print("Timing control = ");
Serial.println(EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS));
tc_update_main(); // call the main update function to read data and set the light states
}
}
@ -472,6 +493,12 @@ void init_webserver()
EEPROM.commit();
Serial.print("Timing control = ");
Serial.println(EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS));
for (uint8_t i = 0 ; i < LIGHTS_COUNT; i++)
{
// set the default transition time for all lights
process_lightdata(i, default_transitiontime);
}
}
}
}
@ -502,7 +529,8 @@ void init_webserver()
}
// process the received data for every light
for (int light = 0; light < LIGHTS_COUNT; light++) {
for (int light = 0; light < LIGHTS_COUNT; light++)
{
if (server.hasArg("bri" + (String)light))
{
@ -513,11 +541,14 @@ void init_webserver()
Serial.println(bri[light]);
}
if (server.hasArg("on" + (String)light)) {
if (server.hasArg("on" + (String)light))
{
uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS);
if (server.arg("on" + (String)light) == "true" && light_state[light] == false) {
if (server.arg("on" + (String)light) == "true" && light_state[light] == false)
{
light_state[light] = true;
if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 0) {
if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 0)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON);
}
Serial.print("Light ");
@ -525,9 +556,11 @@ void init_webserver()
Serial.print(" state set to ");
Serial.println(light_state[light]);
} else if (server.arg("on" + (String)light) == "false" && light_state[light] == true) {
} else if (server.arg("on" + (String)light) == "false" && light_state[light] == true)
{
light_state[light] = false;
if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 1) {
if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 1)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF);
}
Serial.print("Light ");
@ -537,23 +570,25 @@ void init_webserver()
}
EEPROM.commit();
} else {
// light is off
if (tc_enabled == TIMING_CONTROL_DISABLED)
{
process_lightdata(light, default_transitiontime);
}
}
// start alerting for every light
if (server.hasArg("alert")) {
if (light_state[light]) {
if (server.hasArg("alert"))
{
if (light_state[light])
{
current_bri[light] = 0;
} else {
current_bri[light] = 255;
}
}
// set the light step level
if (server.hasArg("transition") && light_state[light]) {
Serial.println("Webinterface transitiontime = " + (String)transitiontime);
transitiontime = server.arg("transition").toFloat();
step_level[light] = ((float)bri[light] - current_bri[light]) / transitiontime;
}
} // process all lights
#endif // DISABLE_WEB_CONTROL
@ -700,7 +735,7 @@ void init_webserver()
http_content += "<div class=\"pure-control-group\">";
http_content += "<label for=\"transition\">Transition time (s)</label>";
http_content += "<input id=\"transition\" name=\"transition\" type=\"text\" placeholder=\"10\" value=\"" + (String)transitiontime + "\">";
http_content += "<input id=\"transition\" name=\"transition\" type=\"text\" placeholder=\"10\" value=\"" + (String)default_transitiontime + "\">";
http_content += "</div>";
// Wifi settings