@@ -23,6 +23,7 @@ ws_sdcard::ws_sdcard()
23
23
: _sd_spi_cfg(WsV2.pin_sd_cs, DEDICATED_SPI, SPI_SD_CLOCK) {
24
24
is_mode_offline = false ;
25
25
_use_test_data = false ;
26
+ _is_soft_rtc = false ;
26
27
_sz_cur_log_file = 0 ;
27
28
_sd_cur_log_files = 0 ;
28
29
@@ -145,17 +146,33 @@ bool ws_sdcard::InitPCF8523() {
145
146
146
147
/* *************************************************************************/
147
148
/* !
148
- @brief Initializes a software RTC.
149
- @returns True if the RTC was successfully initialized, False
149
+ @brief Initializes a "soft" RTC for devices without a physical
150
+ RTC module attached.
151
+ @returns True if the soft RTC was successfully initialized, False
150
152
otherwise.
151
153
*/
152
154
/* *************************************************************************/
153
155
bool ws_sdcard::InitSoftRTC () {
154
- // NOTE: RTC_Soft always returns void
155
- _rtc_soft-> begin ( DateTime ( F (__DATE__), F (__TIME__))) ;
156
- return true ;
156
+ _is_soft_rtc = true ;
157
+ _soft_rtc_counter = 0 ;
158
+ return _is_soft_rtc ;
157
159
}
158
160
161
+ /* *************************************************************************/
162
+ /* !
163
+ @brief Increments the "soft" RTC.
164
+ */
165
+ /* *************************************************************************/
166
+ void ws_sdcard::TickSoftRTC () { _soft_rtc_counter++; }
167
+
168
+ /* *************************************************************************/
169
+ /* !
170
+ @brief Returns the current timestamp from the RTC.
171
+ @returns The current timestamp from the RTC.
172
+ */
173
+ /* *************************************************************************/
174
+ uint32_t ws_sdcard::GetSoftRTCTime () { return _soft_rtc_counter; }
175
+
159
176
/* *************************************************************************/
160
177
/* !
161
178
@brief Initializes and configures a RTC for logging.
@@ -171,11 +188,11 @@ bool ws_sdcard::ConfigureRTC(const char *rtc_type) {
171
188
return InitDS3231 ();
172
189
} else if (strcmp (rtc_type, " PCF8523" ) == 0 ) {
173
190
return InitPCF8523 ();
174
- } else if (strcmp (rtc_type, " SOFT_RTC " ) == 0 ) {
191
+ } else if (strcmp (rtc_type, " SOFT " ) == 0 ) {
175
192
return InitSoftRTC ();
176
- }
177
- WS_DEBUG_PRINTLN (
178
- " [SD] Runtime Error: Unknown RTC type found in JSON string!" );
193
+ } else
194
+ WS_DEBUG_PRINTLN (
195
+ " [SD] Runtime Error: Unknown RTC type found in JSON string!" );
179
196
return false ;
180
197
}
181
198
@@ -577,17 +594,21 @@ bool ws_sdcard::CreateNewLogFile() {
577
594
return false ;
578
595
}
579
596
580
- File32 file ;
597
+ String logFilename ;
581
598
// Generate a name for the new log file using the RTC's timestamp
582
- String logFilename = " log_" + String (GetTimestamp ()) + " .log" ;
599
+ if (!_is_soft_rtc)
600
+ logFilename = " log_" + String (GetTimestamp ()) + " .log" ;
601
+ else
602
+ logFilename = " log_" + String (millis ()) + " .log" ;
583
603
static char log_filename_buffer[256 ];
584
604
strncpy (log_filename_buffer, logFilename.c_str (),
585
605
sizeof (log_filename_buffer) - 1 );
586
606
log_filename_buffer[sizeof (log_filename_buffer) - 1 ] = ' \0 ' ;
587
607
_log_filename = log_filename_buffer;
588
608
_sz_cur_log_file = 0 ; // Reset the current log file size
589
609
590
- // Attempt to create the new log file
610
+ // Attempt to create a new log file
611
+ File32 file;
591
612
if (!file.open (_log_filename, O_RDWR | O_CREAT | O_AT_END))
592
613
return false ;
593
614
WS_DEBUG_PRINT (" [SD] Created new log file on SD card: " );
@@ -689,7 +710,7 @@ bool ws_sdcard::parseConfigFile() {
689
710
690
711
WS_DEBUG_PRINTLN (" Configuring RTC..." );
691
712
#ifndef OFFLINE_MODE_WOKWI
692
- const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT_RTC " ;
713
+ const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT " ;
693
714
WS_DEBUG_PRINT (" RTC Type: " );
694
715
WS_DEBUG_PRINTLN (json_rtc);
695
716
if (!ConfigureRTC (json_rtc)) {
@@ -808,8 +829,11 @@ uint32_t ws_sdcard::GetTimestamp() {
808
829
now = _rtc_ds1307->now ();
809
830
else if (_rtc_pcf8523 != nullptr )
810
831
now = _rtc_pcf8523->now ();
811
- else if (_rtc_soft != nullptr ) {
812
- now = _rtc_soft->now ();
832
+ else if (_is_soft_rtc) {
833
+ uint32_t cur_time = GetSoftRTCTime ();
834
+ TickSoftRTC ();
835
+ return cur_time; // early-return as we are not converting this "soft rtc" to
836
+ // unixtime
813
837
} else { // we're either using a simulator or have undefined behavior
814
838
return 0 ;
815
839
}
0 commit comments