From 039f18c80ddf90ec2a4721c8f855c52664e1b7c0 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 7 Feb 2020 16:12:55 -0800 Subject: [PATCH] don't burn so much cpu spinning in loop() --- src/main.ino | 7 +++++-- src/screen.cpp | 12 +++++++++--- src/screen.h | 6 +++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main.ino b/src/main.ino index 7d9eba31..0550fc23 100644 --- a/src/main.ino +++ b/src/main.ino @@ -371,8 +371,10 @@ void setup() void loop() { + uint32_t msecstosleep = 1000 * 30; // How long can we sleep before we again need to service the main loop? + gps.loop(); - screen_loop(); + msecstosleep = min(screen_loop(), msecstosleep); service.loop(); loopBLE(); @@ -428,5 +430,6 @@ void loop() // No GPS lock yet, let the OS put the main CPU in low power mode for 100ms (or until another interrupt comes in) // i.e. don't just keep spinning in loop as fast as we can. - //delay(100); + DEBUG_MSG("msecs %d\n", msecstosleep); + delay(msecstosleep); } \ No newline at end of file diff --git a/src/screen.cpp b/src/screen.cpp index e55a6e2d..589b060b 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -259,10 +259,10 @@ void screen_setup() #endif } -void screen_loop() +uint32_t screen_loop() { if (!disp) - return; + return 30 * 1000; #ifdef T_BEAM_V10 if (axp192_found && pmu_irq) @@ -295,11 +295,17 @@ void screen_loop() #endif static bool showingBootScreen = true; - ui.update(); + + + + uint32_t msecstosleep = ui.update(); // Once we finish showing the bootscreen, remove it from the loop if (showingBootScreen && ui.getUiState()->currentFrame == 1) { ui.setFrames(nonBootFrames, frameCount - 1); } + + // If we are scrolling do 30fps, otherwise just 1 fps (to save CPU) + return (ui.getUiState()->frameState == IN_TRANSITION ? 10 : 500); } diff --git a/src/screen.h b/src/screen.h index 2f85d472..747ffbed 100644 --- a/src/screen.h +++ b/src/screen.h @@ -1,4 +1,8 @@ #pragma once void screen_print(const char * text); -void screen_loop(), screen_setup(), screen_on(), screen_off(), screen_show_logo(); \ No newline at end of file + +/// @return how many msecs can we sleep before we want service again +uint32_t screen_loop(); + +void screen_setup(), screen_on(), screen_off(), screen_show_logo(); \ No newline at end of file