add displayio examples
This commit is contained in:
parent
2b2bd39476
commit
8e245d6373
14 changed files with 4595 additions and 0 deletions
4194
CircuitPython_displayio/Helvetica-Bold-16.bdf
Normal file
4194
CircuitPython_displayio/Helvetica-Bold-16.bdf
Normal file
File diff suppressed because it is too large
Load diff
BIN
CircuitPython_displayio/castle_sprite_sheet.bmp
Normal file
BIN
CircuitPython_displayio/castle_sprite_sheet.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.2 KiB |
BIN
CircuitPython_displayio/cp_sprite_sheet.bmp
Normal file
BIN
CircuitPython_displayio/cp_sprite_sheet.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
40
CircuitPython_displayio/displayio_display_driver.py
Normal file
40
CircuitPython_displayio/displayio_display_driver.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import board
|
||||
import displayio
|
||||
import adafruit_ili9341
|
||||
|
||||
# Release any previously configured displays
|
||||
displayio.release_displays()
|
||||
|
||||
# Setup SPI bus
|
||||
spi_bus = board.SPI()
|
||||
|
||||
# Digital pins to use
|
||||
tft_cs = board.D10
|
||||
tft_dc = board.D9
|
||||
|
||||
# Setup the display bus
|
||||
display_bus = displayio.FourWire(spi_bus, command=tft_dc, chip_select=tft_cs)
|
||||
|
||||
# Setup the Display
|
||||
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)
|
||||
|
||||
#
|
||||
# DONE - now you can use the display however you want
|
||||
#
|
||||
|
||||
bitmap = displayio.Bitmap(320, 240, 2)
|
||||
|
||||
palette = displayio.Palette(2)
|
||||
palette[0] = 0
|
||||
palette[1] = 0xFFFFFF
|
||||
|
||||
for x in range(10, 20):
|
||||
for y in range(10, 20):
|
||||
bitmap[x, y] = 1
|
||||
|
||||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
|
||||
|
||||
group = displayio.Group()
|
||||
group.append(tile_grid)
|
||||
display.show(group)
|
||||
display.refresh_soon()
|
||||
67
CircuitPython_displayio/displayio_display_manual.py
Normal file
67
CircuitPython_displayio/displayio_display_manual.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import board
|
||||
import displayio
|
||||
|
||||
# Release any previously configured displays
|
||||
displayio.release_displays()
|
||||
|
||||
# Setup SPI bus
|
||||
spi_bus = board.SPI()
|
||||
|
||||
# Digital pins to use
|
||||
tft_cs = board.D10
|
||||
tft_dc = board.D9
|
||||
|
||||
# Setup the display bus
|
||||
display_bus = displayio.FourWire(spi_bus, command=tft_dc, chip_select=tft_cs)
|
||||
|
||||
# Setup the initialization sequence
|
||||
# stolen from adafruit_ili9341.py
|
||||
INIT_SEQUENCE = (
|
||||
b"\x01\x80\x80" # Software reset then delay 0x80 (128ms)
|
||||
b"\xEF\x03\x03\x80\x02"
|
||||
b"\xCF\x03\x00\xC1\x30"
|
||||
b"\xED\x04\x64\x03\x12\x81"
|
||||
b"\xE8\x03\x85\x00\x78"
|
||||
b"\xCB\x05\x39\x2C\x00\x34\x02"
|
||||
b"\xF7\x01\x20"
|
||||
b"\xEA\x02\x00\x00"
|
||||
b"\xc0\x01\x23" # Power control VRH[5:0]
|
||||
b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0]
|
||||
b"\xc5\x02\x3e\x28" # VCM control
|
||||
b"\xc7\x01\x86" # VCM control2
|
||||
b"\x36\x01\x38" # Memory Access Control
|
||||
b"\x37\x01\x00" # Vertical scroll zero
|
||||
b"\x3a\x01\x55" # COLMOD: Pixel Format Set
|
||||
b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors)
|
||||
b"\xb6\x03\x08\x82\x27" # Display Function Control
|
||||
b"\xF2\x01\x00" # 3Gamma Function Disable
|
||||
b"\x26\x01\x01" # Gamma curve selected
|
||||
b"\xe0\x0f\x0F\x31\x2B\x0C\x0E\x08\x4E\xF1\x37\x07\x10\x03\x0E\x09\x00" # Set Gamma
|
||||
b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F" # Set Gamma
|
||||
b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms)
|
||||
b"\x29\x80\x78" # Display on then delay 0x78 (120ms)
|
||||
)
|
||||
|
||||
# Setup the Display
|
||||
display = displayio.Display(display_bus, INIT_SEQUENCE, width=320, height=240)
|
||||
|
||||
#
|
||||
# DONE - now you can use the display however you want
|
||||
#
|
||||
|
||||
bitmap = displayio.Bitmap(320, 240, 2)
|
||||
|
||||
palette = displayio.Palette(2)
|
||||
palette[0] = 0
|
||||
palette[1] = 0xFFFFFF
|
||||
|
||||
for x in range(10, 20):
|
||||
for y in range(10, 20):
|
||||
bitmap[x, y] = 1
|
||||
|
||||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
|
||||
|
||||
group = displayio.Group()
|
||||
group.append(tile_grid)
|
||||
display.show(group)
|
||||
display.refresh_soon()
|
||||
21
CircuitPython_displayio/displayio_font.py
Normal file
21
CircuitPython_displayio/displayio_font.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import board
|
||||
import displayio
|
||||
from adafruit_bitmap_font import bitmap_font
|
||||
from adafruit_display_text import label
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# Set text, font, and color
|
||||
text = "HELLO WORLD"
|
||||
font = bitmap_font.load_font("/Helvetica-Bold-16.bdf")
|
||||
color = 0xFF00FF
|
||||
|
||||
# Create the tet label
|
||||
text_area = label.Label(font, text=text, color=color)
|
||||
|
||||
# Set the location
|
||||
text_area.x = 20
|
||||
text_area.y = 20
|
||||
|
||||
# Show it
|
||||
display.show(text_area)
|
||||
25
CircuitPython_displayio/displayio_imageload.py
Normal file
25
CircuitPython_displayio/displayio_imageload.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import board
|
||||
import displayio
|
||||
import adafruit_imageload
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
bitmap, palette = adafruit_imageload.load("/purple.bmp",
|
||||
bitmap=displayio.Bitmap,
|
||||
palette=displayio.Palette)
|
||||
|
||||
# Create a TileGrid to hold the bitmap
|
||||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
|
||||
|
||||
# Create a Group to hold the TileGrid
|
||||
group = displayio.Group()
|
||||
|
||||
# Add the TileGrid to the Group
|
||||
group.append(tile_grid)
|
||||
|
||||
# Add the Group to the Display
|
||||
display.show(group)
|
||||
|
||||
# Loop forever so you can enjoy your image
|
||||
while True:
|
||||
pass
|
||||
26
CircuitPython_displayio/displayio_ondiskbitmap.py
Normal file
26
CircuitPython_displayio/displayio_ondiskbitmap.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import board
|
||||
import displayio
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# Open the file
|
||||
with open("/purple.bmp", "rb") as bitmap_file:
|
||||
|
||||
# Setup the file as the bitmap data source
|
||||
bitmap = displayio.OnDiskBitmap(bitmap_file)
|
||||
|
||||
# Create a TileGrid to hold the bitmap
|
||||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
|
||||
|
||||
# Create a Group to hold the TileGrid
|
||||
group = displayio.Group()
|
||||
|
||||
# Add the TileGrid to the Group
|
||||
group.append(tile_grid)
|
||||
|
||||
# Add the Group to the Display
|
||||
display.show(group)
|
||||
|
||||
# Loop forever so you can enjoy your image
|
||||
while True:
|
||||
pass
|
||||
65
CircuitPython_displayio/displayio_parallelbus.py
Normal file
65
CircuitPython_displayio/displayio_parallelbus.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import board
|
||||
import displayio
|
||||
|
||||
# Release any previously configured displays
|
||||
displayio.release_displays()
|
||||
|
||||
# Setup the display bus
|
||||
# Tested with a Metro M4 Express
|
||||
display_bus = displayio.ParallelBus(data0=board.D13,
|
||||
command=board.D6,
|
||||
chip_select=board.D7,
|
||||
write=board.D5,
|
||||
read=board.D4)
|
||||
|
||||
# Setup the initialization sequence
|
||||
# stolen from adafruit_ili9341.py
|
||||
INIT_SEQUENCE = (
|
||||
b"\x01\x80\x80" # Software reset then delay 0x80 (128ms)
|
||||
b"\xEF\x03\x03\x80\x02"
|
||||
b"\xCF\x03\x00\xC1\x30"
|
||||
b"\xED\x04\x64\x03\x12\x81"
|
||||
b"\xE8\x03\x85\x00\x78"
|
||||
b"\xCB\x05\x39\x2C\x00\x34\x02"
|
||||
b"\xF7\x01\x20"
|
||||
b"\xEA\x02\x00\x00"
|
||||
b"\xc0\x01\x23" # Power control VRH[5:0]
|
||||
b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0]
|
||||
b"\xc5\x02\x3e\x28" # VCM control
|
||||
b"\xc7\x01\x86" # VCM control2
|
||||
b"\x36\x01\x38" # Memory Access Control
|
||||
b"\x37\x01\x00" # Vertical scroll zero
|
||||
b"\x3a\x01\x55" # COLMOD: Pixel Format Set
|
||||
b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors)
|
||||
b"\xb6\x03\x08\x82\x27" # Display Function Control
|
||||
b"\xF2\x01\x00" # 3Gamma Function Disable
|
||||
b"\x26\x01\x01" # Gamma curve selected
|
||||
b"\xe0\x0f\x0F\x31\x2B\x0C\x0E\x08\x4E\xF1\x37\x07\x10\x03\x0E\x09\x00" # Set Gamma
|
||||
b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F" # Set Gamma
|
||||
b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms)
|
||||
b"\x29\x80\x78" # Display on then delay 0x78 (120ms)
|
||||
)
|
||||
|
||||
# Setup the Display
|
||||
display = displayio.Display(display_bus, INIT_SEQUENCE, width=320, height=240)
|
||||
|
||||
#
|
||||
# DONE - now you can use the display however you want
|
||||
#
|
||||
|
||||
bitmap = displayio.Bitmap(320, 240, 2)
|
||||
|
||||
palette = displayio.Palette(2)
|
||||
palette[0] = 0
|
||||
palette[1] = 0xFFFFFF
|
||||
|
||||
for x in range(10, 20):
|
||||
for y in range(10, 20):
|
||||
bitmap[x, y] = 1
|
||||
|
||||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
|
||||
|
||||
group = displayio.Group()
|
||||
group.append(tile_grid)
|
||||
display.show(group)
|
||||
display.refresh_soon()
|
||||
32
CircuitPython_displayio/displayio_pixels.py
Normal file
32
CircuitPython_displayio/displayio_pixels.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import board
|
||||
import displayio
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# Create a bitmap with two colors
|
||||
bitmap = displayio.Bitmap(display.width, display.height, 2)
|
||||
|
||||
# Create a two color palette
|
||||
palette = displayio.Palette(2)
|
||||
palette[0] = 0x000000
|
||||
palette[1] = 0xffffff
|
||||
|
||||
# Create a TileGrid using the Bitmap and Palette
|
||||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
|
||||
|
||||
# Create a Group
|
||||
group = displayio.Group()
|
||||
|
||||
# Add the TileGrid to the Group
|
||||
group.append(tile_grid)
|
||||
|
||||
# Add the Group to the Display
|
||||
display.show(group)
|
||||
|
||||
# Draw a pixel
|
||||
bitmap[80, 50] = 1
|
||||
|
||||
# Draw even more pixels
|
||||
for x in range(150, 170):
|
||||
for y in range(100, 110):
|
||||
bitmap[x, y] = 1
|
||||
38
CircuitPython_displayio/displayio_sprite_sheet.py
Normal file
38
CircuitPython_displayio/displayio_sprite_sheet.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import time
|
||||
import board
|
||||
import displayio
|
||||
import adafruit_imageload
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# Load the sprite sheet (bitmap)
|
||||
sprite_sheet, palette = adafruit_imageload.load("/cp_sprite_sheet.bmp",
|
||||
bitmap=displayio.Bitmap,
|
||||
palette=displayio.Palette)
|
||||
|
||||
# Create a sprite (tilegrid)
|
||||
sprite = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
|
||||
width = 1,
|
||||
height = 1,
|
||||
tile_width = 16,
|
||||
tile_height = 16)
|
||||
|
||||
# Create a Group to hold the sprite
|
||||
group = displayio.Group(scale=1)
|
||||
|
||||
# Add the sprite to the Group
|
||||
group.append(sprite)
|
||||
|
||||
# Add the Group to the Display
|
||||
display.show(group)
|
||||
|
||||
# Set sprite location
|
||||
group.x = 120
|
||||
group.y = 80
|
||||
|
||||
# Loop through each sprite in the sprite sheet
|
||||
source_index = 0
|
||||
while True:
|
||||
sprite[0] = source_index % 6
|
||||
source_index += 1
|
||||
time.sleep(2)
|
||||
21
CircuitPython_displayio/displayio_text.py
Normal file
21
CircuitPython_displayio/displayio_text.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import board
|
||||
import displayio
|
||||
import terminalio
|
||||
from adafruit_display_text import label
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# Set text, font, and color
|
||||
text = "HELLO WORLD"
|
||||
font = terminalio.FONT
|
||||
color = 0x0000FF
|
||||
|
||||
# Create the tet label
|
||||
text_area = label.Label(font, text="HELLO WORLD", color=0x00FF00)
|
||||
|
||||
# Set the location
|
||||
text_area.x = 100
|
||||
text_area.y = 80
|
||||
|
||||
# Show it
|
||||
display.show(text_area)
|
||||
66
CircuitPython_displayio/displayio_tilegrids.py
Normal file
66
CircuitPython_displayio/displayio_tilegrids.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import board
|
||||
import displayio
|
||||
import adafruit_imageload
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# Load the sprite sheet (bitmap)
|
||||
sprite_sheet, palette = adafruit_imageload.load("/castle_sprite_sheet.bmp",
|
||||
bitmap=displayio.Bitmap,
|
||||
palette=displayio.Palette)
|
||||
|
||||
# Create the sprite TileGrid
|
||||
sprite = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
|
||||
width = 1,
|
||||
height = 1,
|
||||
tile_width = 16,
|
||||
tile_height = 16,
|
||||
default_tile = 0)
|
||||
|
||||
# Create the castle TileGrid
|
||||
castle = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
|
||||
width = 6,
|
||||
height = 5,
|
||||
tile_width = 16,
|
||||
tile_height = 16)
|
||||
|
||||
# Create a Group to hold the sprite and add it
|
||||
sprite_group = displayio.Group()
|
||||
sprite_group.append(sprite)
|
||||
|
||||
# Create a Group to hold the castle and add it
|
||||
castle_group = displayio.Group(scale=3)
|
||||
castle_group.append(castle)
|
||||
|
||||
# Create a Group to hold the sprite and castle
|
||||
group = displayio.Group()
|
||||
|
||||
# Add the sprite and castle to the group
|
||||
group.append(castle_group)
|
||||
group.append(sprite_group)
|
||||
|
||||
# Castle tile assignments
|
||||
# corners
|
||||
castle[0, 0] = 3 # upper left
|
||||
castle[5, 0] = 5 # upper right
|
||||
castle[0, 4] = 9 # lower left
|
||||
castle[5, 4] = 11 # lower right
|
||||
# top / bottom walls
|
||||
for x in range(1, 5):
|
||||
castle[x, 0] = 4 # top
|
||||
castle[x, 4] = 10 # bottom
|
||||
# left/ right walls
|
||||
for y in range(1, 4):
|
||||
castle[0, y] = 6 # left
|
||||
castle[5, y] = 8 # right
|
||||
# floor
|
||||
for x in range(1, 5):
|
||||
for y in range(1, 4):
|
||||
castle[x, y] = 7 # floor
|
||||
|
||||
# put the sprite somewhere in the castle
|
||||
sprite.x = 110
|
||||
sprite.y = 70
|
||||
|
||||
# Add the Group to the Display
|
||||
display.show(group)
|
||||
BIN
CircuitPython_displayio/purple.bmp
Normal file
BIN
CircuitPython_displayio/purple.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
Loading…
Reference in a new issue