// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2012-2024 Scott Penrose and WII5 Buoy contributors // // This file is part of WII5 Buoy firmware. // See LICENSE for full terms. /** * @file WII5RTC.cpp * @brief Real-time clock helper. */ /* WII5RTC - */ #include #include #include #include "RTClib.h" RTC_DS3231 rtc; uint32_t syncProvider() { return rtc.now().unixtime(); } void WII5RTC::begin() { if (! rtc.begin()) { SerialConsole.println("RTC: Couldn't find RTC"); } // TODO Do the lost power thing // Then connect to GPS console.printf(F("RTC: startup date time from RTC=")); console.printDateTime(rtc.now().unixtime()); console.printNewLine(); setSyncProvider(syncProvider); // the function to get the time from the RTC if(timeStatus() != timeSet) SerialConsole.println("RTC: Unable to sync with the RTC"); else SerialConsole.println("RTC: has set the system time"); } /* What type of device Whuihc Library Can they suse SPI or I2c or, writred wrong? What are the pins */ float WII5RTC::getTemperature() { return rtc.getTemperature(); } void WII5RTC::setRTC(uint32_t t) { if (t == 0) { t = now(); } rtc.adjust(t); console.log(LOG_INFO, F("RTC: Adjusted to new time provided")); console.printf(F("# TimeSet=")); console.printDateTime(t); console.printf(F(" RTC=")); console.printDateTime(rtc.now().unixtime()); console.printNewLine(); } void WII5RTC::test() { // TODO Move to Strings for space char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // TODO Move this code into begin ! if (rtc.lostPower()) { SerialConsole.println(F("RTC lost power, lets set the time!")); // following line sets the RTC to the date & time this sketch was compiled // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Fixed date temporary rtc.adjust(1483268462); // TODO Log this ! } elapsedMillis doIt = 0; while (doIt < 15000) { DateTime nowrtc = rtc.now(); time_t nowsys = now(); // System vs RTC SerialConsole.print(F("# UnixTimeStamp System = ")); SerialConsole.print(nowsys); SerialConsole.print(F(" RTC = ")); SerialConsole.print(nowrtc.unixtime()); SerialConsole.println(); SerialConsole.print(nowrtc.year(), DEC); SerialConsole.print('/'); SerialConsole.print(nowrtc.month(), DEC); SerialConsole.print('/'); SerialConsole.print(nowrtc.day(), DEC); SerialConsole.print(" ("); SerialConsole.print(daysOfTheWeek[nowrtc.dayOfTheWeek()]); SerialConsole.print(") "); SerialConsole.print(nowrtc.hour(), DEC); SerialConsole.print(':'); SerialConsole.print(nowrtc.minute(), DEC); SerialConsole.print(':'); SerialConsole.print(nowrtc.second(), DEC); SerialConsole.println(); SerialConsole.print(F(" since midnight 1/1/1970 = ")); SerialConsole.print(nowrtc.unixtime()); SerialConsole.print("s = "); SerialConsole.print(nowrtc.unixtime() / 86400L); SerialConsole.println("d"); // calculate a date which is 7 days and 30 seconds into the future DateTime future (nowrtc + TimeSpan(7,12,30,6)); SerialConsole.print(F(" now + 7d + 30s: ")); SerialConsole.print(future.year(), DEC); SerialConsole.print('/'); SerialConsole.print(future.month(), DEC); SerialConsole.print('/'); SerialConsole.print(future.day(), DEC); SerialConsole.print(' '); SerialConsole.print(future.hour(), DEC); SerialConsole.print(':'); SerialConsole.print(future.minute(), DEC); SerialConsole.print(':'); SerialConsole.print(future.second(), DEC); SerialConsole.println(); SerialConsole.println(); delay(1000); } SerialConsole.println(F("End of test")); } WII5RTC wii5RTC;