Compare commits

...

2 Commits

3 changed files with 42 additions and 41 deletions

View File

@@ -36,7 +36,7 @@ typedef enum
#define NIGHTMODE_END_HR 7 #define NIGHTMODE_END_HR 7
#define NIGHTMODE_END_MIN 0 #define NIGHTMODE_END_MIN 0
// Timings // Timings in ms
#define PERIOD_ANIMATION 200 #define PERIOD_ANIMATION 200
#define PERIOD_HEARTBEAT 1000 #define PERIOD_HEARTBEAT 1000
#define PERIOD_MATRIX_UPDATE 100 #define PERIOD_MATRIX_UPDATE 100

View File

@@ -1,5 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include "wordclock_functions.h" #include "render_functions.h"
#include "ledmatrix.h" #include "ledmatrix.h"
#include "udp_logger.h" #include "udp_logger.h"

View File

@@ -105,8 +105,8 @@ static unsigned long last_step = millis(); // t
static bool night_mode = false; // stores state of nightmode static bool night_mode = false; // stores state of nightmode
static bool state_auto_change = false; // stores state of automatic state change 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 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 main_color_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_snake = colors_24bit[1]; // color of the random snake animation
static uint8_t current_state = (uint8_t)ST_CLOCK; // stores current state 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) 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(); led_matrix.draw_on_matrix_instant();
/** Use WiFiMaanger for handling initial Wifi setup **/ /** Use WiFiMaanger for handling initial Wifi setup **/
// Local intialization. Once its business is done, there is no need to keep it around // Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager; WiFiManager wifiManager;
// fetches ssid and pass from eeprom and tries to connect /* fetches ssid and pass from eeprom and tries to connect. if it does not connect it starts an access point with
// if it does not connect it starts an access point with the specified name * the specified name and goes into a blocking loop awaiting configuration. */
// here "wordclockAP"
// and goes into a blocking loop awaiting configuration
wifiManager.autoConnect(AP_SSID); wifiManager.autoConnect(AP_SSID);
// if you get here you have connected to the WiFi // if you get here you have connected to the WiFi
@@ -217,11 +214,11 @@ void setup()
// display IP // display IP
uint8_t address = WiFi.localIP()[3]; uint8_t address = WiFi.localIP()[3];
led_matrix.print_char(1, 0, 'I', maincolor_clock); led_matrix.print_char(1, 0, 'I', main_color_clock);
led_matrix.print_char(5, 0, 'P', maincolor_clock); led_matrix.print_char(5, 0, 'P', main_color_clock);
led_matrix.print_number(0, 6, (address / 100), maincolor_clock); led_matrix.print_number(0, 6, (address / 100), main_color_clock);
led_matrix.print_number(4, 6, (address / 10) % 10, maincolor_clock); led_matrix.print_number(4, 6, (address / 10) % 10, main_color_clock);
led_matrix.print_number(8, 6, address % 10, maincolor_clock); led_matrix.print_number(8, 6, address % 10, main_color_clock);
led_matrix.draw_on_matrix_instant(); led_matrix.draw_on_matrix_instant();
delay(2000); delay(2000);
@@ -239,8 +236,8 @@ void setup()
int hours = ntp_client.getHours24(); int hours = ntp_client.getHours24();
int minutes = ntp_client.getMinutes(); int minutes = ntp_client.getMinutes();
String timeMessage = time_to_string(hours, minutes); String timeMessage = time_to_string(hours, minutes);
show_string_on_clock(timeMessage, maincolor_clock); show_string_on_clock(timeMessage, main_color_clock);
draw_minute_indicator(minutes, maincolor_clock); draw_minute_indicator(minutes, main_color_clock);
led_matrix.draw_on_matrix_smooth(filter_factor); led_matrix.draw_on_matrix_smooth(filter_factor);
// init all animation modes // init all animation modes
@@ -338,7 +335,8 @@ void update_state_machine()
void handle_current_state() void handle_current_state()
{ {
// handle mode behaviours (trigger loopCycles of different modes depending on current mode) // 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) switch (current_state)
{ {
@@ -347,15 +345,15 @@ void handle_current_state()
int hours = ntp_client.getHours24(); int hours = ntp_client.getHours24();
int minutes = ntp_client.getMinutes(); int minutes = ntp_client.getMinutes();
(void)show_string_on_clock(time_to_string((uint8_t)hours, (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, maincolor_clock); draw_minute_indicator((uint8_t)minutes, main_color_clock);
break; break;
} }
case ST_DICLOCK: // state diclock case ST_DICLOCK: // state diclock
{ {
int hours = ntp_client.getHours24(); int hours = ntp_client.getHours24();
int minutes = ntp_client.getMinutes(); 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; break;
} }
case ST_SPIRAL: // state spiral case ST_SPIRAL: // state spiral
@@ -394,10 +392,10 @@ void handle_current_state()
if (state_auto_change) if (state_auto_change)
{ {
led_matrix.flush(); 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 // init snake for next run
random_snake(true, 8, maincolor_snake, -1); random_snake(true, 8, main_color_snake, -1);
} }
} }
else else
@@ -427,11 +425,12 @@ void handle_current_state()
*/ */
void update_matrix() void update_matrix()
{ {
unsigned long current_time = millis();
// periodically write colors to matrix // 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); 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() void send_heartbeat()
{ {
unsigned long current_time = millis();
// send regularly heartbeat messages via UDP multicast // 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 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(); last_heartbeat = current_time;
// Check wifi status (only if no apmode) // Check wifi status (only if no apmode)
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
@@ -466,8 +466,9 @@ void send_heartbeat()
*/ */
void check_night_mode() void check_night_mode()
{ {
unsigned long current_time = millis();
// check if nightmode need to be activated // 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 hours = ntp_client.getHours24();
int minutes = ntp_client.getMinutes(); int minutes = ntp_client.getMinutes();
@@ -480,8 +481,7 @@ void check_night_mode()
{ {
set_night_mode(false); set_night_mode(false);
} }
last_nightmode_check = current_time;
last_nightmode_check = millis();
} }
} }
@@ -492,8 +492,9 @@ void check_night_mode()
*/ */
void ntp_time_update() void ntp_time_update()
{ {
unsigned long current_time = millis();
// NTP time update // 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(); int ntp_retval = ntp_client.updateNTP();
switch (ntp_retval) switch (ntp_retval)
@@ -728,7 +729,7 @@ void handle_button()
void set_main_color(uint8_t red, uint8_t green, uint8_t blue) 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_RED, red);
EEPROM.put(ADR_MC_GREEN, green); EEPROM.put(ADR_MC_GREEN, green);
EEPROM.put(ADR_MC_BLUE, blue); 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); uint8_t blue = EEPROM.read(ADR_MC_BLUE);
if (int(red) + int(green) + int(blue) < 50) if (int(red) + int(green) + int(blue) < 50)
{ {
maincolor_clock = colors_24bit[2]; main_color_clock = colors_24bit[2];
} }
else else
{ {
maincolor_clock = LEDMatrix::color_24bit(red, green, blue); main_color_clock = LEDMatrix::color_24bit(red, green, blue);
} }
} }