Skip to content

Commit 3d9378d

Browse files
author
Scott Powell
committed
* Fix for VolatileRTCClock wrapping around to initial synced time every 49 days
1 parent c4e99a8 commit 3d9378d

File tree

8 files changed

+27
-5
lines changed

8 files changed

+27
-5
lines changed

examples/companion_radio/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,5 @@ void loop() {
227227
#ifdef DISPLAY_CLASS
228228
ui_task.loop();
229229
#endif
230+
rtc_clock.tick();
230231
}

examples/simple_repeater/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@ void loop() {
114114
#ifdef DISPLAY_CLASS
115115
ui_task.loop();
116116
#endif
117+
rtc_clock.tick();
117118
}

examples/simple_room_server/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,5 @@ void loop() {
110110
#ifdef DISPLAY_CLASS
111111
ui_task.loop();
112112
#endif
113+
rtc_clock.tick();
113114
}

examples/simple_secure_chat/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class MyMesh : public BaseChatMesh, ContactVisitor {
548548

549549
StdRNG fast_rng;
550550
SimpleMeshTables tables;
551-
MyMesh the_mesh(radio_driver, fast_rng, *new VolatileRTCClock(), tables); // TODO: test with 'rtc_clock' in target.cpp
551+
MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables);
552552

553553
void halt() {
554554
while (1) ;
@@ -587,4 +587,5 @@ void setup() {
587587

588588
void loop() {
589589
the_mesh.loop();
590+
rtc_clock.tick();
590591
}

examples/simple_sensor/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,5 @@ void loop() {
144144
#ifdef DISPLAY_CLASS
145145
ui_task.loop();
146146
#endif
147+
rtc_clock.tick();
147148
}

src/MeshCore.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class RTCClock {
7272
*/
7373
virtual void setCurrentTime(uint32_t time) = 0;
7474

75+
/**
76+
* override in classes that need to periodically update internal state
77+
*/
78+
virtual void tick() { /* no op */}
79+
7580
uint32_t getCurrentTimeUnique() {
7681
uint32_t t = getCurrentTime();
7782
if (t <= last_unique) {

src/helpers/ArduinoHelpers.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
#include <Arduino.h>
55

66
class VolatileRTCClock : public mesh::RTCClock {
7-
long millis_offset;
7+
uint32_t base_time;
8+
uint64_t accumulator;
9+
unsigned long prev_millis;
810
public:
9-
VolatileRTCClock() { millis_offset = 1715770351; } // 15 May 2024, 8:50pm
10-
uint32_t getCurrentTime() override { return (millis()/1000 + millis_offset); }
11-
void setCurrentTime(uint32_t time) override { millis_offset = time - millis()/1000; }
11+
VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm
12+
uint32_t getCurrentTime() override { return base_time + accumulator/1000; }
13+
void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); }
14+
15+
void tick() override {
16+
unsigned long now = millis();
17+
accumulator += (now - prev_millis);
18+
prev_millis = now;
19+
}
1220
};
1321

1422
class ArduinoMillis : public mesh::MillisecondClock {

src/helpers/AutoDiscoverRTCClock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ class AutoDiscoverRTCClock : public mesh::RTCClock {
1414
void begin(TwoWire& wire);
1515
uint32_t getCurrentTime() override;
1616
void setCurrentTime(uint32_t time) override;
17+
18+
void tick() override {
19+
_fallback->tick(); // is typically VolatileRTCClock, which now needs tick()
20+
}
1721
};

0 commit comments

Comments
 (0)