st7789_240x135_simpletest.py
1 """ 2 This test will initialize the display using displayio and draw a solid green 3 background, a smaller purple rectangle, and some yellow text. 4 """ 5 import board 6 import terminalio 7 import displayio 8 from adafruit_display_text import label 9 from adafruit_st7789 import ST7789 10 11 # First set some parameters used for shapes and text 12 BORDER = 20 13 FONTSCALE = 2 14 BACKGROUND_COLOR = 0x00FF00 # Bright Green 15 FOREGROUND_COLOR = 0xAA0088 # Purple 16 TEXT_COLOR = 0xFFFF00 17 18 # Release any resources currently in use for the displays 19 displayio.release_displays() 20 21 spi = board.SPI() 22 tft_cs = board.D5 23 tft_dc = board.D6 24 25 display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) 26 display = ST7789( 27 display_bus, rotation=270, width=240, height=135, rowstart=40, colstart=53 28 ) 29 30 # Make the display context 31 splash = displayio.Group(max_size=10) 32 display.show(splash) 33 34 color_bitmap = displayio.Bitmap(display.width, display.height, 1) 35 color_palette = displayio.Palette(1) 36 color_palette[0] = BACKGROUND_COLOR 37 38 bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) 39 splash.append(bg_sprite) 40 41 # Draw a smaller inner rectangle 42 inner_bitmap = displayio.Bitmap( 43 display.width - BORDER * 2, display.height - BORDER * 2, 1 44 ) 45 inner_palette = displayio.Palette(1) 46 inner_palette[0] = FOREGROUND_COLOR 47 inner_sprite = displayio.TileGrid( 48 inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER 49 ) 50 splash.append(inner_sprite) 51 52 # Draw a label 53 text = "Hello World!" 54 text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR) 55 text_width = text_area.bounding_box[2] * FONTSCALE 56 text_group = displayio.Group( 57 max_size=10, 58 scale=FONTSCALE, 59 x=display.width // 2 - text_width // 2, 60 y=display.height // 2, 61 ) 62 text_group.append(text_area) # Subgroup for text scaling 63 splash.append(text_group) 64 65 while True: 66 pass