import lvgl as lv
import lcd_bus

import ili9341
from machine import SPI

spi_bus = SPI.Bus(host=1, mosi=14, miso=41, sck=13)

display_bus = lcd_bus.SPIBus(
    spi_bus=spi_bus,
    dc=4,
    cs=6,
    freq=40000000,
)

display = ili9341.ILI9341(
    data_bus=display_bus,
    display_width=240,
    display_height=320,
    reset_pin=5,
    # comment the next line if you still don't get something on the display
    reset_state=ili9341.STATE_LOW,
    backlight_pin=40,  # NOT USED...YET
    color_space=lv.COLOR_FORMAT.RGB565,
    color_byte_order=ili9341.BYTE_ORDER_BGR,
    rgb565_byte_swap=True,
)


def main():
    display.set_power(True)
    display.init()
    display.set_rotation(lv.DISPLAY_ROTATION._90)

    # setting this to anything but 100 or 0 when you don't have the
    # backlight state set to PWM isn't going to do anything
    display.set_backlight(100)

    display.invert_colors()

    scrn = lv.screen_active()
    scrn.set_style_bg_color(lv.color_hex(0xFFB6C1), 0)

    slider = lv.slider(scrn)
    slider.set_size(300, 50)
    slider.center()

    label = lv.label(scrn)
    label.set_text("HELLO WORLD!")
    label.align(lv.ALIGN.CENTER, 0, -50)

    import task_handler

    th = task_handler.TaskHandler()


main()
