AceTime
1.7.1
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
|
6 #ifndef ACE_TIME_SYSTEM_CLOCK_H
7 #define ACE_TIME_SYSTEM_CLOCK_H
12 extern "C" unsigned long millis();
13 class SystemClockLoopTest;
14 class SystemClockLoopTest_syncNow;
15 class SystemClockCoroutineTest;
68 if (mBackupClock !=
nullptr) {
92 while ((uint16_t) ((uint16_t)
clockMillis() - mPrevKeepAliveMillis)
94 mPrevKeepAliveMillis += 1000;
101 void setNow(acetime_t epochSeconds)
override {
105 if (mReferenceClock !=
nullptr) {
106 mReferenceClock->
setNow(epochSeconds);
124 if (mReferenceClock) {
125 acetime_t nowSeconds = mReferenceClock->
getNow();
135 return mLastSyncTime;
155 return (int32_t) (
clockMillis() - mPrevSyncAttemptMillis) / 1000;
165 return (int32_t) (mNextSyncAttemptMillis -
clockMillis()) / 1000;
186 friend class ::SystemClockLoopTest;
187 friend class ::SystemClockCoroutineTest;
188 friend class ::SystemClockLoopTest_syncNow;
212 Clock* referenceClock ,
215 mReferenceClock(referenceClock),
216 mBackupClock(backupClock) {}
226 Clock* referenceClock ,
229 mReferenceClock = referenceClock;
230 mBackupClock = backupClock;
233 mPrevSyncAttemptMillis = 0;
234 mNextSyncAttemptMillis = 0;
235 mPrevKeepAliveMillis = 0;
266 if (mBackupClock !=
nullptr) {
267 mBackupClock->
setNow(nowSeconds);
297 mLastSyncTime = epochSeconds;
298 acetime_t skew = mEpochSeconds - epochSeconds;
300 if (skew == 0)
return;
302 mEpochSeconds = epochSeconds;
306 if (mBackupClock != mReferenceClock) {
313 mNextSyncAttemptMillis = ms;
318 mPrevSyncAttemptMillis = ms;
323 mSyncStatusCode = code;
327 Clock* mReferenceClock;
332 uint32_t mPrevSyncAttemptMillis = 0;
333 uint32_t mNextSyncAttemptMillis = 0;
334 mutable uint16_t mPrevKeepAliveMillis = 0;
335 int16_t mClockSkew = 0;
336 bool mIsInit =
false;
void setSyncStatusCode(uint8_t code)
Set the status code of most recent sync attempt.
static const uint8_t kSyncStatusOk
Sync was successful.
static const uint8_t kSyncStatusTimedOut
Sync request timed out.
static const uint8_t kSyncStatusUnknown
Sync was never done.
acetime_t getLastSyncTime() const
Return the time (seconds since Epoch) of the last successful syncNow() call.
bool isInit() const
Return true if initialized by setNow() or syncNow().
virtual acetime_t getNow() const =0
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
static const uint8_t kSyncStatusError
Sync request failed.
int32_t getSecondsSinceSyncAttempt() const
Return the number of seconds since the previous sync attempt, successful or not.
void syncNow(acetime_t epochSeconds)
Set the current mEpochSeconds to the given epochSeconds.
void keepAlive()
Call this (or getNow() every 65.535 seconds or faster to keep the internal counter in sync with milli...
void setNextSyncAttemptMillis(uint32_t ms)
Set the millis to next sync attempt.
SystemClock(Clock *referenceClock, Clock *backupClock)
Constructor.
virtual void setNow(acetime_t)
Set the time to the indicated seconds.
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
void setPrevSyncAttemptMillis(uint32_t ms)
Set the millis of prev sync attempt.
void setup()
Attempt to retrieve the time from the backupClock if it exists.
Clock * getReferenceClock() const
Get referenceClock.
void initSystemClock(Clock *referenceClock, Clock *backupClock)
Same as constructor but allows delayed initialization, e.g.
Abstract base class for objects that provide and store time.
void forceSync()
Manually force a sync with the referenceClock if it exists.
int16_t getClockSkew() const
Difference between this clock compared to reference at last sync.
static const acetime_t kInvalidSeconds
Error value returned by getNow() and other methods when this object is not yet initialized.
virtual unsigned long clockMillis() const
Return the Arduino millis().
SystemClock()
Empty constructor primarily for tests.
void backupNow(acetime_t nowSeconds)
Write the nowSeconds to the backupClock (which can be an RTC that has non-volatile memory).
uint8_t getSyncStatusCode() const
Get sync status code.
int32_t getSecondsToSyncAttempt() const
Return the number of seconds until the next syncNow() attempt.
A Clock that uses the Arduino millis() function to advance the time returned to the user.
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).