The Adafruit ESP32-S2 Feather TFT with a 240x135 pixel display makes for a compact GIF playing platform.
Download the Project Bundle
The examples use code files and sample animated GIF images. To get everything you need, click on the Download Project Bundle link below, and uncompress the .zip file.
Hook the Feather ESP32-S2 TFT to your computer via a known good USB data+power cable. It should show up as a thumb drive named CIRCUITPY.
Using File Explorer/Finder (depending on your Operating System), drag the contents of the uncompressed bundle directory onto your board's CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.
The code below displays a single GIF image. To run it, copy one of the GIF images to the name sample.gif. Copy the file code-single-displayio-feather.py to code.py.
# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT # # Play a single animated GIF file (display_bus "fast" method) # # Documentation: # https://docs.circuitpython.org/en/latest/shared-bindings/gifio/ # Updated 4/5/2023 import time import gc import board import gifio import digitalio import displayio display = board.DISPLAY splash = displayio.Group() display.root_group = splash # Set BOOT button on ESP32-S2 Feather TFT to stop GIF button = digitalio.DigitalInOut(board.BUTTON) button.switch_to_input(pull=digitalio.Pull.UP) # Open GIF file sample.gif odg = gifio.OnDiskGif('/sample.gif') start = time.monotonic() next_delay = odg.next_frame() # Load the first frame end = time.monotonic() overhead = end - start face = displayio.TileGrid( odg.bitmap, pixel_shader=displayio.ColorConverter( input_colorspace=displayio.Colorspace.RGB565_SWAPPED ), ) splash.append(face) board.DISPLAY.refresh() # Display repeatedly & directly. while True: # Sleep for the frame delay specified by the GIF, # minus the overhead measured to advance between frames. time.sleep(max(0, next_delay - overhead)) # If the BOOT button is pressed, stop the GIF if button.value is False: print("Button Press, Stop\n") break next_delay = odg.next_frame() # End While - cleanup memory odg.deinit() odg = None gc.collect()
How It Works
First the code imports all the libraries needed for the example. All are included inside CircuitPython. Using displayio
, the CircuitPython display library, the screen is defined (the parameters are in the Feather definition file so they don't need to be looked up, things like the screen width: 240 px and height 135 px).
The file sample.gif is opened via gifio
. You may get an error that sample.gif is not found, please copy one of the example GIF files to sample.gif.
The first frame is loaded using odg.next_frame()
. The time it takes to call the function is noted to make an adjustment for the time between frames later.
A displayio
TileGrid
is made consisting of the first frame and the entire display.
Finally, there is a loop which constantly fetches frames and displays them with a file-defined pause between frames. As calling the next_frame
takes time (measured earlier), that time is subtracted from the frame rate specified in the file.
If the BOOT button is pressed, the memory is cleaned up and the program ends. This is just a convenience and a user's program need not use a button. It is suggested cleaning up the memory after using gifio
.
Text editor powered by tinymce.