代码: 全选
import displayio
import board
import rgbmatrix
import framebufferio
import time
import gifio
import os
# Get a dictionary of GIF filenames at the passed base directory
def get_files(base):
allfiles = os.listdir(base)
file_names = []
for _, filetext in enumerate(allfiles):
if not filetext.startswith("."):
if filetext not in ('boot_out.txt', 'System Volume Information'):
if filetext.endswith(".gif"):
file_names.append(filetext)
return file_names
# Set for 2 matrix
bit_depth = 6
base_width = 64
base_height = 32
chain_across = 1
tile_down = 2
serpentine = True
width = base_width * chain_across
height = base_height * tile_down
addr_pins = [board.MTX_ADDRA, board.MTX_ADDRB, board.MTX_ADDRC, board.MTX_ADDRD]
rgb_pins = [
board.MTX_R1,
board.MTX_G1,
board.MTX_B1,
board.MTX_R2,
board.MTX_G2,
board.MTX_B2,
]
clock_pin = board.MTX_CLK
latch_pin = board.MTX_LAT
oe_pin = board.MTX_OE
displayio.release_displays()
matrix = rgbmatrix.RGBMatrix(
width=width,
height=height,
bit_depth=bit_depth,
rgb_pins=rgb_pins,
addr_pins=addr_pins,
clock_pin=clock_pin,
latch_pin=latch_pin,
output_enable_pin=oe_pin,
tile=tile_down,
serpentine=serpentine,
)
display = framebufferio.FramebufferDisplay(matrix)
splash = displayio.Group()
display.root_group = splash
DIRECTORY = "/gifs/"
files = get_files(DIRECTORY)
while True:
for i in range(len(files)):
odg = gifio.OnDiskGif(DIRECTORY+files[i])
# Load the first frame
start = time.monotonic()
next_delay = odg.next_frame()
end = time.monotonic()
overhead = end - start # how long did loading a frame take
gif_tile = displayio.TileGrid(
odg.bitmap,
pixel_shader=displayio.ColorConverter(
input_colorspace=displayio.Colorspace.RGB565_SWAPPED
),
)
splash.append(gif_tile)
start = time.monotonic()
# Display the GIF for 30 seconds
while time.monotonic() < start + 30:
frame_shown = time.monotonic()
display.refresh()
next_delay = odg.next_frame()
time.sleep(max(0, next_delay - overhead))
splash.remove(gif_tile)