Skip to content

Commit 3042478

Browse files
committed
first commit
1 parent 24ad4e8 commit 3042478

File tree

7 files changed

+121
-0
lines changed

7 files changed

+121
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import time
2+
import os
3+
import board
4+
import displayio
5+
from digitalio import DigitalInOut, Pull
6+
from adafruit_matrixportal.matrix import Matrix
7+
from adafruit_debouncer import Debouncer
8+
9+
SPRITESHEET_FOLDER = "/bmps"
10+
DEFAULT_FRAME_DURATION = 0.1 # 100ms
11+
AUTO_ADVANCE_LOOPS = 3
12+
FRAME_DURATION_OVERRIDES = {
13+
"three_rings1-sheet.bmp": 0.15,
14+
"hop1-sheet.bmp": 0.05,
15+
"firework1-sheet.bmp": 0.03,
16+
}
17+
18+
# --- Display setup ---
19+
matrix = Matrix(bit_depth=4)
20+
sprite_group = displayio.Group(max_size=1)
21+
matrix.display.show(sprite_group)
22+
23+
# --- Button setup ---
24+
pin_down = DigitalInOut(board.BUTTON_DOWN)
25+
pin_down.switch_to_input(pull=Pull.UP)
26+
button_down = Debouncer(pin_down)
27+
pin_up = DigitalInOut(board.BUTTON_UP)
28+
pin_up.switch_to_input(pull=Pull.UP)
29+
button_up = Debouncer(pin_up)
30+
31+
auto_advance = True
32+
33+
file_list = sorted(
34+
[
35+
f
36+
for f in os.listdir(SPRITESHEET_FOLDER)
37+
if (f.endswith(".bmp") and not f.startswith("."))
38+
]
39+
)
40+
41+
if len(file_list) == 0:
42+
raise RuntimeError("No images found")
43+
44+
current_image = None
45+
current_frame = 0
46+
current_loop = 0
47+
frame_count = 0
48+
frame_duration = DEFAULT_FRAME_DURATION
49+
50+
51+
def load_image():
52+
"""
53+
Load an image as a sprite
54+
"""
55+
# pylint: disable=global-statement
56+
global current_frame, current_loop, frame_count, frame_duration
57+
while sprite_group:
58+
sprite_group.pop()
59+
60+
bitmap = displayio.OnDiskBitmap(
61+
open(SPRITESHEET_FOLDER + "/" + file_list[current_image], "rb")
62+
)
63+
64+
frame_count = int(bitmap.height / matrix.display.height)
65+
frame_duration = DEFAULT_FRAME_DURATION
66+
if file_list[current_image] in FRAME_DURATION_OVERRIDES:
67+
frame_duration = FRAME_DURATION_OVERRIDES[file_list[current_image]]
68+
69+
sprite = displayio.TileGrid(
70+
bitmap,
71+
pixel_shader=displayio.ColorConverter(),
72+
width=1,
73+
height=1,
74+
tile_width=bitmap.width,
75+
tile_height=matrix.display.height,
76+
)
77+
78+
sprite_group.append(sprite)
79+
current_frame = 0
80+
current_loop = 0
81+
82+
83+
def advance_image():
84+
"""
85+
Advance to the next image in the list and loop back at the end
86+
"""
87+
# pylint: disable=global-statement
88+
global current_image
89+
if current_image is not None:
90+
current_image += 1
91+
if current_image is None or current_image >= len(file_list):
92+
current_image = 0
93+
load_image()
94+
95+
96+
def advance_frame():
97+
"""
98+
Advance to the next frame and loop back at the end
99+
"""
100+
# pylint: disable=global-statement
101+
global current_frame, current_loop
102+
current_frame = current_frame + 1
103+
if current_frame >= frame_count:
104+
current_frame = 0
105+
current_loop = current_loop + 1
106+
sprite_group[0][0] = current_frame
107+
108+
109+
advance_image()
110+
111+
while True:
112+
if auto_advance and current_loop >= AUTO_ADVANCE_LOOPS:
113+
advance_image()
114+
button_down.update()
115+
button_up.update()
116+
if button_up.fell:
117+
auto_advance = not auto_advance
118+
if button_down.fell:
119+
advance_image()
120+
advance_frame()
121+
time.sleep(frame_duration)

0 commit comments

Comments
 (0)