Refactor power saving logic into display drivers and add ST7789 support
This commit is contained in:
@@ -40,6 +40,11 @@ public:
|
||||
// Optional: Orientation control (not commonly needed for bitmap displays)
|
||||
virtual void set_rotation(uint8_t rotation) { (void)rotation; }
|
||||
|
||||
// Power saving hooks
|
||||
virtual void on_idle_2min() {}
|
||||
virtual void on_idle_10min() {}
|
||||
virtual void on_user_interaction() {}
|
||||
|
||||
// Factory method - creates display based on type, using board_config.h for pins
|
||||
static LowLevelDisplay* create(DisplayType type, int width, int height);
|
||||
};
|
||||
|
||||
@@ -141,3 +141,24 @@ void LowLevelDisplayEPaper::sleep() {
|
||||
printf("Putting e-paper display to sleep...\n");
|
||||
EPD_4IN2_V2_Sleep();
|
||||
}
|
||||
|
||||
void LowLevelDisplayEPaper::on_idle_2min() {
|
||||
// E-paper doesn't dim
|
||||
}
|
||||
|
||||
void LowLevelDisplayEPaper::on_idle_10min() {
|
||||
if (!is_sleeping) {
|
||||
sleep();
|
||||
is_sleeping = true;
|
||||
printf("E-Paper: Entered sleep mode\n");
|
||||
}
|
||||
}
|
||||
|
||||
void LowLevelDisplayEPaper::on_user_interaction() {
|
||||
if (is_sleeping) {
|
||||
printf("E-Paper: Waking from sleep...\n");
|
||||
init(); // Re-initialize to wake up
|
||||
is_sleeping = false;
|
||||
printf("E-Paper: Ready\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,14 @@ public:
|
||||
void clear_display(); // Full clear with refresh
|
||||
void full_refresh(); // Force full screen refresh (slower but removes ghosting)
|
||||
void sleep(); // Put display in low power mode
|
||||
|
||||
// Power saving hooks
|
||||
void on_idle_2min() override;
|
||||
void on_idle_10min() override;
|
||||
void on_user_interaction() override;
|
||||
|
||||
private:
|
||||
bool is_sleeping = false;
|
||||
};
|
||||
|
||||
#endif // LOW_LEVEL_DISPLAY_EPAPER_H
|
||||
|
||||
@@ -69,11 +69,51 @@ void LowLevelDisplayST7789::refresh() {
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7789::set_backlight(bool on) {
|
||||
// TODO: Implement
|
||||
(void)on;
|
||||
set_brightness(on ? 100 : 0);
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7789::set_brightness(uint8_t brightness) {
|
||||
st7789_set_brightness(brightness);
|
||||
}
|
||||
|
||||
uint8_t LowLevelDisplayST7789::get_brightness() const {
|
||||
return st7789_get_brightness();
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7789::set_rotation(uint8_t rotation) {
|
||||
// TODO: Implement
|
||||
(void)rotation;
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7789::on_idle_2min() {
|
||||
if (!is_dimmed && !is_sleeping) {
|
||||
saved_brightness = get_brightness();
|
||||
set_brightness(5); // Dim to 5%
|
||||
is_dimmed = true;
|
||||
printf("ST7789: Dimmed to 5%%\n");
|
||||
}
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7789::on_idle_10min() {
|
||||
if (!is_sleeping) {
|
||||
st7789_sleep();
|
||||
is_sleeping = true;
|
||||
is_dimmed = true; // Sleep implies dimmed
|
||||
printf("ST7789: Entered sleep mode\n");
|
||||
}
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7789::on_user_interaction() {
|
||||
if (is_sleeping) {
|
||||
st7789_wake();
|
||||
// Restore brightness if we have a saved value, or default to 100
|
||||
set_brightness(saved_brightness > 0 ? saved_brightness : 100);
|
||||
is_sleeping = false;
|
||||
is_dimmed = false;
|
||||
printf("ST7789: Woke from sleep\n");
|
||||
} else if (is_dimmed) {
|
||||
set_brightness(saved_brightness > 0 ? saved_brightness : 100);
|
||||
is_dimmed = false;
|
||||
printf("ST7789: Restored brightness\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,7 @@
|
||||
#define LOW_LEVEL_DISPLAY_ST7789_H
|
||||
|
||||
#include "low_level_display.h"
|
||||
|
||||
// ST7789 configuration structure (similar to ST7796)
|
||||
struct st7789_config {
|
||||
void* spi; // SPI instance
|
||||
int gpio_din; // MOSI pin
|
||||
int gpio_clk; // Clock pin
|
||||
int gpio_cs; // Chip select pin
|
||||
int gpio_dc; // Data/Command pin
|
||||
int gpio_rst; // Reset pin
|
||||
int gpio_bl; // Backlight pin
|
||||
};
|
||||
#include "st7789.h"
|
||||
|
||||
class LowLevelDisplayST7789 : public LowLevelDisplay {
|
||||
private:
|
||||
@@ -42,8 +32,22 @@ public:
|
||||
// Backlight control
|
||||
void set_backlight(bool on) override;
|
||||
|
||||
// Brightness control
|
||||
void set_brightness(uint8_t brightness) override;
|
||||
uint8_t get_brightness() const override;
|
||||
|
||||
// Orientation control
|
||||
void set_rotation(uint8_t rotation) override;
|
||||
|
||||
// Power saving hooks
|
||||
void on_idle_2min() override;
|
||||
void on_idle_10min() override;
|
||||
void on_user_interaction() override;
|
||||
|
||||
private:
|
||||
uint8_t saved_brightness = 100;
|
||||
bool is_dimmed = false;
|
||||
bool is_sleeping = false;
|
||||
};
|
||||
|
||||
#endif // LOW_LEVEL_DISPLAY_ST7789_H
|
||||
|
||||
@@ -94,3 +94,36 @@ void LowLevelDisplayST7796::set_rotation(uint8_t rotation) {
|
||||
// TODO: Add MADCTL register manipulation for rotation
|
||||
(void)rotation;
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7796::on_idle_2min() {
|
||||
if (!is_dimmed && !is_sleeping) {
|
||||
saved_brightness = get_brightness();
|
||||
set_brightness(5); // Dim to 5%
|
||||
is_dimmed = true;
|
||||
printf("TFT: Dimmed to 5%%\n");
|
||||
}
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7796::on_idle_10min() {
|
||||
if (!is_sleeping) {
|
||||
sleep();
|
||||
is_sleeping = true;
|
||||
is_dimmed = true; // Sleep implies dimmed
|
||||
printf("TFT: Entered sleep mode\n");
|
||||
}
|
||||
}
|
||||
|
||||
void LowLevelDisplayST7796::on_user_interaction() {
|
||||
if (is_sleeping) {
|
||||
wake();
|
||||
// Restore brightness if we have a saved value, or default to 100
|
||||
set_brightness(saved_brightness > 0 ? saved_brightness : 100);
|
||||
is_sleeping = false;
|
||||
is_dimmed = false;
|
||||
printf("TFT: Woke from sleep\n");
|
||||
} else if (is_dimmed) {
|
||||
set_brightness(saved_brightness > 0 ? saved_brightness : 100);
|
||||
is_dimmed = false;
|
||||
printf("TFT: Restored brightness\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,16 @@ public:
|
||||
// Color inversion control
|
||||
void set_invert_color(bool inv) { invert_color = inv; }
|
||||
bool get_invert_color() const { return invert_color; }
|
||||
|
||||
// Power saving hooks
|
||||
void on_idle_2min() override;
|
||||
void on_idle_10min() override;
|
||||
void on_user_interaction() override;
|
||||
|
||||
private:
|
||||
uint8_t saved_brightness = 100;
|
||||
bool is_dimmed = false;
|
||||
bool is_sleeping = false;
|
||||
};
|
||||
|
||||
#endif // LOW_LEVEL_DISPLAY_ST7796_H
|
||||
|
||||
Reference in New Issue
Block a user