diff --git a/src/BASICS.h b/src/BASICS.h index 52a7a10..878d93b 100644 --- a/src/BASICS.h +++ b/src/BASICS.h @@ -13,7 +13,7 @@ typedef int64_t microseconds_t; -typedef unsigned long millis_t; +typedef unsigned long milliseconds_t; double doStep(double valueCurrent, double valueMin, double valueMax, microseconds_t millisecondsTotal, microseconds_t microsecondsDelta); diff --git a/src/config.cpp b/src/config.cpp index b08e2de..d3921a4 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -16,7 +16,7 @@ bool dirty = false; bool notify = false; -millis_t lastDirtyMillis = 0; +milliseconds_t lastDirtyMillis = 0; void config_setup() { if (!config_load()) { diff --git a/src/display/Display.h b/src/display/Display.h index 17b0b7e..f1c2ba5 100644 --- a/src/display/Display.h +++ b/src/display/Display.h @@ -29,7 +29,7 @@ private: Adafruit_NeoPixel leds; - millis_t fpsLastMillis = 0; + milliseconds_t fpsLastMillis = 0; int fps = 0; diff --git a/src/mode.cpp b/src/mode.cpp index f3b8006..2afebcc 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -102,7 +102,7 @@ void mode_step() { return; } auto currentMicros = (int64_t) micros(); - microseconds_t dt = (microseconds_t) min(1000000.0, max(1.0, (double) (currentMicros - lastMicros) * config.speed)); + microseconds_t microseconds = (microseconds_t) min(1000000.0, max(1.0, (double) (currentMicros - lastMicros) * config.speed)); lastMicros = currentMicros; - mode->loop(dt); + mode->loop(microseconds); } diff --git a/src/mode/Clock/Clock.h b/src/mode/Clock/Clock.h index 71beac6..30b1587 100644 --- a/src/mode/Clock/Clock.h +++ b/src/mode/Clock/Clock.h @@ -18,7 +18,7 @@ public: protected: - void step(microseconds_t dt) override { + void step(microseconds_t microseconds) override { if (realtimeChanged) { markDirty(); } diff --git a/src/mode/GameOfLife/Cell.h b/src/mode/GameOfLife/Cell.h index 3b27e8b..b773d49 100644 --- a/src/mode/GameOfLife/Cell.h +++ b/src/mode/GameOfLife/Cell.h @@ -13,12 +13,12 @@ public: Color color = BLACK; - void animate(microseconds_t dt) { + void animate(microseconds_t microseconds) { // TODO fading does not work as expected if (alive) { - fade = doStep(fade, 0.0, 255.0, 200, +dt); + fade = doStep(fade, 0.0, 255.0, 200, +microseconds); } else { - fade = doStep(fade, 0.0, 255.0, 200, -dt); + fade = doStep(fade, 0.0, 255.0, 200, -microseconds); } } diff --git a/src/mode/GameOfLife/GameOfLife.h b/src/mode/GameOfLife/GameOfLife.h index 9761c2e..cfa4cec 100644 --- a/src/mode/GameOfLife/GameOfLife.h +++ b/src/mode/GameOfLife/GameOfLife.h @@ -39,7 +39,7 @@ public: colorMode(colorMode), cellsSize(display.pixelCount * sizeof(Cell)) { cells = (Cell *) malloc(cellsSize); - cellsEnd = cells + cellsSize; + cellsEnd = cells + display.pixelCount; for (Cell *cell = cells; cell < cells + display.pixelCount; cell++) { kill(cell); } @@ -76,8 +76,8 @@ public: protected: - void step(microseconds_t dt) override { - runtime += dt; + void step(microseconds_t microseconds) override { + runtime += microseconds; if (runtime >= 500000) { runtime = 0; if (lastAliveCount == aliveCount) { @@ -93,7 +93,7 @@ protected: } } for (Cell *cell = cells; cell < cellsEnd; cell++) { - cell->animate(dt); + cell->animate(microseconds); } // TODO don't always markDirty. Be more efficient diff --git a/src/mode/Mode.h b/src/mode/Mode.h index ae24311..d5586b3 100644 --- a/src/mode/Mode.h +++ b/src/mode/Mode.h @@ -23,8 +23,8 @@ class Mode { private: struct Timer { - millis_t interval; - millis_t last; + milliseconds_t interval; + milliseconds_t last; }; Display &_display; @@ -36,6 +36,10 @@ private: {0, 0}, }; + int8_t lastSecond = -1; + + milliseconds_t lastSecondChange_Milliseconds = 0; + protected: const uint8_t width; @@ -48,15 +52,17 @@ protected: tm now = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + milliseconds_t realtimeMilliseconds; + time_t epoch = 0; - virtual void tick(uint8_t index, millis_t dt) {}; + virtual void tick(uint8_t index, milliseconds_t milliseconds) {}; - virtual void step(microseconds_t dt) {}; + virtual void step(microseconds_t microseconds) {}; virtual void draw(Display &display) {}; - void timer(uint8_t index, millis_t interval) { + void timer(uint8_t index, milliseconds_t interval) { if (index >= countof(timers)) { return; } @@ -81,12 +87,12 @@ public: virtual const char *getName() = 0; - void loop(microseconds_t dt) { - realtime(); + void loop(microseconds_t micros) { + handleRealtime(); handleTimers(); - step(dt); + step(micros); if (dirty) { dirty = false; @@ -96,7 +102,7 @@ public: private: - void realtime() { + void handleRealtime() { time_t tmp; time(&tmp); realtimeOK = tmp > 1600000000; @@ -111,16 +117,22 @@ private: } else { realtimeChanged = false; } + + if (lastSecond < 0 || lastSecond != now.tm_sec) { + lastSecond = (int8_t) now.tm_sec; + lastSecondChange_Milliseconds = millis(); + } + realtimeMilliseconds = millis() - lastSecondChange_Milliseconds; } void handleTimers() { - millis_t ms = millis(); + milliseconds_t ms = millis(); for (Timer *timer = timers; timer < timers + sizeof(timers); timer++) { if (timer->interval > 0) { - millis_t dt = ms - timer->last; - if (dt >= timer->interval) { + milliseconds_t milliseconds = ms - timer->last; + if (milliseconds >= timer->interval) { timer->last = ms; - tick(timer - timers, dt); + tick(timer - timers, milliseconds); } } } diff --git a/src/mode/NewYear/Firework.h b/src/mode/NewYear/Firework.h index 38865df..ab07895 100644 --- a/src/mode/NewYear/Firework.h +++ b/src/mode/NewYear/Firework.h @@ -60,7 +60,7 @@ public: return true; } - void step(microseconds_t dt) { + void step(microseconds_t microseconds) { switch (state) { case INITIAL: break; @@ -84,13 +84,13 @@ public: case INITIAL: break; case RISE: - position.y = doStep(position.y, 0.0, height, 1000, -dt); + position.y = doStep(position.y, 0.0, height, 1000, -microseconds); break; case EXPLODE: - explosion = doStep(explosion, 0.0, explosionRadius, 500, +dt); + explosion = doStep(explosion, 0.0, explosionRadius, 500, +microseconds); break; case SPARKLE: - sparkle = doStep(sparkle, 0.0, sparkleMax, 1000, +dt); + sparkle = doStep(sparkle, 0.0, sparkleMax, 1000, +microseconds); break; } } diff --git a/src/mode/NewYear/NewYear.h b/src/mode/NewYear/NewYear.h index cda6af5..60414a3 100644 --- a/src/mode/NewYear/NewYear.h +++ b/src/mode/NewYear/NewYear.h @@ -12,10 +12,6 @@ private: Firework fireworks[MAX_FIREWORKS]; - int8_t lastSecond = -1; - - millis_t lastSecondMillis = 0; - uint8_t days = 0; uint8_t level = 0; @@ -36,7 +32,7 @@ public: protected: - void step(microseconds_t dt) override { + void step(microseconds_t microseconds) override { if (!realtimeOK) { setMode(NO_TIME); } else if (now.tm_mon != 1 || now.tm_mday != 1 || now.tm_hour != 0) { @@ -49,14 +45,14 @@ protected: } else { setMode(FIREWORK); for (auto &firework: fireworks) { - firework.step(dt); + firework.step(microseconds); } } } void loopLastDay() { setMode(LAST_DAY); - int levelTmp = getLevel(); + int levelTmp = (int) round(32 * realtimeMilliseconds / 1000.0);; if (level != levelTmp) { level = levelTmp; markDirty(); @@ -70,7 +66,7 @@ protected: } } - void tick(uint8_t index, millis_t dt) override { + void tick(uint8_t index, milliseconds_t milliseconds) override { launch(); } @@ -104,7 +100,6 @@ private: timer(0, 0); switch (state) { case LAST_DAY: - lastSecond = -1; break; case FIREWORK: timer(0, 500); @@ -115,16 +110,6 @@ private: } } - int getLevel() { - if (lastSecond < 0 || lastSecond != now.tm_sec) { - lastSecond = (int8_t) now.tm_sec; - lastSecondMillis = millis(); - } - millis_t mils = millis() - lastSecondMillis; - int levelTmp = (int) round(32 * mils / 1000.0); - return levelTmp; - } - void launch() { for (auto &firework: fireworks) { if (firework.launch()) { diff --git a/src/mode/Pong/Pong.h b/src/mode/Pong/Pong.h index 3cbc53d..36956f3 100644 --- a/src/mode/Pong/Pong.h +++ b/src/mode/Pong/Pong.h @@ -42,10 +42,10 @@ public: protected: - void tick(uint8_t index, millis_t dt) override { + void tick(uint8_t index, milliseconds_t milliseconds) override { switch (status) { case SCORE: - timeout -= dt; + timeout -= milliseconds; if (timeout <= 0) { status = PLAY; } @@ -63,7 +63,7 @@ protected: break; case OVER: - timeout -= dt; + timeout -= milliseconds; if (timeout <= 0) { resetPlayer(); status = SCORE; diff --git a/src/mode/SpaceInvaders/SpaceInvaders.h b/src/mode/SpaceInvaders/SpaceInvaders.h index d4f76c9..031bfdf 100644 --- a/src/mode/SpaceInvaders/SpaceInvaders.h +++ b/src/mode/SpaceInvaders/SpaceInvaders.h @@ -70,10 +70,10 @@ public: protected: - void step(microseconds_t dt) override { - stepRockets(dt); - stepInvaders(dt); - stepHero(dt); + void step(microseconds_t microseconds) override { + stepRockets(microseconds); + stepInvaders(microseconds); + stepHero(microseconds); collide(); if (invadersAlive == 0) { @@ -99,8 +99,8 @@ protected: private: - void stepRockets(microseconds_t dt) { - rocketRuntime += dt; + void stepRockets(microseconds_t microseconds) { + rocketRuntime += microseconds; if (rocketRuntime > 200000) { rocketRuntime = rocketRuntime % 200000; for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) { @@ -111,18 +111,18 @@ private: rocket->y -= 1; } } else if (rocket->flash > 0) { - if (rocket->flash < dt) { + if (rocket->flash < microseconds) { rocket->flash = 0; } else { - rocket->flash -= dt; + rocket->flash -= microseconds; } } } } } - void stepInvaders(microseconds_t dt) { - swarmRuntime += dt; + void stepInvaders(microseconds_t microseconds) { + swarmRuntime += microseconds; if (swarmDown && swarmRuntime > 500000) { swarmDown = false; @@ -148,8 +148,8 @@ private: } } - void stepHero(microseconds_t dt) { - heroRuntime += dt; + void stepHero(microseconds_t microseconds) { + heroRuntime += microseconds; if (heroRuntime >= 50000) { heroRuntime = heroRuntime % 50000; if (heroLeft) { diff --git a/src/mode/Starfield/Starfield.h b/src/mode/Starfield/Starfield.h index 735655e..c2e5c56 100644 --- a/src/mode/Starfield/Starfield.h +++ b/src/mode/Starfield/Starfield.h @@ -30,9 +30,9 @@ public: protected: - void step(microseconds_t dt) override { + void step(microseconds_t microseconds) override { for (auto &star: stars) { - const Vector velocity = star.minus(center).multiply((double) dt / 200000.0); + const Vector velocity = star.minus(center).multiply((double) microseconds / 200000.0); star = star.plus(velocity); if (star.x < 0 || star.x >= width || star.y < 0 || star.y >= height) { star = center.plus(Vector::polar(random(360), 1));