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

A version of SystemClock that mixes in the ace_routine::Coroutine class so that that the non-block methods of mReferenceClock are called. More...

#include <SystemClockCoroutine.h>

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

Public Member Functions

 SystemClockCoroutine (Clock *referenceClock, Clock *backupClock, uint16_t syncPeriodSeconds=3600, uint16_t initialSyncPeriodSeconds=5, uint16_t requestTimeoutMillis=1000, common::TimingStats *timingStats=nullptr)
 Constructor. More...
 
int runCoroutine () override
 
uint8_t getRequestStatus () const
 Return the current request status. More...
 
- Public Member Functions inherited from ace_time::clock::SystemClock
void setup ()
 Attempt to retrieve the time from the backupClock if it exists.
 
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.
 
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().
 
- Public Member Functions inherited from ace_time::clock::Clock
virtual ~Clock ()
 Virtual destructor. More...
 
virtual void sendRequest () const
 Send a time request asynchronously.
 
virtual bool isResponseReady () const
 Return true if a response is ready.
 
virtual acetime_t readResponse () const
 Returns number of seconds since AceTime epoch (2000-01-01). More...
 

Static Public Attributes

static const uint8_t kStatusUnknown = 0
 Request state unknown. More...
 
static const uint8_t kStatusSent = 1
 Request has been sent and waiting for response. More...
 
static const uint8_t kStatusOk = 2
 Request received and valid.
 
static const uint8_t kStatusTimedOut = 3
 Request timed out.
 
- Static Public Attributes inherited from ace_time::clock::Clock
static const acetime_t kInvalidSeconds = LocalTime::kInvalidSeconds
 

Friends

class ::SystemClockCoroutineTest_runCoroutine
 

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 version of SystemClock that mixes in the ace_routine::Coroutine class so that that the non-block methods of mReferenceClock are called.

This is helpful when the referenceClock issues a network request to an NTP server.

Initially, the class attempts to sync with its referenceClock every initialSyncPeriodSeconds. If the request fails, then it retries with an exponential backoff (doubling the delay every iteration), until the sync period becomes greater than syncPeriodSeconds, then the delay is set permanently to syncPeriodSeconds.

Definition at line 33 of file SystemClockCoroutine.h.

Constructor & Destructor Documentation

◆ SystemClockCoroutine()

ace_time::clock::SystemClockCoroutine::SystemClockCoroutine ( 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)
requestTimeoutMillisnumber of milliseconds before the request to referenceClock times out
timingStatsinternal statistics

Definition at line 63 of file SystemClockCoroutine.h.

Member Function Documentation

◆ getRequestStatus()

uint8_t ace_time::clock::SystemClockCoroutine::getRequestStatus ( ) const
inline

Return the current request status.

Mostly for debugging.

Definition at line 143 of file SystemClockCoroutine.h.

◆ runCoroutine()

int ace_time::clock::SystemClockCoroutine::runCoroutine ( )
inlineoverride

The CoroutineScheduler will use this method if enabled. Don't forget to call setupCoroutine() (inherited from Coroutine) in the global setup() to register this coroutine into the CoroutineScheduler.

Definition at line 83 of file SystemClockCoroutine.h.

Member Data Documentation

◆ kStatusSent

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

Request has been sent and waiting for response.

For diagnostics only.

Definition at line 39 of file SystemClockCoroutine.h.

◆ kStatusUnknown

const uint8_t ace_time::clock::SystemClockCoroutine::kStatusUnknown = 0
static

Request state unknown.

For diagnostics only.

Definition at line 36 of file SystemClockCoroutine.h.


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