Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions drivers/st7701/st7701.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ void ST7701::start_frame_xfer()
// pwm control
if(lcd_bl != PIN_UNUSED) {
pwm_config cfg = pwm_get_default_config();
pwm_config_set_wrap(&cfg, BACKLIGHT_PWM_TOP);
// PWM frequency for CTRL-Pin on AP3031 must be below 1000 Hz for seamless dimming between 0% and 100%
pwm_config_set_clkdiv_int(&cfg, 20);
pwm_init(pwm_gpio_to_slice_num(lcd_bl), &cfg, true);
gpio_set_function(lcd_bl, GPIO_FUNC_PWM);
set_backlight(0); // Turn backlight off initially to avoid nasty surprises
Expand Down Expand Up @@ -665,11 +666,8 @@ void ST7701::start_frame_xfer()
}

void ST7701::set_backlight(uint8_t brightness) {
// At least on my hardware this gives reasonable control over the possible range of backlight brightness
uint16_t value;
if (brightness == 0) value = 0;
else if (brightness == 255) value = BACKLIGHT_PWM_TOP;
else value = 181 + (brightness * brightness) / 85;
float gamma = 1.8;
uint16_t value = (uint16_t)(pow((float)(brightness) / 255.0f, gamma) * 65535.0f + 0.5f);
pwm_set_gpio_level(lcd_bl, value);
}

Expand Down
1 change: 0 additions & 1 deletion drivers/st7701/st7701.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace pimoroni {
uint lcd_dot_clk = 22;

static const uint32_t SPI_BAUD = 8'000'000;
static const uint32_t BACKLIGHT_PWM_TOP = 6200;

public:
// Parallel init
Expand Down
43 changes: 43 additions & 0 deletions examples/brightness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from presto import Presto
from touch import Button
from time import sleep

presto = Presto()
display = presto.display

FG_COLOR = display.create_pen(0, 0, 255)
BG_COLOR = display.create_pen(255, 255, 255)

touch = presto.touch

button_down = Button(10, 125, 105, 105)
button_up = Button(125, 125, 105, 105)

brightness = 100

while True:
touch.poll()

display.set_pen(BG_COLOR)
display.clear()

display.set_pen(FG_COLOR)
display.text("brightness: " + str(brightness)+ " %", 10, 10)

display.rectangle(*button_down.bounds)
display.rectangle(*button_up.bounds)

if button_down.is_pressed():
brightness -= 1
if brightness < 0: brightness = 0

if button_up.is_pressed():
brightness += 1
if brightness > 100: brightness = 100

presto.set_backlight(brightness / 100)

presto.update()
sleep(0.02)


Loading