diff --git a/include/wordclock_constants.h b/include/wordclock_constants.h index 0f3c6e7..2c95014 100644 --- a/include/wordclock_constants.h +++ b/include/wordclock_constants.h @@ -36,7 +36,7 @@ typedef enum #define NIGHTMODE_END_HR 7 #define NIGHTMODE_END_MIN 0 -// Timings +// Timings in ms #define PERIOD_ANIMATION 200 #define PERIOD_HEARTBEAT 1000 #define PERIOD_MATRIX_UPDATE 100 diff --git a/src/wordclock_esp8266.cpp b/src/wordclock_esp8266.cpp index 30a3101..04c7d75 100644 --- a/src/wordclock_esp8266.cpp +++ b/src/wordclock_esp8266.cpp @@ -105,8 +105,8 @@ static unsigned long last_step = millis(); // t static bool night_mode = false; // stores state of nightmode static bool state_auto_change = false; // stores state of automatic state change static float filter_factor = DEFAULT_SMOOTHING_FACTOR; // stores smoothing factor for led transition -static uint32_t maincolor_clock = colors_24bit[2]; // color of the clock and digital clock -static uint32_t maincolor_snake = colors_24bit[1]; // color of the random snake animation +static uint32_t main_color_clock = colors_24bit[2]; // color of the clock and digital clock +static uint32_t main_color_snake = colors_24bit[1]; // color of the random snake animation static uint8_t current_state = (uint8_t)ST_CLOCK; // stores current state static int watchdog_counter = 30; // Watchdog counter to trigger restart if NTP update was not possible 30 times in a row (5min) @@ -155,14 +155,11 @@ void setup() led_matrix.draw_on_matrix_instant(); /** Use WiFiMaanger for handling initial Wifi setup **/ - // Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; - // fetches ssid and pass from eeprom and tries to connect - // if it does not connect it starts an access point with the specified name - // here "wordclockAP" - // and goes into a blocking loop awaiting configuration + /* fetches ssid and pass from eeprom and tries to connect. if it does not connect it starts an access point with + * the specified name and goes into a blocking loop awaiting configuration. */ wifiManager.autoConnect(AP_SSID); // if you get here you have connected to the WiFi @@ -180,7 +177,7 @@ void setup() setupOTA(HOSTNAME); webserver.on("/cmd", handle_command); // process commands - webserver.on("/data", handle_data_request); // process datarequests + webserver.on("/data", handle_data_request); // process data requests webserver.on("/leddirect", HTTP_POST, handle_led_direct); // Call the 'handle_led_direct' function when a POST request is made to URI "/leddirect" webserver.begin(); @@ -217,11 +214,11 @@ void setup() // display IP uint8_t address = WiFi.localIP()[3]; - led_matrix.print_char(1, 0, 'I', maincolor_clock); - led_matrix.print_char(5, 0, 'P', maincolor_clock); - led_matrix.print_number(0, 6, (address / 100), maincolor_clock); - led_matrix.print_number(4, 6, (address / 10) % 10, maincolor_clock); - led_matrix.print_number(8, 6, address % 10, maincolor_clock); + led_matrix.print_char(1, 0, 'I', main_color_clock); + led_matrix.print_char(5, 0, 'P', main_color_clock); + led_matrix.print_number(0, 6, (address / 100), main_color_clock); + led_matrix.print_number(4, 6, (address / 10) % 10, main_color_clock); + led_matrix.print_number(8, 6, address % 10, main_color_clock); led_matrix.draw_on_matrix_instant(); delay(2000); @@ -239,8 +236,8 @@ void setup() int hours = ntp_client.getHours24(); int minutes = ntp_client.getMinutes(); String timeMessage = time_to_string(hours, minutes); - show_string_on_clock(timeMessage, maincolor_clock); - draw_minute_indicator(minutes, maincolor_clock); + show_string_on_clock(timeMessage, main_color_clock); + draw_minute_indicator(minutes, main_color_clock); led_matrix.draw_on_matrix_smooth(filter_factor); // init all animation modes @@ -298,13 +295,13 @@ void loop() webserver.handleClient(); // handle webserver logger.log_string("After handleClient"); - send_heartbeat(); // send heartbeat update + send_heartbeat(); // send heartbeat update logger.log_string("After send_heartbeat"); - handle_current_state(); // handle current state - main process + handle_current_state(); // handle current state - main process logger.log_string("After handle_current_state"); - update_matrix(); // update matrix + update_matrix(); // update matrix logger.log_string("After update_matrix"); handle_button(); // handle button press @@ -313,10 +310,10 @@ void loop() update_state_machine(); // handle state changes logger.log_string("After update_state_machine"); - ntp_time_update(); // ntp time update + ntp_time_update(); // ntp time update logger.log_string("After ntp_time_update"); - check_night_mode(); // check night mode + check_night_mode(); // check night mode logger.log_string("After check_night_mode"); } @@ -338,7 +335,8 @@ void update_state_machine() void handle_current_state() { // handle mode behaviours (trigger loopCycles of different modes depending on current mode) - if (!night_mode && (millis() - last_step > PERIODS[state_auto_change][current_state]) && (millis() - last_led_direct > TIMEOUT_LEDDIRECT)) + unsigned long current_time = millis(); + if (!night_mode && ((current_time - last_step) > PERIODS[state_auto_change][current_state]) && ((current_time - last_led_direct) > TIMEOUT_LEDDIRECT)) { switch (current_state) { @@ -347,15 +345,15 @@ void handle_current_state() int hours = ntp_client.getHours24(); int minutes = ntp_client.getMinutes(); - (void)show_string_on_clock(time_to_string((uint8_t)hours, (uint8_t)minutes), maincolor_clock); - draw_minute_indicator((uint8_t)minutes, maincolor_clock); + (void)show_string_on_clock(time_to_string((uint8_t)hours, (uint8_t)minutes), main_color_clock); + draw_minute_indicator((uint8_t)minutes, main_color_clock); break; } case ST_DICLOCK: // state diclock { int hours = ntp_client.getHours24(); int minutes = ntp_client.getMinutes(); - show_digital_clock((uint8_t)hours, (uint8_t)minutes, maincolor_clock); + show_digital_clock((uint8_t)hours, (uint8_t)minutes, main_color_clock); break; } case ST_SPIRAL: // state spiral @@ -394,10 +392,10 @@ void handle_current_state() if (state_auto_change) { led_matrix.flush(); - if (random_snake(false, 8, maincolor_snake, -1)) + if (random_snake(false, 8, main_color_snake, -1)) { // init snake for next run - random_snake(true, 8, maincolor_snake, -1); + random_snake(true, 8, main_color_snake, -1); } } else @@ -427,11 +425,12 @@ void handle_current_state() */ void update_matrix() { + unsigned long current_time = millis(); // periodically write colors to matrix - if (millis() - last_animation_step > PERIOD_MATRIX_UPDATE) + if ((current_time - last_animation_step) > PERIOD_MATRIX_UPDATE) { led_matrix.draw_on_matrix_smooth(filter_factor); - last_animation_step = millis(); + last_animation_step = current_time; } } @@ -442,11 +441,12 @@ void update_matrix() */ void send_heartbeat() { + unsigned long current_time = millis(); // send regularly heartbeat messages via UDP multicast - if (millis() - last_heartbeat > PERIOD_HEARTBEAT) + if ((current_time - last_heartbeat) > PERIOD_HEARTBEAT) { - logger.log_string("Heartbeat, state: " + state_names[current_state] + ", FreeHeap: " + ESP.getFreeHeap() + ", HeapFrag: " + ESP.getHeapFragmentation() + ", MaxFreeBlock: " + ESP.getMaxFreeBlockSize() + "\nCounter: " + dbg_counter + " , Hours: " + (float)(dbg_counter) / 3600.0 + "\n"); // TODO CHANGE - last_heartbeat = millis(); + logger.log_string("Heartbeat, state: " + state_names[current_state] + ", FreeHeap: " + ESP.getFreeHeap() + ", HeapFrag: " + ESP.getHeapFragmentation() + ", MaxFreeBlock: " + ESP.getMaxFreeBlockSize() + "\nCounter: " + dbg_counter + " , Hours: " + (float)(dbg_counter) / 3600.0 + "\n"); // TODO CHANGE + last_heartbeat = current_time; // Check wifi status (only if no apmode) if (WiFi.status() != WL_CONNECTED) @@ -466,8 +466,9 @@ void send_heartbeat() */ void check_night_mode() { + unsigned long current_time = millis(); // check if nightmode need to be activated - if (millis() - last_nightmode_check > PERIOD_NIGHTMODE_CHECK) + if ((current_time - last_nightmode_check) > PERIOD_NIGHTMODE_CHECK) { int hours = ntp_client.getHours24(); int minutes = ntp_client.getMinutes(); @@ -480,8 +481,7 @@ void check_night_mode() { set_night_mode(false); } - - last_nightmode_check = millis(); + last_nightmode_check = current_time; } } @@ -492,8 +492,9 @@ void check_night_mode() */ void ntp_time_update() { + unsigned long current_time = millis(); // NTP time update - if (millis() - last_ntp_update > PERIOD_NTP_UPDATE) + if ((current_time - last_ntp_update) > PERIOD_NTP_UPDATE) { int ntp_retval = ntp_client.updateNTP(); switch (ntp_retval) @@ -728,7 +729,7 @@ void handle_button() void set_main_color(uint8_t red, uint8_t green, uint8_t blue) { - maincolor_clock = LEDMatrix::color_24bit(red, green, blue); + main_color_clock = LEDMatrix::color_24bit(red, green, blue); EEPROM.put(ADR_MC_RED, red); EEPROM.put(ADR_MC_GREEN, green); EEPROM.put(ADR_MC_BLUE, blue); @@ -736,7 +737,7 @@ void set_main_color(uint8_t red, uint8_t green, uint8_t blue) } /** - * @brief Load maincolor from EEPROM + * @brief Load main_color from EEPROM * */ @@ -747,11 +748,11 @@ void load_main_color() uint8_t blue = EEPROM.read(ADR_MC_BLUE); if (int(red) + int(green) + int(blue) < 50) { - maincolor_clock = colors_24bit[2]; + main_color_clock = colors_24bit[2]; } else { - maincolor_clock = LEDMatrix::color_24bit(red, green, blue); + main_color_clock = LEDMatrix::color_24bit(red, green, blue); } }