diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 89adca59..82c8c21d 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -227,4 +227,5 @@ void loop() { #ifdef DISPLAY_CLASS ui_task.loop(); #endif + rtc_clock.tick(); } diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 7db0e7d4..5843df74 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -114,4 +114,5 @@ void loop() { #ifdef DISPLAY_CLASS ui_task.loop(); #endif + rtc_clock.tick(); } diff --git a/examples/simple_room_server/main.cpp b/examples/simple_room_server/main.cpp index 8f6b6d58..1a3b4d6e 100644 --- a/examples/simple_room_server/main.cpp +++ b/examples/simple_room_server/main.cpp @@ -110,4 +110,5 @@ void loop() { #ifdef DISPLAY_CLASS ui_task.loop(); #endif + rtc_clock.tick(); } diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index eac35898..da1bac5b 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/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(); } diff --git a/examples/simple_sensor/main.cpp b/examples/simple_sensor/main.cpp index 2dacd1b4..a5fcc148 100644 --- a/examples/simple_sensor/main.cpp +++ b/examples/simple_sensor/main.cpp @@ -144,4 +144,5 @@ void loop() { #ifdef DISPLAY_CLASS ui_task.loop(); #endif + rtc_clock.tick(); } diff --git a/src/MeshCore.h b/src/MeshCore.h index 5c7e1760..94bf351d 100644 --- a/src/MeshCore.h +++ b/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) { diff --git a/src/helpers/ArduinoHelpers.h b/src/helpers/ArduinoHelpers.h index a736c9b0..97596daa 100644 --- a/src/helpers/ArduinoHelpers.h +++ b/src/helpers/ArduinoHelpers.h @@ -4,11 +4,19 @@ #include 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 { diff --git a/src/helpers/AutoDiscoverRTCClock.h b/src/helpers/AutoDiscoverRTCClock.h index 02eedf52..11364cd8 100644 --- a/src/helpers/AutoDiscoverRTCClock.h +++ b/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() + } };