Added a FSM to the main loop to prevent resets and improved performance. Added some comment v-lines. Improved readability of the code.

This commit is contained in:
Kai Lauterbach 2023-05-13 23:01:27 +02:00
parent 7e6dd20d31
commit c177404a75
5 changed files with 62 additions and 25 deletions

View file

@ -157,19 +157,34 @@ void setup()
void loop() void loop()
{ {
server.handleClient(); static uint8_t state = 0;
ESP.wdtFeed(); switch (state)
{
case 0:
server.handleClient();
state = 1;
ESP.wdtFeed();
break;
case 1:
lightEngine();
state = 2;
ESP.wdtFeed();
case 2:
tc_update_loop();
state = 3;
ESP.wdtFeed();
break;
case 3:
test_pwm_main();
state = 0;
ESP.wdtFeed();
break;
default:
state = 0;
}
lightEngine(); delay(10);
//Serial.println("tc_enabled = " + (String)tc_enabled);
tc_update_loop();
ESP.wdtFeed();
test_pwm_main();
delay(100);
} }
//********************************// //********************************//
@ -177,6 +192,7 @@ void loop()
void read_eeprom_config() void read_eeprom_config()
{ {
// -------------------- //
uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS);
if (tmp == TIMING_CONTROL_DISABLED) if (tmp == TIMING_CONTROL_DISABLED)
{ {
@ -195,6 +211,7 @@ void read_eeprom_config()
} }
Serial.println("Timing Control status: " + (String)tc_enabled); Serial.println("Timing Control status: " + (String)tc_enabled);
// -------------------- //
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) > 2) if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) > 2)
{ {
// set the default value on uninitialized EEPROM // set the default value on uninitialized EEPROM
@ -213,6 +230,7 @@ void read_eeprom_config()
} }
Serial.println("Scene setting: " + (String)EEPROM.read(EEPROM_SCENE_ADDRESS)); Serial.println("Scene setting: " + (String)EEPROM.read(EEPROM_SCENE_ADDRESS));
// -------------------- //
#ifdef USE_STATIC_IP #ifdef USE_STATIC_IP
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) > 1) if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) > 1)
{ {
@ -230,6 +248,7 @@ void read_eeprom_config()
#endif #endif
Serial.println("Dynamic IP setting: " + (String)EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS)); Serial.println("Dynamic IP setting: " + (String)EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS));
// -------------------- //
for (uint8_t light = 0; light < LIGHTS_COUNT; light++) for (uint8_t light = 0; light < LIGHTS_COUNT; light++)
{ {
apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light); apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light);

View file

@ -56,7 +56,9 @@ void lightEngine()
{ {
in_transition = true; in_transition = true;
current_bri[i] += step_level[i] / BRI_MOD_STEPS_PER_SEC; 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]; current_bri[i] = bri[i];
//Serial.println("Reached target bri[" + (String)i + "] = " + (String)bri[i]); //Serial.println("Reached target bri[" + (String)i + "] = " + (String)bri[i]);
@ -73,6 +75,7 @@ void lightEngine()
{ {
in_transition = true; in_transition = true;
current_bri[i] -= step_level[i] / BRI_MOD_STEPS_PER_SEC; current_bri[i] -= step_level[i] / BRI_MOD_STEPS_PER_SEC;
if (current_bri[i] < 0) if (current_bri[i] < 0)
{ {
current_bri[i] = 0; current_bri[i] = 0;

View file

@ -76,7 +76,7 @@ void test_pwm_main()
bri[0] = 0; bri[0] = 0;
current_bri[0] = 0; current_bri[0] = 0;
current_pwm[0] = 0; current_pwm[0] = 0;
transitiontime[0] = 0; transitiontime[0] = default_transitiontime;
process_lightdata(0, transitiontime[0]); process_lightdata(0, transitiontime[0]);
light_state[1] = true; light_state[1] = true;
@ -113,7 +113,7 @@ void test_pwm_main()
bri[1] = 0; bri[1] = 0;
current_bri[1] = 0; current_bri[1] = 0;
current_pwm[1] = 0; current_pwm[1] = 0;
transitiontime[1] = 0; transitiontime[1] = default_transitiontime;
process_lightdata(1, transitiontime[1]); process_lightdata(1, transitiontime[1]);
light_state[2] = true; light_state[2] = true;
@ -150,7 +150,7 @@ void test_pwm_main()
bri[2] = 0; bri[2] = 0;
current_bri[2] = 0; current_bri[2] = 0;
current_pwm[2] = 0; current_pwm[2] = 0;
transitiontime[2] = 0; transitiontime[2] = default_transitiontime;
process_lightdata(2, transitiontime[2]); process_lightdata(2, transitiontime[2]);
light_state[3] = true; light_state[3] = true;
@ -192,7 +192,7 @@ void test_pwm_main()
bri[i] = 0; bri[i] = 0;
current_bri[i] = 0; current_bri[i] = 0;
current_pwm[i] = 0; current_pwm[i] = 0;
transitiontime[i] = 0; transitiontime[i] = default_transitiontime;
process_lightdata(i, transitiontime[i]); process_lightdata(i, transitiontime[i]);
} }

View file

@ -180,10 +180,11 @@ void tc_update_main()
// no new predecessor or successor found, start over // no new predecessor or successor found, start over
current_target_data_block = 255; current_target_data_block = 255;
Serial.println("No predecessor or successor found, start over...");
// disable the lights // disable the lights
for (uint8_t i = 0; i < LIGHTS_COUNT; i++) for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
{ {
light_state[i] = false;
bri[i] = 0; bri[i] = 0;
current_bri[i] = 0; current_bri[i] = 0;
current_pwm[i] = 0; current_pwm[i] = 0;
@ -207,10 +208,15 @@ void tc_update_main()
if (target_data_block >= NUMBER_OF_TIMER_DATA_BLOCKS) if (target_data_block >= NUMBER_OF_TIMER_DATA_BLOCKS)
{ {
// we are not between two valid data points, do nothing // we are not between two valid data points, do nothing
// TODO check if setting all lights to false is correct here Serial.println("tdb is >= num data blocks, abort operation...");
for (uint8_t i = 0; i < LIGHTS_COUNT; i++) for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
{ {
light_state[i] = false; light_state[i] = false;
bri[i] = 0;
current_bri[i] = 0;
current_pwm[i] = 0;
transitiontime[i] = default_transitiontime;
process_lightdata(i, transitiontime[i]);
} }
target_data_block = 255; target_data_block = 255;
current_target_data_block = 255; current_target_data_block = 255;
@ -233,7 +239,7 @@ void tc_update_main()
} }
Serial.println("-----\ntdb = " + (String)target_data_block); Serial.println("-----\ntdb = " + (String)target_data_block);
Serial.print("target time: "); Serial.print("target dataset time: ");
Serial.print(tc_data[target_data_block].hh); Serial.print(tc_data[target_data_block].hh);
Serial.print(":"); Serial.print(":");
Serial.print(tc_data[target_data_block].mm); Serial.print(tc_data[target_data_block].mm);
@ -271,13 +277,9 @@ void tc_update_main()
} }
// enable the lights // enable the lights
light_state[0] = true;
light_state[1] = true;
light_state[2] = true;
light_state[3] = true;
for (uint8_t i = 0; i < LIGHTS_COUNT; i++) for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
{ {
light_state[i] = true;
Serial.println("light_state[" + (String)i + "] = " + (String)light_state[i]); Serial.println("light_state[" + (String)i + "] = " + (String)light_state[i]);
} }

View file

@ -24,6 +24,7 @@ void handleNotFound()
void init_webserver() void init_webserver()
{ {
// -------------------- //
server.on("/state", HTTP_PUT, []() server.on("/state", HTTP_PUT, []()
{ // HTTP PUT request used to set a new light state { // HTTP PUT request used to set a new light state
DynamicJsonDocument root(512); DynamicJsonDocument root(512);
@ -101,6 +102,7 @@ void init_webserver()
} }
}); });
// -------------------- //
server.on("/state", HTTP_GET, []() server.on("/state", HTTP_GET, []()
{ // HTTP GET request used to fetch current light state { // HTTP GET request used to fetch current light state
uint8_t light = server.arg("light").toInt() - 1; uint8_t light = server.arg("light").toInt() - 1;
@ -121,6 +123,7 @@ void init_webserver()
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
}); });
// -------------------- //
server.on("/detect", []() server.on("/detect", []()
{ // HTTP GET request used to discover the light type { // HTTP GET request used to discover the light type
char macString[32] = { 0 }; char macString[32] = { 0 };
@ -138,6 +141,7 @@ void init_webserver()
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
}); });
// -------------------- //
server.on("/test_pwm", []() server.on("/test_pwm", []()
{ {
test_pwm = true; test_pwm = true;
@ -147,6 +151,7 @@ void init_webserver()
server.send(200, "text/html", "OK"); server.send(200, "text/html", "OK");
}); });
// -------------------- //
server.on("/tc_data_blocks_read", []() server.on("/tc_data_blocks_read", []()
{ {
String output = tc_getJsonData(); String output = tc_getJsonData();
@ -154,6 +159,7 @@ void init_webserver()
server.send(200, "application/json", output); server.send(200, "application/json", output);
}); });
// -------------------- //
server.on("/tc_data_blocks_store", []() server.on("/tc_data_blocks_store", []()
{ {
if (server.hasArg("data")) if (server.hasArg("data"))
@ -165,21 +171,25 @@ void init_webserver()
} }
}); });
// -------------------- //
server.on("/js_top", []() server.on("/js_top", []()
{ {
server.send(200, "text/html", replacePlaceholder(loadSPIFFSFile("/top.js"))); server.send(200, "text/html", replacePlaceholder(loadSPIFFSFile("/top.js")));
}); });
// -------------------- //
server.on("/js_bottom", []() server.on("/js_bottom", []()
{ {
server.send(200, "text/html", replacePlaceholder(loadSPIFFSFile("/bottom.js"))); server.send(200, "text/html", replacePlaceholder(loadSPIFFSFile("/bottom.js")));
}); });
// -------------------- //
server.on("/css", []() server.on("/css", []()
{ {
server.send(200, "text/css", loadSPIFFSFile("/style.css")); server.send(200, "text/css", loadSPIFFSFile("/style.css"));
}); });
// -------------------- //
server.on("/", []() server.on("/", []()
{ {
@ -392,6 +402,7 @@ void init_webserver()
}); });
// -------------------- //
server.on("/reset", []() server.on("/reset", []()
{ // trigger manual reset { // trigger manual reset
server.send(200, "text/html", "reset"); server.send(200, "text/html", "reset");
@ -603,3 +614,5 @@ String getLightControlHTML()
// load file // load file
return loadSPIFFSFile("/light_control_template.html"); return loadSPIFFSFile("/light_control_template.html");
} }
//********************************//