@@ -30,7 +30,8 @@ ws_sdcard::ws_sdcard()
3030
3131 if (!_sd.begin (_sd_spi_cfg)) {
3232 WS_DEBUG_PRINTLN (
33- " SD initialization failed.\n Do not reformat the card!\n Is the card "
33+ " [SD] Runtime Error: SD initialization failed.\n Do not reformat the "
34+ " card!\n Is the card "
3435 " correctly inserted?\n Is there a wiring/soldering problem\n " );
3536 is_mode_offline = false ;
3637 } else {
@@ -47,16 +48,16 @@ ws_sdcard::ws_sdcard()
4748/* *************************************************************************/
4849ws_sdcard::~ws_sdcard () {
4950 if (is_mode_offline) {
50- _sd.end (); // Close the SD card
51- is_mode_offline = false ; // Disable offline mode
51+ _sd.end (); // Close the SD card
5252 }
53+ is_mode_offline = false ;
5354}
5455
5556void ws_sdcard::calculateFileLimits () {
5657 // Calculate the maximum number of log files that can be stored on the SD card
5758 csd_t csd;
5859 if (!_sd.card ()->readCSD (&csd)) {
59- WS_DEBUG_PRINTLN (" ERROR : Could not read sdcard information" );
60+ WS_DEBUG_PRINTLN (" Runtime Error : Could not read sdcard information" );
6061 return ;
6162 }
6263
@@ -83,9 +84,11 @@ void ws_sdcard::calculateFileLimits() {
8384bool ws_sdcard::InitDS1307 () {
8485 _rtc_ds1307 = new RTC_DS1307 ();
8586 if (!_rtc_ds1307->begin ()) {
86- WS_DEBUG_PRINTLN (" [SD] Failed to initialize DS1307 RTC" );
87- delete _rtc_ds1307;
88- return false ;
87+ if (!_rtc_ds1307->begin (&Wire1)) {
88+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Failed to initialize DS1307 RTC" );
89+ delete _rtc_ds1307;
90+ return false ;
91+ }
8992 }
9093 if (!_rtc_ds1307->isrunning ())
9194 _rtc_ds1307->adjust (DateTime (F (__DATE__), F (__TIME__)));
@@ -102,9 +105,11 @@ bool ws_sdcard::InitDS1307() {
102105bool ws_sdcard::InitDS3231 () {
103106 _rtc_ds3231 = new RTC_DS3231 ();
104107 if (!_rtc_ds3231->begin ()) {
105- WS_DEBUG_PRINTLN (" [SD] Failed to initialize DS3231 RTC" );
106- delete _rtc_ds3231;
107- return false ;
108+ if (!_rtc_ds3231->begin (&Wire1)) {
109+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Failed to initialize DS3231 RTC" );
110+ delete _rtc_ds3231;
111+ return false ;
112+ }
108113 }
109114 if (_rtc_ds3231->lostPower ())
110115 _rtc_ds3231->adjust (DateTime (F (__DATE__), F (__TIME__)));
@@ -121,9 +126,11 @@ bool ws_sdcard::InitDS3231() {
121126bool ws_sdcard::InitPCF8523 () {
122127 _rtc_pcf8523 = new RTC_PCF8523 ();
123128 if (!_rtc_pcf8523->begin ()) {
124- WS_DEBUG_PRINTLN (" [SD] Failed to initialize PCF8523 RTC" );
125- delete _rtc_pcf8523;
126- return false ;
129+ if (!_rtc_pcf8523->begin (&Wire1)) {
130+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Failed to initialize PCF8523 RTC" );
131+ delete _rtc_pcf8523;
132+ return false ;
133+ }
127134 }
128135 if (_rtc_pcf8523->lostPower ())
129136 _rtc_pcf8523->adjust (DateTime (F (__DATE__), F (__TIME__)));
@@ -167,7 +174,7 @@ bool ws_sdcard::ConfigureRTC(const char *rtc_type) {
167174 WS_DEBUG_PRINTLN (" [SD] Enabled software RTC" );
168175 } else {
169176 WS_DEBUG_PRINTLN (
170- " [SD] FATAL Parsing error - Unknown RTC type found in JSON string!" );
177+ " [SD] Runtime Error: Unknown RTC type found in JSON string!" );
171178 did_init = false ;
172179 }
173180
@@ -416,11 +423,10 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
416423
417424 // Get the encoded size of the signal message first so we can resize the
418425 // buffer prior to encoding
419- WS_DEBUG_PRINTLN (" Encoding D2b signal message..." );
420426 if (!pb_get_encoded_size (&tempBufSz,
421427 wippersnapper_signal_BrokerToDevice_fields,
422428 &msg_signal)) {
423- WS_DEBUG_PRINTLN (" [SD] ERROR : Unable to get signal message size!" );
429+ WS_DEBUG_PRINTLN (" [SD] Runtime Error : Unable to get signal message size!" );
424430 return false ;
425431 }
426432
@@ -429,11 +435,11 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
429435 pb_ostream_t ostream = pb_ostream_from_buffer (tempBuf.data (), tempBuf.size ());
430436 if (!ws_pb_encode (&ostream, wippersnapper_signal_BrokerToDevice_fields,
431437 &msg_signal)) {
432- WS_DEBUG_PRINTLN (" [SD] ERROR: Unable to encode D2B signal message!" );
438+ WS_DEBUG_PRINTLN (
439+ " [SD] Runtime Error: Unable to encode D2B signal message!" );
433440 return false ;
434441 }
435442 WsV2._sharedConfigBuffers .push_back (std::move (tempBuf));
436- WS_DEBUG_PRINTLN (" Encoded the D2b signal message" );
437443 return true ;
438444}
439445
@@ -447,14 +453,14 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
447453/* *************************************************************************/
448454bool ws_sdcard::CreateNewLogFile () {
449455 if (_sd_cur_log_files >= _sd_max_num_log_files) {
450- WS_DEBUG_PRINTLN (
451- " [SD] Maximum number of log files for SD card capacity reached!" );
456+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Maximum number of log files for SD "
457+ " card capacity reached!" );
452458 return false ;
453459 }
454460
455461 File32 file;
456462 // Generate a name for the new log file using the RTC's timestamp
457- String logFilename = " log_" + String (GetTimestamp ()) + " .json " ;
463+ String logFilename = " log_" + String (GetTimestamp ()) + " .log " ;
458464 static char log_filename_buffer[256 ];
459465 strncpy (log_filename_buffer, logFilename.c_str (),
460466 sizeof (log_filename_buffer) - 1 );
@@ -496,18 +502,16 @@ bool ws_sdcard::ValidateChecksum(JsonDocument &doc) {
496502*/
497503/* *************************************************************************/
498504bool ws_sdcard::parseConfigFile () {
499- // TODO: THIS IS JUST DEBUG!
505+ DeserializationError error;
506+ JsonDocument doc;
507+ // TODO: THIS IS JUST FOR DEBUG Testing, remove for PR review
500508 WS_DEBUG_PRINT (" SD card capacity: " );
501509 WS_DEBUG_PRINTLN (_sd_capacity);
502510 WS_DEBUG_PRINT (" Maximum number of log files: " );
503511 WS_DEBUG_PRINTLN (_sd_max_num_log_files);
504512 WS_DEBUG_PRINT (" Maximum size of log file: " );
505513 WS_DEBUG_PRINTLN (_max_sz_log_file);
506514
507- int max_json_len = 4096 ;
508- DeserializationError error;
509- JsonDocument doc;
510-
511515#ifndef OFFLINE_MODE_DEBUG
512516 WS_DEBUG_PRINTLN (" [SD] Parsing config.json..." );
513517 doc = WsV2._config_doc ; // Use the config document from the filesystem
@@ -516,22 +520,23 @@ bool ws_sdcard::parseConfigFile() {
516520 if (!_use_test_data) {
517521 WS_DEBUG_PRINTLN (" [SD] Parsing Serial Input..." );
518522 WS_DEBUG_PRINT (_serialInput);
519- error = deserializeJson (doc, _serialInput.c_str (), max_json_len );
523+ error = deserializeJson (doc, _serialInput.c_str (), MAX_LEN_CFG_JSON );
520524 } else {
521525 WS_DEBUG_PRINTLN (" [SD] Parsing Test Data..." );
522- error = deserializeJson (doc, json_test_data, max_json_len );
526+ error = deserializeJson (doc, json_test_data, MAX_LEN_CFG_JSON );
523527 }
524528#endif
525529 // It is not possible to continue running in offline mode without a valid
526530 // config file
527531 if (error) {
528- WS_DEBUG_PRINTLN (" [SD] Unable to deserialize config JSON, error code: " +
529- String (error.c_str ()));
532+ WS_DEBUG_PRINTLN (
533+ " [SD] Runtime Error: Unable to deserialize config.json. Error Code: " +
534+ String (error.c_str ()));
530535 return false ;
531536 }
532537 WS_DEBUG_PRINTLN (" [SD] Successfully deserialized JSON config file!" );
533538
534- // Check the file's integrity
539+ // Check config.json file's integrity
535540 if (!ValidateChecksum (doc)) {
536541 WS_DEBUG_PRINTLN (" [SD] Checksum mismatch, file has been modified from its "
537542 " original state!" );
@@ -541,14 +546,14 @@ bool ws_sdcard::parseConfigFile() {
541546 // Begin parsing the JSON document
542547 JsonObject exportedFromDevice = doc[" exportedFromDevice" ];
543548 if (exportedFromDevice.isNull ()) {
544- WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - No exportedFromDevice object "
545- " found in JSON string! Unable to configure hardware !" );
549+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Required exportedFromDevice not "
550+ " found in config file !" );
546551 return false ;
547552 }
548553 JsonArray components_ar = doc[" components" ].as <JsonArray>();
549554 if (components_ar.isNull ()) {
550- WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - No components array found in "
551- " JSON string !" );
555+ WS_DEBUG_PRINTLN (
556+ " [SD] Runtime Error: Required components array not found !" );
552557 return false ;
553558 }
554559
@@ -561,11 +566,11 @@ bool ws_sdcard::parseConfigFile() {
561566#ifndef OFFLINE_MODE_WOKWI
562567 const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT_RTC" ;
563568 if (!ConfigureRTC (json_rtc)) {
564- WS_DEBUG_PRINTLN (" [SD] Failed to to configure RTC!" );
569+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Failed to to configure RTC!" );
565570 return false ;
566571 }
567572#else
568- WS_DEBUG_PRINTLN (" [SD] Skipping RTC configuration for Wokwi Simulator ..." );
573+ WS_DEBUG_PRINTLN (" [SD] Did not configure RTC for Wokwi..." );
569574#endif
570575
571576 // Parse each component from JSON->PB and push into a shared buffer
@@ -576,8 +581,7 @@ bool ws_sdcard::parseConfigFile() {
576581 // Parse the component API type
577582 const char *component_api_type = component[" componentAPI" ];
578583 if (component_api_type == nullptr ) {
579- WS_DEBUG_PRINTLN (
580- " [SD] FATAL Parsing error - Missing component API type!" );
584+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Missing component API type!" );
581585 return false ;
582586 }
583587
@@ -592,8 +596,9 @@ bool ws_sdcard::parseConfigFile() {
592596 component[" period" ] | 0.0 , component[" value" ],
593597 component[" sampleMode" ] | UNKNOWN_VALUE,
594598 component[" direction" ] | UNKNOWN_VALUE, component[" pull" ])) {
595- WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - Unable to parse "
596- " DigitalIO component!" );
599+ WS_DEBUG_PRINT (
600+ " [SD] Runtime Error: Unable to parse DigitalIO Component, Pin: " );
601+ WS_DEBUG_PRINTLN (component[" pinName" ] | UNKNOWN_VALUE);
597602 return false ;
598603 }
599604
@@ -609,7 +614,8 @@ bool ws_sdcard::parseConfigFile() {
609614 component[" period" ] | 0.0 ,
610615 component[" analogReadMode" ] | UNKNOWN_VALUE)) {
611616 WS_DEBUG_PRINTLN (
612- " [SD] FATAL Parsing error - Unable to parse AnalogIO component!" );
617+ " [SD] Runtime Error: Unable to parse AnalogIO Component, Pin: " );
618+ WS_DEBUG_PRINTLN (component[" pinName" ] | UNKNOWN_VALUE);
613619 return false ;
614620 }
615621
@@ -627,24 +633,23 @@ bool ws_sdcard::parseConfigFile() {
627633 component[" sensorType1" ] | UNKNOWN_VALUE,
628634 component[" sensorType2" ] | UNKNOWN_VALUE)) {
629635 WS_DEBUG_PRINTLN (
630- " [SD] FATAL Parsing error - Unable to parse DS18X20 component!" );
636+ " [SD] Runtime Error: Unable to parse DS18X20 Component, Pin: " );
637+ WS_DEBUG_PRINTLN (component[" pinName" ] | UNKNOWN_VALUE);
631638 return false ;
632639 }
633-
634640 msg_signal_b2d.which_payload =
635641 wippersnapper_signal_BrokerToDevice_ds18x20_add_tag;
636642 msg_signal_b2d.payload .ds18x20_add = msg_DS18X20Add;
637643 } else {
638- // Unknown component API type
639- WS_DEBUG_PRINTLN (" [SD] Unknown component API type found: " +
644+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Unknown Component API Type: " +
640645 String (component_api_type));
641646 return false ;
642647 }
643648
644649 // App handles the signal messages, in-order
645650 if (!AddSignalMessageToSharedBuffer (msg_signal_b2d)) {
646- WS_DEBUG_PRINTLN (
647- " [SD] FATAL Error - Unable to add signal message to shared buffer!" );
651+ WS_DEBUG_PRINTLN (" [SD] Runtime Error: Unable to add signal message(s) "
652+ " to shared buffer!" );
648653 return false ;
649654 }
650655 }
@@ -801,7 +806,7 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
801806 file = _sd.open (_log_filename, O_RDWR | O_CREAT | O_AT_END);
802807 if (!file) {
803808 WS_DEBUG_PRINTLN (
804- " [SD] FATAL Error - Unable to open the log file for writing!" );
809+ " [SD] Runtime Error: Unable to open log file for writing!" );
805810 return false ;
806811 }
807812 BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
@@ -819,8 +824,9 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
819824 _sz_cur_log_file = szJson + 2 ; // +2 bytes for "\n"
820825
821826 if (_sz_cur_log_file >= _max_sz_log_file) {
822- WS_DEBUG_PRINTLN (" [SD] Log file has exceeded maximum size! Attempting to "
823- " create a new file..." );
827+ WS_DEBUG_PRINTLN (
828+ " [SD] NOTE: Log file has exceeded maximum size! Attempting to "
829+ " create a new file..." );
824830 if (!CreateNewLogFile ())
825831 return false ;
826832 return false ;
0 commit comments