diff --git a/basic1.cpp b/basic1.cpp index d74bea6..4026387 100644 --- a/basic1.cpp +++ b/basic1.cpp @@ -157,7 +157,8 @@ struct GameConfig { // ============================================================================ // Display dimming settings -#define DIM_TIMEOUT_MS (5 * 60 * 1000) // 5 minutes in milliseconds +#define DIM_TIMEOUT_MS (2 * 60 * 1000) // 2 minutes to dim +#define SLEEP_TIMEOUT_MS (10 * 60 * 1000) // 10 minutes to sleep #define DIM_CHECK_INTERVAL_MS 10000 // Check every 10 seconds #define DIM_BRIGHTNESS 5 // Dimmed brightness level (0-100) #define NORMAL_BRIGHTNESS 100 // Normal brightness level (0-100) @@ -165,6 +166,7 @@ struct GameConfig { // Display dimming state static uint32_t last_interaction_time = 0; // Last time user interacted static bool is_dimmed = false; // Current dimming state +static bool is_sleeping = false; // Current sleep state static uint8_t saved_brightness = NORMAL_BRIGHTNESS; // Brightness before dimming static volatile bool dim_check_flag = false; // Flag set by timer to check dimming static LowLevelDisplay* global_display = nullptr; // Global display pointer for timer callback @@ -183,20 +185,29 @@ static LowLevelDisplay* global_display = nullptr; // Global display pointer for static inline void record_user_interaction(LowLevelDisplay* display) { last_interaction_time = to_ms_since_boot(get_absolute_time()); - // If display was dimmed, restore it based on display type - if (is_dimmed) { + // If display was sleeping, wake it up + if (is_sleeping) { if (display->get_type() == DISPLAY_TYPE_ST7796) { - // TFT: Wake from sleep mode (touch was always active) LowLevelDisplayST7796* tft = static_cast(display); tft->wake(); + tft->set_brightness(saved_brightness); printf("TFT display woken from sleep\n"); } else if (display->get_type() == DISPLAY_TYPE_EPAPER) { - // E-ink: Wake from sleep and re-initialize printf("Waking e-paper display from sleep...\n"); LowLevelDisplayEPaper* epaper = static_cast(display); - epaper->init(); // Re-initialize after sleep + epaper->init(); printf("E-paper display ready\n"); } + is_sleeping = false; + is_dimmed = false; + } + // If display was dimmed, restore brightness + else if (is_dimmed) { + if (display->get_type() == DISPLAY_TYPE_ST7796) { + LowLevelDisplayST7796* tft = static_cast(display); + tft->set_brightness(saved_brightness); + printf("TFT display brightness restored\n"); + } is_dimmed = false; } } @@ -235,23 +246,32 @@ static inline void check_and_apply_dimming(LowLevelDisplay* display) { uint32_t current_time = to_ms_since_boot(get_absolute_time()); uint32_t elapsed = current_time - last_interaction_time; - // Check if timeout has elapsed and display is not yet dimmed - if (!is_dimmed && elapsed >= DIM_TIMEOUT_MS) { + // Check for sleep timeout + if (!is_sleeping && elapsed >= SLEEP_TIMEOUT_MS) { if (display->get_type() == DISPLAY_TYPE_ST7796) { - // TFT: Put to sleep (saves power, touch stays active) - saved_brightness = display->get_brightness(); LowLevelDisplayST7796* tft = static_cast(display); tft->sleep(); - printf("TFT display put to sleep after %d seconds of inactivity\n", - DIM_TIMEOUT_MS / 1000); - printf("Touch controller remains active - touch to wake\n"); + printf("TFT display put to sleep after %d minutes of inactivity\n", + SLEEP_TIMEOUT_MS / 60000); } else if (display->get_type() == DISPLAY_TYPE_EPAPER) { - // E-ink: Put to sleep (turns off display) LowLevelDisplayEPaper* epaper = static_cast(display); epaper->sleep(); - printf("E-paper display put to sleep after %d seconds of inactivity\n", - DIM_TIMEOUT_MS / 1000); + printf("E-paper display put to sleep after %d minutes of inactivity\n", + SLEEP_TIMEOUT_MS / 60000); } + is_sleeping = true; + is_dimmed = true; // Sleep implies dimmed state + } + // Check for dim timeout + else if (!is_dimmed && !is_sleeping && elapsed >= DIM_TIMEOUT_MS) { + if (display->get_type() == DISPLAY_TYPE_ST7796) { + saved_brightness = display->get_brightness(); + LowLevelDisplayST7796* tft = static_cast(display); + tft->set_brightness(DIM_BRIGHTNESS); + printf("TFT display dimmed after %d minutes of inactivity\n", + DIM_TIMEOUT_MS / 60000); + } + // E-paper doesn't support dimming, so we skip it until sleep timeout is_dimmed = true; } } @@ -562,12 +582,11 @@ int main() add_alarm_in_ms(DIM_CHECK_INTERVAL_MS, dim_check_alarm_callback, nullptr, true); if (display->get_type() == DISPLAY_TYPE_ST7796) { - printf("Auto-sleep enabled: TFT will sleep after %d minutes of inactivity\n", - DIM_TIMEOUT_MS / 60000); - printf("Touch controller remains active to wake display\n"); + printf("Power saving enabled: Dim after %d min, Sleep after %d min\n", + DIM_TIMEOUT_MS / 60000, SLEEP_TIMEOUT_MS / 60000); } else if (display->get_type() == DISPLAY_TYPE_EPAPER) { printf("Auto-sleep enabled: E-paper will sleep after %d minutes of inactivity\n", - DIM_TIMEOUT_MS / 60000); + SLEEP_TIMEOUT_MS / 60000); } printf("Dimming check timer set to %d seconds\n", DIM_CHECK_INTERVAL_MS / 1000);