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:
parent
7e6dd20d31
commit
c177404a75
5 changed files with 62 additions and 25 deletions
|
@ -157,19 +157,34 @@ void setup()
|
|||
|
||||
void loop()
|
||||
{
|
||||
static uint8_t state = 0;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case 0:
|
||||
server.handleClient();
|
||||
|
||||
state = 1;
|
||||
ESP.wdtFeed();
|
||||
|
||||
break;
|
||||
case 1:
|
||||
lightEngine();
|
||||
|
||||
//Serial.println("tc_enabled = " + (String)tc_enabled);
|
||||
tc_update_loop();
|
||||
|
||||
state = 2;
|
||||
ESP.wdtFeed();
|
||||
|
||||
case 2:
|
||||
tc_update_loop();
|
||||
state = 3;
|
||||
ESP.wdtFeed();
|
||||
break;
|
||||
case 3:
|
||||
test_pwm_main();
|
||||
delay(100);
|
||||
state = 0;
|
||||
ESP.wdtFeed();
|
||||
break;
|
||||
default:
|
||||
state = 0;
|
||||
}
|
||||
|
||||
delay(10);
|
||||
}
|
||||
|
||||
//********************************//
|
||||
|
@ -177,6 +192,7 @@ void loop()
|
|||
void read_eeprom_config()
|
||||
{
|
||||
|
||||
// -------------------- //
|
||||
uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS);
|
||||
if (tmp == TIMING_CONTROL_DISABLED)
|
||||
{
|
||||
|
@ -195,6 +211,7 @@ void read_eeprom_config()
|
|||
}
|
||||
Serial.println("Timing Control status: " + (String)tc_enabled);
|
||||
|
||||
// -------------------- //
|
||||
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) > 2)
|
||||
{
|
||||
// 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));
|
||||
|
||||
// -------------------- //
|
||||
#ifdef USE_STATIC_IP
|
||||
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) > 1)
|
||||
{
|
||||
|
@ -230,6 +248,7 @@ void read_eeprom_config()
|
|||
#endif
|
||||
Serial.println("Dynamic IP setting: " + (String)EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS));
|
||||
|
||||
// -------------------- //
|
||||
for (uint8_t light = 0; light < LIGHTS_COUNT; light++)
|
||||
{
|
||||
apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light);
|
||||
|
|
|
@ -56,7 +56,9 @@ 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]);
|
||||
|
@ -73,6 +75,7 @@ void lightEngine()
|
|||
{
|
||||
in_transition = true;
|
||||
current_bri[i] -= step_level[i] / BRI_MOD_STEPS_PER_SEC;
|
||||
|
||||
if (current_bri[i] < 0)
|
||||
{
|
||||
current_bri[i] = 0;
|
||||
|
|
|
@ -76,7 +76,7 @@ void test_pwm_main()
|
|||
bri[0] = 0;
|
||||
current_bri[0] = 0;
|
||||
current_pwm[0] = 0;
|
||||
transitiontime[0] = 0;
|
||||
transitiontime[0] = default_transitiontime;
|
||||
process_lightdata(0, transitiontime[0]);
|
||||
|
||||
light_state[1] = true;
|
||||
|
@ -113,7 +113,7 @@ void test_pwm_main()
|
|||
bri[1] = 0;
|
||||
current_bri[1] = 0;
|
||||
current_pwm[1] = 0;
|
||||
transitiontime[1] = 0;
|
||||
transitiontime[1] = default_transitiontime;
|
||||
process_lightdata(1, transitiontime[1]);
|
||||
|
||||
light_state[2] = true;
|
||||
|
@ -150,7 +150,7 @@ void test_pwm_main()
|
|||
bri[2] = 0;
|
||||
current_bri[2] = 0;
|
||||
current_pwm[2] = 0;
|
||||
transitiontime[2] = 0;
|
||||
transitiontime[2] = default_transitiontime;
|
||||
process_lightdata(2, transitiontime[2]);
|
||||
|
||||
light_state[3] = true;
|
||||
|
@ -192,7 +192,7 @@ void test_pwm_main()
|
|||
bri[i] = 0;
|
||||
current_bri[i] = 0;
|
||||
current_pwm[i] = 0;
|
||||
transitiontime[i] = 0;
|
||||
transitiontime[i] = default_transitiontime;
|
||||
process_lightdata(i, transitiontime[i]);
|
||||
}
|
||||
|
||||
|
|
|
@ -180,10 +180,11 @@ void tc_update_main()
|
|||
// no new predecessor or successor found, start over
|
||||
current_target_data_block = 255;
|
||||
|
||||
Serial.println("No predecessor or successor found, start over...");
|
||||
|
||||
// disable the lights
|
||||
for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
|
||||
{
|
||||
light_state[i] = false;
|
||||
bri[i] = 0;
|
||||
current_bri[i] = 0;
|
||||
current_pwm[i] = 0;
|
||||
|
@ -207,10 +208,15 @@ void tc_update_main()
|
|||
if (target_data_block >= NUMBER_OF_TIMER_DATA_BLOCKS)
|
||||
{
|
||||
// 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++)
|
||||
{
|
||||
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;
|
||||
current_target_data_block = 255;
|
||||
|
@ -233,7 +239,7 @@ void tc_update_main()
|
|||
}
|
||||
|
||||
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(":");
|
||||
Serial.print(tc_data[target_data_block].mm);
|
||||
|
@ -271,13 +277,9 @@ void tc_update_main()
|
|||
}
|
||||
|
||||
// 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++)
|
||||
{
|
||||
light_state[i] = true;
|
||||
Serial.println("light_state[" + (String)i + "] = " + (String)light_state[i]);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ void handleNotFound()
|
|||
void init_webserver()
|
||||
{
|
||||
|
||||
// -------------------- //
|
||||
server.on("/state", HTTP_PUT, []()
|
||||
{ // HTTP PUT request used to set a new light state
|
||||
DynamicJsonDocument root(512);
|
||||
|
@ -101,6 +102,7 @@ void init_webserver()
|
|||
}
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/state", HTTP_GET, []()
|
||||
{ // HTTP GET request used to fetch current light state
|
||||
uint8_t light = server.arg("light").toInt() - 1;
|
||||
|
@ -121,6 +123,7 @@ void init_webserver()
|
|||
server.send(200, "text/plain", output);
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/detect", []()
|
||||
{ // HTTP GET request used to discover the light type
|
||||
char macString[32] = { 0 };
|
||||
|
@ -138,6 +141,7 @@ void init_webserver()
|
|||
server.send(200, "text/plain", output);
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/test_pwm", []()
|
||||
{
|
||||
test_pwm = true;
|
||||
|
@ -147,6 +151,7 @@ void init_webserver()
|
|||
server.send(200, "text/html", "OK");
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/tc_data_blocks_read", []()
|
||||
{
|
||||
String output = tc_getJsonData();
|
||||
|
@ -154,6 +159,7 @@ void init_webserver()
|
|||
server.send(200, "application/json", output);
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/tc_data_blocks_store", []()
|
||||
{
|
||||
if (server.hasArg("data"))
|
||||
|
@ -165,21 +171,25 @@ void init_webserver()
|
|||
}
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/js_top", []()
|
||||
{
|
||||
server.send(200, "text/html", replacePlaceholder(loadSPIFFSFile("/top.js")));
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/js_bottom", []()
|
||||
{
|
||||
server.send(200, "text/html", replacePlaceholder(loadSPIFFSFile("/bottom.js")));
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/css", []()
|
||||
{
|
||||
server.send(200, "text/css", loadSPIFFSFile("/style.css"));
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/", []()
|
||||
{
|
||||
|
||||
|
@ -392,6 +402,7 @@ void init_webserver()
|
|||
|
||||
});
|
||||
|
||||
// -------------------- //
|
||||
server.on("/reset", []()
|
||||
{ // trigger manual reset
|
||||
server.send(200, "text/html", "reset");
|
||||
|
@ -603,3 +614,5 @@ String getLightControlHTML()
|
|||
// load file
|
||||
return loadSPIFFSFile("/light_control_template.html");
|
||||
}
|
||||
|
||||
//********************************//
|
||||
|
|
Loading…
Reference in a new issue