Unverified Commit c889a3c1 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Add NTP waitSet callback (#705)

Allows printing "."s or flashing an LED while NTP is waiting for sync.
parent cfc05dab
......@@ -51,6 +51,24 @@ it is not already running. Using this method, the above code becomes:
void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");
NTP.waitSet();
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}
bool NTP.waitSet(void (\*cb)(), uint32_t timeout)
-------------------------------------------------
Allows for a callback that will be called every 1/10th of a second while waiting for
NTP sync. For example, using lambdas you can simply print "."s:"
.. code :: cpp
void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");
NTP.waitSet([]() { Serial.print("."); });
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
......
......@@ -57,13 +57,12 @@ void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
NTP.waitSet([]() {
Serial.print(".");
now = time(nullptr);
}
});
Serial.println("");
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
......
......@@ -164,13 +164,10 @@ void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
NTP.waitSet([]() { Serial.print("."); } );
Serial.println("");
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
......
......@@ -45,13 +45,12 @@ void setup() {
NTP.begin("pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
NTP.waitSet([]() {
Serial.print(".");
now = time(nullptr);
}
});
Serial.println("");
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
......
......@@ -22,16 +22,14 @@ const char *path = "/";
// Set time via NTP, as required for x.509 validation
void setClock() {
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
NTP.begin("pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
NTP.waitSet([]() {
Serial.print(".");
now = time(nullptr);
}
});
Serial.println("");
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
......
......@@ -77,12 +77,19 @@ public:
}
bool waitSet(uint32_t timeout = 10000) {
return waitSet(nullptr, timeout);
}
bool waitSet(void (*cb)(), uint32_t timeout = 10000) {
if (!running()) {
begin("pool.ntp.org");
}
uint32_t start = millis();
while ((time(nullptr) < 10000000) && (millis() - start < timeout)) {
delay(10);
delay(100);
if (cb) {
cb();
}
}
return time(nullptr) < 10000000;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment