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,29 +14,32 @@ bool WatchyRTC::_canConnectTo(int addr) {
void WatchyRTC::init(){ void WatchyRTC::init(){
if (_canConnectTo(RTC_DS_ADDR)) { if (_canConnectTo(RTC_DS_ADDR)) {
rtcType = DS3232_RTC_TYPE; rtcType = DS3232_RTC_TYPE;
_rtc = DS3232();
return; return;
} }
if (_canConnectTo(RTC_PCF_ADDR)) { if (_canConnectTo(RTC_PCF_ADDR)) {
rtcType = PCF8563_RTC_TYPE; rtcType = PCF8563_RTC_TYPE;
_rtc = PCF8563();
return; return;
} }
rtcType = NO_RTC_TYPE; rtcType = NO_RTC_TYPE;
_rtc = AbstractRTC();
} }
void WatchyRTC::config(String datetime){ void WatchyRTC::config(String datetime){
if (rtcType == DS3232_RTC_TYPE) { if (rtcType == DS3232_RTC_TYPE) {
_DSConfig(datetime); _DSConfig(datetime);
} else { } else if (rtcType == PCF8563_RTC_TYPE) {
_PCFConfig(datetime); _PCFConfig(datetime);
} }
} }
void WatchyRTC::clearAlarm(){ void WatchyRTC::clearAlarm(){
if(rtcType == DS3232_RTC_TYPE){ if (rtcType == DS3232_RTC_TYPE) {
rtc_ds.alarm(ALARM_2); rtc_ds.alarm(ALARM_2);
}else{ } else if (rtcType == PCF8563_RTC_TYPE) {
int nextAlarmMinute = 0; int nextAlarmMinute = 0;
rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC
nextAlarmMinute = rtc_pcf.getMinute(); nextAlarmMinute = rtc_pcf.getMinute();
@ -46,10 +49,10 @@ void WatchyRTC::clearAlarm(){
} }
void WatchyRTC::read(tmElements_t &tm){ void WatchyRTC::read(tmElements_t &tm){
if(rtcType == DS3232_RTC_TYPE){ if (rtcType == DS3232_RTC_TYPE) {
rtc_ds.read(tm); rtc_ds.read(tm);
tm.Year = tm.Year - 30; //reset to offset from 2000 tm.Year = tm.Year - 30; //reset to offset from 2000
}else{ } else if (rtcType == PCF8563_RTC_TYPE) {
tm.Month = rtc_pcf.getMonth(); tm.Month = rtc_pcf.getMonth();
if(tm.Month == 0){ //PCF8563 POR sets month = 0 for some reason if(tm.Month == 0){ //PCF8563 POR sets month = 0 for some reason
tm.Month = 1; tm.Month = 1;
@ -66,11 +69,11 @@ void WatchyRTC::read(tmElements_t &tm){
} }
void WatchyRTC::set(tmElements_t tm){ void WatchyRTC::set(tmElements_t tm){
if(rtcType == DS3232_RTC_TYPE){ if (rtcType == DS3232_RTC_TYPE) {
tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS; tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS;
time_t t = makeTime(tm); time_t t = makeTime(tm);
rtc_ds.set(t); 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.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); rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second);
clearAlarm(); clearAlarm();
@ -78,15 +81,15 @@ void WatchyRTC::set(tmElements_t tm){
} }
uint8_t WatchyRTC::temperature(){ uint8_t WatchyRTC::temperature(){
if(rtcType == DS3232_RTC_TYPE){ if (rtcType == DS3232_RTC_TYPE) {
return rtc_ds.temperature(); return rtc_ds.temperature();
}else{
return 255; //error
} }
return NO_TEMPERATURE_ERR;
} }
void WatchyRTC::_DSConfig(String datetime){ void WatchyRTC::_DSConfig(String datetime){
if(datetime != ""){ if (datetime != "") {
tmElements_t tm; tmElements_t tm;
tm.Year = _getValue(datetime, ':', 0).toInt() - YEAR_OFFSET_DS;//offset from 1970, since year is stored in uint8_t 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.Month = _getValue(datetime, ':', 1).toInt();
@ -104,7 +107,7 @@ void WatchyRTC::_DSConfig(String datetime){
} }
void WatchyRTC::_PCFConfig(String datetime){ void WatchyRTC::_PCFConfig(String datetime){
if(datetime != ""){ if (datetime != "") {
int Year = _getValue(datetime, ':', 0).toInt(); int Year = _getValue(datetime, ':', 0).toInt();
int Month = _getValue(datetime, ':', 1).toInt(); int Month = _getValue(datetime, ':', 1).toInt();
int Day = _getValue(datetime, ':', 2).toInt(); int Day = _getValue(datetime, ':', 2).toInt();
@ -116,6 +119,7 @@ void WatchyRTC::_PCFConfig(String datetime){
//hr, min, sec //hr, min, sec
rtc_pcf.setTime(Hour, Minute, Second); rtc_pcf.setTime(Hour, Minute, Second);
} }
clearAlarm(); clearAlarm();
} }

View File

@ -24,6 +24,7 @@
class AbstractRTC { class AbstractRTC {
public: public:
virtual ~AbstractRTC() {}
virtual void config(String datetime) {} virtual void config(String datetime) {}
virtual void clearAlarm() {} virtual void clearAlarm() {}
virtual void read(tmElements_t &tm) {} virtual void read(tmElements_t &tm) {}
@ -31,6 +32,18 @@ public:
virtual uint8_t temperature() { return NO_TEMPERATURE_ERR; } 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 { class WatchyRTC {
public: public:
DS3232RTC rtc_ds; DS3232RTC rtc_ds;