NTPClient now does updates asynchronously. Thanks to https://github.com/arduino-libraries/NTPClient/pull/22

This commit is contained in:
2019-05-31 23:58:34 +02:00
parent f9fba8a8f9
commit 29e136b8db
2 changed files with 151 additions and 69 deletions

View File

@ -5,48 +5,53 @@
#include <Udp.h>
#define SEVENZYYEARS 2208988800UL
#define FRACTIONSPERMILLI (4294967UL)
#define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337
class NTPClient;
typedef void (*NTPUpdateCallbackFunction)(NTPClient* c);
class NTPClient {
private:
UDP* _udp;
bool _udpSetup = false;
const char* _poolServerName = "pool.ntp.org"; // Default time server
const char* _poolServerName = "time.nist.gov"; // Default time server
int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0;
int _timeOffset = 0;
bool _timeValid = false;
unsigned long _updateInterval = 60000; // In ms
unsigned int _updateInterval = 60000; // In ms
unsigned int _retryInterval = 1000; // In ms
unsigned long _currentEpoc = 0; // In s
unsigned long _currentFraction = 0; // In 1/(2^32) s
unsigned long _lastUpdate = 0; // In ms
unsigned long _lastRequest = 0; // IN ms
byte _packetBuffer[NTP_PACKET_SIZE];
void sendNTPPacket();
NTPUpdateCallbackFunction _updateCallback = NULL;
boolean _timeValid = false;
void sendNTPPacket();
bool checkResponse();
public:
NTPClient(UDP& udp);
NTPClient(UDP& udp, long timeOffset);
NTPClient(UDP& udp, int timeOffset);
NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
/**
* Set time server name
*
* @param poolServerName
*/
void setPoolServerName(const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
/**
* Starts the underlying UDP client with the default local port
*/
void begin();
bool getTimeValid();
/**
* Starts the underlying UDP client with the specified local port
*/
@ -61,17 +66,28 @@ class NTPClient {
bool update();
/**
* This will force the update from the NTP Server.
* Has the time ever been sucessfully updated
*
*/
bool updated();
/**
* Register a callback function for when the time gets updated
*
*/
void setUpdateCallback(NTPUpdateCallbackFunction f);
/**
* This will force the update from the NTP Server.
* This can block for a full second
* @return true on success, false on failure
*/
bool forceUpdate();
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
boolean getTimeValid() const;
int getDay();
int getHours();
int getMinutes();
int getSeconds();
/**
* Changes the time offset. Useful for changing timezones dynamically
@ -82,17 +98,27 @@ class NTPClient {
* Set the update interval to another frequency. E.g. useful when the
* timeOffset should not be set in the constructor
*/
void setUpdateInterval(unsigned long updateInterval);
void setUpdateInterval(int updateInterval);
/**
* Set the retry interval to another frequency in ms
*/
void setRetryInterval(int retryInterval);
/**
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime() const;
String getFormattedTime();
/**
* @return time in seconds since Jan. 1, 1970
*/
unsigned long getEpochTime() const;
unsigned long getEpochTime();
/**
* @return time in milliseconds since Jan. 1, 1970
*/
unsigned long long getEpochMillis();
/**
* Stops the underlying UDP client