PaulStoffregen / Time

Time library for Arduino
http://playground.arduino.cc/code/time
1.25k stars 666 forks source link

Easiest way to use setSyncProvider() #79

Closed Ariel-International closed 6 years ago

Ariel-International commented 7 years ago

It took me some time to figure out how to use this function with various providers. The argument requested is the address of a function which returns the Unix time in time_t format, which is an unsigned long or uint32_t.

The problem is that addressing a sub-class is not that simple and in my experience it depends on how the clas is built.

The simple solution is to create a new sub which retrieves the value and passes it to the function.

Example:

time_t RDTEpoch32Time() { return Epoch32Time(); //get Epoch time from your RTC }

time_t NTPgetTime() { return NTP.getTime(); //get Epoch time from NTP }

getExternalTime RtcTime = &RDTEpoch32Time; getExternalTime NtpTime = &NTPgetTime;

To sync the time use: setSyncProvider(RtcTime); or setSyncProvider(NtpTime);

Please notice the sub reference has no (), otherwise it becomes a subroutine call, I guess. This works! RDT and NTP are object declared by respective libraries.