Refactor power saving logic into display drivers and add ST7789 support

This commit is contained in:
Adolfo Reyna
2026-02-10 22:20:53 -05:00
parent ce1f06ccbf
commit fe5d58b663
11 changed files with 232 additions and 88 deletions

View File

@@ -12,6 +12,7 @@
#include "hardware/gpio.h"
#include "hardware/spi.h"
#include "hardware/pwm.h"
#include "pico/binary_info.h"
#include "pico/stdlib.h"
@@ -60,6 +61,12 @@ static uint16_t height;
static uint16_t x_offset;
static uint16_t y_offset;
// Backlight control
static uint pwm_slice;
static uint pwm_channel;
static uint8_t current_brightness = 100;
static bool pwm_initialized = false;
static inline void cs_select() {
if (config->gpio_cs >= 0) {
asm volatile("nop \n nop \n nop");
@@ -173,10 +180,19 @@ void st7789_init(const struct st7789_config *c, uint16_t w, uint16_t h) {
gpio_init(config->gpio_rst);
gpio_set_dir(config->gpio_rst, GPIO_OUT);
// Initialize backlight pin
gpio_init(config->gpio_bl);
gpio_set_dir(config->gpio_bl, GPIO_OUT);
gpio_put(config->gpio_bl, 1); // Turn on backlight
// Initialize backlight pin with PWM
if (config->gpio_bl >= 0) {
gpio_set_function(config->gpio_bl, GPIO_FUNC_PWM);
pwm_slice = pwm_gpio_to_slice_num(config->gpio_bl);
pwm_channel = pwm_gpio_to_channel(config->gpio_bl);
// Set PWM frequency to ~1kHz
pwm_set_wrap(pwm_slice, 65535);
pwm_set_chan_level(pwm_slice, pwm_channel, 65535); // 100% duty cycle
pwm_set_enabled(pwm_slice, true);
pwm_initialized = true;
}
// Reset display
reset_pulse();
@@ -403,3 +419,48 @@ void st7789_draw_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16
}
}
}
void st7789_set_brightness(uint8_t brightness) {
if (!pwm_initialized || config->gpio_bl < 0) return;
// Clamp brightness
if (brightness > 100) brightness = 100;
current_brightness = brightness;
// Convert 0-100 to 0-65535
uint16_t level = (uint16_t)((brightness * 65535) / 100);
pwm_set_chan_level(pwm_slice, pwm_channel, level);
}
uint8_t st7789_get_brightness(void) {
return current_brightness;
}
void st7789_sleep(void) {
// Turn off backlight
if (pwm_initialized && config->gpio_bl >= 0) {
pwm_set_chan_level(pwm_slice, pwm_channel, 0);
}
// Display off
write_command(ST7789_DISPOFF);
sleep_ms(10);
// Sleep in
write_command(ST7789_SLPIN);
sleep_ms(120);
}
void st7789_wake(void) {
// Sleep out
write_command(ST7789_SLPOUT);
sleep_ms(120);
// Display on
write_command(ST7789_DISPON);
sleep_ms(10);
// Restore brightness
st7789_set_brightness(current_brightness);
}

View File

@@ -38,6 +38,11 @@ void st7789_draw_circle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
void st7789_fill_circle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
void st7789_draw_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color);
void st7789_sleep(void);
void st7789_wake(void);
void st7789_set_brightness(uint8_t brightness);
uint8_t st7789_get_brightness(void);
#ifdef __cplusplus
}
#endif