@@ -406,15 +406,13 @@ bool ws_sdcard::parseConfigFile() {
406
406
return false ;
407
407
}
408
408
409
- // Configure the status LED
410
- float brightness = exportedFromDevice[" statusLEDBrightness" ];
411
- setStatusLEDBrightness (brightness);
412
-
413
409
// Mock the check-in process using the JSON's values
414
410
CheckIn (exportedFromDevice[" totalGPIOPins" ],
415
411
exportedFromDevice[" totalAnalogPins" ],
416
412
exportedFromDevice[" referenceVoltage" ]);
417
413
414
+ setStatusLEDBrightness (exportedFromDevice[" statusLEDBrightness" ]);
415
+
418
416
// Initialize and configure RTC
419
417
const char *json_rtc = exportedFromDevice[" rtc" ];
420
418
if (json_rtc == nullptr ) {
@@ -427,6 +425,23 @@ bool ws_sdcard::parseConfigFile() {
427
425
return false ;
428
426
}
429
427
428
+ // Create new logging file on device from the RTC's timestamp
429
+ #ifndef OFFLINE_MODE_DEBUG
430
+ // TODO: Refactor this out into a func
431
+ // TODO: Implement a counter within the log funcs to track # of lines in the
432
+ // file and implement a MAX_LINE cutoff
433
+ String logFilename = " log_" + String (GetTimestamp ()) + " .json" ;
434
+ _log_filename = logFilename.c_str ();
435
+ File32 file;
436
+ if (!file.open (_log_filename, FILE_WRITE)) {
437
+ WS_DEBUG_PRINTLN (
438
+ " [SD] FATAL - Failed to create initial logging file on SD card!" );
439
+ return false ;
440
+ }
441
+ WS_DEBUG_PRINT (" [SD] Created new log file on SD card: " );
442
+ WS_DEBUG_PRINTLN (_log_filename);
443
+ #endif
444
+
430
445
// Parse the "components" array into a JsonObject
431
446
JsonArray components_ar = doc[" components" ].as <JsonArray>();
432
447
if (components_ar.isNull ()) {
@@ -522,7 +537,6 @@ bool ws_sdcard::parseConfigFile() {
522
537
*/
523
538
/* *************************************************************************/
524
539
uint32_t ws_sdcard::GetTimestamp () {
525
- // Obtain RTC timestamp (TODO - refactor this out)
526
540
DateTime now;
527
541
if (_rtc_ds3231 != nullptr )
528
542
now = _rtc_ds3231->now ();
@@ -531,7 +545,7 @@ uint32_t ws_sdcard::GetTimestamp() {
531
545
else if (_rtc_pcf8523 != nullptr )
532
546
now = _rtc_pcf8523->now ();
533
547
else {
534
- // TODO! implement software millis() version of now() and unixtime()
548
+ now = _rtc_soft-> now ();
535
549
}
536
550
537
551
if (_wokwi_runner)
@@ -629,6 +643,30 @@ const char *SensorTypeToString(wippersnapper_sensor_SensorType sensorType) {
629
643
}
630
644
}
631
645
646
+ void ws_sdcard::BuildJSONDoc (JsonDocument &doc, const char *pin, float value,
647
+ wippersnapper_sensor_SensorType read_type) {
648
+ doc[" timestamp" ] = GetTimestamp ();
649
+ doc[" pin" ] = pin;
650
+ doc[" value" ] = value;
651
+ doc[" si_unit" ] = SensorTypeToString (read_type);
652
+ }
653
+
654
+ void ws_sdcard::BuildJSONDoc (JsonDocument &doc, const char *pin, uint16_t value,
655
+ wippersnapper_sensor_SensorType read_type) {
656
+ doc[" timestamp" ] = GetTimestamp ();
657
+ doc[" pin" ] = pin;
658
+ doc[" value" ] = value;
659
+ doc[" si_unit" ] = SensorTypeToString (read_type);
660
+ }
661
+
662
+ void ws_sdcard::BuildJSONDoc (JsonDocument &doc, const char *pin, bool value,
663
+ wippersnapper_sensor_SensorType read_type) {
664
+ doc[" timestamp" ] = GetTimestamp ();
665
+ doc[" pin" ] = pin;
666
+ doc[" value" ] = value;
667
+ doc[" si_unit" ] = SensorTypeToString (read_type);
668
+ }
669
+
632
670
/* *************************************************************************/
633
671
/* !
634
672
@brief Logs a GPIO sensor event to the SD card.
@@ -644,20 +682,29 @@ const char *SensorTypeToString(wippersnapper_sensor_SensorType sensorType) {
644
682
bool ws_sdcard::LogGPIOSensorEventToSD (
645
683
uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
646
684
// Get the pin name in the correct format ("A0", "A1", etc.)
685
+ // TODO: Maybe send c_pin_name to sprintf and include A/D specifier from here?
647
686
char c_pin_name[12 ];
648
687
sprintf (c_pin_name, " A%d" , pin);
649
688
650
- // Get the RTC's timestamp
651
- uint32_t timestamp = GetTimestamp ();
652
-
653
689
// Create the JSON document
654
690
JsonDocument doc;
691
+ BuildJSONDoc (doc, c_pin_name, value, read_type);
655
692
656
- doc[" timestamp" ] = timestamp;
657
- doc[" pin" ] = c_pin_name;
658
- doc[" value" ] = value;
659
- doc[" si_unit" ] = SensorTypeToString (read_type);
660
- serializeJson (doc, Serial);
693
+ // Serialize the JSON document
694
+ #ifndef OFFLINE_MODE_DEBUG
695
+ // TODO: Make this an attempt to open and return false on failure
696
+ File32 file = _sd.open (" log.json" , FILE_WRITE);
697
+ BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
698
+ serializeJson (doc, file); // Serialize the JSON to the file in 64-byte chunks
699
+ // TODO: I am not sure if this works, consult PDF ch 4.7
700
+ bufferedFile.print (" \n " );
701
+ bufferedFile.flush (); // Send the remaining bytes
702
+ #else
703
+ serializeJson (doc, Serial); // TODO: Add buffering here, too?
704
+ Serial.print (" \n " ); // JSON requires a newline at the end of each log line
705
+ #endif
706
+
707
+ // TODO: Does this need to be a boolean?
661
708
return true ;
662
709
}
663
710
@@ -679,17 +726,25 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
679
726
char c_pin_name[12 ];
680
727
sprintf (c_pin_name, " A%d" , pin);
681
728
682
- // Get the RTC's timestamp
683
- uint32_t timestamp = GetTimestamp ();
684
-
685
- // Append to the file in JSONL format
729
+ // Create the JSON document
686
730
JsonDocument doc;
687
- doc[" timestamp" ] = timestamp;
688
- doc[" pin" ] = c_pin_name;
689
- doc[" value" ] = value;
690
- doc[" si_unit" ] = SensorTypeToString (read_type);
691
- serializeJson (doc, Serial);
692
- Serial.println (" " ); // JSON requires a newline at the end of each log line
731
+ BuildJSONDoc (doc, c_pin_name, value, read_type);
732
+
733
+ // Serialize the JSON document
734
+ #ifndef OFFLINE_MODE_DEBUG
735
+ // TODO: Make this an attempt to open and return false on failure
736
+ File32 file = _sd.open (" log.json" , FILE_WRITE);
737
+ BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
738
+ serializeJson (doc, file); // Serialize the JSON to the file in 64-byte chunks
739
+ // TODO: I am not sure if this works, consult PDF ch 4.7
740
+ bufferedFile.print (" \n " );
741
+ bufferedFile.flush (); // Send the remaining bytes
742
+ #else
743
+ serializeJson (doc, Serial); // TODO: Add buffering here, too?
744
+ Serial.print (" \n " ); // JSON requires a newline at the end of each log line
745
+ #endif
746
+
747
+ // TODO: Does this need to be a boolean?
693
748
return true ;
694
749
}
695
750
@@ -709,19 +764,30 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
709
764
uint8_t pin, bool value, wippersnapper_sensor_SensorType read_type) {
710
765
// Get the pin name in the correct format ("A0", "A1", etc.)
711
766
char c_pin_name[12 ];
712
- sprintf (c_pin_name, " A%d" , pin);
713
-
714
- // Get the RTC's timestamp
715
- uint32_t timestamp = GetTimestamp ();
767
+ sprintf (c_pin_name, " D%d" , pin);
716
768
717
769
// Create the JSON document
718
770
JsonDocument doc;
719
- doc[" timestamp" ] = timestamp;
720
- doc[" pin" ] = c_pin_name;
721
- doc[" value" ] = value;
722
- doc[" si_unit" ] = SensorTypeToString (read_type);
723
- serializeJson (doc, Serial);
724
- Serial.println (" " );
771
+ BuildJSONDoc (doc, c_pin_name, value, read_type);
772
+
773
+ // Serialize the JSON document
774
+ #ifndef OFFLINE_MODE_DEBUG
775
+ // TODO: Make this an attempt to open and return false on failure
776
+ File32 file;
777
+ if (!_sd.open (" log.json" , FILE_WRITE)) {
778
+ WS_DEBUG_PRINTLN (" [SD] FATAL Error - Unable to open JSON log file!" );
779
+ }
780
+ BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
781
+ serializeJson (doc, file); // Serialize the JSON to the file in 64-byte chunks
782
+ // TODO: I am not sure if this works, consult PDF ch 4.7
783
+ bufferedFile.print (" \n " );
784
+ bufferedFile.flush (); // Send the remaining bytes
785
+ #else
786
+ serializeJson (doc, Serial); // TODO: Add buffering here, too?
787
+ Serial.print (" \n " ); // JSON requires a newline at the end of each log line
788
+ #endif
789
+
790
+ // TODO: Does this need to be a boolean?
725
791
return true ;
726
792
}
727
793
0 commit comments