clean up frame management code (-6 bytes)

- saves 6 bytes compiling Mystic Balloon on Arduino 1.8.1
This commit is contained in:
Josh Goebel 2017-01-19 03:04:11 -05:00 committed by Scott Allen
parent 449532f1e9
commit 6050dda391
2 changed files with 27 additions and 15 deletions

View File

@ -20,9 +20,9 @@ Arduboy2Base::Arduboy2Base()
previousButtonState = 0; previousButtonState = 0;
// frame management // frame management
setFrameRate(60); setFrameRate(60);
frameCount = 0; frameCount = -1;
nextFrameStart = 0; nextFrameStart = 0;
post_render = false; justRendered = false;
// init not necessary, will be reset after first use // init not necessary, will be reset after first use
// lastFrameStart // lastFrameStart
// lastFrameDurationMs // lastFrameDurationMs
@ -193,28 +193,40 @@ bool Arduboy2Base::everyXFrames(uint8_t frames)
bool Arduboy2Base::nextFrame() bool Arduboy2Base::nextFrame()
{ {
unsigned long now = millis(); unsigned long now = millis();
bool tooSoonForNextFrame = now < nextFrameStart;
// post render if (justRendered) {
if (post_render) {
lastFrameDurationMs = now - lastFrameStart; lastFrameDurationMs = now - lastFrameStart;
frameCount++; justRendered = false;
post_render = false; return false;
} }
else if (tooSoonForNextFrame) {
// if we have MORE than 1ms to spare (hence our comparison with 2),
// lets sleep for power savings. We don't compare against 1 to avoid
// potential rounding errors - say we're actually 0.5 ms away, but a 1
// is returned if we go to sleep we might sleep a full 1ms and then
// we'd be running the frame slighly late. So the last 1ms we stay
// awake for perfect timing.
// if it's not time for the next frame yet // This is likely trading power savings for absolute timing precision
if (now < nextFrameStart) { // and the power savings might be the better goal. At 60 FPS trusting
// if we have more than 1ms to spare, lets sleep // chance here might actually achieve a "truer" 60 FPS than the 16ms
// we should be woken up by timer0 every 1ms, so this should be ok // frame duration we get due to integer math.
if ((uint8_t)(nextFrameStart - now) > 1)
// We should be woken up by timer0 every 1ms, so it's ok to sleep.
if ((uint8_t)(nextFrameStart - now) >= 2)
idle(); idle();
return false; return false;
} }
// pre-render // pre-render
nextFrameStart = now + eachFrameMillis; justRendered = true;
lastFrameStart = now; lastFrameStart = now;
post_render = true; nextFrameStart = now + eachFrameMillis;
return post_render; frameCount++;
return true;
} }
bool Arduboy2Base::nextFrameDEV() bool Arduboy2Base::nextFrameDEV()

View File

@ -1122,7 +1122,7 @@ class Arduboy2Base : public Arduboy2Core
uint8_t eachFrameMillis; uint8_t eachFrameMillis;
unsigned long lastFrameStart; unsigned long lastFrameStart;
unsigned long nextFrameStart; unsigned long nextFrameStart;
bool post_render; bool justRendered;
uint8_t lastFrameDurationMs; uint8_t lastFrameDurationMs;
}; };