Require some inactivity before sleeping in tracker

This commit is contained in:
Correl Roush 2023-01-31 16:48:26 -05:00
parent 2f081a299e
commit 52d13bb40b

View file

@ -390,6 +390,7 @@ public:
// Sidebar // Sidebar
time_t current_time = time(nullptr); time_t current_time = time(nullptr);
last_interaction_time = current_time;
next_clock_update = next_minute(current_time); next_clock_update = next_minute(current_time);
drawSidebar(); drawSidebar();
drawClock(current_time); drawClock(current_time);
@ -424,7 +425,10 @@ public:
App::Choices loop () { App::Choices loop () {
auto t = M5.Touch.getDetail(); auto t = M5.Touch.getDetail();
const time_t current_time = time(nullptr);
if (t.wasPressed()) { if (t.wasPressed()) {
last_interaction_time = current_time;
switch (mode) { switch (mode) {
case Playing: case Playing:
if (sidebarRect.contains(t.x, t.y)) { if (sidebarRect.contains(t.x, t.y)) {
@ -452,17 +456,18 @@ public:
break; break;
} }
} }
const time_t current_time = time(nullptr);
if (current_time >= next_clock_update) { if (current_time >= next_clock_update) {
ESP_LOGD("LifeTracker", "Updating clock and battery display"); ESP_LOGD("LifeTracker", "Updating clock and battery display");
next_clock_update = next_minute(current_time); next_clock_update = next_minute(current_time);
drawClock(current_time); drawClock(current_time);
drawBattery(); drawBattery();
} else { } else {
if (current_time - last_interaction_time > INACTIVITY_THRESHOLD) {
int seconds = next_clock_update - current_time; int seconds = next_clock_update - current_time;
ESP_LOGD("LifeTracker", "Sleep for %d seconds", seconds); ESP_LOGD("LifeTracker", "Sleep for %d seconds", seconds);
M5.Power.lightSleep(seconds * 1000000, true); // Delay in microseconds M5.Power.lightSleep(seconds * 1000000, true); // Delay in microseconds
} }
}
return App::LifeTracker; return App::LifeTracker;
} }
void shutdown() { void shutdown() {
@ -490,7 +495,9 @@ protected:
Player players[4]; Player players[4];
LGFX_Button button_ok; LGFX_Button button_ok;
LGFX_Button button_cancel; LGFX_Button button_cancel;
time_t last_interaction_time;
time_t next_clock_update; time_t next_clock_update;
const int INACTIVITY_THRESHOLD = 10; // Seconds of inactivity before app will enter sleep
void drawPlayer(int player) { void drawPlayer(int player) {
if (player < 0 || player >= 4) return; if (player < 0 || player >= 4) return;