Refactor power saving logic into display drivers and add ST7789 support
This commit is contained in:
97
basic1.cpp
97
basic1.cpp
@@ -160,56 +160,31 @@ struct GameConfig {
|
||||
#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)
|
||||
|
||||
// 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 bool is_idle_2min_triggered = false; // Flag for 2min trigger
|
||||
static bool is_idle_10min_triggered = false; // Flag for 10min trigger
|
||||
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
|
||||
|
||||
/**
|
||||
* @brief Update last interaction time and restore display if dimmed
|
||||
* @brief Update last interaction time and notify display driver
|
||||
*
|
||||
* Call this whenever the user interacts with the device (touch, button press)
|
||||
* to reset the dimming timer and restore display if it was dimmed/sleeping.
|
||||
*
|
||||
* For TFT displays: Restores brightness level
|
||||
* For e-ink displays: Wakes display from sleep and refreshes screen
|
||||
* Call this whenever the user interacts with the device (touch, button press).
|
||||
* The display driver handles specific wake/restore logic.
|
||||
*
|
||||
* @param display Pointer to display interface
|
||||
*/
|
||||
static inline void record_user_interaction(LowLevelDisplay* display) {
|
||||
last_interaction_time = to_ms_since_boot(get_absolute_time());
|
||||
|
||||
// If display was sleeping, wake it up
|
||||
if (is_sleeping) {
|
||||
if (display->get_type() == DISPLAY_TYPE_ST7796) {
|
||||
LowLevelDisplayST7796* tft = static_cast<LowLevelDisplayST7796*>(display);
|
||||
tft->wake();
|
||||
tft->set_brightness(saved_brightness);
|
||||
printf("TFT display woken from sleep\n");
|
||||
} else if (display->get_type() == DISPLAY_TYPE_EPAPER) {
|
||||
printf("Waking e-paper display from sleep...\n");
|
||||
LowLevelDisplayEPaper* epaper = static_cast<LowLevelDisplayEPaper*>(display);
|
||||
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<LowLevelDisplayST7796*>(display);
|
||||
tft->set_brightness(saved_brightness);
|
||||
printf("TFT display brightness restored\n");
|
||||
}
|
||||
is_dimmed = false;
|
||||
}
|
||||
// Reset idle flags
|
||||
is_idle_2min_triggered = false;
|
||||
is_idle_10min_triggered = false;
|
||||
|
||||
// Notify display driver of interaction
|
||||
display->on_user_interaction();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,12 +208,10 @@ static int64_t dim_check_alarm_callback(alarm_id_t id, void *user_data) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if display should be dimmed and apply dimming if needed
|
||||
* @brief Check if idle thresholds have been met and notify display driver
|
||||
*
|
||||
* Checks if the timeout has elapsed since last interaction and dims/sleeps
|
||||
* the display based on its type:
|
||||
* - TFT displays (ST7796): Reduces brightness to minimum
|
||||
* - E-ink displays: Puts display to sleep mode (turns off)
|
||||
* Checks elapsed time since last interaction and calls the appropriate
|
||||
* display driver methods (on_idle_2min or on_idle_10min).
|
||||
*
|
||||
* @param display Pointer to display interface
|
||||
*/
|
||||
@@ -246,33 +219,16 @@ 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 for sleep timeout
|
||||
if (!is_sleeping && elapsed >= SLEEP_TIMEOUT_MS) {
|
||||
if (display->get_type() == DISPLAY_TYPE_ST7796) {
|
||||
LowLevelDisplayST7796* tft = static_cast<LowLevelDisplayST7796*>(display);
|
||||
tft->sleep();
|
||||
printf("TFT display put to sleep after %d minutes of inactivity\n",
|
||||
SLEEP_TIMEOUT_MS / 60000);
|
||||
} else if (display->get_type() == DISPLAY_TYPE_EPAPER) {
|
||||
LowLevelDisplayEPaper* epaper = static_cast<LowLevelDisplayEPaper*>(display);
|
||||
epaper->sleep();
|
||||
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 10 minute timeout (Sleep)
|
||||
if (!is_idle_10min_triggered && elapsed >= SLEEP_TIMEOUT_MS) {
|
||||
display->on_idle_10min();
|
||||
is_idle_10min_triggered = true;
|
||||
is_idle_2min_triggered = true; // Implicitly triggered
|
||||
}
|
||||
// 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<LowLevelDisplayST7796*>(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;
|
||||
// Check for 2 minute timeout (Dim)
|
||||
else if (!is_idle_2min_triggered && elapsed >= DIM_TIMEOUT_MS) {
|
||||
display->on_idle_2min();
|
||||
is_idle_2min_triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -582,11 +538,10 @@ int main()
|
||||
add_alarm_in_ms(DIM_CHECK_INTERVAL_MS, dim_check_alarm_callback, nullptr, true);
|
||||
|
||||
if (display->get_type() == DISPLAY_TYPE_ST7796) {
|
||||
printf("Power saving enabled: Dim after %d min, Sleep after %d min\n",
|
||||
printf("Power saving: Dim at %d min, Sleep at %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",
|
||||
SLEEP_TIMEOUT_MS / 60000);
|
||||
} else {
|
||||
printf("Power saving: Sleep at %d min\n", SLEEP_TIMEOUT_MS / 60000);
|
||||
}
|
||||
printf("Dimming check timer set to %d seconds\n", DIM_CHECK_INTERVAL_MS / 1000);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user