Rely on two types with if statements

* I worry about the simple constructors here though. I believe that
  causes things to be instantiated on the stack which is then blown
  away. That would mean that I would need to rely on the heap with
  the new keyword, but if I do that I will need to deallocate later.
* Digging into DS3232RTC, it looks like it has no deallocator, and I
  kind of suspect the same for Rtc_Pcf8563. Either way, we will want
  to ensure that we can delete those classes where we need to. I
  would hate to introduce a memory leak somewhere on something so
  small
pull/120/head
Andre LaFleur 2021-12-18 11:14:15 -07:00
parent b7800f0b21
commit 3ca79867b9
2 changed files with 30 additions and 13 deletions

View File

@ -14,21 +14,24 @@ bool WatchyRTC::_canConnectTo(int addr) {
void WatchyRTC::init(){
if (_canConnectTo(RTC_DS_ADDR)) {
rtcType = DS3232_RTC_TYPE;
_rtc = DS3232();
return;
}
if (_canConnectTo(RTC_PCF_ADDR)) {
rtcType = PCF8563_RTC_TYPE;
_rtc = PCF8563();
return;
}
rtcType = NO_RTC_TYPE;
_rtc = AbstractRTC();
}
void WatchyRTC::config(String datetime){
if (rtcType == DS3232_RTC_TYPE) {
_DSConfig(datetime);
} else {
} else if (rtcType == PCF8563_RTC_TYPE) {
_PCFConfig(datetime);
}
}
@ -36,7 +39,7 @@ void WatchyRTC::config(String datetime){
void WatchyRTC::clearAlarm(){
if (rtcType == DS3232_RTC_TYPE) {
rtc_ds.alarm(ALARM_2);
}else{
} else if (rtcType == PCF8563_RTC_TYPE) {
int nextAlarmMinute = 0;
rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC
nextAlarmMinute = rtc_pcf.getMinute();
@ -49,7 +52,7 @@ void WatchyRTC::read(tmElements_t &tm){
if (rtcType == DS3232_RTC_TYPE) {
rtc_ds.read(tm);
tm.Year = tm.Year - 30; //reset to offset from 2000
}else{
} else if (rtcType == PCF8563_RTC_TYPE) {
tm.Month = rtc_pcf.getMonth();
if(tm.Month == 0){ //PCF8563 POR sets month = 0 for some reason
tm.Month = 1;
@ -70,7 +73,7 @@ void WatchyRTC::set(tmElements_t tm){
tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS;
time_t t = makeTime(tm);
rtc_ds.set(t);
}else{
} else if (rtcType == PCF8563_RTC_TYPE) {
rtc_pcf.setDate(tm.Day, _getDayOfWeek(tm.Day, tm.Month, tm.Year+YEAR_OFFSET_PCF), tm.Month, 0, tm.Year);
rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second);
clearAlarm();
@ -80,9 +83,9 @@ void WatchyRTC::set(tmElements_t tm){
uint8_t WatchyRTC::temperature(){
if (rtcType == DS3232_RTC_TYPE) {
return rtc_ds.temperature();
}else{
return 255; //error
}
return NO_TEMPERATURE_ERR;
}
void WatchyRTC::_DSConfig(String datetime){
@ -116,6 +119,7 @@ void WatchyRTC::_PCFConfig(String datetime){
//hr, min, sec
rtc_pcf.setTime(Hour, Minute, Second);
}
clearAlarm();
}

View File

@ -24,6 +24,7 @@
class AbstractRTC {
public:
virtual ~AbstractRTC() {}
virtual void config(String datetime) {}
virtual void clearAlarm() {}
virtual void read(tmElements_t &tm) {}
@ -31,6 +32,18 @@ public:
virtual uint8_t temperature() { return NO_TEMPERATURE_ERR; }
};
class DS3232 : public AbstractRTC {
public:
DS3232RTC rtc_ds; // TODO: We should not have public member variables
~DS3232() {}
};
class PCF8563 : public AbstractRTC {
public:
Rtc_Pcf8563 rtc_pcf; // TODO: We should not have public member variables
~PCF8563() {}
};
class WatchyRTC {
public:
DS3232RTC rtc_ds;