Rework TimeManager & NTP handling. Remove module ESP8266TimerInterrupt.
This commit is contained in:
@@ -7,53 +7,54 @@
|
||||
|
||||
typedef enum
|
||||
{
|
||||
NTP_UPDATE_FAILED = 0,
|
||||
NTP_UPDATE_OK = 1,
|
||||
NTP_UPDATE_PENDING = 2,
|
||||
NTP_UPDATE_RETRY_DELAY = 3,
|
||||
NTP_UPDATE_TOO_EARLY = 4,
|
||||
NTP_UPDATE_SETUP_FAILED = 5,
|
||||
} NtpUpdateState;
|
||||
TIME_UPDATE_FAILED = 0,
|
||||
TIME_UPDATE_OK = 1,
|
||||
TIME_UPDATE_PENDING = 2,
|
||||
} TimeUpdateState;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TM_INIT = 0,
|
||||
TM_INITIAL_SYNC = 1,
|
||||
TM_RETRY_SYNC = 2,
|
||||
TM_NORMAL = 3,
|
||||
TM_PROLONGED_SYNC_FAIL = 4,
|
||||
TM_NORMAL = 2,
|
||||
TM_SYNC_OVERDUE = 3,
|
||||
TM_SYNC_TIMEOUT = 4,
|
||||
TM_SETUP_FAILED = 5,
|
||||
} TimeManagerState;
|
||||
|
||||
class TimeManager
|
||||
{
|
||||
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
|
||||
#define NTP_MAX_UPDATE_TIME_US (5 * 1000 * 1000) // 5000ms max update time
|
||||
|
||||
public:
|
||||
// constructor
|
||||
// constructors
|
||||
TimeManager();
|
||||
TimeManager(const char *tz,
|
||||
const char *ntp_server,
|
||||
uint32 ntp_update_period_s,
|
||||
uint32 ntp_retry_delay_us,
|
||||
bool (*is_wifi_connected)(void),
|
||||
uint32 ntp_max_offline_time_s,
|
||||
UDPLogger *logger);
|
||||
|
||||
// ntp methods
|
||||
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(); // main NTP time update method, called in loop
|
||||
// init
|
||||
void init();
|
||||
|
||||
// ISR method
|
||||
void increment_time_now_local(void); // should be called by timer ISR
|
||||
// callback
|
||||
void time_set_cb(void); // callback which is called when NTP time was set
|
||||
|
||||
// ntp methods
|
||||
bool ntp_sync_successful(void) const; // was there a NTP sync once?
|
||||
bool ntp_sync_overdue(void); // function to check if NTP sync is overdue
|
||||
bool ntp_sync_timeout(void); // function to check if maximum time since last NTP sync has been reached
|
||||
TimeUpdateState get_time(); // main time update method, called in loop
|
||||
|
||||
// getter for time values
|
||||
bool tm_isdst(void); // true if summertime
|
||||
int tm_day(void);
|
||||
int tm_hour(void);
|
||||
int tm_min(void);
|
||||
int tm_mon(void);
|
||||
int tm_year(void);
|
||||
struct tm time_info(void);
|
||||
bool isdst(void) const; // true if summertime (daylight saving time)
|
||||
int day(void) const;
|
||||
int hour(void) const;
|
||||
int minute(void) const;
|
||||
int month(void) const;
|
||||
int year(void) const;
|
||||
struct tm time_info(void) const;
|
||||
|
||||
// getter
|
||||
TimeManagerState tm_state(void) const; // get current state
|
||||
@@ -63,26 +64,17 @@ public:
|
||||
void log_time(struct tm time_info) const; // log argument time_info
|
||||
|
||||
private:
|
||||
// setup methods
|
||||
void _set_up_ntp(void); // set up NTP server
|
||||
void _set_up_timer_isr(void); // set up timer interrupt
|
||||
void _set_up_ntp(void); // set up NTP server
|
||||
bool (*_is_wifi_connected)(void); // function to check if wifi is connected
|
||||
|
||||
const char *_ntp_server = "pool.ntp.org"; // ntp server address
|
||||
const char *_tz; // timezone
|
||||
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.
|
||||
time_t _now = 0; // local time value
|
||||
time_t _ntp_sync_timestamp_s = 0; // timestamp of last successful ntp sync
|
||||
TimeManagerState _tm_state = TM_INIT; // Main state
|
||||
UDPLogger *_logger; // logger instance
|
||||
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 */
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
#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_TIME_UPDATE_US (1 * 1000 * 1000) // 1000ms
|
||||
#define PERIOD_PONG_US (10 * 1000) // 10ms
|
||||
#define PERIOD_SNAKE_US (50 * 1000) // 50ms
|
||||
#define PERIOD_STATE_CHANGE_US (10 * 1000 * 1000) // 10s
|
||||
@@ -69,11 +69,9 @@
|
||||
#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 minus one at compile time.
|
||||
#define NTP_START_YEAR (1900) // NTP minimum year is 1900
|
||||
#define NTP_UPDATE_PERIOD_S (6 * 3600) // 6h period between NTP 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
|
||||
#define BUILD_YEAR (__DATE__ + 7) // Will expand to current year at compile time as string.
|
||||
#define NTP_MINIMUM_RX_YEAR (atoi(BUILD_YEAR) - 1) // Will expand to current year minus one at compile time.
|
||||
#define NTP_START_YEAR (1900) // NTP minimum year is 1900
|
||||
#define NTP_MAX_OFFLINE_TIME_S (7 * 24 * 3600) // Watchdog value, maximum offline time before a restart is triggered
|
||||
|
||||
#endif /* WORDCLOCK_CONSTANTS_H */
|
||||
|
||||
@@ -82,7 +82,7 @@ void send_heartbeat(void);
|
||||
void set_dynamic_brightness(bool state);
|
||||
void set_main_color(uint8_t red, uint8_t green, uint8_t blue);
|
||||
void set_night_mode(bool on);
|
||||
void state_change(uint8_t newState);
|
||||
void state_change(ClockState_en new_state);
|
||||
void update_matrix(void);
|
||||
void write_settings_to_EEPROM(void);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user