cleanup milli-/microseconds + moved realtimeMilliseconds to Mode

This commit is contained in:
Patrick Haßel 2023-01-09 10:10:01 +01:00
parent d53d60fb2c
commit 2debaa2158
13 changed files with 63 additions and 66 deletions

View File

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

View File

@ -16,7 +16,7 @@ bool dirty = false;
bool notify = false;
millis_t lastDirtyMillis = 0;
milliseconds_t lastDirtyMillis = 0;
void config_setup() {
if (!config_load()) {

View File

@ -29,7 +29,7 @@ private:
Adafruit_NeoPixel leds;
millis_t fpsLastMillis = 0;
milliseconds_t fpsLastMillis = 0;
int fps = 0;

View File

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

View File

@ -18,7 +18,7 @@ public:
protected:
void step(microseconds_t dt) override {
void step(microseconds_t microseconds) override {
if (realtimeChanged) {
markDirty();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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