* Fix for VolatileRTCClock wrapping around to initial synced time every 49 days

This commit is contained in:
Scott Powell
2025-10-30 16:45:50 +11:00
parent c4e99a841a
commit 3d9378d91e
8 changed files with 27 additions and 5 deletions

View File

@@ -227,4 +227,5 @@ void loop() {
#ifdef DISPLAY_CLASS
ui_task.loop();
#endif
rtc_clock.tick();
}

View File

@@ -114,4 +114,5 @@ void loop() {
#ifdef DISPLAY_CLASS
ui_task.loop();
#endif
rtc_clock.tick();
}

View File

@@ -110,4 +110,5 @@ void loop() {
#ifdef DISPLAY_CLASS
ui_task.loop();
#endif
rtc_clock.tick();
}

View File

@@ -548,7 +548,7 @@ public:
StdRNG fast_rng;
SimpleMeshTables tables;
MyMesh the_mesh(radio_driver, fast_rng, *new VolatileRTCClock(), tables); // TODO: test with 'rtc_clock' in target.cpp
MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables);
void halt() {
while (1) ;
@@ -587,4 +587,5 @@ void setup() {
void loop() {
the_mesh.loop();
rtc_clock.tick();
}

View File

@@ -144,4 +144,5 @@ void loop() {
#ifdef DISPLAY_CLASS
ui_task.loop();
#endif
rtc_clock.tick();
}

View File

@@ -72,6 +72,11 @@ public:
*/
virtual void setCurrentTime(uint32_t time) = 0;
/**
* override in classes that need to periodically update internal state
*/
virtual void tick() { /* no op */}
uint32_t getCurrentTimeUnique() {
uint32_t t = getCurrentTime();
if (t <= last_unique) {

View File

@@ -4,11 +4,19 @@
#include <Arduino.h>
class VolatileRTCClock : public mesh::RTCClock {
long millis_offset;
uint32_t base_time;
uint64_t accumulator;
unsigned long prev_millis;
public:
VolatileRTCClock() { millis_offset = 1715770351; } // 15 May 2024, 8:50pm
uint32_t getCurrentTime() override { return (millis()/1000 + millis_offset); }
void setCurrentTime(uint32_t time) override { millis_offset = time - millis()/1000; }
VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm
uint32_t getCurrentTime() override { return base_time + accumulator/1000; }
void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); }
void tick() override {
unsigned long now = millis();
accumulator += (now - prev_millis);
prev_millis = now;
}
};
class ArduinoMillis : public mesh::MillisecondClock {

View File

@@ -14,4 +14,8 @@ public:
void begin(TwoWire& wire);
uint32_t getCurrentTime() override;
void setCurrentTime(uint32_t time) override;
void tick() override {
_fallback->tick(); // is typically VolatileRTCClock, which now needs tick()
}
};