/
main_spagLEDi.py
307 lines (235 loc) · 9.4 KB
/
main_spagLEDi.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
# import RPi.GPIO as GPIO
from time import sleep
import networking_may as networking
import random
import math
import pygame
from Colors import Colors
from UIElements.SmallSheep import SmallSheep
from UIElements.BigSheep12 import BigSheep12
from UIElements.BigSheep34 import BigSheep34
from UIElements.Signs import Signs
from Animations_Sheep.SpinningHeadAnimation import SpinningHeadAnimation
from Animations_Sheep.SheepConfettiAnimation import SheepConfettiAnimation
from Animations_Sheep.RainbowAnimation import RainbowAnimation
from Animations_Sheep.FadeInOutAnimation import FadeInOutAnimation
from Animations_Sheep.AlternateAnimation import AlternateAnimation
from Animations_Sheep.SnakeAnimation import SnakeAnimation
from Animations_Sheep.FibonacciAnimation import FibonacciAnimation
from Animations_Sheep.BrokenAnimation import BrokenAnimation
from Animations_Stars.BrokenSignsAnimation import BrokenSignsAnimation
from Animations_Stars.AlwaysOnSignsAnimation import AlwaysOnSignsAnimation
from Animations_Stars.SignsConfettiAnimation import SignsConfettiAnimation
from Animations_Stars.FadeInOutSignsAnimation import FadeInOutSignsAnimation
from Animations_Stars.AlternateSignsAnimation import AlternateSignsAnimation
from Animations_Stars.RainbowSignsAnimation import RainbowSignsAnimation
from Simulator.SheepSimulator import SheepSimulator
########################################################
# consts & current loop data
########################################################
FRAMES_IN_SEC = 50
MAX_DURATION = 1
MIN_DURATION = 0.1
DIFF_DURATION = 0.1
NUM_OF_BITS = 32
current_frame = 0
duration = 0.4
animations = []
faster = False
slower = True
smallSheep = SmallSheep()
bigSheep12 = BigSheep12()
bigSheep34 = BigSheep34()
signs = Signs()
########################################################
# simulator
########################################################
USE_SIMULATOR = True
pygame.init()
simulator = SheepSimulator()
########################################################
# button defs
########################################################
# must divide 5 * NUM_OF_BITS
SLOW_BUTTON_FRAMES = 16
FAST_BUTTON_FRAMES = 8
# GPIO.cleanup()
# GPIO.setmode(GPIO.BCM)
# GPIO.setup(17, GPIO.IN, GPIO.PUD_DOWN)
# GPIO.setup(22, GPIO.IN, GPIO.PUD_DOWN)
# GPIO.setup(4, GPIO.OUT)
# GPIO.setup(27, GPIO.OUT)
# def lit_button1():
# GPIO.output(4, GPIO.HIGH)
# return
# def lit_button2():
# GPIO.output(27, GPIO.HIGH)
# return
# def unlit_button1():
# GPIO.output(4, GPIO.LOW)
# return
# def unlit_button2():
# GPIO.output(27, GPIO.LOW)
# return
def btn1_clicked(arg):
global faster
faster = True
def btn2_clicked(arg):
global slower
slower = True
# GPIO.add_event_detect(17, GPIO.FALLING,
# callback=btn1_clicked, bouncetime=300)
# GPIO.add_event_detect(22, GPIO.FALLING,
# callback=btn2_clicked, bouncetime=300)
########################################################
# animations
########################################################
def config_new_animation():
print "duration - " + str(duration)
global current_frame
current_frame = 0
animationType = random.randrange(1, 8)
if (animationType == 0):
show_spinning_head_animation()
elif (animationType == 1):
show_fade_in_out_animation()
elif (animationType == 2):
show_alternate_animation()
elif (animationType == 3):
show_sheep_confetti_animation()
elif (animationType == 4):
show_rainbow_animation()
elif (animationType == 5):
show_snake_animation()
elif (animationType == 6):
show_broken_animation()
else:
show_fibonacci_animation()
def num_of_blocks(self, original, div):
while original % div != 0:
div = div / 2
return original / div
def show_alternate_animation():
print "alternate"
hue1 = random.random()
hue2 = Colors.reduce_by_1(hue1 + 0.25)
hue3 = Colors.reduce_by_1(hue1 + 0.5)
hue4 = Colors.reduce_by_1(hue1 + 0.75)
global animations
animations = [AlternateAnimation(smallSheep, NUM_OF_BITS / 4, hue2),
AlternateAnimation(bigSheep12, NUM_OF_BITS / 4, hue1),
AlternateAnimation(bigSheep34, NUM_OF_BITS / 4, hue3),
AlternateSignsAnimation(signs, NUM_OF_BITS / 4, hue4)]
def show_fade_in_out_animation():
print "fade_in_out"
hue1 = random.random()
hue2 = Colors.reduce_by_1(hue1 + 0.111)
hue3 = Colors.reduce_by_1(hue1 + 0.222)
hue4 = Colors.reduce_by_1(hue1 + 0.333)
global animations
animations = [FadeInOutAnimation(smallSheep, NUM_OF_BITS / 2, hue2),
FadeInOutAnimation(bigSheep12, NUM_OF_BITS / 2, hue1),
FadeInOutAnimation(bigSheep34, NUM_OF_BITS / 2, hue3),
FadeInOutSignsAnimation(signs, NUM_OF_BITS / 2, hue4)]
def show_sheep_confetti_animation():
print "confetti"
max_leds_per_cycle = 0.05
min_leds_per_cycle = 0.005
diff_in_leds = DIFF_DURATION * (max_leds_per_cycle - min_leds_per_cycle) / (MAX_DURATION - MIN_DURATION)
current_diff = int((MAX_DURATION - duration) / DIFF_DURATION)
leds_per_cycle = min_leds_per_cycle + diff_in_leds * current_diff
global animations
animations = [SheepConfettiAnimation(smallSheep, leds_per_cycle),
SheepConfettiAnimation(bigSheep12, leds_per_cycle),
SheepConfettiAnimation(bigSheep34, leds_per_cycle),
SignsConfettiAnimation(signs, leds_per_cycle)]
def show_rainbow_animation():
print "rainbow"
global animations
animations = [RainbowAnimation(smallSheep, NUM_OF_BITS / 4),
RainbowAnimation(bigSheep12, NUM_OF_BITS / 4),
RainbowAnimation(bigSheep34, NUM_OF_BITS / 4),
RainbowSignsAnimation (signs, NUM_OF_BITS / 4)]
def show_snake_animation():
print "snake"
global animations
animations = [SnakeAnimation(smallSheep, NUM_OF_BITS / 4),
SnakeAnimation(bigSheep12, NUM_OF_BITS / 4),
SnakeAnimation(bigSheep34, NUM_OF_BITS / 4),
SignsConfettiAnimation(signs, 1)]
def show_spinning_head_animation():
print "spinning head"
hue = random.random()
global animations
animations = [SpinningHeadAnimation(smallSheep, hue, NUM_OF_BITS),
SpinningHeadAnimation(bigSheep12, hue, NUM_OF_BITS),
SpinningHeadAnimation(bigSheep34, hue, NUM_OF_BITS)]
def show_fibonacci_animation():
print "fibonacci"
hue1 = random.random()
hue2 = Colors.reduce_by_1(hue1 + 0.111)
hue3 = Colors.reduce_by_1(hue1 + 0.222)
global animations
animations = [FibonacciAnimation(smallSheep, NUM_OF_BITS / 8, hue2),
FibonacciAnimation(bigSheep12, NUM_OF_BITS / 8, hue1),
FibonacciAnimation(bigSheep34, NUM_OF_BITS / 8, hue3),
AlwaysOnSignsAnimation(signs, [30, 30, 30])]
def show_broken_animation():
print "broken"
global animations
animations = [BrokenAnimation(smallSheep, NUM_OF_BITS / 2, 3),
BrokenAnimation(bigSheep12, NUM_OF_BITS / 2, 6),
BrokenAnimation(bigSheep34, NUM_OF_BITS / 2, 6),
BrokenSignsAnimation(signs, NUM_OF_BITS / 2, 3)]
########################################################
# main
########################################################
while True:
current_frame += 1
total_num_of_frames = int(duration * NUM_OF_BITS * FRAMES_IN_SEC)
# animation ended
if (current_frame == total_num_of_frames):
config_new_animation()
# slower animation
if slower:
slower = False
duration = min(MAX_DURATION, duration + DIFF_DURATION)
total_num_of_frames = int(duration * NUM_OF_BITS * FRAMES_IN_SEC)
config_new_animation()
# faster animation
if faster:
faster = False
duration = max(MIN_DURATION, duration - DIFF_DURATION)
total_num_of_frames = int(duration * NUM_OF_BITS * FRAMES_IN_SEC)
config_new_animation()
# apply animations
time_percent = float(current_frame) / total_num_of_frames
for animation in animations:
animation.apply(time_percent)
# send data
if (USE_SIMULATOR):
simulator.draw_LEDs(smallSheep.get_array(),
bigSheep12.get_array(),
bigSheep34.get_array(),
signs.get_array())
else:
networking.send(current_frame,
smallSheep.get_array(),
bigSheep12.get_array(),
bigSheep34.get_array(),
signs.get_array())
# light buttons
slow_on = math.floor(current_frame / float(SLOW_BUTTON_FRAMES)) % 2 == 0
# if slow_on:
# lit_button2()
# else:
# unlit_button2()
# fast_on = math.floor(current_frame / float(FAST_BUTTON_FRAMES)) % 2 == 0
# if fast_on:
# lit_button1()
# else:
# unlit_button1()
for event in pygame.event.get(): # user did something
if event.type == pygame.QUIT:
done = True
sleep(0.02) # 50 frames in seconds