Files
wordclock/include/time_manager.h

53 lines
2.0 KiB
C++

#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 */