From f373421a68527eed617b0611347d738c737e7935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Mon, 9 Jan 2023 10:44:11 +0100 Subject: [PATCH] Mode::timers now working with microseconds --- src/mode/Mode.h | 42 +++++++++++++++++++++----------------- src/mode/NewYear/NewYear.h | 2 +- src/mode/Pong/Pong.h | 18 ++++++++-------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/mode/Mode.h b/src/mode/Mode.h index dffb6f1..8628c32 100644 --- a/src/mode/Mode.h +++ b/src/mode/Mode.h @@ -23,8 +23,8 @@ class Mode { private: struct Timer { - milliseconds_t interval; - milliseconds_t last; + microseconds_t interval; + microseconds_t rest; }; Display &_display; @@ -52,22 +52,22 @@ protected: tm now = {0, 0, 0, 0, 0, 0, 0, 0, 0}; - milliseconds_t realtimeMilliseconds; + milliseconds_t realtimeMilliseconds = 0; time_t epoch = 0; - virtual void tick(uint8_t index, milliseconds_t milliseconds) {}; + virtual void tick(uint8_t index, microseconds_t microseconds) {}; virtual void step(microseconds_t microseconds) {}; virtual void draw(Display &display) {}; - void timer(uint8_t index, milliseconds_t interval) { + void timer(uint8_t index, milliseconds_t milliseconds) { if (index >= countof(timers)) { return; } - timers[index].interval = interval; - timers[index].last = millis(); + timers[index].interval = milliseconds * 1000; + timers[index].rest = milliseconds * 1000; } void markDirty() { @@ -87,13 +87,10 @@ public: virtual const char *getName() = 0; - void loop(microseconds_t micros) { + void loop(microseconds_t microseconds) { handleRealtime(); - - handleTimers(); - - step(micros); - + handleTimers(microseconds); + step(microseconds); if (dirty) { dirty = false; draw(_display); @@ -103,6 +100,11 @@ public: private: void handleRealtime() { + realtimeUpdate(); + realtimeMillisecondsUpdate(); + } + + void realtimeUpdate() { time_t tmp; time(&tmp); realtimeOK = tmp > 1600000000; @@ -117,7 +119,9 @@ private: } else { realtimeChanged = false; } + } + void realtimeMillisecondsUpdate() { if (lastSecond < 0 || lastSecond != now.tm_sec) { lastSecond = (int8_t) now.tm_sec; lastSecondChange_Milliseconds = millis(); @@ -125,14 +129,14 @@ private: realtimeMilliseconds = millis() - lastSecondChange_Milliseconds; } - void handleTimers() { - milliseconds_t ms = millis(); + void handleTimers(microseconds_t microseconds) { for (Timer *timer = timers; timer < timers + countof(timers); timer++) { if (timer->interval > 0) { - milliseconds_t milliseconds = ms - timer->last; - if (milliseconds >= timer->interval) { - timer->last = ms; - tick(timer - timers, milliseconds); + if (microseconds >= timer->rest) { + timer->rest = timer->interval; + tick(timer - timers, timer->interval); + } else { + timer->rest -= microseconds; } } } diff --git a/src/mode/NewYear/NewYear.h b/src/mode/NewYear/NewYear.h index b09103f..a37b1fd 100644 --- a/src/mode/NewYear/NewYear.h +++ b/src/mode/NewYear/NewYear.h @@ -65,7 +65,7 @@ protected: } } - void tick(uint8_t index, milliseconds_t milliseconds) override { + void tick(uint8_t index, microseconds_t microseconds) override { launch(); } diff --git a/src/mode/Pong/Pong.h b/src/mode/Pong/Pong.h index 5db93ad..00ab919 100644 --- a/src/mode/Pong/Pong.h +++ b/src/mode/Pong/Pong.h @@ -23,7 +23,7 @@ private: Status status = PLAY; - microseconds_t timeout = 0; + microseconds_t timeoutMicroseconds = 0; public: @@ -42,11 +42,11 @@ public: protected: - void tick(uint8_t index, milliseconds_t milliseconds) override { + void tick(uint8_t index, microseconds_t microseconds) override { switch (status) { case SCORE: - timeout -= milliseconds; - if (timeout <= 0) { + timeoutMicroseconds -= microseconds; + if (timeoutMicroseconds <= 0) { status = PLAY; } break; @@ -61,11 +61,11 @@ protected: break; case OVER: - timeout -= milliseconds; - if (timeout <= 0) { + timeoutMicroseconds -= microseconds; + if (timeoutMicroseconds <= 0) { resetPlayer(); status = PLAY; - timeout = 0; + timeoutMicroseconds = 0; } break; } @@ -124,7 +124,7 @@ private: } if (player0.score >= 10 || player1.score >= 10) { status = OVER; - timeout = 2000; + timeoutMicroseconds = 2000 * 1000; } } @@ -144,7 +144,7 @@ private: velocity.x = direction; velocity.y = 0; status = SCORE; - timeout = 2000; + timeoutMicroseconds = 2000 * 1000; } };