Просмотр исходного кода

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

Scott Powell 9 месяцев назад
Родитель
Сommit
3d9378d91e

+ 1 - 0
examples/companion_radio/main.cpp

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

+ 1 - 0
examples/simple_repeater/main.cpp

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

+ 1 - 0
examples/simple_room_server/main.cpp

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

+ 2 - 1
examples/simple_secure_chat/main.cpp

@@ -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();
 }

+ 1 - 0
examples/simple_sensor/main.cpp

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

+ 5 - 0
src/MeshCore.h

@@ -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) {

+ 12 - 4
src/helpers/ArduinoHelpers.h

@@ -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 {

+ 4 - 0
src/helpers/AutoDiscoverRTCClock.h

@@ -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()
+  }
 };