lvgl-micropython / lvgl_micropython

LVGL module for MicroPython
MIT License
86 stars 27 forks source link

ST7735 Backlight #134

Closed dcmcshan closed 2 weeks ago

dcmcshan commented 1 month ago

Does the display driver support backlight pwm? my LCD backlight is full brightness at 0, and dark at 1023. I'd like to be able to dim it. I can do this directly on the pin, but I'd like to do it through the driver, if it supports it.

        self.display = ST7735.ST7735R_Red(
            data_bus=self.display_bus,
            display_width=80,
            display_height=160,
            # we are going to let the driver handle the allocation of the frame buffers
            frame_buffer1=self.fb1,
            frame_buffer2=self.fb2,
            reset_pin=PIN_LCD_RESET,
            reset_state=ST7735.STATE_LOW,
            power_on_state=ST7735.STATE_HIGH,
            backlight_pin=PIN_LCD_BACKLIGHT,
            backlight_on_state=ST7735.STATE_LOW,
            offset_x=26,
            offset_y=1,
            color_space=lv.COLOR_FORMAT.RGB565,
            rgb565_byte_swap=True,
            color_byte_order=ST7735.BYTE_ORDER_BGR
        )
kdschlosser commented 1 month ago

Yes the display driver supports using PWM. It does so using a 0.0 to 100.0 scale which gets mapped to the 0 to 65535 range. I never considered the PWM working in reverse where 0 is full on and the higher the number the dimmer it gets. That's rather backwards.

You do have the ability to override the display drivers handling by using something along the lines of...

import st7735

class ST7735R_Red(st7735.ST7735R_Red):

    def set_backlight(self, value):
        self._backlight_pin.duty_u16(int(((value * -1023.0) / 100.0) + 1023.0))  # NOQA

display = ST7735R_Red(
        data_bus=self.display_bus,
        display_width=80,
        display_height=160,
        reset_pin=PIN_LCD_RESET,
        reset_state=st7735.STATE_LOW,
        power_on_state=st7735.STATE_HIGH,
        backlight_pin=PIN_LCD_BACKLIGHT,
        backlight_on_state=st7735.STATE_PWM,
        offset_x=26,
        offset_y=1,
        color_space=lv.COLOR_FORMAT.RGB565,
        rgb565_byte_swap=True,
        color_byte_order=st7735.BYTE_ORDER_BGR
)

and if you are using the new style display drivers...

import st7735

class ST7735(st7735.ST7735):

    def set_backlight(self, value):
        self._backlight_pin.duty_u16(int(((value * -1023.0) / 100.0) + 1023.0))  # NOQA

display = ST7735(
        data_bus=self.display_bus,
        display_width=80,
        display_height=160,
        reset_pin=PIN_LCD_RESET,
        reset_state=st7735.STATE_LOW,
        power_on_state=st7735.STATE_HIGH,
        backlight_pin=PIN_LCD_BACKLIGHT,
        backlight_on_state=st7735.STATE_PWM,
        offset_x=26,
        offset_y=1,
        color_space=lv.COLOR_FORMAT.RGB565,
        rgb565_byte_swap=True,
        color_byte_order=st7735.BYTE_ORDER_BGR
)

display.init(st7735.TYPE_R_RED)

That will make the backlight work properly in your use case.