Minor refactoring.

This commit is contained in:
2023-08-22 21:20:48 +02:00
parent 0f2b35be2a
commit a0cf358b53

View File

@@ -323,19 +323,22 @@ void loop()
void update_state_machine() void update_state_machine()
{ {
if (state_auto_change && (millis() - last_state_change > PERIOD_STATE_CHANGE) && !night_mode) unsigned long current_time = millis();
if (state_auto_change && (current_time - last_state_change > PERIOD_STATE_CHANGE) && !night_mode)
{ {
// increment state variable and trigger state change // increment state variable and trigger state change
state_change((current_state + 1) % NUM_STATES); state_change((current_state + 1) % NUM_STATES);
// save last automatic state change // save last automatic state change
last_state_change = millis(); last_state_change = current_time;
} }
} }
void handle_current_state() void handle_current_state()
{ {
// handle mode behaviours (trigger loopCycles of different modes depending on current mode)
unsigned long current_time = millis(); unsigned long current_time = millis();
// handle mode behaviours (trigger loopCycles of different modes depending on current mode)
if (!night_mode && ((current_time - last_step) > PERIODS[state_auto_change][current_state]) && ((current_time - last_led_direct) > TIMEOUT_LEDDIRECT)) if (!night_mode && ((current_time - last_step) > PERIODS[state_auto_change][current_state]) && ((current_time - last_led_direct) > TIMEOUT_LEDDIRECT))
{ {
switch (current_state) switch (current_state)
@@ -426,6 +429,7 @@ void handle_current_state()
void update_matrix() void update_matrix()
{ {
unsigned long current_time = millis(); unsigned long current_time = millis();
// periodically write colors to matrix // periodically write colors to matrix
if ((current_time - last_animation_step) > PERIOD_MATRIX_UPDATE) if ((current_time - last_animation_step) > PERIOD_MATRIX_UPDATE)
{ {
@@ -442,6 +446,7 @@ void update_matrix()
void send_heartbeat() void send_heartbeat()
{ {
unsigned long current_time = millis(); unsigned long current_time = millis();
// send regularly heartbeat messages via UDP multicast // send regularly heartbeat messages via UDP multicast
if ((current_time - last_heartbeat) > PERIOD_HEARTBEAT) if ((current_time - last_heartbeat) > PERIOD_HEARTBEAT)
{ {
@@ -467,6 +472,7 @@ void send_heartbeat()
void check_night_mode() void check_night_mode()
{ {
unsigned long current_time = millis(); unsigned long current_time = millis();
// check if nightmode need to be activated // check if nightmode need to be activated
if ((current_time - last_nightmode_check) > PERIOD_NIGHTMODE_CHECK) if ((current_time - last_nightmode_check) > PERIOD_NIGHTMODE_CHECK)
{ {
@@ -493,6 +499,7 @@ void check_night_mode()
void ntp_time_update() void ntp_time_update()
{ {
unsigned long current_time = millis(); unsigned long current_time = millis();
// NTP time update // NTP time update
if ((current_time - last_ntp_update) > PERIOD_NTP_UPDATE) if ((current_time - last_ntp_update) > PERIOD_NTP_UPDATE)
{ {
@@ -620,8 +627,7 @@ void state_change(uint8_t newState)
{ {
if (night_mode) if (night_mode)
{ {
// deactivate Nightmode set_night_mode(false); // deactivate Nightmode
set_night_mode(false);
} }
// first clear matrix // first clear matrix
led_matrix.flush(); led_matrix.flush();
@@ -686,6 +692,7 @@ void handle_button()
{ {
static bool lastButtonState = false; static bool lastButtonState = false;
bool buttonPressed = !digitalRead(BUTTON_PIN); bool buttonPressed = !digitalRead(BUTTON_PIN);
// check rising edge // check rising edge
if (buttonPressed == true && lastButtonState == false) if (buttonPressed == true && lastButtonState == false)
{ {