From 619b80232b3e7bbc11e800ee062baf5fe7df5314 Mon Sep 17 00:00:00 2001 From: Andre LaFleur Date: Sat, 18 Dec 2021 14:09:10 -0700 Subject: [PATCH] Pull in a couple other methods --- src/WatchyRTC.cpp | 24 ++---------------------- src/WatchyRTC.h | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/WatchyRTC.cpp b/src/WatchyRTC.cpp index df73d01..1938452 100644 --- a/src/WatchyRTC.cpp +++ b/src/WatchyRTC.cpp @@ -33,11 +33,7 @@ void WatchyRTC::config(String datetime){ } void WatchyRTC::clearAlarm(){ - if (rtcType == DS3232_RTC_TYPE) { - ((DS3232*) _rtc)->rtc_ds.alarm(ALARM_2); - } else if (rtcType == PCF8563_RTC_TYPE) { - _rtc->clearAlarm(); - } + _rtc->clearAlarm(); } void WatchyRTC::read(tmElements_t &tm){ @@ -61,23 +57,7 @@ void WatchyRTC::read(tmElements_t &tm){ } void WatchyRTC::set(tmElements_t tm){ - if (rtcType == DS3232_RTC_TYPE) { - tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS3232; - time_t t = makeTime(tm); - ((DS3232*) _rtc)->rtc_ds.set(t); - } else if (rtcType == PCF8563_RTC_TYPE) { - ((PCF8563 *) _rtc)->rtc_pcf.setDate(tm.Day, _getDayOfWeek(tm.Day, tm.Month, tm.Year+YEAR_OFFSET_PCF), tm.Month, 0, tm.Year); - ((PCF8563 *) _rtc)->rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second); - clearAlarm(); - } -} - -int WatchyRTC::_getDayOfWeek(int d, int m, int y) { - static int t[] = { 0, 3, 2, 5, 0, 3, - 5, 1, 4, 6, 2, 4 }; - y -= m < 3; - return ( y + y / 4 - y / 100 + - y / 400 + t[m - 1] + d) % 7; + _rtc->set(tm); } uint8_t WatchyRTC::temperature(){ diff --git a/src/WatchyRTC.h b/src/WatchyRTC.h index 1953e47..e73a501 100644 --- a/src/WatchyRTC.h +++ b/src/WatchyRTC.h @@ -56,6 +56,7 @@ class DS3232 : public AbstractRTC { public: DS3232RTC rtc_ds; // TODO: We should not have public member variables ~DS3232() {} + void config(String datetime) { if (datetime != "") { tmElements_t tm; @@ -75,12 +76,23 @@ public: rtc_ds.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0); //alarm wakes up Watchy every minute rtc_ds.alarmInterrupt(ALARM_2, true); //enable alarm interrupt } + + void clearAlarm() { + rtc_ds.alarm(ALARM_2); + } + + void set(tmElements_t tm) { + tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS3232; + time_t t = makeTime(tm); + rtc_ds.set(t); + } }; class PCF8563 : public AbstractRTC { public: Rtc_Pcf8563 rtc_pcf; // TODO: We should not have public member variables ~PCF8563() {} + void config(String datetime) { if (datetime != "") { int Year = getValue(datetime, ':', 0).toInt(); @@ -90,13 +102,17 @@ public: int Minute = getValue(datetime, ':', 4).toInt(); int Second = getValue(datetime, ':', 5).toInt(); //day, weekday, month, century(1=1900, 0=2000), year(0-99) - rtc_pcf.setDate(Day, getDayOfWeek(Day, Month, Year), Month, 0, Year - YEAR_OFFSET_PCF);//offset from 2000 + int dayOfWeek = getDayOfWeek(Day, Month, Year); + + // offset from 2000 + rtc_pcf.setDate(Day, dayOfWeek, Month, 0, Year - YEAR_OFFSET_PCF); //hr, min, sec rtc_pcf.setTime(Hour, Minute, Second); } clearAlarm(); } + void clearAlarm() { int nextAlarmMinute = 0; rtc_pcf.clearAlarm(); // resets the alarm flag in the RTC @@ -104,6 +120,13 @@ public: nextAlarmMinute = (nextAlarmMinute == 59) ? 0 : (nextAlarmMinute + 1); //set alarm to trigger 1 minute from now rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99); } + + void set(tmElements_t tm) { + int dayOfWeek = getDayOfWeek(tm.Day, tm.Month, tm.Year + YEAR_OFFSET_PCF); + rtc_pcf.setDate(tm.Day, dayOfWeek, tm.Month, 0, tm.Year); + rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second); + clearAlarm(); + } private: int getDayOfWeek(int d, int m, int y) { static int t[] = { 0, 3, 2, 5, 0, 3, @@ -127,7 +150,6 @@ class WatchyRTC { uint8_t temperature(); private: bool _canConnectTo(int addr); - int _getDayOfWeek(int d, int m, int y); AbstractRTC* _rtc; };