cleanup milli-/microseconds + moved realtimeMilliseconds to Mode
This commit is contained in:
parent
d53d60fb2c
commit
2debaa2158
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
typedef int64_t microseconds_t;
|
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);
|
double doStep(double valueCurrent, double valueMin, double valueMax, microseconds_t millisecondsTotal, microseconds_t microsecondsDelta);
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,7 @@ bool dirty = false;
|
|||||||
|
|
||||||
bool notify = false;
|
bool notify = false;
|
||||||
|
|
||||||
millis_t lastDirtyMillis = 0;
|
milliseconds_t lastDirtyMillis = 0;
|
||||||
|
|
||||||
void config_setup() {
|
void config_setup() {
|
||||||
if (!config_load()) {
|
if (!config_load()) {
|
||||||
|
|||||||
@ -29,7 +29,7 @@ private:
|
|||||||
|
|
||||||
Adafruit_NeoPixel leds;
|
Adafruit_NeoPixel leds;
|
||||||
|
|
||||||
millis_t fpsLastMillis = 0;
|
milliseconds_t fpsLastMillis = 0;
|
||||||
|
|
||||||
int fps = 0;
|
int fps = 0;
|
||||||
|
|
||||||
|
|||||||
@ -102,7 +102,7 @@ void mode_step() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto currentMicros = (int64_t) micros();
|
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;
|
lastMicros = currentMicros;
|
||||||
mode->loop(dt);
|
mode->loop(microseconds);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void step(microseconds_t dt) override {
|
void step(microseconds_t microseconds) override {
|
||||||
if (realtimeChanged) {
|
if (realtimeChanged) {
|
||||||
markDirty();
|
markDirty();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,12 +13,12 @@ public:
|
|||||||
|
|
||||||
Color color = BLACK;
|
Color color = BLACK;
|
||||||
|
|
||||||
void animate(microseconds_t dt) {
|
void animate(microseconds_t microseconds) {
|
||||||
// TODO fading does not work as expected
|
// TODO fading does not work as expected
|
||||||
if (alive) {
|
if (alive) {
|
||||||
fade = doStep(fade, 0.0, 255.0, 200, +dt);
|
fade = doStep(fade, 0.0, 255.0, 200, +microseconds);
|
||||||
} else {
|
} else {
|
||||||
fade = doStep(fade, 0.0, 255.0, 200, -dt);
|
fade = doStep(fade, 0.0, 255.0, 200, -microseconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -39,7 +39,7 @@ public:
|
|||||||
colorMode(colorMode),
|
colorMode(colorMode),
|
||||||
cellsSize(display.pixelCount * sizeof(Cell)) {
|
cellsSize(display.pixelCount * sizeof(Cell)) {
|
||||||
cells = (Cell *) malloc(cellsSize);
|
cells = (Cell *) malloc(cellsSize);
|
||||||
cellsEnd = cells + cellsSize;
|
cellsEnd = cells + display.pixelCount;
|
||||||
for (Cell *cell = cells; cell < cells + display.pixelCount; cell++) {
|
for (Cell *cell = cells; cell < cells + display.pixelCount; cell++) {
|
||||||
kill(cell);
|
kill(cell);
|
||||||
}
|
}
|
||||||
@ -76,8 +76,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void step(microseconds_t dt) override {
|
void step(microseconds_t microseconds) override {
|
||||||
runtime += dt;
|
runtime += microseconds;
|
||||||
if (runtime >= 500000) {
|
if (runtime >= 500000) {
|
||||||
runtime = 0;
|
runtime = 0;
|
||||||
if (lastAliveCount == aliveCount) {
|
if (lastAliveCount == aliveCount) {
|
||||||
@ -93,7 +93,7 @@ protected:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Cell *cell = cells; cell < cellsEnd; cell++) {
|
for (Cell *cell = cells; cell < cellsEnd; cell++) {
|
||||||
cell->animate(dt);
|
cell->animate(microseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO don't always markDirty. Be more efficient
|
// TODO don't always markDirty. Be more efficient
|
||||||
|
|||||||
@ -23,8 +23,8 @@ class Mode {
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
struct Timer {
|
struct Timer {
|
||||||
millis_t interval;
|
milliseconds_t interval;
|
||||||
millis_t last;
|
milliseconds_t last;
|
||||||
};
|
};
|
||||||
|
|
||||||
Display &_display;
|
Display &_display;
|
||||||
@ -36,6 +36,10 @@ private:
|
|||||||
{0, 0},
|
{0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int8_t lastSecond = -1;
|
||||||
|
|
||||||
|
milliseconds_t lastSecondChange_Milliseconds = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
const uint8_t width;
|
const uint8_t width;
|
||||||
@ -48,15 +52,17 @@ protected:
|
|||||||
|
|
||||||
tm now = {0, 0, 0, 0, 0, 0, 0, 0, 0};
|
tm now = {0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
|
milliseconds_t realtimeMilliseconds;
|
||||||
|
|
||||||
time_t epoch = 0;
|
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) {};
|
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)) {
|
if (index >= countof(timers)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -81,12 +87,12 @@ public:
|
|||||||
|
|
||||||
virtual const char *getName() = 0;
|
virtual const char *getName() = 0;
|
||||||
|
|
||||||
void loop(microseconds_t dt) {
|
void loop(microseconds_t micros) {
|
||||||
realtime();
|
handleRealtime();
|
||||||
|
|
||||||
handleTimers();
|
handleTimers();
|
||||||
|
|
||||||
step(dt);
|
step(micros);
|
||||||
|
|
||||||
if (dirty) {
|
if (dirty) {
|
||||||
dirty = false;
|
dirty = false;
|
||||||
@ -96,7 +102,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void realtime() {
|
void handleRealtime() {
|
||||||
time_t tmp;
|
time_t tmp;
|
||||||
time(&tmp);
|
time(&tmp);
|
||||||
realtimeOK = tmp > 1600000000;
|
realtimeOK = tmp > 1600000000;
|
||||||
@ -111,16 +117,22 @@ private:
|
|||||||
} else {
|
} else {
|
||||||
realtimeChanged = false;
|
realtimeChanged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (lastSecond < 0 || lastSecond != now.tm_sec) {
|
||||||
|
lastSecond = (int8_t) now.tm_sec;
|
||||||
|
lastSecondChange_Milliseconds = millis();
|
||||||
|
}
|
||||||
|
realtimeMilliseconds = millis() - lastSecondChange_Milliseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleTimers() {
|
void handleTimers() {
|
||||||
millis_t ms = millis();
|
milliseconds_t ms = millis();
|
||||||
for (Timer *timer = timers; timer < timers + sizeof(timers); timer++) {
|
for (Timer *timer = timers; timer < timers + sizeof(timers); timer++) {
|
||||||
if (timer->interval > 0) {
|
if (timer->interval > 0) {
|
||||||
millis_t dt = ms - timer->last;
|
milliseconds_t milliseconds = ms - timer->last;
|
||||||
if (dt >= timer->interval) {
|
if (milliseconds >= timer->interval) {
|
||||||
timer->last = ms;
|
timer->last = ms;
|
||||||
tick(timer - timers, dt);
|
tick(timer - timers, milliseconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,7 +60,7 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void step(microseconds_t dt) {
|
void step(microseconds_t microseconds) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case INITIAL:
|
case INITIAL:
|
||||||
break;
|
break;
|
||||||
@ -84,13 +84,13 @@ public:
|
|||||||
case INITIAL:
|
case INITIAL:
|
||||||
break;
|
break;
|
||||||
case RISE:
|
case RISE:
|
||||||
position.y = doStep(position.y, 0.0, height, 1000, -dt);
|
position.y = doStep(position.y, 0.0, height, 1000, -microseconds);
|
||||||
break;
|
break;
|
||||||
case EXPLODE:
|
case EXPLODE:
|
||||||
explosion = doStep(explosion, 0.0, explosionRadius, 500, +dt);
|
explosion = doStep(explosion, 0.0, explosionRadius, 500, +microseconds);
|
||||||
break;
|
break;
|
||||||
case SPARKLE:
|
case SPARKLE:
|
||||||
sparkle = doStep(sparkle, 0.0, sparkleMax, 1000, +dt);
|
sparkle = doStep(sparkle, 0.0, sparkleMax, 1000, +microseconds);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,10 +12,6 @@ private:
|
|||||||
|
|
||||||
Firework fireworks[MAX_FIREWORKS];
|
Firework fireworks[MAX_FIREWORKS];
|
||||||
|
|
||||||
int8_t lastSecond = -1;
|
|
||||||
|
|
||||||
millis_t lastSecondMillis = 0;
|
|
||||||
|
|
||||||
uint8_t days = 0;
|
uint8_t days = 0;
|
||||||
|
|
||||||
uint8_t level = 0;
|
uint8_t level = 0;
|
||||||
@ -36,7 +32,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void step(microseconds_t dt) override {
|
void step(microseconds_t microseconds) override {
|
||||||
if (!realtimeOK) {
|
if (!realtimeOK) {
|
||||||
setMode(NO_TIME);
|
setMode(NO_TIME);
|
||||||
} else if (now.tm_mon != 1 || now.tm_mday != 1 || now.tm_hour != 0) {
|
} else if (now.tm_mon != 1 || now.tm_mday != 1 || now.tm_hour != 0) {
|
||||||
@ -49,14 +45,14 @@ protected:
|
|||||||
} else {
|
} else {
|
||||||
setMode(FIREWORK);
|
setMode(FIREWORK);
|
||||||
for (auto &firework: fireworks) {
|
for (auto &firework: fireworks) {
|
||||||
firework.step(dt);
|
firework.step(microseconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loopLastDay() {
|
void loopLastDay() {
|
||||||
setMode(LAST_DAY);
|
setMode(LAST_DAY);
|
||||||
int levelTmp = getLevel();
|
int levelTmp = (int) round(32 * realtimeMilliseconds / 1000.0);;
|
||||||
if (level != levelTmp) {
|
if (level != levelTmp) {
|
||||||
level = levelTmp;
|
level = levelTmp;
|
||||||
markDirty();
|
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();
|
launch();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +100,6 @@ private:
|
|||||||
timer(0, 0);
|
timer(0, 0);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case LAST_DAY:
|
case LAST_DAY:
|
||||||
lastSecond = -1;
|
|
||||||
break;
|
break;
|
||||||
case FIREWORK:
|
case FIREWORK:
|
||||||
timer(0, 500);
|
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() {
|
void launch() {
|
||||||
for (auto &firework: fireworks) {
|
for (auto &firework: fireworks) {
|
||||||
if (firework.launch()) {
|
if (firework.launch()) {
|
||||||
|
|||||||
@ -42,10 +42,10 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void tick(uint8_t index, millis_t dt) override {
|
void tick(uint8_t index, milliseconds_t milliseconds) override {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case SCORE:
|
case SCORE:
|
||||||
timeout -= dt;
|
timeout -= milliseconds;
|
||||||
if (timeout <= 0) {
|
if (timeout <= 0) {
|
||||||
status = PLAY;
|
status = PLAY;
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ protected:
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case OVER:
|
case OVER:
|
||||||
timeout -= dt;
|
timeout -= milliseconds;
|
||||||
if (timeout <= 0) {
|
if (timeout <= 0) {
|
||||||
resetPlayer();
|
resetPlayer();
|
||||||
status = SCORE;
|
status = SCORE;
|
||||||
|
|||||||
@ -70,10 +70,10 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void step(microseconds_t dt) override {
|
void step(microseconds_t microseconds) override {
|
||||||
stepRockets(dt);
|
stepRockets(microseconds);
|
||||||
stepInvaders(dt);
|
stepInvaders(microseconds);
|
||||||
stepHero(dt);
|
stepHero(microseconds);
|
||||||
|
|
||||||
collide();
|
collide();
|
||||||
if (invadersAlive == 0) {
|
if (invadersAlive == 0) {
|
||||||
@ -99,8 +99,8 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void stepRockets(microseconds_t dt) {
|
void stepRockets(microseconds_t microseconds) {
|
||||||
rocketRuntime += dt;
|
rocketRuntime += microseconds;
|
||||||
if (rocketRuntime > 200000) {
|
if (rocketRuntime > 200000) {
|
||||||
rocketRuntime = rocketRuntime % 200000;
|
rocketRuntime = rocketRuntime % 200000;
|
||||||
for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) {
|
for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) {
|
||||||
@ -111,18 +111,18 @@ private:
|
|||||||
rocket->y -= 1;
|
rocket->y -= 1;
|
||||||
}
|
}
|
||||||
} else if (rocket->flash > 0) {
|
} else if (rocket->flash > 0) {
|
||||||
if (rocket->flash < dt) {
|
if (rocket->flash < microseconds) {
|
||||||
rocket->flash = 0;
|
rocket->flash = 0;
|
||||||
} else {
|
} else {
|
||||||
rocket->flash -= dt;
|
rocket->flash -= microseconds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stepInvaders(microseconds_t dt) {
|
void stepInvaders(microseconds_t microseconds) {
|
||||||
swarmRuntime += dt;
|
swarmRuntime += microseconds;
|
||||||
|
|
||||||
if (swarmDown && swarmRuntime > 500000) {
|
if (swarmDown && swarmRuntime > 500000) {
|
||||||
swarmDown = false;
|
swarmDown = false;
|
||||||
@ -148,8 +148,8 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stepHero(microseconds_t dt) {
|
void stepHero(microseconds_t microseconds) {
|
||||||
heroRuntime += dt;
|
heroRuntime += microseconds;
|
||||||
if (heroRuntime >= 50000) {
|
if (heroRuntime >= 50000) {
|
||||||
heroRuntime = heroRuntime % 50000;
|
heroRuntime = heroRuntime % 50000;
|
||||||
if (heroLeft) {
|
if (heroLeft) {
|
||||||
|
|||||||
@ -30,9 +30,9 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void step(microseconds_t dt) override {
|
void step(microseconds_t microseconds) override {
|
||||||
for (auto &star: stars) {
|
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);
|
star = star.plus(velocity);
|
||||||
if (star.x < 0 || star.x >= width || star.y < 0 || star.y >= height) {
|
if (star.x < 0 || star.x >= width || star.y < 0 || star.y >= height) {
|
||||||
star = center.plus(Vector::polar(random(360), 1));
|
star = center.plus(Vector::polar(random(360), 1));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user