Mode::timers now working with microseconds

This commit is contained in:
Patrick Haßel 2023-01-09 10:44:11 +01:00
parent cd97a73ab0
commit f373421a68
3 changed files with 33 additions and 29 deletions

View File

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

View File

@ -65,7 +65,7 @@ protected:
}
}
void tick(uint8_t index, milliseconds_t milliseconds) override {
void tick(uint8_t index, microseconds_t microseconds) override {
launch();
}

View File

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