-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathht16k33_animation_demo.py
320 lines (258 loc) · 7.99 KB
/
ht16k33_animation_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
Test script for display animations on an HT16K33 with alphanumeric display
The display must be initialized with auto_write=False.
"""
#
# Segment bits on the HT16K33 with alphanumeric display.
#
# Add the values of the segments you need to create a bitmask
#
N = 16384
M = 8192
L = 4096
K = 2048
J = 1024
I = 512
H = 256
G2= 128
G1= 64
F = 32
E = 16
D = 8
C = 4
B = 2
A = 1
# The number of seconds to delay between writing segments
DEFAULT_CHAR_DELAY_SEC = 0.2
# The number of cycles to go for each animation
DEFAULT_CYCLES = 5
# Brightness of the display (0 to 15)
DEFAULT_DISPLAY_BRIGHTNESS = 2
from time import sleep
import board
import busio
from adafruit_ht16k33.segments import Seg14x4
# Initialize the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
# Initialize the HT16K33 with alphanumeric display featherwing.
#
# You MUST set auto_write=False
display = Seg14x4(i2c, auto_write=False)
display.brightness = DEFAULT_DISPLAY_BRIGHTNESS
def animate(digits, bitmasks, delay=DEFAULT_CHAR_DELAY_SEC, auto_write=True):
'''
Main driver for all alphanumeric display animations (WIP!!!)
Param: digits - a list of the digits to write to, in order, like [0, 1, 3]. The digits are
0 to 3 starting at the left most digit.
Param: bitmasks - a list of the bitmasks to write, in sequence, to the specified digits.
Param: delay - The delay, in seconds (or fractions of), between writing bitmasks to a digit.
Param: auto_write - Whether to actually write to the display immediately or not.
Returns: Nothing
'''
if not isinstance(digits, list):
raise ValueError("The first parameter MUST be a list!")
elif not isinstance(bitmasks, list):
raise ValueError("The second parameter MUST be a list!")
elif delay < 0:
raise ValueError("The delay between frames must be positive!")
else:
for dig in digits:
for bits in bitmasks:
display.set_digit_raw(dig, bits)
if auto_write:
display.show()
sleep(delay)
def chase_forward_and_reverse(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES):
cy = 0
while cy < cycles:
animate([0, 1, 2, 3], [A, 0], delay)
animate([3], [B, C, D, 0], delay)
animate([2, 1, 0], [D, 0], delay)
animate([0], [E, F, H, G2, 0], delay)
animate([1, 2], [G1, G2, 0], delay)
animate([3], [G1, J, A, 0], delay)
animate([2, 1], [A, 0], delay)
animate([0], [A, F, E, D, 0], delay)
animate([1, 2], [D, 0], delay)
animate([3], [D, C, B, J, G1, 0], delay)
animate([2, 1], [G2, G1, 0], delay)
animate([0], [H, 0], delay)
cy += 1
def prelude_to_spinners(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES):
cy = 0
auto_write = False
while cy < cycles:
animate([1, 2], [A], 0, auto_write)
display.show()
sleep(delay)
animate([0, 3], [A], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+F], 0, auto_write)
animate([3], [A+B], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+E+F], 0, auto_write)
animate([3], [A+B+C], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+D+E+F], 0, auto_write)
animate([3], [A+B+C+D], 0, auto_write)
display.show()
sleep(delay)
animate([1], [A+D], 0, auto_write)
animate([2], [A+D], 0, auto_write)
display.show()
sleep(delay)
animate([1], [A+D+M], 0, auto_write)
animate([2], [A+D+K], 0, auto_write)
display.show()
sleep(delay)
animate([1], [A+D+M+H], 0, auto_write)
animate([2], [A+D+K+J], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+E+F+J+D], 0, auto_write)
animate([3], [A+B+C+H+D], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+E+F+J+K+D], 0, auto_write)
animate([3], [A+B+C+H+M+D], 0, auto_write)
display.show()
sleep(delay)
display.fill(0)
display.show()
sleep(delay)
cy += 1
def spinners(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES):
cy = 0
auto_write = False
while cy < cycles:
animate([0], [H+M], 0, auto_write)
animate([1], [J+K], 0, auto_write)
animate([2], [H+M], 0, auto_write)
animate([3], [J+K], 0, auto_write)
display.show()
sleep(delay)
animate([0], [G1+G2], 0, auto_write)
animate([1], [G1+G2], 0, auto_write)
animate([2], [G1+G2], 0, auto_write)
animate([3], [G1+G2], 0, auto_write)
display.show()
sleep(delay)
animate([0], [J+K], 0, auto_write)
animate([1], [H+M], 0, auto_write)
animate([2], [J+K], 0, auto_write)
animate([3], [H+M], 0, auto_write)
display.show()
sleep(delay)
cy += 1
display.fill(0)
def enclosed_spinners(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES):
cy = 0
auto_write = False
while cy < cycles:
animate([0], [A+D+E+F+H+M], 0, auto_write)
animate([1], [A+D+J+K], 0, auto_write)
animate([2], [A+D+H+M], 0, auto_write)
animate([3], [A+B+C+D+J+K], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+D+E+F+G1+G2], 0, auto_write)
animate([1], [A+D+G1+G2], 0, auto_write)
animate([2], [A+D+G1+G2], 0, auto_write)
animate([3], [A+B+C+D+G1+G2], 0, auto_write)
display.show()
sleep(delay)
animate([0], [A+D+E+F+J+K], 0, auto_write)
animate([1], [A+D+H+M], 0, auto_write)
animate([2], [A+D+J+K], 0, auto_write)
animate([3], [A+B+C+D+H+M], 0, auto_write)
display.show()
sleep(delay)
cy += 1
display.fill(0)
def count_down(delay=DEFAULT_CHAR_DELAY_SEC, cycles=DEFAULT_CYCLES):
auto_write = False
numbers = [ [A+B+C+D+G1+G2+N], [A+B+D+E+G1+G2+N], [B+C+N] ]
index = 0
display.fill(0)
while index < len(numbers):
animate([index], numbers[index], 0, auto_write)
display.show()
sleep(1)
display.fill(0)
sleep(0.5)
index += 1
sleep(1)
display.fill(0)
try:
text = "Init"
display.fill(1)
display.show()
sleep(1)
display.fill(0)
display.show()
display.print(text)
display.show()
sleep(2)
display.fill(0)
display.show()
sleep(1)
count_down(0.1, 1)
sleep(0.2)
text = "Go!!"
display.print(text)
display.show()
sleep(1.5)
display.fill(0)
display.show()
sleep(0.5)
print()
while True:
# Arrow
print("Arrow")
animate([0, 1, 2], [G1+G2], 0.1)
animate([3], [G1+H+K], 0.1)
sleep(1.0)
display.fill(0)
sleep(1.0)
# Flying
print("Flying")
cy = 0
while cy < DEFAULT_CYCLES:
animate([0], [H+J, G1+G2, K+M, G1+G2], DEFAULT_CHAR_DELAY_SEC)
cy += 1
animate([0], [0])
sleep(1.0)
display.fill(0)
sleep(1.0)
# Chase forward and reverse.
print("Chase forward and reverse")
chase_forward_and_reverse(0.01, 5)
sleep(1.0)
display.fill(0)
sleep(1.0)
# Testing writing to more than one segment simultaneously
print("Prelude to Spinners")
prelude_to_spinners(0.1, 5)
sleep(1.0)
display.fill(0)
display.show()
sleep(1.0)
print("Spinners")
spinners(0.1, 20)
sleep(1.0)
display.fill(0)
display.show()
sleep(1.0)
print("Enclosed Spinners")
enclosed_spinners(0.1, 20)
sleep(1.0)
display.fill(0)
display.show()
sleep(1.0)
print()
except KeyboardInterrupt:
display.fill(0)
display.show()