from machine import Pin, SPI
import time
import st7789py as st7789
import random
import vga2_16x32 as font

#--------------------------------------------------
tft = st7789.ST7789(
    SPI(2, baudrate=40000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19)),
    240,
    320,
    reset=Pin(2, Pin.OUT),
    cs=Pin(15, Pin.OUT),
    dc=Pin(4, Pin.OUT),
    backlight=None,
    rotation=0,
    color_order=st7789.BGR
    )

#--------------------------------------------------
def main():
    while True:
        for rotation in range(4):
            tft.rotation(rotation)
            tft.fill(0)
            col_max = tft.width - font.WIDTH * 5
            row_max = tft.height - font.HEIGHT
            if col_max < 0 or row_max < 0:
                raise RuntimeError(
                    "This font is too big to display on this screen."
                )

            for _ in range(10):
                tft.text(
                    font,
                    "Cardputer",
                    random.randint(0, col_max),
                    random.randint(0, row_max),
                    st7789.color565(
                        random.getrandbits(8),
                        random.getrandbits(8),
                        random.getrandbits(8),
                    ),
                    st7789.color565(
                        random.getrandbits(8),
                        random.getrandbits(8),
                        random.getrandbits(8),
                    ),
                )


main()