@@ -23,6 +23,7 @@ ws_sdcard::ws_sdcard()
2323 : _sd_spi_cfg(WsV2.pin_sd_cs, DEDICATED_SPI, SPI_SD_CLOCK) {
2424 is_mode_offline = false ;
2525 _use_test_data = false ;
26+ _is_soft_rtc = false ;
2627 _sz_cur_log_file = 0 ;
2728 _sd_cur_log_files = 0 ;
2829
@@ -145,17 +146,33 @@ bool ws_sdcard::InitPCF8523() {
145146
146147/* *************************************************************************/
147148/* !
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
150152 otherwise.
151153*/
152154/* *************************************************************************/
153155bool 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 ;
157159}
158160
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+
159176/* *************************************************************************/
160177/* !
161178 @brief Initializes and configures a RTC for logging.
@@ -171,11 +188,11 @@ bool ws_sdcard::ConfigureRTC(const char *rtc_type) {
171188 return InitDS3231 ();
172189 } else if (strcmp (rtc_type, " PCF8523" ) == 0 ) {
173190 return InitPCF8523 ();
174- } else if (strcmp (rtc_type, " SOFT_RTC " ) == 0 ) {
191+ } else if (strcmp (rtc_type, " SOFT " ) == 0 ) {
175192 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!" );
179196 return false ;
180197}
181198
@@ -577,17 +594,21 @@ bool ws_sdcard::CreateNewLogFile() {
577594 return false ;
578595 }
579596
580- File32 file ;
597+ String logFilename ;
581598 // 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" ;
583603 static char log_filename_buffer[256 ];
584604 strncpy (log_filename_buffer, logFilename.c_str (),
585605 sizeof (log_filename_buffer) - 1 );
586606 log_filename_buffer[sizeof (log_filename_buffer) - 1 ] = ' \0 ' ;
587607 _log_filename = log_filename_buffer;
588608 _sz_cur_log_file = 0 ; // Reset the current log file size
589609
590- // Attempt to create the new log file
610+ // Attempt to create a new log file
611+ File32 file;
591612 if (!file.open (_log_filename, O_RDWR | O_CREAT | O_AT_END))
592613 return false ;
593614 WS_DEBUG_PRINT (" [SD] Created new log file on SD card: " );
@@ -689,7 +710,7 @@ bool ws_sdcard::parseConfigFile() {
689710
690711 WS_DEBUG_PRINTLN (" Configuring RTC..." );
691712#ifndef OFFLINE_MODE_WOKWI
692- const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT_RTC " ;
713+ const char *json_rtc = exportedFromDevice[" rtc" ] | " SOFT " ;
693714 WS_DEBUG_PRINT (" RTC Type: " );
694715 WS_DEBUG_PRINTLN (json_rtc);
695716 if (!ConfigureRTC (json_rtc)) {
@@ -808,8 +829,11 @@ uint32_t ws_sdcard::GetTimestamp() {
808829 now = _rtc_ds1307->now ();
809830 else if (_rtc_pcf8523 != nullptr )
810831 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
813837 } else { // we're either using a simulator or have undefined behavior
814838 return 0 ;
815839 }
0 commit comments