Rework TimeManager & NTP handling. Remove module ESP8266TimerInterrupt.

This commit is contained in:
2024-05-28 19:06:39 +02:00
parent 2e50dd0e3f
commit ff90c610ae
5 changed files with 252 additions and 293 deletions

View File

@@ -28,8 +28,6 @@
#include <Adafruit_NeoPixel.h> // NeoPixel library used to run the NeoPixel LEDs: https://github.com/adafruit/Adafruit_NeoPixel
#include <base64.hpp>
#include <EEPROM.h> // from ESP8266 Arduino Core (automatically installed when ESP8266 was installed via Boardmanager)
#include <ESP8266_ISR_Timer.h> // https://github.com/khoih-prog/ESP8266TimerInterrupt
#include <ESP8266TimerInterrupt.h> // https://github.com/khoih-prog/ESP8266TimerInterrupt
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager WiFi Configuration Magic
@@ -71,20 +69,16 @@ static Pong pong = Pong(&led_matrix, &logger);
static Snake snake = Snake(&led_matrix, &logger);
static Tetris tetris = Tetris(&led_matrix, &logger);
// Time ManagerW
static TimeManager tm_mgr = TimeManager(MY_TZ, NTP_SERVER_URL,
NTP_UPDATE_PERIOD_S,
NTP_RETRY_DELAY_US,
NTP_MAX_OFFLINE_TIME_S,
&logger);
// Time Manager
static TimeManager tm_mgr;
// State variablesW
// State variables
static bool flg_night_mode = false; // State of nightmode
static bool flg_reset_wifi_creds = false; // Used to reset stored wifi credentials
static float filter_factor = DEFAULT_SMOOTHING_FACTOR; // Stores smoothing factor for led transition, value of 1 represents no smoothing.
static uint32_t main_color_clock = colors_24bit[2]; // Color of the clock and digital clock
static uint8_t current_brightness = DEFAULT_BRIGHTNESS; // Current brightness of LEDs
static uint8_t current_state = (uint8_t)ST_CLOCK; // Stores current state
static ClockState_en current_state = ST_CLOCK; // Stores current state
// Other variables
static uint32 last_led_direct_us = 0; // Time of last direct LED command (=> fall back to normal mode after timeout)
@@ -104,18 +98,6 @@ static const uint32_t period_timings[NUM_STATES] = {PERIOD_TIME_UPDATE_US, PERIO
PERIOD_ANIMATION_US, PERIOD_TETRIS_US, PERIOD_SNAKE_US,
PERIOD_PONG_US, PERIOD_ANIMATION_US};
// ----------------------------------------------------------------------------------
// STATIC VARIABLES
// ----------------------------------------------------------------------------------
ESP8266Timer ITimer; // ESP8266 Timer
// ----------------------------------------------------------------------------------
// ISR
// ----------------------------------------------------------------------------------
void IRAM_ATTR TimerHandler()
{
tm_mgr.increment_time_now_local();
}
// ----------------------------------------------------------------------------------
// SETUP
@@ -153,7 +135,7 @@ void setup()
led_matrix.set_current_limit(CURRENT_LIMIT_LED);
// Turn on minutes LEDs (blue)
led_matrix.set_min_indicator(15, colors_24bit[6]);
led_matrix.set_min_indicator((uint8_t)0b1111, colors_24bit[6]);
led_matrix.draw_on_matrix_instant();
/* Use WiFiMaanger for handling initial Wifi setup */
@@ -172,7 +154,7 @@ void setup()
WiFi.persistent(true);
// Turn off minutes LEDs
led_matrix.set_min_indicator(15, 0);
led_matrix.set_min_indicator((uint8_t)0b1111, 0);
led_matrix.draw_on_matrix_instant();
// init ESP8266 File manager (LittleFS)
@@ -194,13 +176,16 @@ void setup()
cold_start_setup();
}
// get initial time
if (tm_mgr.ntp_time_update() == NTP_UPDATE_OK)
// set up time manager and get initial time
tm_mgr = TimeManager(MY_TZ, NTP_SERVER_URL, check_wifi_status, NTP_MAX_OFFLINE_TIME_S, &logger);
tm_mgr.init();
if (tm_mgr.get_time() == TIME_UPDATE_OK)
{
// show the current time for short time in words
String timeMessage = time_to_string(tm_mgr.tm_hour(), tm_mgr.tm_min());
String timeMessage = time_to_string(tm_mgr.hour(), tm_mgr.minute());
show_string_on_clock(timeMessage, main_color_clock);
draw_minute_indicator(tm_mgr.tm_min(), main_color_clock);
draw_minute_indicator(tm_mgr.minute(), main_color_clock);
led_matrix.draw_on_matrix_smooth(filter_factor);
}
else
@@ -209,11 +194,8 @@ void setup()
}
// init all animation modes
// init snake
random_snake(true, 8, colors_24bit[1], -1);
// init spiral
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 6);
// init random tetris
random_tetris(true);
// Set range limits
@@ -280,17 +262,16 @@ void loop()
if ((current_time_us - last_time_update_us) >= PERIOD_TIME_UPDATE_US)
{
if (tm_mgr.ntp_time_update() == NTP_UPDATE_OK) // NTP time update
{
logger.log_string("NTP sync successful!");
}
(void)tm_mgr.get_time(); // NTP time update
if (tm_mgr.ntp_update_failed_prolonged() == true)
if (tm_mgr.ntp_sync_timeout())
{
logger.log_string("Trigger restart due to being offline for too long...");
delay(100);
ESP.restart();
}
last_time_update_us = system_get_time();
}
if ((current_time_us - last_nightmode_check_us) >= PERIOD_NIGHTMODE_CHECK_US)
@@ -366,87 +347,87 @@ void handle_current_state()
{
switch (current_state)
{
case ST_CLOCK: // state clock
{
if (tm_mgr.ntp_sync_successful() && tm_mgr.tm_state() == TM_NORMAL)
case ST_CLOCK: // state clock
{
(void)show_string_on_clock(time_to_string((uint8_t)tm_mgr.tm_hour(), (uint8_t)tm_mgr.tm_min()), main_color_clock);
draw_minute_indicator((uint8_t)tm_mgr.tm_min(), main_color_clock);
if (tm_mgr.tm_state() == TM_NORMAL)
{
(void)show_string_on_clock(time_to_string((uint8_t)tm_mgr.hour(), (uint8_t)tm_mgr.minute()), main_color_clock);
draw_minute_indicator((uint8_t)tm_mgr.minute(), main_color_clock);
}
else if (tm_mgr.ntp_sync_overdue()) // if NTP sync is overdue
{
(void)show_string_on_clock(time_to_string((uint8_t)tm_mgr.hour(), (uint8_t)tm_mgr.minute()), main_color_clock);
draw_minute_indicator((uint8_t)tm_mgr.minute(), colors_24bit[6]); // in blue to indicate a network problem
}
else // if no NTP sync has been done, only show 4 blue minute indicators
{
// clear matrix
led_matrix.flush();
// Turn on minutes LEDs (blue)
led_matrix.set_min_indicator((uint8_t)0b1111, colors_24bit[6]);
led_matrix.draw_on_matrix_instant();
}
break;
}
else if (tm_mgr.ntp_sync_successful() && tm_mgr.tm_state() == TM_RETRY_SYNC)
case ST_DICLOCK: // state diclock
{
(void)show_string_on_clock(time_to_string((uint8_t)tm_mgr.tm_hour(), (uint8_t)tm_mgr.tm_min()), main_color_clock);
draw_minute_indicator((uint8_t)tm_mgr.tm_min(), colors_24bit[6]); // in blue to indicate a network problem
if (tm_mgr.ntp_sync_successful())
{
show_digital_clock((uint8_t)tm_mgr.hour(), (uint8_t)tm_mgr.minute(), main_color_clock);
}
else
{
// clear matrix
led_matrix.flush();
// Turn on minutes LEDs (blue)
led_matrix.set_min_indicator((uint8_t)0b1111, colors_24bit[6]);
led_matrix.draw_on_matrix_instant();
}
break;
}
else
case ST_SPIRAL: // state spiral
{
// clear matrix
led_matrix.flush();
// Turn on minutes LEDs (blue)
led_matrix.set_min_indicator(15, colors_24bit[6]);
led_matrix.draw_on_matrix_instant();
int res = draw_spiral(false, spiral_direction, MATRIX_WIDTH - 2);
if ((bool)res && spiral_direction == 0)
{
// change spiral direction to closing (draw empty LEDs)
spiral_direction = true;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
}
else if (res && spiral_direction == 1)
{
// reset spiral direction to normal drawing LEDs
spiral_direction = false;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
}
break;
}
break;
}
case ST_DICLOCK: // state diclock
{
if (tm_mgr.ntp_sync_successful())
case ST_TETRIS: // state tetris
{
show_digital_clock((uint8_t)tm_mgr.tm_hour(), (uint8_t)tm_mgr.tm_min(), main_color_clock);
tetris.loopCycle();
break;
}
else
case ST_SNAKE: // state snake
{
// clear matrix
led_matrix.flush();
// Turn on minutes LEDs (blue)
led_matrix.set_min_indicator(15, colors_24bit[6]);
led_matrix.draw_on_matrix_instant();
snake.loopCycle();
break;
}
break;
}
case ST_SPIRAL: // state spiral
{
int res = draw_spiral(false, spiral_direction, MATRIX_WIDTH - 2);
if ((bool)res && spiral_direction == 0)
case ST_PINGPONG: // state ping pong
{
// change spiral direction to closing (draw empty LEDs)
spiral_direction = true;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
pong.loopCycle();
break;
}
else if (res && spiral_direction == 1)
case ST_HEARTS:
{
// reset spiral direction to normal drawing LEDs
spiral_direction = false;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
draw_heart_animation();
break;
}
default:
{
break;
}
break;
}
case ST_TETRIS: // state tetris
{
tetris.loopCycle();
break;
}
case ST_SNAKE: // state snake
{
snake.loopCycle();
break;
}
case ST_PINGPONG: // state ping pong
{
pong.loopCycle();
break;
}
case ST_HEARTS:
{
draw_heart_animation();
break;
}
default:
{
break;
}
}
}
@@ -498,9 +479,9 @@ bool check_wifi_status()
*/
void check_night_mode()
{
// check if nightmode need to be activated
int hours = tm_mgr.tm_hour();
int minutes = tm_mgr.tm_min();
// Check if nightmode needs to be activated. This only toggles at the exact minute.
int hours = tm_mgr.hour();
int minutes = tm_mgr.minute();
if ((hours == night_mode_times_ps->start_hour) && (minutes == night_mode_times_ps->start_min))
{
@@ -522,43 +503,43 @@ void on_state_entry(uint8_t state)
filter_factor = DEFAULT_SMOOTHING_FACTOR;
switch (state)
{
case ST_SPIRAL:
{
spiral_direction = 0; // Init spiral with normal drawing mode
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
break;
}
case ST_TETRIS:
{
filter_factor = 1.0f; // no smoothing
tetris.ctrlStart();
break;
}
case ST_SNAKE:
{
filter_factor = 1.0f; // no smoothing
snake.initGame();
break;
}
case ST_PINGPONG:
{
filter_factor = 1.0f; // no smoothing
pong.initGame(1);
break;
}
default:
{
break;
}
case ST_SPIRAL:
{
spiral_direction = 0; // Init spiral with normal drawing mode
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
break;
}
case ST_TETRIS:
{
filter_factor = 1.0f; // no smoothing
tetris.ctrlStart();
break;
}
case ST_SNAKE:
{
filter_factor = 1.0f; // no smoothing
snake.initGame();
break;
}
case ST_PINGPONG:
{
filter_factor = 1.0f; // no smoothing
pong.initGame(1);
break;
}
default:
{
break;
}
}
}
/**
* @brief execute a state change to given newState
* @brief execute a state change to given new_state
*
* @param newState the new state to be changed to
* @param new_state the new state to be changed to
*/
void state_change(uint8_t newState)
void state_change(ClockState_en new_state)
{
if (flg_night_mode)
{
@@ -566,9 +547,9 @@ void state_change(uint8_t newState)
}
led_matrix.flush(); // first clear matrix
current_state = newState; // set new state
on_state_entry(current_state);
logger.log_string("State change to: " + state_names[current_state]);
current_state = new_state; // set new state
on_state_entry((uint8_t)current_state);
logger.log_string("State change to: " + state_names[(uint8_t)current_state]);
logger.log_string("FreeMemory=" + String(ESP.getFreeHeap()));
}
@@ -660,7 +641,7 @@ void handle_button()
}
else
{
state_change((current_state + 1) % (uint8_t)NUM_STATES);
state_change((ClockState_en)(((uint8_t)current_state + 1) % (uint8_t)NUM_STATES));
}
}
}
@@ -735,6 +716,7 @@ void handle_command()
{
String mode_str = webserver.arg(0);
logger.log_string("Mode change via Webserver to: " + mode_str);
// set current mode/state accordant sent mode
if (mode_str.equals("clock"))
{
@@ -1049,9 +1031,9 @@ uint8_t update_brightness()
{
new_brightness = calculate_dynamic_brightness(brightness_ps->dyn_brightness_min,
brightness_ps->dyn_brightness_max,
tm_mgr.tm_hour(),
tm_mgr.tm_min(),
tm_mgr.tm_isdst());
tm_mgr.hour(),
tm_mgr.minute(),
tm_mgr.isdst());
}
else // use static brightness
{