Skip to content

Commit

Permalink
Backlight control.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBell committed Aug 16, 2024
1 parent 2d9a93d commit e055b2c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
21 changes: 6 additions & 15 deletions drivers/st7701/st7701.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,11 @@ void __no_inline_not_in_flash_func(ST7701::fill_next_line()) {
// if a backlight pin is provided then set it up for
// pwm control
if(lcd_bl != PIN_UNUSED) {
#if 0
pwm_config cfg = pwm_get_default_config();
pwm_set_wrap(pwm_gpio_to_slice_num(lcd_bl), 65535);
pwm_config_set_wrap(&cfg, BACKLIGHT_PWM_TOP);
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
#else
gpio_init(lcd_bl);
gpio_set_dir(lcd_bl, GPIO_OUT);
gpio_put(lcd_bl, 0);
#endif
}

command(reg::SWRESET);
Expand Down Expand Up @@ -462,11 +456,7 @@ void __no_inline_not_in_flash_func(ST7701::fill_next_line()) {
if(lcd_bl != PIN_UNUSED) {
//update(); // Send the new buffer to the display to clear any previous content
sleep_ms(50); // Wait for the update to apply
#if 0
set_backlight(255); // Turn backlight on now surprises have passed
#else
gpio_put(lcd_bl, 1);
#endif
}
}

Expand Down Expand Up @@ -580,10 +570,11 @@ void __no_inline_not_in_flash_func(ST7701::fill_next_line()) {
}

void ST7701::set_backlight(uint8_t brightness) {
// gamma correct the provided 0-255 brightness value onto a
// 0-65535 range for the pwm counter
float gamma = 2.8;
uint16_t value = (uint16_t)(pow((float)(brightness) / 255.0f, gamma) * 65535.0f + 0.5f);
// 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;
pwm_set_gpio_level(lcd_bl, value);
}

Expand Down
1 change: 1 addition & 0 deletions drivers/st7701/st7701.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ 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
2 changes: 2 additions & 0 deletions modules/c/presto/presto.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

MP_DEFINE_CONST_FUN_OBJ_1(Presto___del___obj, Presto___del__);
MP_DEFINE_CONST_FUN_OBJ_2(Presto_update_obj, Presto_update);
MP_DEFINE_CONST_FUN_OBJ_2(Presto_set_backlight_obj, Presto_set_backlight);

/***** Binding of Methods *****/

static const mp_rom_map_elem_t Presto_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&Presto___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&Presto_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_backlight), MP_ROM_PTR(&Presto_set_backlight_obj) },

{ MP_ROM_QSTR(MP_QSTR_WIDTH), MP_ROM_INT(WIDTH) },
{ MP_ROM_QSTR(MP_QSTR_HEIGHT), MP_ROM_INT(HEIGHT) },
Expand Down
12 changes: 12 additions & 0 deletions modules/c/presto/presto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ extern mp_obj_t Presto_update(mp_obj_t self_in, mp_obj_t graphics_in) {
return mp_const_none;
}

mp_obj_t Presto_set_backlight(mp_obj_t self_in, mp_obj_t brightness) {
_Presto_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Presto_obj_t);

float b = mp_obj_get_float(brightness);

if(b < 0 || b > 1.0f) mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0");

self->presto->set_backlight((uint8_t)(b * 255.0f));

return mp_const_none;
}

mp_obj_t Presto___del__(mp_obj_t self_in) {
(void)self_in;
//_Presto_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Presto_obj_t);
Expand Down
1 change: 1 addition & 0 deletions modules/c/presto/presto.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ extern const mp_obj_type_t Presto_type;
extern mp_obj_t Presto_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
extern mp_obj_t Presto_update(mp_obj_t self_in, mp_obj_t graphics_in);
extern mp_int_t Presto_get_framebuffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
extern mp_obj_t Presto_set_backlight(mp_obj_t self_in, mp_obj_t brightness);
extern mp_obj_t Presto___del__(mp_obj_t self_in);

0 comments on commit e055b2c

Please sign in to comment.