Skip to content

Commit 6485e4d

Browse files
committed
Work without a RTC attached
1 parent d0b42ad commit 6485e4d

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

src/Wippersnapper_V2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
#endif
128128

129129
#define WS_VERSION \
130-
"1.0.0-alpha.1-offline" ///< WipperSnapper app. version (semver-formatted)
130+
"1.0.0-offline-beta.1" ///< WipperSnapper app. version (semver-formatted)
131131

132132
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
133133
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks

src/Wippersnapper_demo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "ws_adapters.h"
1212
ws_adapter_wifi wipper;
1313
// Uncomment the following line to use the offline adapter for Pico
14-
// ws_adapter_offline wipper;
14+
//ws_adapter_offline wipper;
1515

1616
#define WS_DEBUG // Enable debug output!
1717

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**************************************************************************/
153155
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;
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
}

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class ws_sdcard {
7575
bool InitDS3231();
7676
bool InitPCF8523();
7777
bool InitSoftRTC();
78+
void TickSoftRTC();
79+
uint32_t GetSoftRTCTime();
7880
wippersnapper_sensor_SensorType ParseSensorType(const char *sensor_type);
7981
bool ParseDigitalIOAdd(wippersnapper_digitalio_DigitalIOAdd &msg_DigitalIOAdd,
8082
const char *pin, float period, bool value,
@@ -117,8 +119,9 @@ class ws_sdcard {
117119
RTC_DS3231 *_rtc_ds3231 = nullptr; ///< DS3231 RTC object
118120
RTC_DS1307 *_rtc_ds1307 = nullptr; ///< DS1307 RTC object
119121
RTC_PCF8523 *_rtc_pcf8523 = nullptr; ///< PCF8523 RTC object
120-
RTC_Millis *_rtc_soft = nullptr; ///< Software RTC object
121-
bool _use_test_data; ///< True if sample data is being used for testing
122+
bool _is_soft_rtc; ///< True if a "soft rtc" is being used, False otherwise
123+
uint32_t _soft_rtc_counter; ///< Holds the counter for a "soft rtc"
124+
bool _use_test_data; ///< True if sample data is being used for testing
122125
};
123126
extern Wippersnapper_V2 WsV2;
124127
#endif // WS_SDCARD_H

0 commit comments

Comments
 (0)