-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.py
More file actions
279 lines (224 loc) · 8.74 KB
/
rule.py
File metadata and controls
279 lines (224 loc) · 8.74 KB
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
import enum, time, math, colorsys
def zero_to_one(num):
"""
Ensure a number is between 0 and 1 by adding or subtracting 1.
:param num: The number to normalize.
:return: A number between 0 and 1.
"""
while num < 0:
num += 1
while num > 1:
num -= 1
return num
class Mode(enum.Enum):
"""
Different modes for color functions - determines whether colors change based on pixels or time.
"""
PIXEL = 'pixel'
TIME = 'time'
class Rule:
"""
Class passable to Segments and LightStrips that determines LED colors at runtime.
"""
def __init__(self):
self.func_chain = [] # Every time a new function is generated, append it here.
def __call__(self, **kwargs):
"""
Evaluate this rule based on kwargs.
:param kwargs: Properties needed to determine color, e.g. led index, segment size, etc.
:return: The generated color.
"""
if len(self.func_chain) == 0:
return 0, 0, 0
return self.func_chain[-1](**kwargs)
# Primary rules - these generate colors and don't modify any functions.
def fill(self, color, start=None, end=None):
"""
Set LEDs in a range to the same color.
:param color: The color to set all pixels to.
:param start: The first pixel to modify (inclusive, optional).
:param end: The last pixel to modify (exclusive, optional).
"""
def f(**kwargs):
if start is None or end is None or start <= kwargs['pixel'] < end:
return color
return 0, 0, 0
self.func_chain.append(f)
return self
def hue_linear(self, frequency=1, mode=Mode.PIXEL):
"""
Fill pixels with color hue increasing with every pixel.
:param frequency: How fast hue should increase.
:param mode: Color determination mode - either pixel or time.
"""
start_time = time.time()
def f(**kwargs):
# Based on mode, determine the independent variable (i.e. what changes the color).
var = 0
if mode == Mode.PIXEL:
var = kwargs['pixel']
elif mode == Mode.TIME:
var = time.time() - start_time
else:
raise RuntimeError("Mode", mode, "is invalid for Rule hue_linear().")
hue = var * frequency / 360
hue = zero_to_one(hue)
rgb = colorsys.hsv_to_rgb(hue, 1, 1)
return tuple(round(c * 255) for c in rgb)
self.func_chain.append(f)
return self
def hue_wave(self, low_hue, high_hue, frequency=1, mode=Mode.PIXEL):
"""
Generate a rainbow sine wave ranging from low_hue to high_hue.
:param low_hue: The low hue value (0 thru 360)
:param high_hue: The high hue value (0 thru 360)
:param frequency: How often waves should appear (inverse of wavelength).
:param mode: Color determination mode - either pixel or time.
"""
start_time = time.time()
def f(**kwargs):
# Based on mode, determine the independent variable (i.e. what changes the color).
var = 0
if mode == Mode.PIXEL:
var = kwargs['pixel']
elif mode == Mode.TIME:
var = time.time() - start_time
else:
raise RuntimeError("Mode", mode, "is invalid for Rule hue_wave().")
mid = ((high_hue + low_hue) / 2) / 360
amplitude = (high_hue - low_hue) / 720
hue = mid + amplitude * math.sin(var * frequency)
# Ensure hue is between 0 and 1
hue = zero_to_one(hue)
# Generate RGB value from hue
rgb = colorsys.hsv_to_rgb(hue, 1, 1)
return tuple(round(c * 255) for c in rgb)
self.func_chain.append(f)
return self
def stripes(self, colors, width):
"""
Generate stripes with alternating colors.
:param colors: The colors to choose from.
:param width: The width of each stripe.
"""
def f(**kwargs):
return colors[(kwargs['pixel'] // width) % len(colors)]
self.func_chain.append(f)
return self
# Secondary rules - modify the existing Rule.
def animate(self, speed):
"""
Animate the original function, causing it to move over time.
:param speed: The speed at which the function should move.
"""
start_time = time.time()
last_func = self.get_last_func()
def f2(**kwargs):
kwargs['pixel'] -= round((time.time() - start_time) * speed)
return last_func(**kwargs)
self.func_chain.append(f2)
return self
def blink(self, time_on, time_off, start_time=None):
"""
Spend time_on with function enabled and time_off black, alternating.
:param time_on: Time (s) with function enabled.
:param time_off: Time (s) with lights off.
:param start_time: Start time override (s).
"""
if start_time is None:
start_time = time.time()
last_func = self.get_last_func()
def f2(**kwargs):
time_elapsed = time.time() - start_time
time_since_blink = time_elapsed % (time_on + time_off)
if time_since_blink < time_on:
return last_func(**kwargs)
else:
return 0, 0, 0
self.func_chain.append(f2)
return self
def crop(self, first=None, last=None):
"""
Only show the pixels within a specified range.
:param first: First pixel in the range (inclusive, optional)
:param last: Last pixel in the range (exclusive, optional)
"""
last_func = self.get_last_func()
def f2(**kwargs):
if first is not None and kwargs['pixel'] < first:
return 0, 0, 0
if last is not None and kwargs['pixel'] >= last:
return 0, 0, 0
return last_func(**kwargs)
self.func_chain.append(f2)
return self
def fade_in(self, fade_time, delay):
"""
Fade from black to the function color.
:param fade_time: How long the fade effect should take.
:param delay: Time in seconds to wait to fade in.
"""
start_time = time.time()
last_func = self.get_last_func()
def f2(**kwargs):
time_elapsed = time.time() - start_time
if time_elapsed < delay:
return 0, 0, 0
full_color = last_func(**kwargs)
if fade_time == 0 or time_elapsed > fade_time + delay:
return full_color
percent = (time.time() - delay - start_time) / fade_time
new_color = tuple(round(c * percent) for c in full_color)
return new_color
self.func_chain.append(f2)
return self
def fade_out(self, fade_time, delay):
"""
Fade from the function color to black.
:param fade_time: How long the fade effect should take.
:param delay: Time in seconds to wait to fade out.
"""
start_time = time.time()
last_func = self.get_last_func()
def f2(**kwargs):
time_elapsed = time.time() - start_time
if time_elapsed > fade_time + delay:
return 0, 0, 0
full_color = last_func(**kwargs)
if time_elapsed < delay:
return full_color
percent = 1 - ((time.time() - delay - start_time) / fade_time)
new_color = round(full_color[0] * percent), round(full_color[1] * percent), round(full_color[2] * percent)
return new_color
self.func_chain.append(f2)
return self
def flip(self):
"""
Flip the original function, so the last pixel is in the place of the first pixel, etc.
"""
last_func = self.get_last_func()
def f2(**kwargs):
kwargs['pixel'] = kwargs['seg_size'] - kwargs['pixel']
return last_func(**kwargs)
self.func_chain.append(f2)
return self
def offset(self, pixels):
"""
Shift this Rule by a certain number of pixels.
:param pixels: The number of pixels to shift.
"""
last_func = self.get_last_func()
def f2(**kwargs):
kwargs['pixel'] += pixels
return last_func(**kwargs)
self.func_chain.append(f2)
return self
# Others - fade, ripple, etc.
# Miscellaneous functions
def get_last_func(self):
"""
:return: The last function in the function chain.
"""
if len(self.func_chain) == 0:
raise RuntimeError("Tried to call modifier function on Rule without defining the rule first.")
return self.func_chain[-1]