Introduced new TimeManager class with much better offline handling. Refactoring.

This commit is contained in:
2024-04-03 21:49:28 +02:00
parent 0543b9c0c7
commit a4be8f1d9e
6 changed files with 335 additions and 143 deletions

52
include/time_manager.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef TIME_MANAGER_H
#define TIME_MANAGER_H
#include <Arduino.h>
#include <time.h>
#include "udp_logger.h"
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
class TimeManager
{
public:
struct tm time_info(void);
bool ntp_sync_successful(void) const; // was there a NTP sync once?
bool ntp_time_update(bool init = false);
bool ntp_update_failed_prolonged(void) const; // indicates if maximum time since last NTP update was too long
void log_time(struct tm time_info) const; // log local_time
void increment_time_now_local(void);
int tm_min(void);
int tm_hour(void);
int tm_year(void);
bool tm_isdst(void); // true if summertime
TimeManager(const char *tz,
const char *ntp_server,
uint32 ntp_update_period_s,
uint32 ntp_retry_delay_us,
uint32 ntp_max_offline_time_s,
UDPLogger *logger);
private:
void set_up_ntp(void) const; // set up NTP server
void set_up_timer_isr(void) const; // set up timer interrupt
const char *_tz; // timezone
const char *_ntp_server; // used ntp server
UDPLogger *_logger; // logger instance
struct tm _time_info; // structure tm holds time information
time_t _time_now_local = 0; // local timer value, updated by timer interrupt and synced by NTP when needed
time_t _time_now_ntp = 0; // NTP timer value, seconds since Epoch (1970) - UTC, only synced by NTP request.
uint32 _ntp_max_offline_time_s; // maximum time in seconds which is considered ok since last NTP update
uint32 _ntp_update_period_s; // NTP request update period in seconds
uint32 _ntp_retry_delay_us; // minimum retry delay in us between two NTP requests
uint32 _ntp_sync_timestamp_us = 0; // timestamp of last successful ntp update
};
inline void TimeManager::increment_time_now_local(void)
{
_time_now_local++;
}
#endif /* TIME_MANAGER_H */

View File

@@ -33,17 +33,18 @@
#define NIGHTMODE_END_MIN (0)
// Timings in us
#define PERIOD_ANIMATION_US (200 * 1000) // 200ms
#define PERIOD_CLOCK_UPDATE_US (1 * 1000 * 1000) // 1s
#define PERIOD_HEARTBEAT_US (1 * 1000 * 1000) // 1s
#define PERIOD_MATRIX_UPDATE_US (100 * 1000) // 100ms
#define PERIOD_NIGHTMODE_CHECK_US (20 * 1000 * 1000) // 20s
#define PERIOD_NTP_UPDATE_US (30 * 1000 * 1000) // 30s
#define PERIOD_PONG_US (10 * 1000) // 10ms
#define PERIOD_SNAKE_US (50 * 1000) // 50ms
#define PERIOD_STATE_CHANGE_US (10 * 1000 * 1000) // 10s
#define PERIOD_TETRIS_US (50 * 1000) // 50ms
#define TIMEOUT_LEDDIRECT_US (5 * 1000 * 1000) // 5s
#define PERIOD_ANIMATION_US (200 * 1000) // 200ms
#define PERIOD_CLOCK_UPDATE_US (1 * 1000 * 1000) // 1s
#define PERIOD_HEARTBEAT_US (1 * 1000 * 1000) // 1s
#define PERIOD_MATRIX_UPDATE_US (100 * 1000) // 100ms
#define PERIOD_NIGHTMODE_CHECK_US (20 * 1000 * 1000) // 20s
#define PERIOD_TIME_UPDATE_US (500 * 1000) // 500ms
#define PERIOD_PONG_US (10 * 1000) // 10ms
#define PERIOD_SNAKE_US (50 * 1000) // 50ms
#define PERIOD_STATE_CHANGE_US (10 * 1000 * 1000) // 10s
#define PERIOD_TETRIS_US (50 * 1000) // 50ms
#define TIMEOUT_LEDDIRECT_US (5 * 1000 * 1000) // 5s
#define PERIOD_BRIGHTNESS_UPDATE_US (5 * 60 * 1000 * 1000) // 300s
#define SHORT_PRESS_US (100 * 1000) // 100ms
#define LONG_PRESS_US (2 * 1000 * 1000) // 2s
@@ -68,11 +69,11 @@
#define MATRIX_HEIGHT (11)
// NTP macros
#define BUILD_YEAR (__DATE__ + 7) /* Will expand to current year at compile time as string. */
#define NTP_MININUM_RX_YEAR (atoi(BUILD_YEAR) - 1) /* Will expand to current year at compile time minus one. */
#define BUILD_YEAR (__DATE__ + 7) // Will expand to current year at compile time as string.
#define NTP_MININUM_RX_YEAR (atoi(BUILD_YEAR) - 1) // Will expand to current year minus one at compile time.
#define NTP_MININUM_YEAR (1900) // NTP minimum year is 1900
#define NTP_MAX_UPDATE_TIME_US (500000) // 500ms max update time
#define NTP_NEXT_UPDATE_DELAY_US (10000000) // 10s delay time between updates
#define NTP_WATCHDOG_COUNTER_INIT (30) // Watchdog value, count of retries before restart
#define NTP_UPDATE_PERIOD_S (12 * 3600) // 12h period between updates
#define NTP_RETRY_DELAY_US (10 * 1000 * 1000) // 10s retry delay time between failed NTP requests
#define NTP_MAX_OFFLINE_TIME_S (7 * 24 * 3600) // Watchdog value, maxmimum offline time before a restart is triggered
#endif /* WORDCLOCK_CONSTANTS_H */

View File

@@ -61,12 +61,11 @@ typedef enum
// ----------------------------------------------------------------------------------
// FUNCTIONS DECLARATIONS
// ----------------------------------------------------------------------------------
bool get_ntp_time(uint32 timeout);
bool check_wifi_status(void);
String leading_zero2digit(int value);
uint8_t calculate_dynamic_brightness(uint8_t min_brightness, uint8_t max_brightness, int hours, int minutes, bool summertime);
uint8_t update_brightness(void);
void check_night_mode(void);
void check_wifi_status(void);
void cold_start_setup(void);
void draw_main_color(void);
void handle_button(void);
@@ -76,8 +75,6 @@ void handle_data_request(void);
void handle_led_direct(void);
void limit_value_ranges(void);
void log_data(void);
void log_time(tm local_time);
void ntp_time_update(uint32 max_update_time);
void on_state_entry(uint8_t state);
void read_settings_from_EEPROM(void);
void reset_wifi_credentials(void);