From 3a2974b99703ac9c609cffc0b77a54471f552901 Mon Sep 17 00:00:00 2001 From: Andre LaFleur Date: Sat, 18 Dec 2021 11:22:30 -0700 Subject: [PATCH] Crossing the Rubicon * We are now going to make _rtc a pointer, and use that process of polymorphism as part of the refactors. Again, I worry about memory management, but there does not seem to be a very clear way around this --- src/WatchyRTC.cpp | 9 +++++---- src/WatchyRTC.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/WatchyRTC.cpp b/src/WatchyRTC.cpp index 58b5cf5..a8953bf 100644 --- a/src/WatchyRTC.cpp +++ b/src/WatchyRTC.cpp @@ -3,7 +3,8 @@ WatchyRTC::WatchyRTC() : rtc_ds(false) {} - +// TODO: We can probably put all of this logic into AbstractRTC as a class +// function. It would simplify this class even more, which would be nice bool WatchyRTC::_canConnectTo(int addr) { byte error; Wire.beginTransmission(addr); @@ -14,18 +15,18 @@ bool WatchyRTC::_canConnectTo(int addr) { void WatchyRTC::init(){ if (_canConnectTo(RTC_DS_ADDR)) { rtcType = DS3232_RTC_TYPE; - _rtc = DS3232(); + _rtc = new DS3232(); return; } if (_canConnectTo(RTC_PCF_ADDR)) { rtcType = PCF8563_RTC_TYPE; - _rtc = PCF8563(); + _rtc = new PCF8563(); return; } rtcType = NO_RTC_TYPE; - _rtc = AbstractRTC(); + _rtc = new AbstractRTC(); } void WatchyRTC::config(String datetime){ diff --git a/src/WatchyRTC.h b/src/WatchyRTC.h index 8345ab9..14b34cf 100644 --- a/src/WatchyRTC.h +++ b/src/WatchyRTC.h @@ -63,7 +63,7 @@ class WatchyRTC { int _getDayOfWeek(int d, int m, int y); String _getValue(String data, char separator, int index); bool _canConnectTo(int addr); - AbstractRTC _rtc; + AbstractRTC* _rtc; }; #endif \ No newline at end of file