Skip to content

Commit 360557a

Browse files
authored
Merge pull request #2471 from adafruit/TheKitty-patch-5
Add additional files for Learn Guide
2 parents 52cee1e + 542d546 commit 360557a

10 files changed

+252
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Play Multiple GIF files on a n 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+
34+
# Take over display to drive directly
35+
display.auto_refresh = False
36+
display_bus = display.bus
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", 0, odg.bitmap.width - 1))
62+
display_bus.send(43, struct.pack(">hh", 0, odg.bitmap.height - 1))
63+
display_bus.send(44, odg.bitmap)
64+
# End while
65+
# Clean up memory
66+
odg.deinit()
67+
odg = None
68+
gc.collect()
69+
# End for
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Play a single animated GIF file (display_bus "fast" method)
6+
#
7+
# Documentation:
8+
# https://docs.circuitpython.org/en/latest/shared-bindings/gifio/
9+
# Updated 4/5/2023
10+
11+
import time
12+
import gc
13+
import board
14+
import gifio
15+
import digitalio
16+
import displayio
17+
18+
display = board.DISPLAY
19+
splash = displayio.Group()
20+
display.root_group = splash
21+
22+
# Set BOOT button on ESP32-S2 Feather TFT to stop GIF
23+
button = digitalio.DigitalInOut(board.BUTTON)
24+
button.switch_to_input(pull=digitalio.Pull.UP)
25+
26+
# Open GIF file sample.gif
27+
odg = gifio.OnDiskGif('/sample.gif')
28+
29+
start = time.monotonic()
30+
next_delay = odg.next_frame() # Load the first frame
31+
end = time.monotonic()
32+
overhead = end - start
33+
34+
face = displayio.TileGrid(
35+
odg.bitmap,
36+
pixel_shader=displayio.ColorConverter(
37+
input_colorspace=displayio.Colorspace.RGB565_SWAPPED
38+
),
39+
)
40+
splash.append(face)
41+
board.DISPLAY.refresh()
42+
43+
# Display repeatedly & directly.
44+
while True:
45+
# Sleep for the frame delay specified by the GIF,
46+
# minus the overhead measured to advance between frames.
47+
time.sleep(max(0, next_delay - overhead))
48+
# If the BOOT button is pressed, stop the GIF
49+
if button.value is False:
50+
print("Button Press, Stop\n")
51+
break
52+
next_delay = odg.next_frame()
53+
# End While - cleanup memory
54+
odg.deinit()
55+
odg = None
56+
gc.collect()
Loading
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Play Multiple GIF files on a PyPortal
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 adafruit_touchscreen
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+
display = board.DISPLAY
29+
30+
# Take over display to drive directly
31+
display.auto_refresh = False
32+
display_bus = display.bus
33+
34+
# Pyportal has a touchscreen, a touch advances to next GIF file
35+
WIDTH = board.DISPLAY.width
36+
HEIGHT = board.DISPLAY.height
37+
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
38+
board.TOUCH_YD, board.TOUCH_YU,
39+
calibration=((5200, 59000), (5800, 57000)),
40+
size=(WIDTH, HEIGHT))
41+
files = get_files("/")
42+
for i in range(len(files)):
43+
44+
odg = gifio.OnDiskGif(files[i])
45+
# Skip Feather GIFs if put on PyPortal
46+
if odg.width != WIDTH:
47+
print("File "+files[i]+" not right width, skipping\n")
48+
continue
49+
50+
start = time.monotonic()
51+
next_delay = odg.next_frame() # Load the first frame
52+
end = time.monotonic()
53+
call_delay = end - start
54+
55+
# Display GIF file frames until screen touched (for PyPortal)
56+
while True:
57+
sleeptime = max(0, next_delay - call_delay)
58+
time.sleep(sleeptime)
59+
if ts.touch_point is not None:
60+
break
61+
next_delay = odg.next_frame()
62+
display_bus.send(42, struct.pack(">hh", 0, odg.bitmap.width - 1))
63+
display_bus.send(43, struct.pack(">hh", 0, odg.bitmap.height - 1))
64+
display_bus.send(44, odg.bitmap)
65+
# End while
66+
# Clean up memory
67+
odg.deinit()
68+
odg = None
69+
gc.collect()
70+
# End for
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# gifio demo for the Adafruit PyPortal - single file
6+
#
7+
# Documentation:
8+
# https://docs.circuitpython.org/en/latest/shared-bindings/gifio/
9+
# Updated 4/5/2023
10+
#
11+
import time
12+
import gc
13+
import board
14+
import gifio
15+
import displayio
16+
import adafruit_touchscreen
17+
18+
display = board.DISPLAY
19+
splash = displayio.Group()
20+
display.root_group = splash
21+
22+
# Pyportal has a touchscreen, a touch stops the display
23+
WIDTH = board.DISPLAY.width
24+
HEIGHT = board.DISPLAY.height
25+
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
26+
board.TOUCH_YD, board.TOUCH_YU,
27+
calibration=((5200, 59000), (5800, 57000)),
28+
size=(WIDTH, HEIGHT))
29+
30+
odg = gifio.OnDiskGif('/sample.gif')
31+
32+
start = time.monotonic()
33+
next_delay = odg.next_frame() # Load the first frame
34+
end = time.monotonic()
35+
call_delay = end - start
36+
37+
# Depending on your display the next line may need Colorspace.RGB565
38+
# instead of Colorspace.RGB565_SWAPPED
39+
face = displayio.TileGrid(odg.bitmap,
40+
pixel_shader=displayio.ColorConverter
41+
(input_colorspace=displayio.Colorspace.RGB565_SWAPPED))
42+
splash.append(face)
43+
board.DISPLAY.refresh()
44+
45+
# Play the GIF file until screen is touched
46+
while True:
47+
time.sleep(max(0, next_delay - call_delay))
48+
next_delay = odg.next_frame()
49+
if ts.touch_point is not None:
50+
break
51+
# End while
52+
# Clean up memory
53+
odg.deinit()
54+
odg = None
55+
gc.collect()
Loading
Loading
Loading

CircuitPython_gifio/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
## Playing Animated GIF Files in CircuitPython
33

44
Two sample boards: Adafruit PyPortal and ESP32-S2 Feather TFT
5-
Two Speeds: Using displayio and using direct display writes
6-
Two Examples: Display a single GIF and display multiple GIFs
5+
6+
Display a single GIF and display multiple GIFs
77

88
Anne Barela for Adafruit Industries
99

0 commit comments

Comments
 (0)