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