AceTime  0.6
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.
Public Member Functions | Static Public Attributes | Friends | List of all members
ace_time::clock::SystemClockLoop Class Reference

A subclass of SystemClock that sync with its mReferenceClock using a blocking mReferenceClock->getNow() call. More...

#include <SystemClockLoop.h>

Inheritance diagram for ace_time::clock::SystemClockLoop:
Inheritance graph
[legend]
Collaboration diagram for ace_time::clock::SystemClockLoop:
Collaboration graph
[legend]

Public Member Functions

 SystemClockLoop (Clock *referenceClock, Clock *backupClock, uint16_t syncPeriodSeconds=3600, uint16_t initialSyncPeriodSeconds=5, uint16_t requestTimeoutMillis=1000, common::TimingStats *timingStats=nullptr)
 Constructor. More...
 
void loop ()
 Call this from the global loop() method. More...
 
- Public Member Functions inherited from ace_time::clock::SystemClock
void setup ()
 Attempt to retrieve the time from the backupClock if it exists. More...
 
acetime_t getNow () const override
 Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z). More...
 
void setNow (acetime_t epochSeconds) override
 Set the time to the indicated seconds. More...
 
void forceSync ()
 Force a sync with the mReferenceClock. More...
 
acetime_t getLastSyncTime () const
 Return the time (seconds since Epoch) of the last valid sync() call. More...
 
bool isInit () const
 Return true if initialized by setNow() or syncNow(). More...
 
- Public Member Functions inherited from ace_time::clock::Clock
virtual ~Clock ()
 Virtual destructor. More...
 
virtual void sendRequest () const
 Send a time request asynchronously. More...
 
virtual bool isResponseReady () const
 Return true if a response is ready. More...
 
virtual acetime_t readResponse () const
 Returns number of seconds since AceTime epoch (2000-01-01). More...
 

Static Public Attributes

static const uint8_t kStatusReady = 0
 Ready to send request. More...
 
static const uint8_t kStatusSent = 1
 Request sent, waiting for response. More...
 
static const uint8_t kStatusOk = 2
 Request received and is valid. More...
 
static const uint8_t kStatusWaitForRetry = 3
 Request received but is invalid, so retry with exponential backoff. More...
 
- Static Public Attributes inherited from ace_time::clock::Clock
static const acetime_t kInvalidSeconds = LocalTime::kInvalidSeconds
 

Friends

class ::SystemClockLoopTest_loop
 

Additional Inherited Members

- Protected Member Functions inherited from ace_time::clock::SystemClock
 SystemClock (const SystemClock &)=delete
 
SystemClockoperator= (const SystemClock &)=delete
 
 SystemClock (Clock *referenceClock, Clock *backupClock)
 Constructor. More...
 
virtual unsigned long clockMillis () const
 Return the Arduino millis(). More...
 
void keepAlive ()
 Call this (or getNow() every 65.535 seconds or faster to keep the internal counter in sync with millis().
 
void backupNow (acetime_t nowSeconds)
 Write the nowSeconds to the backupClock (which can be an RTC that has non-volatile memory, or simply flash memory which emulates a backupClock.
 
void syncNow (acetime_t epochSeconds)
 Similar to setNow() except that backupNow() is called only if the backupClock is different from the referenceClock. More...
 
- Protected Attributes inherited from ace_time::clock::SystemClock
Clock *const mReferenceClock
 
Clock *const mBackupClock
 
acetime_t mEpochSeconds = kInvalidSeconds
 
acetime_t mLastSyncTime = kInvalidSeconds
 
uint16_t mPrevMillis = 0
 
bool mIsInit = false
 

Detailed Description

A subclass of SystemClock that sync with its mReferenceClock using a blocking mReferenceClock->getNow() call.

The blocking call can be a problem for clocks like NtpClock which makes a network request. If this is a problem, use SystemClockCoroutine instead.

Initial syncing occurs at initialSyncPeriodSeconds interval, until the first successful sync, then subsequent syncing occurs at syncPeriodSeconds interval. Initial syncing implements an exponential backoff when the sync request fails, increasing from initialSyncPeriodSeconds to until a maximum of syncPeriodSeconds.

Definition at line 29 of file SystemClockLoop.h.

Constructor & Destructor Documentation

◆ SystemClockLoop()

ace_time::clock::SystemClockLoop::SystemClockLoop ( Clock referenceClock,
Clock backupClock,
uint16_t  syncPeriodSeconds = 3600,
uint16_t  initialSyncPeriodSeconds = 5,
uint16_t  requestTimeoutMillis = 1000,
common::TimingStats timingStats = nullptr 
)
inlineexplicit

Constructor.

Parameters
referenceClockThe authoritative source of the time. If this is null, the object relies just on clockMillis() and the user to set the proper time using setNow().
backupClockAn RTC chip which continues to keep time even when power is lost. Can be null.
syncPeriodSecondsseconds between normal sync attempts (default 3600)
initialSyncPeriodSecondsseconds between sync attempts when the systemClock is not initialized (default 5), exponentially increasing (2X) at each attempt until syncPeriodSeconds is reached
timingStatsinternal statistics

Definition at line 58 of file SystemClockLoop.h.

Member Function Documentation

◆ loop()

void ace_time::clock::SystemClockLoop::loop ( )
inline

Call this from the global loop() method.

This uses a blocking call to the mReferenceClock.

Definition at line 75 of file SystemClockLoop.h.

Member Data Documentation

◆ kStatusOk

const uint8_t ace_time::clock::SystemClockLoop::kStatusOk = 2
static

Request received and is valid.

Definition at line 38 of file SystemClockLoop.h.

◆ kStatusReady

const uint8_t ace_time::clock::SystemClockLoop::kStatusReady = 0
static

Ready to send request.

Definition at line 32 of file SystemClockLoop.h.

◆ kStatusSent

const uint8_t ace_time::clock::SystemClockLoop::kStatusSent = 1
static

Request sent, waiting for response.

Definition at line 35 of file SystemClockLoop.h.

◆ kStatusWaitForRetry

const uint8_t ace_time::clock::SystemClockLoop::kStatusWaitForRetry = 3
static

Request received but is invalid, so retry with exponential backoff.

Definition at line 41 of file SystemClockLoop.h.


The documentation for this class was generated from the following file: