diff --git a/drivers/st7701/st7701.cpp b/drivers/st7701/st7701.cpp index fc14207..56acfab 100644 --- a/drivers/st7701/st7701.cpp +++ b/drivers/st7701/st7701.cpp @@ -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); @@ -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 } } @@ -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); } diff --git a/drivers/st7701/st7701.hpp b/drivers/st7701/st7701.hpp index 9295147..9ccd2d9 100644 --- a/drivers/st7701/st7701.hpp +++ b/drivers/st7701/st7701.hpp @@ -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 diff --git a/modules/c/presto/presto.c b/modules/c/presto/presto.c index a4bab70..7a8511d 100644 --- a/modules/c/presto/presto.c +++ b/modules/c/presto/presto.c @@ -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) }, diff --git a/modules/c/presto/presto.cpp b/modules/c/presto/presto.cpp index 93cd1a9..7d0befa 100644 --- a/modules/c/presto/presto.cpp +++ b/modules/c/presto/presto.cpp @@ -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); diff --git a/modules/c/presto/presto.h b/modules/c/presto/presto.h index b153651..4246fc0 100644 --- a/modules/c/presto/presto.h +++ b/modules/c/presto/presto.h @@ -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); \ No newline at end of file