Add delays for stability.

This commit is contained in:
2023-08-28 01:27:37 +02:00
parent 16cd90d11d
commit c6f67ed21a

View File

@@ -46,9 +46,6 @@
#include "wordclock_constants.h" #include "wordclock_constants.h"
#include "render_functions.h" #include "render_functions.h"
// DEBUG
uint32_t dbg_counter = 0; // TODO RM
const String state_names[] = {"Clock", "DiClock", "Spiral", "Tetris", "Snake", "PingPong", "Hearts"}; const String state_names[] = {"Clock", "DiClock", "Spiral", "Tetris", "Snake", "PingPong", "Hearts"};
// PERIODS for each state (different for stateAutoChange or Manual mode) // PERIODS for each state (different for stateAutoChange or Manual mode)
const uint16_t PERIODS[2][NUM_STATES] = {{PERIOD_TIME_VISU_UPDATE, // stateAutoChange = 0 const uint16_t PERIODS[2][NUM_STATES] = {{PERIOD_TIME_VISU_UPDATE, // stateAutoChange = 0
@@ -110,6 +107,8 @@ NightModeTimes_st night_mode_times = {
NIGHTMODE_END_HR, NIGHTMODE_END_HR,
NIGHTMODE_END_MIN}; NIGHTMODE_END_MIN};
uint32_t heartbeat_counter = 0; // for heartbeat on-time in seconds
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// SETUP // SETUP
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
@@ -292,65 +291,48 @@ void loop()
static unsigned long last_state_change = 0; // time of last state change static unsigned long last_state_change = 0; // time of last state change
handleOTA(); // handle OTA handleOTA(); // handle OTA
yield();
webserver.handleClient(); // handle webserver webserver.handleClient(); // handle webserver
yield();
// send regularly heartbeat messages via UDP multicast // send regularly heartbeat messages via UDP multicast
if ((current_time_ms - last_heartbeat) > PERIOD_HEARTBEAT) if ((current_time_ms - last_heartbeat) > PERIOD_HEARTBEAT)
{ {
Serial.printf("a");
Serial.println();
send_heartbeat(); // send heartbeat update send_heartbeat(); // send heartbeat update
last_heartbeat = CURRENT_TIME_MS; last_heartbeat = CURRENT_TIME_MS;
yield();
Serial.printf("A");
} }
if (!night_mode && ((current_time_ms - last_animation_step) > PERIODS[state_auto_change][current_state]) && ((current_time_ms - last_led_direct) > TIMEOUT_LEDDIRECT)) if (!night_mode && ((current_time_ms - last_animation_step) > PERIODS[state_auto_change][current_state]) && ((current_time_ms - last_led_direct) > TIMEOUT_LEDDIRECT))
{ {
Serial.printf("b");
handle_current_state(); // handle current state handle_current_state(); // handle current state
last_animation_step = CURRENT_TIME_MS; last_animation_step = CURRENT_TIME_MS;
yield(); delay(10);
Serial.printf("B");
} }
if ((current_time_ms - last_matrix_update) > PERIOD_MATRIX_UPDATE) if ((current_time_ms - last_matrix_update) > PERIOD_MATRIX_UPDATE)
{ {
Serial.printf("c");
update_matrix(); // update matrix update_matrix(); // update matrix
last_matrix_update = CURRENT_TIME_MS; last_matrix_update = CURRENT_TIME_MS;
yield(); delay(10);
Serial.printf("C");
} }
handle_button(); // handle button press handle_button(); // handle button press
if (!night_mode && state_auto_change && (current_time_ms - last_state_change > PERIOD_STATE_CHANGE)) if (!night_mode && state_auto_change && (current_time_ms - last_state_change > PERIOD_STATE_CHANGE))
{ {
Serial.printf("d");
update_state_machine(); // handle state changes update_state_machine(); // handle state changes
last_state_change = CURRENT_TIME_MS; last_state_change = CURRENT_TIME_MS;
yield();
Serial.printf("D");
} }
if ((current_time_ms - last_ntp_update) > PERIOD_NTP_UPDATE) if ((current_time_ms - last_ntp_update) > PERIOD_NTP_UPDATE)
{ {
Serial.printf("e");
ntp_time_update(&last_ntp_update); // ntp time update ntp_time_update(&last_ntp_update); // ntp time update
yield(); delay(10);
Serial.printf("E");
} }
if ((current_time_ms - last_nightmode_check) > PERIOD_NIGHTMODE_CHECK) if ((current_time_ms - last_nightmode_check) > PERIOD_NIGHTMODE_CHECK)
{ {
Serial.printf("f");
check_night_mode(); // check night mode check_night_mode(); // check night mode
last_nightmode_check = CURRENT_TIME_MS; last_nightmode_check = CURRENT_TIME_MS;
Serial.printf("F");
} }
} }
@@ -468,8 +450,10 @@ void update_matrix()
void send_heartbeat() void send_heartbeat()
{ {
logger.log_string("Heartbeat, state: " + state_names[current_state] + ", FreeHeap: " + ESP.getFreeHeap() + logger.log_string("Heartbeat, state: " + state_names[current_state] + ", FreeHeap: " + ESP.getFreeHeap() +
", HeapFrag: " + ESP.getHeapFragmentation() + ", MaxFreeBlock: " + ESP.getMaxFreeBlockSize() + "\nCounter: " + ", HeapFrag: " + ESP.getHeapFragmentation() + ", MaxFreeBlock: " + ESP.getMaxFreeBlockSize() +
dbg_counter + " , Hours: " + (float)(dbg_counter) / 3600.0 + "\n"); // TODO CHANGE "\nCounter: " + heartbeat_counter + ", Hours: " + (float)(heartbeat_counter) / 3600.0 + "\n");
heartbeat_counter++;
// Check wifi status // Check wifi status
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
{ {
@@ -477,7 +461,6 @@ void send_heartbeat()
led_matrix.grid_add_pixel(0, 5, colors_24bit[1]); led_matrix.grid_add_pixel(0, 5, colors_24bit[1]);
led_matrix.draw_on_matrix_instant(); led_matrix.draw_on_matrix_instant();
} }
dbg_counter++; // TODO RM
} }
/** /**