@@ -38,77 +38,118 @@ ws_sdcard::~ws_sdcard() {
3838 }
3939}
4040
41- bool ws_sdcard::initSDCard () {
41+ bool ws_sdcard::InitSDCard () {
4242#ifdef SD_CS_PIN
4343 // Attempt to initialize the SD card
4444 if (_sd.begin (SD_CS_PIN)) {
4545 mode_offline = true ;
4646 }
4747#endif
48- return mode_offline;
48+ return mode_offline;
4949}
5050
5151/* *************************************************************************/
5252/* !
53- @brief Enables logging via a physical RTC to a SD-card, if available.
54- Otherwise, enables logging using millis() timestamps .
53+ @brief Initializes a DS1307 RTC
54+ @returns True if the RTC was successfully initialized, False otherwise .
5555*/
5656/* *************************************************************************/
57- void ws_sdcard::EnableLogging () {
58- // Attempt to search for a DS3231 RTC
59- WS_DEBUG_PRINTLN (" Searching for DS1307 RTC..." );
57+ bool ws_sdcard::InitDS1307 () {
6058 _rtc_ds1307 = new RTC_DS1307 ();
61- if (_rtc_ds1307->begin ()) {
62- WS_DEBUG_PRINTLN (" Found DS1307 RTC!" );
63- if (!_rtc_ds1307->isrunning ()) {
64- Serial.println (" RTC is not running, let's set the time!" );
65- // When time needs to be set on a new device, or after a power loss, the
66- // following line sets the RTC to the date & time this sketch was compiled
67- _rtc_ds1307->adjust (DateTime (F (__DATE__), F (__TIME__)));
68- }
69- } else {
70- WS_DEBUG_PRINT (" Unable to find DS1307 RTC, " );
59+ if (!_rtc_ds1307->begin ()) {
60+ WS_DEBUG_PRINTLN (" [SD] Failed to initialize DS1307 RTC" );
7161 delete _rtc_ds1307;
72- _rtc_ds1307 = nullptr ;
73- // Attempt to search for a DS3231 RTC if DS1307 is not found
74- WS_DEBUG_PRINTLN (" searching for DS3231 RTC..." );
75- _rtc_ds3231 = new RTC_DS3231 ();
76- if (_rtc_ds3231->begin ()) {
77- WS_DEBUG_PRINTLN (" Found DS3231 RTC!" );
78- if (_rtc_ds3231->lostPower ()) {
79- Serial.println (" RTC lost power, let's set the time!" );
80- // When time needs to be set on a new device, or after a power loss, the
81- // following line sets the RTC to the date & time this sketch was
82- // compiled
83- _rtc_ds3231->adjust (DateTime (F (__DATE__), F (__TIME__)));
84- }
85- } else {
86- WS_DEBUG_PRINT (" Unable to find DS3231 RTC, " );
87- delete _rtc_ds3231;
88- _rtc_ds3231 = nullptr ;
89- // Attempt to search for a DS3231 RTC if DS1307 is not found
90- WS_DEBUG_PRINTLN (" searching for PCF8523 RTC..." );
91- _rtc_pcf8523 = new RTC_PCF8523 ();
92- if (_rtc_pcf8523->begin ()) {
93- WS_DEBUG_PRINTLN (" Found PCF8523 RTC!" );
94- if (_rtc_pcf8523->lostPower ()) {
95- Serial.println (" RTC lost power, let's set the time!" );
96- // When time needs to be set on a new device, or after a power loss,
97- // the following line sets the RTC to the date & time this sketch was
98- // compiled
99- _rtc_pcf8523->adjust (DateTime (F (__DATE__), F (__TIME__)));
100- }
101- }
102- }
62+ return false ;
10363 }
64+ if (!_rtc_ds1307->isrunning ())
65+ _rtc_ds1307->adjust (DateTime (F (__DATE__), F (__TIME__)));
66+ return true ;
67+ }
10468
105- // Fallback to millis() if no RTC is found
106- if (_rtc_ds1307 == nullptr && _rtc_ds3231 == nullptr ) {
107- WS_DEBUG_PRINTLN (
108- " [SD] No RTC found, defaulting to use millis() timestamps!" )
109- } else {
110- WS_DEBUG_PRINTLN (" [SD] RTC found, using RTC timestamps!" );
69+ /* *************************************************************************/
70+ /* !
71+ @brief Initializes a DS3231 RTC.
72+ @returns True if the RTC was successfully initialized, False
73+ otherwise.
74+ */
75+ /* *************************************************************************/
76+ bool ws_sdcard::InitDS3231 () {
77+ _rtc_ds3231 = new RTC_DS3231 ();
78+ if (!_rtc_ds3231->begin ()) {
79+ WS_DEBUG_PRINTLN (" [SD] Failed to initialize DS3231 RTC" );
80+ delete _rtc_ds3231;
81+ return false ;
82+ }
83+ if (_rtc_ds3231->lostPower ())
84+ _rtc_ds3231->adjust (DateTime (F (__DATE__), F (__TIME__)));
85+ WS_DEBUG_PRINTLN (" [SD] Enabled DS3231 RTC" );
86+ return true ;
87+ }
88+
89+ /* *************************************************************************/
90+ /* !
91+ @brief Initializes a PCF8523 RTC.
92+ @returns True if the RTC was successfully initialized, False
93+ otherwise.
94+ */
95+ /* *************************************************************************/
96+ bool ws_sdcard::InitPCF8523 () {
97+ _rtc_pcf8523 = new RTC_PCF8523 ();
98+ if (!_rtc_pcf8523->begin ()) {
99+ WS_DEBUG_PRINTLN (" [SD] Failed to initialize PCF8523 RTC" );
100+ delete _rtc_pcf8523;
101+ return false ;
102+ }
103+ if (_rtc_pcf8523->lostPower ())
104+ _rtc_pcf8523->adjust (DateTime (F (__DATE__), F (__TIME__)));
105+ return true ;
106+ }
107+
108+ /* *************************************************************************/
109+ /* !
110+ @brief Initializes a software RTC.
111+ @returns True if the RTC was successfully initialized, False
112+ otherwise.
113+ */
114+ /* *************************************************************************/
115+ bool ws_sdcard::InitSoftRTC () {
116+ _rtc_soft->begin (DateTime (F (__DATE__), F (__TIME__)));
117+ return true ;
118+ }
119+
120+ /* *************************************************************************/
121+ /* !
122+ @brief Initializes and configures a RTC for logging.
123+ @param type
124+ The desired type of RTC to configure.
125+ @returns True if the RTC was successfully configured, False otherwise.
126+ */
127+ /* *************************************************************************/
128+ bool ws_sdcard::ConfigureRTC (sdcard_rtc type) {
129+ bool did_init = false ;
130+ switch (type) {
131+ case DS1307:
132+ did_init = InitDS1307 ();
133+ WS_DEBUG_PRINTLN (" [SD] Enabled DS1307 RTC" );
134+ break ;
135+ case DS3231:
136+ did_init = InitDS3231 ();
137+ WS_DEBUG_PRINTLN (" [SD] Enabled DS3231 RTC" );
138+ break ;
139+ case PCF8523:
140+ did_init = InitPCF8523 ();
141+ WS_DEBUG_PRINTLN (" [SD] Enabled PCF8523 RTC" );
142+ break ;
143+ case SOFT_RTC:
144+ did_init = InitSoftRTC ();
145+ WS_DEBUG_PRINTLN (" [SD] Enabled software RTC" );
146+ break ;
147+ default :
148+ WS_DEBUG_PRINTLN (" [SD] Unknown RTC type" );
149+ did_init = false ;
150+ break ;
111151 }
152+ return did_init;
112153}
113154
114155/* *************************************************************************/
@@ -162,6 +203,7 @@ bool ws_sdcard::parseConfigFile() {
162203 // the MQTT broker while in offline mode, we can still configure
163204 // the hardware by parsing the JSON object's "exportedFromDevice"
164205 // contents and setting up the hardware
206+
165207 WS_DEBUG_PRINT (" [SD] Performing check-in process..." );
166208 JsonObject exportedFromDevice = doc[" exportedFromDevice" ];
167209 if (exportedFromDevice.isNull ()) {
@@ -177,6 +219,27 @@ bool ws_sdcard::parseConfigFile() {
177219 exportedFromDevice[" totalAnalogPins" ]);
178220 WS_DEBUG_PRINTLN (" OK!" );
179221
222+ // Configure RTC based on the RTC type
223+ sdcard_rtc type;
224+ if (strcmp (exportedFromDevice[" rtc" ], " DS1307" )) {
225+ type = DS1307;
226+ } else if (strcmp (exportedFromDevice[" rtc" ], " DS3231" )) {
227+ type = DS3231;
228+ } else if (strcmp (exportedFromDevice[" rtc" ], " PCF8523" )) {
229+ type = PCF8523;
230+ } else if (strcmp (exportedFromDevice[" rtc" ], " SOFT_RTC" )) {
231+ type = SOFT_RTC;
232+ } else {
233+ WS_DEBUG_PRINTLN (
234+ " [SD] FATAL Parsing error - Unknown RTC type found in JSON string!" );
235+ type = UNKNOWN;
236+ }
237+
238+ if (!ConfigureRTC (type)) {
239+ WS_DEBUG_PRINTLN (" [SD] Failed to to configure RTC!" );
240+ return false ;
241+ }
242+
180243 // Parse the "components" array into a JsonObject
181244 JsonArray components_ar = doc[" components" ].as <JsonArray>();
182245 int count = components_ar.size ();
@@ -240,7 +303,7 @@ bool ws_sdcard::parseConfigFile() {
240303 String (component[" direction" ]));
241304 return false ;
242305 }
243-
306+
244307 msg_signal_b2d = wippersnapper_signal_BrokerToDevice_init_zero;
245308 msg_signal_b2d.which_payload =
246309 wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
@@ -491,29 +554,28 @@ bool ws_sdcard::waitForSerialConfig() {
491554 " }\\ n\r\n " ;
492555
493556 _serialInput = " " ; // Clear the serial input buffer
494- if (!_use_test_data) {
557+ if (!_use_test_data) {
495558 WS_DEBUG_PRINTLN (" [SD] Waiting for incoming JSON string..." );
496559 while (true ) {
497- // Check if there is data available to read
498- if (Serial.available () > 0 ) {
499- // Read and append to _serialInput
500- char c = Serial.read ();
501- _serialInput += c;
502-
503- // DEBUG - Check JSON output as an Int and total output
504- // WS_DEBUG_PRINT("[SD] Character read: ");
505- // WS_DEBUG_PRINTLN((int)c);
506- // WS_DEBUG_PRINTLN(_serialInput);
507-
508- // Check for end of JSON string using \n sequence
509- if (_serialInput.endsWith (" \\ n" )) {
510- WS_DEBUG_PRINTLN (" [SD] End of JSON string detected!" );
511- break ;
512- }
560+ // Check if there is data available to read
561+ if (Serial.available () > 0 ) {
562+ // Read and append to _serialInput
563+ char c = Serial.read ();
564+ _serialInput += c;
565+
566+ // DEBUG - Check JSON output as an Int and total output
567+ // WS_DEBUG_PRINT("[SD] Character read: ");
568+ // WS_DEBUG_PRINTLN((int)c);
569+ // WS_DEBUG_PRINTLN(_serialInput);
570+
571+ // Check for end of JSON string using \n sequence
572+ if (_serialInput.endsWith (" \\ n" )) {
573+ WS_DEBUG_PRINTLN (" [SD] End of JSON string detected!" );
574+ break ;
513575 }
576+ }
514577 }
515- }
516-
578+ }
517579
518580 // Strip the '\n' off the end of _serialInput
519581 _serialInput.trim ();
0 commit comments