From a4ab4b01444d98657dc4e88836872993353a2abe Mon Sep 17 00:00:00 2001 From: Andre LaFleur Date: Sat, 18 Dec 2021 14:02:38 -0700 Subject: [PATCH] Get config stuffs into subclasses --- src/WatchyRTC.cpp | 83 +++++++---------------------------------------- src/WatchyRTC.h | 77 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 77 deletions(-) diff --git a/src/WatchyRTC.cpp b/src/WatchyRTC.cpp index 202546c..df73d01 100644 --- a/src/WatchyRTC.cpp +++ b/src/WatchyRTC.cpp @@ -29,22 +29,14 @@ void WatchyRTC::init(){ } void WatchyRTC::config(String datetime){ - if (rtcType == DS3232_RTC_TYPE) { - _DSConfig(datetime); - } else if (rtcType == PCF8563_RTC_TYPE) { - _PCFConfig(datetime); - } + _rtc->config(datetime); } void WatchyRTC::clearAlarm(){ if (rtcType == DS3232_RTC_TYPE) { ((DS3232*) _rtc)->rtc_ds.alarm(ALARM_2); } else if (rtcType == PCF8563_RTC_TYPE) { - int nextAlarmMinute = 0; - ((PCF8563 *) _rtc)->rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC - nextAlarmMinute = ((PCF8563 *) _rtc)->rtc_pcf.getMinute(); - nextAlarmMinute = (nextAlarmMinute == 59) ? 0 : (nextAlarmMinute + 1); //set alarm to trigger 1 minute from now - ((PCF8563 *) _rtc)->rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99); + _rtc->clearAlarm(); } } @@ -70,7 +62,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_DS; + 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) { @@ -80,71 +72,18 @@ void WatchyRTC::set(tmElements_t tm){ } } +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; +} + uint8_t WatchyRTC::temperature(){ if (rtcType == DS3232_RTC_TYPE) { return ((DS3232*) _rtc)->rtc_ds.temperature(); } return NO_TEMPERATURE_ERR; -} - -void WatchyRTC::_DSConfig(String datetime){ - if (datetime != "") { - tmElements_t tm; - tm.Year = _getValue(datetime, ':', 0).toInt() - YEAR_OFFSET_DS;//offset from 1970, since year is stored in uint8_t - tm.Month = _getValue(datetime, ':', 1).toInt(); - tm.Day = _getValue(datetime, ':', 2).toInt(); - tm.Hour = _getValue(datetime, ':', 3).toInt(); - tm.Minute = _getValue(datetime, ':', 4).toInt(); - tm.Second = _getValue(datetime, ':', 5).toInt(); - time_t t = makeTime(tm); - ((DS3232*) _rtc)->rtc_ds.set(t); - } - //https://github.com/JChristensen/DS3232RTC - ((DS3232*) _rtc)->rtc_ds.squareWave(SQWAVE_NONE); //disable square wave output - ((DS3232*) _rtc)->rtc_ds.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0); //alarm wakes up Watchy every minute - ((DS3232*) _rtc)->rtc_ds.alarmInterrupt(ALARM_2, true); //enable alarm interrupt -} - -void WatchyRTC::_PCFConfig(String datetime){ - if (datetime != "") { - int Year = _getValue(datetime, ':', 0).toInt(); - int Month = _getValue(datetime, ':', 1).toInt(); - int Day = _getValue(datetime, ':', 2).toInt(); - int Hour = _getValue(datetime, ':', 3).toInt(); - int Minute = _getValue(datetime, ':', 4).toInt(); - int Second = _getValue(datetime, ':', 5).toInt(); - //day, weekday, month, century(1=1900, 0=2000), year(0-99) - ((PCF8563 *) _rtc)->rtc_pcf.setDate(Day, _getDayOfWeek(Day, Month, Year), Month, 0, Year - YEAR_OFFSET_PCF);//offset from 2000 - //hr, min, sec - ((PCF8563 *) _rtc)->rtc_pcf.setTime(Hour, Minute, 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; -} - -String WatchyRTC::_getValue(String data, char separator, int index) -{ - int found = 0; - int strIndex[] = {0, -1}; - int maxIndex = data.length()-1; - - for(int i=0; i<=maxIndex && found<=index; i++){ - if(data.charAt(i)==separator || i==maxIndex){ - found++; - strIndex[0] = strIndex[1]+1; - strIndex[1] = (i == maxIndex) ? i+1 : i; - } - } - - return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; } \ No newline at end of file diff --git a/src/WatchyRTC.h b/src/WatchyRTC.h index 4fd1308..1953e47 100644 --- a/src/WatchyRTC.h +++ b/src/WatchyRTC.h @@ -10,7 +10,7 @@ #define RTC_DS_ADDR 0x68 #define RTC_PCF_ADDR 0x51 -#define YEAR_OFFSET_DS 1970 +#define YEAR_OFFSET_DS3232 1970 #define YEAR_OFFSET_PCF 2000 #define NO_TEMPERATURE_ERR 255 @@ -30,18 +30,88 @@ public: virtual void read(tmElements_t &tm) {} virtual void set(tmElements_t tm) {} virtual uint8_t temperature() { return NO_TEMPERATURE_ERR; } +protected: + String getValue(String data, char separator, int index) { + int found = 0; + int strIndex[] = {0, -1}; + int maxIndex = data.length()-1; + + for (int i=0; i <= maxIndex && found <= index; i++) { + if (data.charAt(i)==separator || i==maxIndex) { + found++; + strIndex[0] = strIndex[1] + 1; + strIndex[1] = (i == maxIndex) ? i + 1 : i; + } + } + + if (found > index) { + return data.substring(strIndex[0], strIndex[1]); + } + + return ""; + } }; 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; + // offset from 1970, since year is stored in uint8_t + tm.Year = getValue(datetime, ':', 0).toInt() - YEAR_OFFSET_DS3232; + tm.Month = getValue(datetime, ':', 1).toInt(); + tm.Day = getValue(datetime, ':', 2).toInt(); + tm.Hour = getValue(datetime, ':', 3).toInt(); + tm.Minute = getValue(datetime, ':', 4).toInt(); + tm.Second = getValue(datetime, ':', 5).toInt(); + time_t t = makeTime(tm); + rtc_ds.set(t); + } + + // https://github.com/JChristensen/DS3232RTC + rtc_ds.squareWave(SQWAVE_NONE); //disable square wave output + 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 + } }; 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(); + int Month = getValue(datetime, ':', 1).toInt(); + int Day = getValue(datetime, ':', 2).toInt(); + int Hour = getValue(datetime, ':', 3).toInt(); + 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 + //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 + nextAlarmMinute = rtc_pcf.getMinute(); + nextAlarmMinute = (nextAlarmMinute == 59) ? 0 : (nextAlarmMinute + 1); //set alarm to trigger 1 minute from now + rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99); + } +private: + int 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; + } }; class WatchyRTC { @@ -56,11 +126,8 @@ class WatchyRTC { void set(tmElements_t tm); uint8_t temperature(); private: - void _DSConfig(String datetime); - void _PCFConfig(String datetime); - int _getDayOfWeek(int d, int m, int y); - String _getValue(String data, char separator, int index); bool _canConnectTo(int addr); + int _getDayOfWeek(int d, int m, int y); AbstractRTC* _rtc; };