-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
438 lines (379 loc) · 14.4 KB
/
main.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import time
from random import randint
from machine import Pin
from neopixel import NeoPixel
LED_QTY = 256
COLOR = {
"red" : [255, 0, 0],
"green" : [0, 255, 0],
"blue" : [0, 0, 255],
"purple": [255, 0, 255],
"yellow": [255, 255, 0],
"white": [255, 255, 255]
}
class Matrix:
def __init__(self, pin, np):
self.pin = pin
self.np = np
def fill(self, color):
self.np.fill(color)
self.np.write()
def clear(self):
self.np.fill([0, 0, 0])
self.np.write()
def flash_random(self, color, duration, on_dura=0.1, colorful=False, clear=True):
duration = duration / on_dura
loops = 0
while True:
if loops == duration:
break
rand_num = randint(0, LED_QTY - 1)
if colorful:
colors = ["red", "green", "blue", "yellow", "white"]
rand_col = randint(0, len(colors)-1)
self.np[rand_num] = COLOR[colors[rand_col]]
else:
self.np[rand_num] = color
self.np.write()
time.sleep(on_dura)
if clear:
self.clear()
loops = loops + 1
def fade_in(self, color, steps, duration, reverse=False):
step_duration = duration/steps
loops = 0
brightness = [0, 0, 0]
if reverse:
brightness = color
else:
for i in range(3):
if color[i] > 0:
brightness[i] = 1
while True:
if loops == steps:
break
for i in range(3):
if color[i] > 0 and not reverse:
brightness[i] += 1
elif color[i] > 0 and reverse:
brightness[i] -= 1
for i in range(0, LED_QTY):
self.np[i] = brightness
time.sleep(step_duration)
self.np.write()
loops = loops + 1
def checkered_pattern(self, color1, color2, duration):
for i in range(0, LED_QTY):
if i % 2 == 0:
self.np[i] = color1
if i % 2 != 0:
self.np[i] = color2
self.np.write()
time.sleep(duration)
def fill_colorful(self, duration):
for i in range(LED_QTY):
rand1 = randint(0, 10)
rand2 = randint(0, 10)
rand3 = randint(0, 10)
self.np[i] = [rand1, rand2, rand3]
self.np.write()
time.sleep(duration)
def color_changing(self, current_color, fade_to_color, steps, duration):
step_duration = duration/steps
loops = 0
mixed_color = [0, 0, 0]
for i in range(3):
if fade_to_color[i] > 0:
fade_to_color[i] = 1
while True:
if loops == steps:
break
for i in range(3):
if fade_to_color[i] > 0:
fade_to_color[i] += 1
elif current_color[i] > 0:
current_color[i] -= 1
mixed_color[i] = current_color[i] + fade_to_color[i]
for i in range(0, LED_QTY):
self.np[i] = mixed_color
time.sleep(step_duration)
self.np.write()
loops = loops + 1
def show_one_bus_sweep(self, color, duration):
led_duration = duration / LED_QTY
for i in range(0, LED_QTY):
self.np[i] = color
self.np.write()
time.sleep(led_duration)
self.clear()
def show_square(self, x, y, color, duration, clear=True):
if x==0 and y==0:
for j in range(8):
for i in range(8):
if j % 2 == 0:
self.np[i+j*16] = color
else:
self.np[i+j*16 + 8] = color
if x == 1 and y == 0:
for j in range(8):
for i in range(8, 16):
if j % 2 == 0:
self.np[i+j*16] = color
else:
self.np[i+j*16 - 8] = color
if x == 1 and y == 1:
for j in range(8):
for i in range(136,144):
if j % 2 == 0:
self.np[i+j*16] = color
else:
self.np[i+j*16-8] = color
if x == 0 and y == 1:
for j in range(8):
for i in range(128,136):
if j % 2 == 0:
self.np[i+j*16] = color
else:
self.np[i+j*16+8] = color
self.np.write()
time.sleep(duration)
self.clear()
def show_snake(self, color, length, duration):
x = 0
loop_duration = (duration / LED_QTY)
while True:
if x == 256 - length:
break
for i in range(length):
self.np[x+i] = color
self.np.write()
time.sleep(loop_duration)
x += 1
self.clear()
def show_comets(self, color, duration):
color2 = [int(color[0]/2), int(color[1]/2), int(color[2]/2)]
color3 = [int(color[0]/4), int(color[1]/4), int(color[2]/4)]
color4 = [int(color[0]/8), int(color[1]/8), int(color[2]/8)]
color5 = [int(color[0]/16), int(color[1]/16), int(color[2]/16)]
x = 0
loop_duration = (duration / LED_QTY)
while True:
if x == 16-4:
break
for i in range(16):
self.np[x+0+i*16] = color5
self.np[x+1+i*16] = color4
self.np[x+2+i*16] = color3
self.np[x+3+i*16] = color2
self.np[x+4+i*16] = color
self.np.write()
time.sleep(loop_duration)
x += 1
self.clear()
def show_random_pixel_slides(self, color, duration):
x = 0
loop_duration = (duration / LED_QTY)
while True:
if x == 16:
break
for i in range(16):
show = randint(0, 1)
if show:
self.np[x+i*16] = color
self.np.write()
time.sleep(loop_duration)
x += 1
self.clear()
def fill_in_pieces(self, color, duration):
loop_duration = duration / LED_QTY
for i in range(LED_QTY):
self.np[i] = color
self.np.write()
time.sleep(loop_duration)
def tetris_point_fill(self, color, duration):
loop_duration = duration / LED_QTY
led_num = LED_QTY
while led_num >= 0:
for i in range(0, led_num, 10):
self.np[i] = color
self.np.write()
self.np[i] = [0, 0, 0]
self.np.write()
self.np[led_num-1] = color
self.np.write()
led_num -= 1
def tetris_line_fill(self, color, duration):
loop_duration = duration / LED_QTY
line_num = 16
while line_num >= 0:
for j in range(line_num):
for i in range(16):
self.np[i+16*j] = color
self.np.write()
time.sleep(loop_duration)
for i in range(16):
self.np[i+16*j] = [0, 0, 0]
self.np.write()
for i in range(16):
self.np[i+16*j] = color
self.np.write()
line_num -= 1
def flash_in_different_colors(matrix):
matrix.flash_random(COLOR["white"], 1)
matrix.flash_random(COLOR["red"], 1)
matrix.flash_random(COLOR["green"], 1)
matrix.flash_random(COLOR["blue"], 1)
matrix.flash_random(COLOR["purple"], 1)
matrix.flash_random(COLOR["yellow"], 1)
matrix.flash_random(COLOR["white"], 1, on_dura=0.01)
matrix.flash_random(COLOR["white"], 1, on_dura=0.01, colorful=True)
def fade_different_colors(matrix, brightness, single_duration):
matrix.fade_in([brightness, 0, 0], brightness, single_duration)
matrix.fade_in([brightness, 0, 0], brightness, single_duration, reverse=True)
matrix.fade_in([0, brightness, 0], brightness, single_duration)
matrix.fade_in([0, brightness, 0], brightness, single_duration, reverse=True)
matrix.fade_in([0, 0, brightness], brightness, single_duration)
matrix.fade_in([0, 0, brightness], brightness, single_duration, reverse=True)
def show_different_checkered(matrix):
loop = 0
while loop < 20:
matrix.checkered_pattern([20, 0, 0], [0, 0, 20], 0.1)
matrix.checkered_pattern([0, 0, 20], [20, 0, 0], 0.1)
loop += 1
loop = 0
while loop < 20:
matrix.checkered_pattern([0, 20, 0], [20, 0, 20], 0.1)
matrix.checkered_pattern([20, 0, 20], [0, 20, 0], 0.1)
loop += 1
loop = 0
while loop < 20:
matrix.checkered_pattern([20, 20, 20], [0, 0, 0], 0.1)
matrix.checkered_pattern([0, 0, 0], [20, 20, 10], 0.1)
loop += 1
matrix.clear()
def fade_pixels_in_and_out(matrix):
matrix.flash_random([20, 20, 20], 10, clear=False, on_dura=0.01)
matrix.flash_random([0, 0, 0], 10, clear=False, on_dura=0.01)
matrix.flash_random([20, 0, 0], 10, clear=False, on_dura=0.01)
matrix.flash_random([0, 0, 0], 10, clear=False, on_dura=0.01)
matrix.flash_random([0, 20, 0], 10, clear=False, on_dura=0.01)
matrix.flash_random([0, 0, 0], 10, clear=False, on_dura=0.01)
matrix.flash_random([0, 0, 20], 10, clear=False, on_dura=0.01)
matrix.flash_random([0, 0, 0], 10, clear=False, on_dura=0.01)
def show_pixel_noise(matrix):
for _ in range(200):
matrix.fill_colorful(0.04)
def different_color_fadeing(matrix, brightness, dura_per_color):
matrix.color_changing([brightness, 0, 0], [0, brightness, 0], brightness, dura_per_color)
matrix.color_changing([0, brightness, 0], [0, 0, brightness], brightness, dura_per_color)
matrix.color_changing([0, 0, brightness], [brightness, 0, 0], brightness, dura_per_color)
matrix.color_changing([brightness, 0, 0], [0, 0, brightness], brightness, dura_per_color)
def show_sweep_different_colors(matrix, duration_per_color):
matrix.show_one_bus_sweep([255, 0, 0], duration_per_color)
matrix.show_one_bus_sweep([0, 255, 0], duration_per_color)
matrix.show_one_bus_sweep([0, 0, 255], duration_per_color)
matrix.show_one_bus_sweep([255, 255, 255], duration_per_color)
def show_squares(matrix, duration_per_square, loops):
color = [10, 0, 0]
for _ in range(loops):
matrix.show_square(1, 1, color, duration_per_square)
matrix.show_square(0, 0, color, duration_per_square)
matrix.show_square(0, 1, color, duration_per_square)
matrix.show_square(1, 0, color, duration_per_square)
color = [0, 0, 10]
for _ in range(loops):
matrix.show_square(0, 0, color, duration_per_square)
matrix.show_square(1, 0, color, duration_per_square)
matrix.show_square(1, 1, color, duration_per_square)
matrix.show_square(0, 1, color, duration_per_square)
color = [0, 10, 0]
for _ in range(loops):
matrix.show_square(0, 1, color, duration_per_square)
matrix.show_square(1, 1, color, duration_per_square)
matrix.show_square(1, 0, color, duration_per_square)
matrix.show_square(0, 0, color, duration_per_square)
def show_snakes(matrix, length, duration_per_snake):
matrix.show_snake([20, 20, 20], length, duration_per_snake)
matrix.show_snake([20, 0, 0], length, duration_per_snake)
matrix.show_snake([0, 20, 0], length, duration_per_snake)
matrix.show_snake([0, 0, 20], length, duration_per_snake)
def show_colorful_comets(matrix, loops):
for _ in range(loops):
matrix.show_comets([100, 0, 0], 1)
for _ in range(loops):
matrix.show_comets([0, 100, 0], 1)
for _ in range(loops):
matrix.show_comets([0, 0, 100], 1)
for _ in range(loops):
matrix.show_comets([100, 0, 0], 10)
matrix.show_comets([0, 100, 0], 10)
matrix.show_comets([0, 0, 100], 10)
for _ in range(loops):
matrix.show_comets([100, 0, 0], 1)
matrix.show_comets([0, 100, 0], 1)
matrix.show_comets([0, 0, 100], 1)
def show_colorful_slides(matrix):
loop = 0
while loop <=5:
matrix.show_random_pixel_slides([0, 0, 100], 5)
loop += 1
loop = 0
while loop <=5:
matrix.show_random_pixel_slides([0, 100, 0], 5)
loop += 1
loop = 0
while loop <=5:
matrix.show_random_pixel_slides([100, 0, 0], 5)
loop += 1
def show_color_swap(matrix):
matrix.fill_in_pieces([10, 10, 10], 5)
matrix.fill_in_pieces([10, 0, 0], 5)
matrix.fill_in_pieces([10, 0, 10], 5)
matrix.fill_in_pieces([0, 10, 10], 5)
def show_tetris_lines(matrix):
matrix.tetris_line_fill([20, 0, 20], 4)
matrix.tetris_line_fill([10, 10, 10], 4)
matrix.tetris_line_fill([20, 20, 0], 3)
matrix.tetris_line_fill([0, 0, 20], 2)
matrix.tetris_line_fill([20, 20, 20], 1)
matrix.tetris_line_fill([0, 20, 0], 1)
matrix.tetris_line_fill([10, 10, 10], 1)
def main():
pin = Pin(14, Pin.OUT) #
np = NeoPixel(pin, LED_QTY)
matrix = Matrix(pin, np)
matrix.clear()
input("Press enter for start")
while True:
random_number = randint(1, 14)
print("Show Animation Number :", random_number)
if random_number == 1:
flash_in_different_colors(matrix),
elif random_number == 2:
fade_different_colors(matrix, 10, 2),
elif random_number == 3:
show_different_checkered(matrix),
elif random_number == 4:
fade_pixels_in_and_out(matrix),
elif random_number == 5:
show_pixel_noise(matrix),
elif random_number == 6:
different_color_fadeing(matrix, 20, 5),
elif random_number == 7:
show_sweep_different_colors(matrix, 0.1),
elif random_number == 8:
show_squares(matrix, 0.1, 10)
elif random_number == 9:
show_snakes(matrix, 20, 0.1)
elif random_number == 10:
show_colorful_comets(matrix, 10)
elif random_number == 11:
show_colorful_slides(matrix)
elif random_number == 12:
show_color_swap(matrix)
elif random_number == 13:
matrix.tetris_point_fill([10, 10, 10], 0.1)
elif random_number == 14:
show_tetris_lines(matrix)
if __name__ == '__main__':
main()