From 8431d0bd5cad0abdd7ede90bf97c37daf524fdd5 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Tue, 25 May 2021 09:59:19 +0200 Subject: [PATCH] Replace Time with Toki --- platformio.ini | 1 - usermods/RTC/usermod_rtc.h | 8 +- .../UserMod_SunRiseAndSet.h | 2 +- wled00/json.cpp | 6 +- wled00/ntp.cpp | 82 ++++++++++--------- wled00/overlay.cpp | 4 +- wled00/presets.cpp | 4 +- wled00/set.cpp | 4 +- wled00/src/dependencies/time/Readme.txt | 43 ++-------- wled00/src/dependencies/time/Time.cpp | 76 +---------------- wled00/src/dependencies/time/TimeLib.h | 17 +--- wled00/wled.cpp | 2 +- 12 files changed, 65 insertions(+), 184 deletions(-) diff --git a/platformio.ini b/platformio.ini index 183a93c2..d5c936f1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -481,7 +481,6 @@ build_flags = ${common.build_flags_esp8266} board = esp32dev platform = espressif32@3.2 upload_speed = 921600 -upload_port = COM8 lib_deps = ${env.lib_deps} TFT_eSPI build_flags = ${common.build_flags_esp32} -D WLED_DISABLE_BROWNOUT_DET -D WLED_DISABLE_INFRARED diff --git a/usermods/RTC/usermod_rtc.h b/usermods/RTC/usermod_rtc.h index bf0047a7..782244f6 100644 --- a/usermods/RTC/usermod_rtc.h +++ b/usermods/RTC/usermod_rtc.h @@ -14,7 +14,7 @@ class RTCUsermod : public Usermod { void setup() { time_t rtcTime = RTC.get(); if (rtcTime) { - setTime(rtcTime); + toki.setTime(rtcTime,0); updateLocalTime(); } else { if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error @@ -22,11 +22,9 @@ class RTCUsermod : public Usermod { } void loop() { - if (!disabled && millis() - lastTime > 500) { - time_t t = now(); + if (!disabled && toki.isTick()) { + time_t t = toki.second(); if (t != RTC.get()) RTC.set(t); //set RTC to NTP/UI-provided value - - lastTime = millis(); } } diff --git a/usermods/UserModv2_SunRiseAndSet/UserMod_SunRiseAndSet.h b/usermods/UserModv2_SunRiseAndSet/UserMod_SunRiseAndSet.h index 62176ce9..ef1bb37e 100644 --- a/usermods/UserModv2_SunRiseAndSet/UserMod_SunRiseAndSet.h +++ b/usermods/UserModv2_SunRiseAndSet/UserMod_SunRiseAndSet.h @@ -85,7 +85,7 @@ public: if (m_pD2D && (999000000L != ntpLastSyncTime)) { // to prevent needing to import all the timezone stuff from other modules, work completely in UTC - time_t timeUTC = now(); + time_t timeUTC = toki.second(); tmElements_t tmNow; breakTime(timeUTC, tmNow); int nCurMinute = tmNow.Minute; diff --git a/wled00/json.cpp b/wled00/json.cpp index a2cf126d..29d2a015 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -210,10 +210,10 @@ bool deserializeState(JsonObject root) unsigned long timein = root[F("time")] | UINT32_MAX; //backup time source if NTP not synced if (timein != UINT32_MAX) { - time_t prev = now(); + time_t prev = toki.second(); if (millis() - ntpLastSyncTime > 50000000L) { - setTime(timein); - if (abs(now() - prev) > 60L) { + toki.setTime(timein,toki.millisecond()); + if (abs(timein - prev) > 60L) { updateLocalTime(); calculateSunriseAndSunset(); } diff --git a/wled00/ntp.cpp b/wled00/ntp.cpp index f61e00af..2bfae793 100644 --- a/wled00/ntp.cpp +++ b/wled00/ntp.cpp @@ -5,6 +5,8 @@ /* * Acquires time from NTP server */ +//#define WLED_DEBUG_NTP + Timezone* tz; #define TZ_UTC 0 @@ -182,44 +184,46 @@ void sendNTPPacket() bool checkNTPResponse() { int cb = ntpUdp.parsePacket(); - if (cb) { - uint32_t ntpPacketReceivedTime = millis(); - DEBUG_PRINT(F("NTP recv, l=")); - DEBUG_PRINTLN(cb); - byte pbuf[NTP_PACKET_SIZE]; - ntpUdp.read(pbuf, NTP_PACKET_SIZE); // read the packet into the buffer + if (!cb) return false; - Toki::Time arrived = toki.fromNTP(pbuf + 32); - Toki::Time departed = toki.fromNTP(pbuf + 40); - //basic half roundtrip estimation - uint32_t serverDelay = toki.msDifference(arrived, departed); - uint32_t offset = (ntpPacketReceivedTime - ntpPacketSentTime - serverDelay) >> 1; - toki.printTime(departed); - toki.adjust(departed, offset); - toki.setTime(departed); - Serial.print("Arrived: "); - toki.printTime(arrived); - Serial.print("Time: "); - toki.printTime(departed); - Serial.print("Roundtrip: "); - Serial.println(ntpPacketReceivedTime - ntpPacketSentTime); - Serial.print("Offset: "); - Serial.println(offset); - Serial.print("Serverdelay: "); - Serial.println(serverDelay); - - DEBUG_PRINT(F("Unix time = ")); - uint32_t epoch = toki.second(); - if (epoch == 0) return false; - setTime(epoch); //legacy - DEBUG_PRINTLN(epoch); - if (countdownTime - now() > 0) countdownOverTriggered = false; - // if time changed re-calculate sunrise/sunset - updateLocalTime(); - calculateSunriseAndSunset(); - return true; - } - return false; + uint32_t ntpPacketReceivedTime = millis(); + DEBUG_PRINT(F("NTP recv, l=")); + DEBUG_PRINTLN(cb); + byte pbuf[NTP_PACKET_SIZE]; + ntpUdp.read(pbuf, NTP_PACKET_SIZE); // read the packet into the buffer + + Toki::Time arrived = toki.fromNTP(pbuf + 32); + Toki::Time departed = toki.fromNTP(pbuf + 40); + if (departed.sec == 0) return false; + //basic half roundtrip estimation + uint32_t serverDelay = toki.msDifference(arrived, departed); + uint32_t offset = (ntpPacketReceivedTime - ntpPacketSentTime - serverDelay) >> 1; + #ifdef WLED_DEBUG_NTP + //the time the packet departed the NTP server + toki.printTime(departed); + #endif + + toki.adjust(departed, offset); + toki.setTime(departed); + + #ifdef WLED_DEBUG_NTP + Serial.print("Arrived: "); + toki.printTime(arrived); + Serial.print("Time: "); + toki.printTime(departed); + Serial.print("Roundtrip: "); + Serial.println(ntpPacketReceivedTime - ntpPacketSentTime); + Serial.print("Offset: "); + Serial.println(offset); + Serial.print("Serverdelay: "); + Serial.println(serverDelay); + #endif + + if (countdownTime - toki.second() > 0) countdownOverTriggered = false; + // if time changed re-calculate sunrise/sunset + updateLocalTime(); + calculateSunriseAndSunset(); + return true; } void updateLocalTime() @@ -249,13 +253,13 @@ void setCountdown() { if (currentTimezone != tzCurrent) updateTimezone(); countdownTime = tz->toUTC(getUnixTime(countdownHour, countdownMin, countdownSec, countdownDay, countdownMonth, countdownYear)); - if (countdownTime - now() > 0) countdownOverTriggered = false; + if (countdownTime - toki.second() > 0) countdownOverTriggered = false; } //returns true if countdown just over bool checkCountdown() { - unsigned long n = now(); + unsigned long n = toki.second(); if (countdownMode) localTime = countdownTime - n + utcOffsetSecs; if (n > countdownTime) { if (countdownMode) localTime = n - countdownTime + utcOffsetSecs; diff --git a/wled00/overlay.cpp b/wled00/overlay.cpp index e8f53ea2..f852033a 100644 --- a/wled00/overlay.cpp +++ b/wled00/overlay.cpp @@ -82,9 +82,9 @@ void _overlayAnalogClock() void _overlayAnalogCountdown() { - if ((unsigned long)now() < countdownTime) + if ((unsigned long)toki.second() < countdownTime) { - long diff = countdownTime - now(); + long diff = countdownTime - toki.second(); double pval = 60; if (diff > 31557600L) //display in years if more than 365 days { diff --git a/wled00/presets.cpp b/wled00/presets.cpp index a4635bca..c3c15afc 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -69,13 +69,13 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj) writeObjectToFileUsingId("/presets.json", index, fileDoc); } - presetsModifiedTime = now(); //unix time + presetsModifiedTime = toki.second(); //unix time updateFSInfo(); } void deletePreset(byte index) { StaticJsonDocument<24> empty; writeObjectToFileUsingId("/presets.json", index, &empty); - presetsModifiedTime = now(); //unix time + presetsModifiedTime = toki.second(); //unix time updateFSInfo(); } \ No newline at end of file diff --git a/wled00/set.cpp b/wled00/set.cpp index ee6e102a..89c63e9c 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -790,14 +790,14 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply) //set time (unix timestamp) pos = req.indexOf(F("ST=")); if (pos > 0) { - setTime(getNumVal(&req, pos)); + toki.setTime(getNumVal(&req, pos),toki.millisecond()); } //set countdown goal (unix timestamp) pos = req.indexOf(F("CT=")); if (pos > 0) { countdownTime = getNumVal(&req, pos); - if (countdownTime - now() > 0) countdownOverTriggered = false; + if (countdownTime - toki.second() > 0) countdownOverTriggered = false; } pos = req.indexOf(F("LO=")); diff --git a/wled00/src/dependencies/time/Readme.txt b/wled00/src/dependencies/time/Readme.txt index 234242ab..8982657d 100644 --- a/wled00/src/dependencies/time/Readme.txt +++ b/wled00/src/dependencies/time/Readme.txt @@ -1,5 +1,9 @@ Readme file for Arduino Time Library +! MODIFIED DISTRIBUTION FOR WLED +All timekeeping functions are removed, only conversion functions used. +Please see https://github.com/PaulStoffregen/Time for the full, original library + Time is a library that provides timekeeping functionality for Arduino. The code is derived from the Playground DateTime library but is updated @@ -14,26 +18,10 @@ for time synchronization. The functions available in the library include: -hour(); // the hour now (0-23) -minute(); // the minute now (0-59) -second(); // the second now (0-59) -day(); // the day now (1-31) -weekday(); // day of the week (1-7), Sunday is day 1 -month(); // the month now (1-12) -year(); // the full four digit year: (2009, 2010 etc) - -there are also functions to return the hour in 12 hour format -hourFormat12(); // the hour now in 12 hour format -isAM(); // returns true if time now is AM -isPM(); // returns true if time now is PM - -now(); // returns the current time as seconds since Jan 1 1970 - -The time and date functions can take an optional parameter for the time. This prevents +The time and date functions take a parameter for the time. This prevents errors if the time rolls over between elements. For example, if a new minute begins between getting the minute and second, the values will be inconsistent. Using the -following functions eliminates this probglem - time_t t = now(); // store the current time in time variable t +following functions eliminates this problem hour(t); // returns the hour for the given time t minute(t); // returns the minute for the given time t second(t); // returns the second for the given time t @@ -43,25 +31,6 @@ following functions eliminates this probglem year(t); // the year for the given time t -Functions for managing the timer services are: - - setTime(t); // set the system time to the give time t - setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr - // (2010 or 10 sets year to 2010) - adjustTime(adjustment); // adjust system time by adding the adjustment value - timeStatus(); // indicates if time has been set and recently synchronized - // returns one of the following enumerations: - timeNotSet // the time has never been set, the clock started at Jan 1 1970 - timeNeedsSync // the time had been set but a sync attempt did not succeed - timeSet // the time is set and is synced - -Time and Date values are not valid if the status is timeNotSet. Otherwise values can be used but -the returned time may have drifted if the status is timeNeedsSync. - - setSyncProvider(getTimeFunction); // set the external time provider - setSyncInterval(interval); // set the number of seconds between re-sync - - There are many convenience macros in the time.h file for time constants and conversion of time units. diff --git a/wled00/src/dependencies/time/Time.cpp b/wled00/src/dependencies/time/Time.cpp index 326ce85c..21f2e989 100644 --- a/wled00/src/dependencies/time/Time.cpp +++ b/wled00/src/dependencies/time/Time.cpp @@ -25,6 +25,7 @@ examples, add error checking and messages to RTC examples, add examples to DS1307RTC library. 1.4 5 Sep 2014 - compatibility with Arduino 1.5.7 + 2.0 25 May 2021 - removed timing code, only used for conversion between unix and time */ #if ARDUINO >= 100 @@ -45,19 +46,11 @@ void refreshCache(time_t t) { } } -int hour() { // the hour now - return hour(now()); -} - int hour(time_t t) { // the hour for the given time refreshCache(t); return tm.Hour; } -int hourFormat12() { // the hour now in 12 hour format - return hourFormat12(now()); -} - int hourFormat12(time_t t) { // the hour for the given time in 12 hour format refreshCache(t); if( tm.Hour == 0 ) @@ -68,71 +61,39 @@ int hourFormat12(time_t t) { // the hour for the given time in 12 hour format return tm.Hour ; } -uint8_t isAM() { // returns true if time now is AM - return !isPM(now()); -} - uint8_t isAM(time_t t) { // returns true if given time is AM return !isPM(t); } -uint8_t isPM() { // returns true if PM - return isPM(now()); -} - uint8_t isPM(time_t t) { // returns true if PM return (hour(t) >= 12); } -int minute() { - return minute(now()); -} - int minute(time_t t) { // the minute for the given time refreshCache(t); return tm.Minute; } -int second() { - return second(now()); -} - int second(time_t t) { // the second for the given time refreshCache(t); return tm.Second; } -int day(){ - return(day(now())); -} - int day(time_t t) { // the day for the given time (0-6) refreshCache(t); return tm.Day; } -int weekday() { // Sunday is day 1 - return weekday(now()); -} - int weekday(time_t t) { refreshCache(t); return tm.Wday; } - -int month(){ - return month(now()); -} int month(time_t t) { // the month for the given time refreshCache(t); return tm.Month; } -int year() { // as in Processing, the full four digit year: (2009, 2010 etc) - return year(now()); -} - int year(time_t t) { // the year for the given time refreshCache(t); return tmYearToCalendar(tm.Year); @@ -230,33 +191,6 @@ time_t makeTime(tmElements_t &tm){ seconds+= tm.Second; return (time_t)seconds; } -/*=====================================================*/ -/* Low level system time functions */ - -static uint32_t sysTime = 0; //seconds -static uint16_t sysMillis = 0; -static uint32_t prevMillis = 0; - -time_t now() { - // calculate number of seconds passed since last call to now() - while (millis() - prevMillis >= 1000) { - // millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference - sysTime++; - prevMillis += 1000; - } - - return (time_t)sysTime; -} - -uint16_t millisecond() { // the millisecond (0-999) now - return (sysMillis - millis()) % 1000; -} - -void setTime(time_t t, uint16_t ms) { - sysTime = (uint32_t)t; - sysMillis = ms; - prevMillis = millis(); // restart counting from now (thanks to Korman for this fix) -} time_t getUnixTime(int hr,int min,int sec,int dy, int mnth, int yr){ // year can be given as full four digit year or two digts (2010 or 10 for 2010); @@ -272,12 +206,4 @@ time_t getUnixTime(int hr,int min,int sec,int dy, int mnth, int yr){ tm.Minute = min; tm.Second = sec; return makeTime(tm); -} - -void setTime(int hr,int min,int sec,int dy, int mnth, int yr, uint16_t ms){ - setTime(getUnixTime(hr,min,sec,dy,mnth,yr), ms); -} - -void adjustTime(long adjustment) { - sysTime += adjustment; } \ No newline at end of file diff --git a/wled00/src/dependencies/time/TimeLib.h b/wled00/src/dependencies/time/TimeLib.h index 1064638a..5004f071 100644 --- a/wled00/src/dependencies/time/TimeLib.h +++ b/wled00/src/dependencies/time/TimeLib.h @@ -93,34 +93,19 @@ typedef time_t(*getExternalTime)(); #define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) /*============================================================================*/ -/* time and date functions */ -int hour(); // the hour now +/* time and date functions */ int hour(time_t t); // the hour for the given time -int hourFormat12(); // the hour now in 12 hour format int hourFormat12(time_t t); // the hour for the given time in 12 hour format -uint8_t isAM(); // returns true if time now is AM uint8_t isAM(time_t t); // returns true the given time is AM -uint8_t isPM(); // returns true if time now is PM uint8_t isPM(time_t t); // returns true the given time is PM -int minute(); // the minute now int minute(time_t t); // the minute for the given time -int second(); // the second now int second(time_t t); // the second for the given time -int day(); // the day now int day(time_t t); // the day for the given time -int weekday(); // the weekday now (Sunday is day 1) int weekday(time_t t); // the weekday for the given time -int month(); // the month now (Jan is month 1) int month(time_t t); // the month for the given time -int year(); // the full four digit year: (2009, 2010 etc) int year(time_t t); // the year for the given time -uint16_t millisecond(); // the millisecond now -time_t now(); // return the current time as seconds since Jan 1 1970 -void setTime(time_t t, uint16_t ms = 0); -void setTime(int hr,int min,int sec,int day, int month, int yr, uint16_t ms = 0); time_t getUnixTime(int hr,int min,int sec,int day, int month, int yr); //added by Aircoookie to get epoch time -void adjustTime(long adjustment); /* date strings */ #define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 66a2c020..d50f7127 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -272,7 +272,7 @@ void WLED::loop() if (millis() - debugTime > 9999) { DEBUG_PRINTLN("---DEBUG INFO---"); DEBUG_PRINT("Runtime: "); DEBUG_PRINTLN(millis()); - DEBUG_PRINT("Unix time: "); DEBUG_PRINTLN(now()); + DEBUG_PRINT("Unix time: "); toki.printTime(toki.getTime()); DEBUG_PRINT("Free heap: "); DEBUG_PRINTLN(ESP.getFreeHeap()); DEBUG_PRINT("Wifi state: "); DEBUG_PRINTLN(WiFi.status());