Update for non-blocking SyncProvider function #130
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The proposed changes will allow for low-latency, non-blocking code to be used in the user’s SyncProvider function. The specific application in mind is when the sync is provided via NTP since there's an unknown and variable delay between sending the NTP request and getting a response.
Two optional parameters are added to the setSyncProvider function prototype:
void setSyncProvider(getExternalTime getTimeFunction, bool immedRetry = false, bool wait = false);
immedRetry
-- when true, instructs the ‘now()’ function not to update ‘nextSyncTime’ if the user’s SyncProvider function returns ‘0’.wait
-- when, true forcessetSyncProvider()
to wait for valid synchronization.With this new version,
now()
will call the user’s SyncProvider function, assuming thenextSyncTime
condition is satisfied, just like previously. However, if that function returns '0' andimmedRetry
is true, thennow()
will not updatenextSyncTime
. So, each subsequent call tonow()
will also call the user’s SyncProvider function until it returns a valid time. The idea is for the SyncProvider function to be non-blocking and quickly return either '0' or the valid time. For example, a SyncProvider function based on NTP would simply send the NTP request packet and return '0' the first time it's called. On each subsequent call it would do one of three things:Only when Case #1 happens will
now()
updatenextSyncTime
to besyncInterval
seconds in the future. This will halt the continual calls to the user’s SyncProvider function forsyncInterval
seconds.The
wait
argument instructssetSyncProvider()
to continually call the user’s SyncProvider function and not return to user code until a valid time is set. This is needed because of the non-blocking SyncProvider function behavior described above.To maintain backwards compatibility for existing applications the
immedRetry
andwait
arguments can be omitted from the call tosetSyncProvider()
. In this case, the library will behave exactly the same as before.An updated 'TimeNTP_ESP8266WiFi.ino' example is included to demonstrate the new technique.