@@ -30,7 +30,8 @@ ws_sdcard::ws_sdcard()
30
30
31
31
if (!_sd.begin (_sd_spi_cfg)) {
32
32
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 "
34
35
" correctly inserted?\n Is there a wiring/soldering problem\n " );
35
36
is_mode_offline = false ;
36
37
} else {
@@ -47,16 +48,16 @@ ws_sdcard::ws_sdcard()
47
48
/* *************************************************************************/
48
49
ws_sdcard::~ws_sdcard () {
49
50
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
52
52
}
53
+ is_mode_offline = false ;
53
54
}
54
55
55
56
void ws_sdcard::calculateFileLimits () {
56
57
// Calculate the maximum number of log files that can be stored on the SD card
57
58
csd_t csd;
58
59
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" );
60
61
return ;
61
62
}
62
63
@@ -83,9 +84,11 @@ void ws_sdcard::calculateFileLimits() {
83
84
bool ws_sdcard::InitDS1307 () {
84
85
_rtc_ds1307 = new RTC_DS1307 ();
85
86
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
+ }
89
92
}
90
93
if (!_rtc_ds1307->isrunning ())
91
94
_rtc_ds1307->adjust (DateTime (F (__DATE__), F (__TIME__)));
@@ -102,9 +105,11 @@ bool ws_sdcard::InitDS1307() {
102
105
bool ws_sdcard::InitDS3231 () {
103
106
_rtc_ds3231 = new RTC_DS3231 ();
104
107
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
+ }
108
113
}
109
114
if (_rtc_ds3231->lostPower ())
110
115
_rtc_ds3231->adjust (DateTime (F (__DATE__), F (__TIME__)));
@@ -121,9 +126,11 @@ bool ws_sdcard::InitDS3231() {
121
126
bool ws_sdcard::InitPCF8523 () {
122
127
_rtc_pcf8523 = new RTC_PCF8523 ();
123
128
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
+ }
127
134
}
128
135
if (_rtc_pcf8523->lostPower ())
129
136
_rtc_pcf8523->adjust (DateTime (F (__DATE__), F (__TIME__)));
@@ -167,7 +174,7 @@ bool ws_sdcard::ConfigureRTC(const char *rtc_type) {
167
174
WS_DEBUG_PRINTLN (" [SD] Enabled software RTC" );
168
175
} else {
169
176
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!" );
171
178
did_init = false ;
172
179
}
173
180
@@ -416,11 +423,10 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
416
423
417
424
// Get the encoded size of the signal message first so we can resize the
418
425
// buffer prior to encoding
419
- WS_DEBUG_PRINTLN (" Encoding D2b signal message..." );
420
426
if (!pb_get_encoded_size (&tempBufSz,
421
427
wippersnapper_signal_BrokerToDevice_fields,
422
428
&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!" );
424
430
return false ;
425
431
}
426
432
@@ -429,11 +435,11 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
429
435
pb_ostream_t ostream = pb_ostream_from_buffer (tempBuf.data (), tempBuf.size ());
430
436
if (!ws_pb_encode (&ostream, wippersnapper_signal_BrokerToDevice_fields,
431
437
&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!" );
433
440
return false ;
434
441
}
435
442
WsV2._sharedConfigBuffers .push_back (std::move (tempBuf));
436
- WS_DEBUG_PRINTLN (" Encoded the D2b signal message" );
437
443
return true ;
438
444
}
439
445
@@ -447,14 +453,14 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
447
453
/* *************************************************************************/
448
454
bool ws_sdcard::CreateNewLogFile () {
449
455
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!" );
452
458
return false ;
453
459
}
454
460
455
461
File32 file;
456
462
// 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 " ;
458
464
static char log_filename_buffer[256 ];
459
465
strncpy (log_filename_buffer, logFilename.c_str (),
460
466
sizeof (log_filename_buffer) - 1 );
@@ -496,18 +502,16 @@ bool ws_sdcard::ValidateChecksum(JsonDocument &doc) {
496
502
*/
497
503
/* *************************************************************************/
498
504
bool 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
500
508
WS_DEBUG_PRINT (" SD card capacity: " );
501
509
WS_DEBUG_PRINTLN (_sd_capacity);
502
510
WS_DEBUG_PRINT (" Maximum number of log files: " );
503
511
WS_DEBUG_PRINTLN (_sd_max_num_log_files);
504
512
WS_DEBUG_PRINT (" Maximum size of log file: " );
505
513
WS_DEBUG_PRINTLN (_max_sz_log_file);
506
514
507
- int max_json_len = 4096 ;
508
- DeserializationError error;
509
- JsonDocument doc;
510
-
511
515
#ifndef OFFLINE_MODE_DEBUG
512
516
WS_DEBUG_PRINTLN (" [SD] Parsing config.json..." );
513
517
doc = WsV2._config_doc ; // Use the config document from the filesystem
@@ -516,22 +520,23 @@ bool ws_sdcard::parseConfigFile() {
516
520
if (!_use_test_data) {
517
521
WS_DEBUG_PRINTLN (" [SD] Parsing Serial Input..." );
518
522
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 );
520
524
} else {
521
525
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 );
523
527
}
524
528
#endif
525
529
// It is not possible to continue running in offline mode without a valid
526
530
// config file
527
531
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 ()));
530
535
return false ;
531
536
}
532
537
WS_DEBUG_PRINTLN (" [SD] Successfully deserialized JSON config file!" );
533
538
534
- // Check the file's integrity
539
+ // Check config.json file's integrity
535
540
if (!ValidateChecksum (doc)) {
536
541
WS_DEBUG_PRINTLN (" [SD] Checksum mismatch, file has been modified from its "
537
542
" original state!" );
@@ -541,14 +546,14 @@ bool ws_sdcard::parseConfigFile() {
541
546
// Begin parsing the JSON document
542
547
JsonObject exportedFromDevice = doc[" exportedFromDevice" ];
543
548
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 !" );
546
551
return false ;
547
552
}
548
553
JsonArray components_ar = doc[" components" ].as <JsonArray>();
549
554
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 !" );
552
557
return false ;
553
558
}
554
559
@@ -561,11 +566,11 @@ bool ws_sdcard::parseConfigFile() {
561
566
#ifndef OFFLINE_MODE_WOKWI
562
567
const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT_RTC" ;
563
568
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!" );
565
570
return false ;
566
571
}
567
572
#else
568
- WS_DEBUG_PRINTLN (" [SD] Skipping RTC configuration for Wokwi Simulator ..." );
573
+ WS_DEBUG_PRINTLN (" [SD] Did not configure RTC for Wokwi..." );
569
574
#endif
570
575
571
576
// Parse each component from JSON->PB and push into a shared buffer
@@ -576,8 +581,7 @@ bool ws_sdcard::parseConfigFile() {
576
581
// Parse the component API type
577
582
const char *component_api_type = component[" componentAPI" ];
578
583
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!" );
581
585
return false ;
582
586
}
583
587
@@ -592,8 +596,9 @@ bool ws_sdcard::parseConfigFile() {
592
596
component[" period" ] | 0.0 , component[" value" ],
593
597
component[" sampleMode" ] | UNKNOWN_VALUE,
594
598
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);
597
602
return false ;
598
603
}
599
604
@@ -609,7 +614,8 @@ bool ws_sdcard::parseConfigFile() {
609
614
component[" period" ] | 0.0 ,
610
615
component[" analogReadMode" ] | UNKNOWN_VALUE)) {
611
616
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);
613
619
return false ;
614
620
}
615
621
@@ -627,24 +633,23 @@ bool ws_sdcard::parseConfigFile() {
627
633
component[" sensorType1" ] | UNKNOWN_VALUE,
628
634
component[" sensorType2" ] | UNKNOWN_VALUE)) {
629
635
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);
631
638
return false ;
632
639
}
633
-
634
640
msg_signal_b2d.which_payload =
635
641
wippersnapper_signal_BrokerToDevice_ds18x20_add_tag;
636
642
msg_signal_b2d.payload .ds18x20_add = msg_DS18X20Add;
637
643
} 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: " +
640
645
String (component_api_type));
641
646
return false ;
642
647
}
643
648
644
649
// App handles the signal messages, in-order
645
650
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!" );
648
653
return false ;
649
654
}
650
655
}
@@ -801,7 +806,7 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
801
806
file = _sd.open (_log_filename, O_RDWR | O_CREAT | O_AT_END);
802
807
if (!file) {
803
808
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!" );
805
810
return false ;
806
811
}
807
812
BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
@@ -819,8 +824,9 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
819
824
_sz_cur_log_file = szJson + 2 ; // +2 bytes for "\n"
820
825
821
826
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..." );
824
830
if (!CreateNewLogFile ())
825
831
return false ;
826
832
return false ;
0 commit comments