Mode::timers now working with microseconds
This commit is contained in:
parent
cd97a73ab0
commit
f373421a68
@ -23,8 +23,8 @@ class Mode {
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
struct Timer {
|
struct Timer {
|
||||||
milliseconds_t interval;
|
microseconds_t interval;
|
||||||
milliseconds_t last;
|
microseconds_t rest;
|
||||||
};
|
};
|
||||||
|
|
||||||
Display &_display;
|
Display &_display;
|
||||||
@ -52,22 +52,22 @@ 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;
|
milliseconds_t realtimeMilliseconds = 0;
|
||||||
|
|
||||||
time_t epoch = 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 step(microseconds_t microseconds) {};
|
||||||
|
|
||||||
virtual void draw(Display &display) {};
|
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)) {
|
if (index >= countof(timers)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
timers[index].interval = interval;
|
timers[index].interval = milliseconds * 1000;
|
||||||
timers[index].last = millis();
|
timers[index].rest = milliseconds * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
void markDirty() {
|
void markDirty() {
|
||||||
@ -87,13 +87,10 @@ public:
|
|||||||
|
|
||||||
virtual const char *getName() = 0;
|
virtual const char *getName() = 0;
|
||||||
|
|
||||||
void loop(microseconds_t micros) {
|
void loop(microseconds_t microseconds) {
|
||||||
handleRealtime();
|
handleRealtime();
|
||||||
|
handleTimers(microseconds);
|
||||||
handleTimers();
|
step(microseconds);
|
||||||
|
|
||||||
step(micros);
|
|
||||||
|
|
||||||
if (dirty) {
|
if (dirty) {
|
||||||
dirty = false;
|
dirty = false;
|
||||||
draw(_display);
|
draw(_display);
|
||||||
@ -103,6 +100,11 @@ public:
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
void handleRealtime() {
|
void handleRealtime() {
|
||||||
|
realtimeUpdate();
|
||||||
|
realtimeMillisecondsUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void realtimeUpdate() {
|
||||||
time_t tmp;
|
time_t tmp;
|
||||||
time(&tmp);
|
time(&tmp);
|
||||||
realtimeOK = tmp > 1600000000;
|
realtimeOK = tmp > 1600000000;
|
||||||
@ -117,7 +119,9 @@ private:
|
|||||||
} else {
|
} else {
|
||||||
realtimeChanged = false;
|
realtimeChanged = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void realtimeMillisecondsUpdate() {
|
||||||
if (lastSecond < 0 || lastSecond != now.tm_sec) {
|
if (lastSecond < 0 || lastSecond != now.tm_sec) {
|
||||||
lastSecond = (int8_t) now.tm_sec;
|
lastSecond = (int8_t) now.tm_sec;
|
||||||
lastSecondChange_Milliseconds = millis();
|
lastSecondChange_Milliseconds = millis();
|
||||||
@ -125,14 +129,14 @@ private:
|
|||||||
realtimeMilliseconds = millis() - lastSecondChange_Milliseconds;
|
realtimeMilliseconds = millis() - lastSecondChange_Milliseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleTimers() {
|
void handleTimers(microseconds_t microseconds) {
|
||||||
milliseconds_t ms = millis();
|
|
||||||
for (Timer *timer = timers; timer < timers + countof(timers); timer++) {
|
for (Timer *timer = timers; timer < timers + countof(timers); timer++) {
|
||||||
if (timer->interval > 0) {
|
if (timer->interval > 0) {
|
||||||
milliseconds_t milliseconds = ms - timer->last;
|
if (microseconds >= timer->rest) {
|
||||||
if (milliseconds >= timer->interval) {
|
timer->rest = timer->interval;
|
||||||
timer->last = ms;
|
tick(timer - timers, timer->interval);
|
||||||
tick(timer - timers, milliseconds);
|
} else {
|
||||||
|
timer->rest -= microseconds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,7 +65,7 @@ protected:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tick(uint8_t index, milliseconds_t milliseconds) override {
|
void tick(uint8_t index, microseconds_t microseconds) override {
|
||||||
launch();
|
launch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,7 @@ private:
|
|||||||
|
|
||||||
Status status = PLAY;
|
Status status = PLAY;
|
||||||
|
|
||||||
microseconds_t timeout = 0;
|
microseconds_t timeoutMicroseconds = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -42,11 +42,11 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void tick(uint8_t index, milliseconds_t milliseconds) override {
|
void tick(uint8_t index, microseconds_t microseconds) override {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case SCORE:
|
case SCORE:
|
||||||
timeout -= milliseconds;
|
timeoutMicroseconds -= microseconds;
|
||||||
if (timeout <= 0) {
|
if (timeoutMicroseconds <= 0) {
|
||||||
status = PLAY;
|
status = PLAY;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -61,11 +61,11 @@ protected:
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case OVER:
|
case OVER:
|
||||||
timeout -= milliseconds;
|
timeoutMicroseconds -= microseconds;
|
||||||
if (timeout <= 0) {
|
if (timeoutMicroseconds <= 0) {
|
||||||
resetPlayer();
|
resetPlayer();
|
||||||
status = PLAY;
|
status = PLAY;
|
||||||
timeout = 0;
|
timeoutMicroseconds = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -124,7 +124,7 @@ private:
|
|||||||
}
|
}
|
||||||
if (player0.score >= 10 || player1.score >= 10) {
|
if (player0.score >= 10 || player1.score >= 10) {
|
||||||
status = OVER;
|
status = OVER;
|
||||||
timeout = 2000;
|
timeoutMicroseconds = 2000 * 1000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +144,7 @@ private:
|
|||||||
velocity.x = direction;
|
velocity.x = direction;
|
||||||
velocity.y = 0;
|
velocity.y = 0;
|
||||||
status = SCORE;
|
status = SCORE;
|
||||||
timeout = 2000;
|
timeoutMicroseconds = 2000 * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user