Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for non-blocking SyncProvider function #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gfvalvo
Copy link

@gfvalvo gfvalvo commented May 16, 2019

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 forces setSyncProvider() to wait for valid synchronization.

With this new version, now() will call the user’s SyncProvider function, assuming the nextSyncTime condition is satisfied, just like previously. However, if that function returns '0' and immedRetry is true, then now() will not update nextSyncTime. So, each subsequent call to now() 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:

  1. Receive a valid NTP packet, reset itself to send another NTP request packet the next time it is called, and return the time.
  2. Recognize that a packet has not yet been received and immediately return ‘0’.
  3. Time out, reset itself to send another NTP request packet the next time it is called, and return ‘0’.

Only when Case #1 happens will now() update nextSyncTime to be syncInterval seconds in the future. This will halt the continual calls to the user’s SyncProvider function for syncInterval seconds.

The wait argument instructs setSyncProvider() 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 and wait arguments can be omitted from the call to setSyncProvider(). 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant