22
22
ws_sdcard::ws_sdcard () {
23
23
is_mode_offline = false ;
24
24
_use_test_data = false ;
25
- _sz_log_file = 0 ;
25
+ _sz_cur_log_file = 0 ;
26
26
}
27
27
28
28
/* *************************************************************************/
@@ -45,16 +45,36 @@ ws_sdcard::~ws_sdcard() {
45
45
*/
46
46
/* *************************************************************************/
47
47
bool ws_sdcard::InitSDCard () {
48
- if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR) {
49
- is_mode_offline = false ;
48
+ is_mode_offline = false ;
49
+ csd_t csd;
50
+ if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR)
51
+ return is_mode_offline;
52
+
53
+ if (!_sd.begin (WsV2.pin_sd_cs )) {
54
+ WS_DEBUG_PRINTLN (
55
+ " SD initialization failed.\n Do not reformat the card!\n Is the card "
56
+ " correctly inserted?\n Is there a wiring/soldering problem\n " );
50
57
return is_mode_offline;
51
58
}
52
59
53
- if (_sd.begin (WsV2.pin_sd_cs )) {
54
- is_mode_offline = true ;
55
- } else {
56
- is_mode_offline = false ;
60
+ // Calculate the maximum number of log files that can be stored on the SD card
61
+ if (!_sd.card ()->readCSD (&csd)) {
62
+ WS_DEBUG_PRINTLN (" ERROR: Could not read sdcard information" );
63
+ return is_mode_offline;
64
+ }
65
+ // get the complete sdcard capacity in bytes
66
+ _sd_capacity = (uint64_t )512 * csd.capacity ();
67
+ // account for 3-5% fatfs overhead utilization
68
+ size_t sd_capacity_usable = _sd_capacity * (1 - 0.05 );
69
+ // proportionally set sz of each log file to 10% of the SD card's usable capacity
70
+ _max_sz_log_file = sd_capacity_usable / 10 ;
71
+ // Regardless of sd card size, cap log files to 512MB
72
+ if (_max_sz_log_file > MAX_SZ_LOG_FILE) {
73
+ _max_sz_log_file = MAX_SZ_LOG_FILE;
57
74
}
75
+ _sd_max_num_log_files = sd_capacity_usable / _max_sz_log_file;
76
+
77
+ is_mode_offline = true ;
58
78
return is_mode_offline;
59
79
}
60
80
@@ -430,6 +450,12 @@ bool ws_sdcard::AddSignalMessageToSharedBuffer(
430
450
*/
431
451
/* *************************************************************************/
432
452
bool ws_sdcard::CreateNewLogFile () {
453
+ if (_sd_cur_log_files >= _sd_max_num_log_files) {
454
+ WS_DEBUG_PRINTLN (
455
+ " [SD] Maximum number of log files for SD card capacity reached!" );
456
+ return false ;
457
+ }
458
+
433
459
File32 file;
434
460
// Generate a name for the new log file using the RTC's timestamp
435
461
String logFilename = " log_" + String (GetTimestamp ()) + " .json" ;
@@ -444,6 +470,7 @@ bool ws_sdcard::CreateNewLogFile() {
444
470
return false ;
445
471
WS_DEBUG_PRINT (" [SD] Created new log file on SD card: " );
446
472
WS_DEBUG_PRINTLN (_log_filename);
473
+ _sd_cur_log_files++;
447
474
return true ;
448
475
}
449
476
@@ -473,6 +500,14 @@ bool ws_sdcard::ValidateChecksum(JsonDocument &doc) {
473
500
*/
474
501
/* *************************************************************************/
475
502
bool ws_sdcard::parseConfigFile () {
503
+ // TODO: THIS IS JUST DEBUG!
504
+ WS_DEBUG_PRINT (" SD card capacity: " );
505
+ WS_DEBUG_PRINTLN (_sd_capacity);
506
+ WS_DEBUG_PRINT (" Maximum number of log files: " );
507
+ WS_DEBUG_PRINTLN (_sd_max_num_log_files);
508
+ WS_DEBUG_PRINT (" Maximum size of log file: " );
509
+ WS_DEBUG_PRINTLN (_max_sz_log_file);
510
+
476
511
int max_json_len = 4096 ;
477
512
DeserializationError error;
478
513
JsonDocument doc;
@@ -785,12 +820,13 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
785
820
szJson = serializeJson (doc, Serial); // TODO: Add buffering here, too?
786
821
Serial.print (" \n " ); // JSONL format specifier
787
822
#endif
788
- _sz_log_file = szJson + 2 ; // +2 bytes for "\n"
823
+ _sz_cur_log_file = szJson + 2 ; // +2 bytes for "\n"
789
824
790
- if (_sz_log_file > MAX_LOG_FILE_SZ) {
791
- WS_DEBUG_PRINTLN (" [SD] Log file size has exceeded maximum size, creating "
792
- " a new file..." );
793
- CreateNewLogFile ();
825
+ if (_sz_cur_log_file >= _max_sz_log_file) {
826
+ WS_DEBUG_PRINTLN (" [SD] Log file has exceeded maximum size! Attempting to "
827
+ " create a new file..." );
828
+ if (!CreateNewLogFile ())
829
+ return false ;
794
830
return false ;
795
831
}
796
832
0 commit comments