|
| 1 | +# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# |
| 5 | +# Play Multiple GIF files on an ESP32-S2 Feather TFT |
| 6 | +# Requires CircuitPython 8.1.0-beta.1 or later |
| 7 | +# Updated 4/4/2023 |
| 8 | + |
| 9 | +import time |
| 10 | +import gc |
| 11 | +import os |
| 12 | +import struct |
| 13 | +import board |
| 14 | +import gifio |
| 15 | +import digitalio |
| 16 | + |
| 17 | +# Get a dictionary of GIF filenames at the passed base directory |
| 18 | +def get_files(base): |
| 19 | + allfiles = os.listdir(base) |
| 20 | + file_names = [] |
| 21 | + for _, filetext in enumerate(allfiles): |
| 22 | + if not filetext.startswith("."): |
| 23 | + if filetext not in ('boot_out.txt', 'System Volume Information'): |
| 24 | + if filetext.endswith(".gif"): |
| 25 | + file_names.append(filetext) |
| 26 | + return file_names |
| 27 | + |
| 28 | +# Set BOOT button on ESP32-S2 Feather TFT to advance to next GIF |
| 29 | +button = digitalio.DigitalInOut(board.BUTTON) |
| 30 | +button.switch_to_input(pull=digitalio.Pull.UP) |
| 31 | + |
| 32 | +display = board.DISPLAY |
| 33 | +display.auto_refresh = False |
| 34 | + |
| 35 | +COL_OFFSET = 40 # The Feather TFT needs to have the display |
| 36 | +ROW_OFFSET = 53 # offset by these values for direct writes |
| 37 | + |
| 38 | +files = get_files("/") |
| 39 | +for i in range(len(files)): |
| 40 | + |
| 41 | + odg = gifio.OnDiskGif(files[i]) |
| 42 | + # Skip PyPortal GIFs if put on ESP32-S2 Feather TFT |
| 43 | + if odg.width != board.DISPLAY.width: |
| 44 | + print("File "+files[i]+" not right width, skipping\n") |
| 45 | + continue |
| 46 | + |
| 47 | + start = time.monotonic() |
| 48 | + next_delay = odg.next_frame() # Load the first frame |
| 49 | + end = time.monotonic() |
| 50 | + call_delay = end - start |
| 51 | + |
| 52 | + # Display GIF file frames until screen touched (for PyPortal) |
| 53 | + while True: |
| 54 | + sleeptime = max(0, next_delay - call_delay) |
| 55 | + time.sleep(sleeptime) |
| 56 | + # If the BOOT button is pressed, advance to next GIF file |
| 57 | + if button.value is False: |
| 58 | + print("Button Press, Advance\n") |
| 59 | + break |
| 60 | + next_delay = odg.next_frame() |
| 61 | + display.bus.send(42, struct.pack(">hh", COL_OFFSET, |
| 62 | + odg.bitmap.width - 1 + COL_OFFSET)) |
| 63 | + display.bus.send(43, struct.pack(">hh", ROW_OFFSET, |
| 64 | + odg.bitmap.height - 1 + ROW_OFFSET)) |
| 65 | + display.bus.send(44, odg.bitmap) |
| 66 | + # End while |
| 67 | + # Clean up memory |
| 68 | + odg.deinit() |
| 69 | + odg = None |
| 70 | + gc.collect() |
| 71 | +# End for |
0 commit comments