Skip to content

Instantly share code, notes, and snippets.

@neildavis
Created August 1, 2023 12:25
Show Gist options
  • Save neildavis/b42e2beb065a31e5b7953901853f7f45 to your computer and use it in GitHub Desktop.
Save neildavis/b42e2beb065a31e5b7953901853f7f45 to your computer and use it in GitHub Desktop.
ILI9341 CircuitPython RP2040
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text. All drawing is done
using native displayio modules.
Pinouts are for the 2.4" TFT FeatherWing or Breakout with a Feather M4 or M0.
"""
import board
import terminalio
import displayio
import busio
from adafruit_display_text import label
import adafruit_ili9341
import supervisor
supervisor.runtime.autoreload = False
# Release any resources currently in use for the displays
displayio.release_displays()
# Configure display
tft_dc = board.GP4
tft_reset = board.GP5
tft_cs = board.GP1
tft_blk = board.GP7
tft_width = 320
tft_height = 240
# Configure SPI
spi_clk = board.GP2
spi_mosi = board.GP3
spi_miso = board.GP0 # use is optional
spi_baud = 10000000
spi_polarity = 0
spi_phase = 0
spi = busio.SPI(clock=spi_clk, MOSI=spi_mosi)
#spi = busio.SPI(clock=spi_clk, MOSI=spi_mosi, MISO=spi_miso)
display_bus = displayio.FourWire(
spi, polarity=spi_polarity, phase=spi_phase, baudrate=spi_baud,
command=tft_dc, chip_select=tft_cs, reset=tft_reset
)
display = adafruit_ili9341.ILI9341(bus=display_bus,
width=tft_width, height=tft_height,
backlight_pin=tft_blk)
# Make the display context
splash = displayio.Group()
display.show(splash)
# Draw a green background
color_bitmap = displayio.Bitmap(tft_width, tft_height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x00FF00 # Bright Green
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(tft_width - 40, tft_height - 40, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0xAA0088 # Purple
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=20, y=20)
splash.append(inner_sprite)
# Draw a label
text_group = displayio.Group(scale=3, x=57, y=120)
text = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00)
text_group.append(text_area) # Subgroup for text scaling
splash.append(text_group)
while True:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment