Introduction of state machine for TimeManager. Minor refactoring.

This commit is contained in:
2024-04-04 16:40:16 +02:00
parent 5a51707452
commit 73b390d8cd
4 changed files with 105 additions and 45 deletions

View File

@@ -12,17 +12,29 @@ typedef enum
NTP_UPDATE_PENDING = 2,
NTP_UPDATE_RETRY_DELAY = 3,
NTP_UPDATE_TOO_EARLY = 4,
} ntp_update_state;
NTP_UPDATE_SETUP_FAILED = 5,
} NtpUpdateState;
typedef enum
{
TM_INIT = 0,
TM_INITIAL_SYNC = 1,
TM_RETRY_SYNC = 2,
TM_NORMAL = 3,
TM_PROLONGED_SYNC_FAIL = 4,
TM_SETUP_FAILED = 5,
} TimeManagerState;
class TimeManager
{
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
public:
bool ntp_sync_successful(void) const; // was there a NTP sync once?
ntp_update_state ntp_time_update(bool init = false);
bool ntp_update_failed_prolonged(void) const; // indicates if maximum time since last NTP update was too long
bool ntp_sync_successful(void) const; // was there a NTP sync once?
bool ntp_update_failed_prolonged(void); // indicates if maximum time since last NTP update was too long
NtpUpdateState ntp_time_update();
struct tm time_info(void);
TimeManagerState tm_state(void) const;
void increment_time_now_local(void);
void log_time() const; // log _time_info
void log_time(struct tm time_info) const; // log argument time_info
@@ -42,11 +54,12 @@ public:
UDPLogger *logger);
private:
void set_up_ntp(void) const; // set up NTP server
void set_up_timer_isr(void) const; // set up timer interrupt
void _set_up_ntp(void); // set up NTP server
void _set_up_timer_isr(void); // set up timer interrupt
const char *_tz; // timezone
const char *_ntp_server; // used ntp server
const char *_ntp_server; // ntp server address
UDPLogger *_logger; // logger instance
TimeManagerState _tm_state = TM_INIT; // Main state
struct tm _time_info = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // 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.

View File

@@ -34,7 +34,7 @@
// Timings in us
#define PERIOD_ANIMATION_US (200 * 1000) // 200ms
#define PERIOD_CLOCK_UPDATE_US (1 * 1000 * 1000) // 1s
#define PERIOD_CLOCK_UPDATE_US (1 * 1000 * 1000) // Must be 1s! Do not change!
#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