diff --git a/src/WatchyRTC.cpp b/src/WatchyRTC.cpp index 298c7b6..b4b0c49 100644 --- a/src/WatchyRTC.cpp +++ b/src/WatchyRTC.cpp @@ -8,12 +8,12 @@ void WatchyRTC::init(){ Wire.beginTransmission(RTC_DS_ADDR); error = Wire.endTransmission(); if(error == 0){ - rtcType = DS3231; + rtcType = DS3231_RTC_TYPE; }else{ Wire.beginTransmission(RTC_PCF_ADDR); error = Wire.endTransmission(); if(error == 0){ - rtcType = PCF8563; + rtcType = PCF8563_RTC_TYPE; }else{ //RTC Error } @@ -21,7 +21,7 @@ void WatchyRTC::init(){ } void WatchyRTC::config(String datetime){ - if(rtcType == DS3231){ + if(rtcType == DS3231_RTC_TYPE){ _DSConfig(datetime); }else{ _PCFConfig(datetime); @@ -29,7 +29,7 @@ void WatchyRTC::config(String datetime){ } void WatchyRTC::clearAlarm(){ - if(rtcType == DS3231){ + if(rtcType == DS3231_RTC_TYPE){ rtc_ds.alarm(ALARM_2); }else{ int nextAlarmMinute = 0; @@ -41,7 +41,7 @@ void WatchyRTC::clearAlarm(){ } void WatchyRTC::read(tmElements_t &tm){ - if(rtcType == DS3231){ + if(rtcType == DS3231_RTC_TYPE){ rtc_ds.read(tm); tm.Year = tm.Year - 30; //reset to offset from 2000 }else{ @@ -61,7 +61,7 @@ void WatchyRTC::read(tmElements_t &tm){ } void WatchyRTC::set(tmElements_t tm){ - if(rtcType == DS3231){ + if(rtcType == DS3231_RTC_TYPE){ tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS; time_t t = makeTime(tm); rtc_ds.set(t); @@ -73,7 +73,7 @@ void WatchyRTC::set(tmElements_t tm){ } uint8_t WatchyRTC::temperature(){ - if(rtcType == DS3231){ + if(rtcType == DS3231_RTC_TYPE){ return rtc_ds.temperature(); }else{ return 255; //error diff --git a/src/WatchyRTC.h b/src/WatchyRTC.h index ea5350b..20975c4 100644 --- a/src/WatchyRTC.h +++ b/src/WatchyRTC.h @@ -4,13 +4,29 @@ #include #include -#define DS3231 0 -#define PCF8563 1 +#define DS3231_RTC_TYPE 0 +#define PCF8563_RTC_TYPE 1 #define RTC_DS_ADDR 0x68 #define RTC_PCF_ADDR 0x51 #define YEAR_OFFSET_DS 1970 #define YEAR_OFFSET_PCF 2000 +// TODO: So we're relying on an rtcType as a multiplexer, making our WatchyRTC code a bit +// more complex. A way around this is to use a command pattern instead: +// https://sourcemaking.com/design_patterns/command +// +// That way we don't have to have a bunch of conditionals floating around in this class. +// It will simplify the process of adding a new RTC chip later as well. + +class AbstractRTC { +public: + virtual void config(String datetime); + virtual void clearAlarm(); + virtual void read(tmElements_t &tm); + virtual void set(tmElements_t tm); + virtual uint8_t temperature(); +}; + class WatchyRTC { public: DS3232RTC rtc_ds;