AceTime
1.7.2
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
11 #include "../hw/ClockInterface.h"
13 class SystemClockCoroutineTest;
14 class SystemClockLoopTest;
15 class SystemClockLoopTest_loop;
16 class SystemClockLoopTest_setup;
17 class SystemClockLoopTest_backupNow;
18 class SystemClockLoopTest_syncNow;
19 class SystemClockLoopTest_getNow;
59 template <
typename T_CI>
76 if (mBackupClock !=
nullptr) {
100 while ((uint16_t) ((uint16_t)
clockMillis() - mPrevKeepAliveMillis)
102 mPrevKeepAliveMillis += 1000;
106 return mEpochSeconds;
109 void setNow(acetime_t epochSeconds)
override {
113 if (mReferenceClock !=
nullptr) {
114 mReferenceClock->
setNow(epochSeconds);
132 if (mReferenceClock) {
133 acetime_t nowSeconds = mReferenceClock->
getNow();
143 return mLastSyncTime;
163 return (int32_t) (
clockMillis() - mPrevSyncAttemptMillis) / 1000;
173 return (int32_t) (mNextSyncAttemptMillis -
clockMillis()) / 1000;
194 friend class ::SystemClockLoopTest;
195 friend class ::SystemClockCoroutineTest;
196 friend class ::SystemClockLoopTest_loop;
197 friend class ::SystemClockLoopTest_syncNow;
198 friend class ::SystemClockLoopTest_setup;
199 friend class ::SystemClockLoopTest_backupNow;
200 friend class ::SystemClockLoopTest_getNow;
224 Clock* referenceClock ,
227 mReferenceClock(referenceClock),
228 mBackupClock(backupClock) {}
238 Clock* referenceClock ,
241 mReferenceClock = referenceClock;
242 mBackupClock = backupClock;
245 mPrevSyncAttemptMillis = 0;
246 mNextSyncAttemptMillis = 0;
247 mPrevKeepAliveMillis = 0;
278 if (mBackupClock !=
nullptr) {
279 mBackupClock->
setNow(nowSeconds);
309 mLastSyncTime = epochSeconds;
310 acetime_t skew = mEpochSeconds - epochSeconds;
312 if (skew == 0)
return;
314 mEpochSeconds = epochSeconds;
318 if (mBackupClock != mReferenceClock) {
325 mNextSyncAttemptMillis = ms;
330 mPrevSyncAttemptMillis = ms;
335 mSyncStatusCode = code;
339 Clock* mReferenceClock;
344 uint32_t mPrevSyncAttemptMillis = 0;
345 uint32_t mNextSyncAttemptMillis = 0;
346 mutable uint16_t mPrevKeepAliveMillis = 0;
347 int16_t mClockSkew = 0;
348 bool mIsInit =
false;
353 using SystemClock = SystemClockTemplate<hw::ClockInterface>;
Clock * getReferenceClock() const
Get referenceClock.
SystemClockTemplate()
Empty constructor primarily for tests.
int16_t getClockSkew() const
Difference between this clock compared to reference at last sync.
uint8_t getSyncStatusCode() const
Get sync status code.
void setup()
Attempt to retrieve the time from the backupClock if it exists.
static const uint8_t kSyncStatusTimedOut
Sync request timed out.
virtual acetime_t getNow() const =0
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
int32_t getSecondsToSyncAttempt() const
Return the number of seconds until the next syncNow() attempt.
void keepAlive()
Call this (or getNow() every 65.535 seconds or faster to keep the internal counter in sync with milli...
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
SystemClockTemplate(Clock *referenceClock, Clock *backupClock)
Constructor.
void setPrevSyncAttemptMillis(uint32_t ms)
Set the millis of prev sync attempt.
virtual void setNow(acetime_t)
Set the time to the indicated seconds.
void backupNow(acetime_t nowSeconds)
Write the nowSeconds to the backupClock (which can be an RTC that has non-volatile memory).
void syncNow(acetime_t epochSeconds)
Set the current mEpochSeconds to the given epochSeconds.
static const uint8_t kSyncStatusOk
Sync was successful.
acetime_t getLastSyncTime() const
Return the time (seconds since Epoch) of the last successful syncNow() call.
static const uint8_t kSyncStatusError
Sync request failed.
void forceSync()
Manually force a sync with the referenceClock if it exists.
unsigned long clockMillis() const
Return the Arduino millis().
Abstract base class for objects that provide and store time.
static const acetime_t kInvalidSeconds
Error value returned by getNow() and other methods when this object is not yet initialized.
A Clock that uses the Arduino millis() function to advance the time returned to the user.
int32_t getSecondsSinceSyncAttempt() const
Return the number of seconds since the previous sync attempt, successful or not.
bool isInit() const
Return true if initialized by setNow() or syncNow().
void initSystemClock(Clock *referenceClock, Clock *backupClock)
Same as constructor but allows delayed initialization, e.g.
void setNextSyncAttemptMillis(uint32_t ms)
Set the millis to next sync attempt.
static const uint8_t kSyncStatusUnknown
Sync was never done.
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
void setSyncStatusCode(uint8_t code)
Set the status code of most recent sync attempt.