From 23b1328cba055cb688bd8bd360b94c0b9f0bf5af Mon Sep 17 00:00:00 2001 From: Lewis Jackson <> Date: Thu, 1 Jun 2023 17:22:37 +0300 Subject: [PATCH] Add WIP weather stuff --- src/WatchFacePages/Weather.cpp | 15 +++++++++++++++ src/WatchFacePages/Weather.h | 2 ++ src/WatchFeatures/RTC.cpp | 35 +++++++++++++++++++--------------- src/WatchFeatures/RTC.h | 5 ++--- src/config.h | 3 +++ 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/WatchFacePages/Weather.cpp b/src/WatchFacePages/Weather.cpp index 3053562..bb96bc3 100644 --- a/src/WatchFacePages/Weather.cpp +++ b/src/WatchFacePages/Weather.cpp @@ -5,6 +5,8 @@ #include #include +RTC_DATA_ATTR uint64_t WatchFacePages::Weather::m_lastSyncTime = 0; + WatchFacePages::Weather::Weather(WatchyDisplay & display, WatchFeatures::WatchFeatures & features) : m_display(display), m_features(features) { @@ -23,4 +25,17 @@ void WatchFacePages::Weather::DrawPage(bool partialRefresh) m_display.setFullWindow(); m_display.fillScreen(GxEPD_WHITE); m_display.display(partialRefresh); +} + +void WatchFacePages::Weather::Resync() +{ + // if(!m_features.wifi.Connect()) { + // return; + // } + + // uint64_t currentTime = m_features.rtc.GetTimestamp(); + + // if(m_lastSyncTime == 0 || m_lastSyncTime + 60 * 60 * 1000 < millis()) { + // m_lastSyncTime = millis(); + // } } \ No newline at end of file diff --git a/src/WatchFacePages/Weather.h b/src/WatchFacePages/Weather.h index 630e0f9..051525d 100644 --- a/src/WatchFacePages/Weather.h +++ b/src/WatchFacePages/Weather.h @@ -20,7 +20,9 @@ public: void DrawPage(bool partialRefresh = false) override; private: + void Resync(); WatchyDisplay & m_display; WatchFeatures::WatchFeatures & m_features; + static RTC_DATA_ATTR uint64_t m_lastSyncTime; }; \ No newline at end of file diff --git a/src/WatchFeatures/RTC.cpp b/src/WatchFeatures/RTC.cpp index 1834755..7fbe6b4 100644 --- a/src/WatchFeatures/RTC.cpp +++ b/src/WatchFeatures/RTC.cpp @@ -10,14 +10,14 @@ RTC_DATA_ATTR bool WatchFeatures::RTC::m_initialTimer = true; void WatchFeatures::RTC::Get(tmElements_t & tm) { - rtc_pcf.getDateTime(); - tm.Year = y2kYearToTm(rtc_pcf.getYear()); - tm.Month = rtc_pcf.getMonth(); - tm.Day = rtc_pcf.getDay(); - tm.Wday = rtc_pcf.getWeekday() + 1; // TimeLib has Wday range of 1-7, but PCF8563 stores day of week in 0-6 range - tm.Hour = rtc_pcf.getHour(); - tm.Minute = rtc_pcf.getMinute(); - tm.Second = rtc_pcf.getSecond(); + m_rtcPcf.getDateTime(); + tm.Year = y2kYearToTm(m_rtcPcf.getYear()); + tm.Month = m_rtcPcf.getMonth(); + tm.Day = m_rtcPcf.getDay(); + tm.Wday = m_rtcPcf.getWeekday() + 1; // TimeLib has Wday range of 1-7, but PCF8563 stores day of week in 0-6 range + tm.Hour = m_rtcPcf.getHour(); + tm.Minute = m_rtcPcf.getMinute(); + tm.Second = m_rtcPcf.getSecond(); } void WatchFeatures::RTC::Set(tmElements_t tm) @@ -25,7 +25,12 @@ void WatchFeatures::RTC::Set(tmElements_t tm) time_t t = makeTime(tm); // make and break to calculate tm.Wday breakTime(t, tm); // day, weekday, month, century(1=1900, 0=2000), year(0-99) - rtc_pcf.setDateTime(tm.Day, tm.Wday - 1, tm.Month, 0, tmYearToY2k(tm.Year), tm.Hour, tm.Minute, tm.Second); + m_rtcPcf.setDateTime(tm.Day, tm.Wday - 1, tm.Month, 0, tmYearToY2k(tm.Year), tm.Hour, tm.Minute, tm.Second); +} + +uint64_t WatchFeatures::RTC::GetTimestamp() +{ + return m_rtcPcf.getTimestamp64(); } void WatchFeatures::RTC::SetTimer() @@ -115,8 +120,8 @@ bool WatchFeatures::RTC::CheckWakeup() } // Timer doesn't work reliably unless it's cleared first - rtc_pcf.clearTimer(); - rtc_pcf.setTimer(interval, frequency, true); + m_rtcPcf.clearTimer(); + m_rtcPcf.setTimer(interval, frequency, true); return true; } @@ -126,16 +131,16 @@ bool WatchFeatures::RTC::CheckWakeup() void WatchFeatures::RTC::Resync() { - rtc_pcf.getDateTime(); + m_rtcPcf.getDateTime(); // Sleep just long enough to get to a multiple of the update interval, makes updates happen on exact turn of the minute - int seconds = UPDATE_INTERVAL - (rtc_pcf.getSecond() % UPDATE_INTERVAL); + int seconds = UPDATE_INTERVAL - (m_rtcPcf.getSecond() % UPDATE_INTERVAL); if (seconds < 0) { seconds = 0; } // Timer doesn't work reliably unless it's cleared first - rtc_pcf.clearTimer(); + m_rtcPcf.clearTimer(); if (seconds == 0) { // If there's no time to wait, just call CheckWakeup() immediately and it'll set the repeating timer @@ -143,7 +148,7 @@ void WatchFeatures::RTC::Resync() m_initialTimer = true; CheckWakeup(); } else { - rtc_pcf.setTimer(seconds, TMR_1Hz, false); + m_rtcPcf.setTimer(seconds, TMR_1Hz, false); m_timerSet = true; m_initialTimer = true; diff --git a/src/WatchFeatures/RTC.h b/src/WatchFeatures/RTC.h index 9abf79c..1c2bcd0 100644 --- a/src/WatchFeatures/RTC.h +++ b/src/WatchFeatures/RTC.h @@ -12,12 +12,10 @@ namespace WatchFeatures } class WatchFeatures::RTC { -public: - Rtc_Pcf8563 rtc_pcf; - public: void Get(tmElements_t & tm); void Set(tmElements_t tm); + uint64_t GetTimestamp(); void SetTimer(); bool CheckWakeup(); // Checks to really wake up or not, also resets the timer after the initial sleep void Resync(); // Resync the timer cycle, both initially and after RTC resync @@ -25,6 +23,7 @@ public: static void OffsetTime(tmElements_t & tm, int offsetInSeconds); private: + Rtc_Pcf8563 m_rtcPcf; static RTC_DATA_ATTR bool m_timerSet, m_initialTimer; }; diff --git a/src/config.h b/src/config.h index a2b5b30..ea206b8 100644 --- a/src/config.h +++ b/src/config.h @@ -34,6 +34,7 @@ #define EEPROM_MAGIC2 0x0d #define EEPROM_VERSION 1 +#define OPENWEATHERMAP_API_KEY "" #include "secrets.h" #if !defined(WIFI_SSID) || !defined(WIFI_PASS) #error "Please define WIFI_SSID and WIFI_PASS in secrets.h" @@ -45,3 +46,5 @@ #define UPDATE_INTERVAL 60 // seconds #define WAKE_ON_ACCEL_EVENTS false // useful if saving battery by not updating every minute + +#define WEATHER_UPDATE_INTERVAL 3600 // seconds \ No newline at end of file