-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanimation.py
272 lines (225 loc) · 9.46 KB
/
animation.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
""" The various classes relating to animations """
from __future__ import print_function
from __future__ import division
import math
from random import randint
import time
class Animation(object):
"""The base class for any animation. An animation is a state machine that produces frames.
Keep calling get_frame until is_complete returns true.
"""
def __init__(self, milliseconds=None):
""" Animation base class initializer
Parameters
----------
milliseconds : function, optional
Optional function that returns the number of milliseconds elapsed. A default implementation
is provided that uses the system time. Provide an alternative function if time must be
externally controlled. This is useful in unit testing scenarios.
"""
object.__init__(self)
if milliseconds is None:
self._milliseconds = lambda: int(round(time.time() * 1000))
else:
self._milliseconds = milliseconds
def get_frame(self):
"""Return an array of tuples (r,g,b) i.e. the frame for the current state of the animation"""
raise RuntimeError("GetFrame should only be called in derived classes")
def is_complete(self):
"""True if this animation is complete and can be removed"""
return False
class LeftRightAnimation(Animation):
""" Light runs side to side """
def __init__(self, milliseconds, length, frames_per_second):
"""
Parameters
----------
length : integer
The number of lights in the animation
"""
Animation.__init__(self, milliseconds)
self._ = [length]
self._index = 0
self._last_frame_time = 0
self._milliseconds_per_frame = 1000 / frames_per_second
self._direction = 1
def get_frame(self):
now = self._milliseconds()
if now - self._last_frame_time > self._milliseconds_per_frame:
self._last_frame_time = now
self._index += self._direction
leds = [(0, 0, 0)] * 88
return self._lights
class FireAnimation(Animation):
""" Fire animation """
def __init__(self):
Animation.__init__(self)
self._lights = []
self._r = 255
self._g = 255 - 40
self._b = 40
self._end = 0
def get_frame(self):
if self._milliseconds() > self._end:
self._end = self._milliseconds() + 50
self._lights = []
for _ in range(0, 88):
flicker = randint(0, 150)
r1 = self._r - flicker
g1 = self._g - flicker
b1 = self._b - flicker
if r1 < 0:
r1 = 0
if g1 < 0:
g1 = 0
if b1 < 0:
b1 = 0
self._lights.append((r1, g1, b1))
return self._lights
def is_complete(self):
return False
class BeatAnimation(Animation):
"""Lights up on a specific beat"""
def __init__(self, width, bpm, milliseconds=None):
Animation.__init__(self, milliseconds=None)
self._ms_per_beat = 1000 / (bpm / 60)
self._current_pulse_time = self._milliseconds()
self._width = width
self._bell_curve = BeatAnimation.create_bell_curve(width)
@classmethod
def create_bell_curve(cls, width):
""" Create a bell curve with width elements and values between 0 and 1.
The standard deviation value of 0.4 means the curve will peak at 1.
Starting at around -1.5 to 1.5 gives a nice ramp up and down."""
# Need to step between -1.5 and 1.5
step = 2.0 / width
x = -1
curve = []
for _ in range(width):
curve.append(cls.normpdf(x, 0, 0.4))
x += step
return curve
def get_frame(self):
now = self._milliseconds()
time_into_beat = now % self._ms_per_beat
# Brightness decreases as time progresses into the beat. Adjust to an interval between 0-255.
brightness = int((self._ms_per_beat - time_into_beat) / self._ms_per_beat * 256)
pixels = [(brightness, brightness, brightness)] * self._width
# Apply a bell curve to pixels - this avoids all of them lighting up and instead a soft ramp up and ramp down
for i, (r, g, b) in enumerate(pixels):
factor = self._bell_curve[i]
pixels[i] = (int(r * factor), int(g * factor), int(b * factor))
return pixels
def is_complete(self):
return False
@classmethod
def normpdf(cls, x, mean, standard_deviation):
""" Calculates the normal distribution using a probability density function
Found it here https://stackoverflow.com/a/12413491 """
var = float(standard_deviation)**2
pi = 3.1415926
denom = (2*pi*var)**.5
num = math.exp(-(float(x)-float(mean))**2/(2*var))
return num/denom
class LightUpAnimation(Animation):
"""Simple animation that lights up the given key for a short period of time"""
def __init__(self, keys, key_pressed, duration=1000, milliseconds=None):
"""Initializes a new LightUpAnimation instance"""
Animation.__init__(self)
self._end = self._milliseconds() + duration
self._leds = [(0, 0, 0)] * keys
self._key_pressed = key_pressed
def get_frame(self):
"""Return an array of integers representing the current state of the animation"""
self._leds[self._key_pressed] = (255, 255, 255)
return self._leds
def is_complete(self):
"""True if this animation is complete and can be removed"""
return self._milliseconds() > self._end
class PressureKeyPressAnimation(LightUpAnimation):
"""Simple animation that happens when you press a key. It is pressure sensitive."""
def __init__(self, keys, key_pressed, velocity, duration=1000, milliseconds=None):
"""Initializes a new PressureKeyPressAnimation instance"""
LightUpAnimation.__init__(self, keys, key_pressed, duration, milliseconds)
self._velocity = velocity
self._duration = duration
def get_key_color(self):
"""Returns the color of the key pressed during animation. Override to change."""
return (self._velocity, self._velocity, self._velocity)
def get_frame(self):
"""Return an array of integers representing the current state of the animation."""
time_left = self._end - self._milliseconds()
if time_left < 0:
time_left = 0
r, g, b = self.get_key_color()
fade_factor = time_left / self._duration
r = int(r * fade_factor)
g = int(g * fade_factor)
b = int(b * fade_factor)
self._leds[self._key_pressed] = (r, g, b)
return self._leds
class RunLeftAnimation(Animation):
"""Simple animation that happens when you press a key"""
def __init__(self, key_pressed):
"""Initializes a new KeyPressAnimation instance"""
Animation.__init__(self)
self._key_pressed = key_pressed
r = randint(0, 3)
if r == 0:
self._color = (255, 0, 0)
if r == 1:
self._color = (255, 255, 255)
if r == 2:
self._color = (255, 255, 0)
if r == 3:
self._color = (0, 255, 255)
def get_frame(self):
"""Return an array of integers representing the current state of the animation"""
leds = [(0, 0, 0)] * 88
leds[self._key_pressed] = self._color
self._key_pressed -= 1
return leds
def is_complete(self):
"""True if this animation is complete and can be removed"""
return self._key_pressed < 0
class ChristmasKeyPressAnimation(PressureKeyPressAnimation):
"""Simple animation that happens when you press a key. It is pressure sensitive."""
def __init__(self, keys, key_pressed, velocity, duration=1000):
"""Initializes a new ChristmasKeyPressAnimation instance"""
PressureKeyPressAnimation.__init__(self, keys, key_pressed, velocity, duration)
color = randint(0, 2)
if color == 0:
self._color = (velocity, 0, 0)
if color == 1:
self._color = (0, velocity, 0)
if color == 2:
self._color = (velocity, velocity, velocity)
def get_key_color(self):
"""Overrides the default and return a Christmasey color"""
return self._color
class RunningAnimation(Animation):
"""Animation the moves each time a key is pressed"""
def __init__(self, keys, milliseconds=None):
Animation.__init__(self, milliseconds)
# This will hold the active key animations
self._animations = []
self._keys = keys
self._key = 0
def key_pressed(self, velocity):
"""Call whenever a key is pressed"""
self._animations.append(PressureKeyPressAnimation(self._keys, self._key, velocity, 1000, self._milliseconds))
self._key = self._key + 1
self._key = self._key % self._keys
def get_frame(self):
leds = [(0, 0, 0)] * self._keys
for current_animation in self._animations:
new_frame = current_animation.get_frame()
for i, frame_pixel in enumerate(new_frame):
r, g, b = (frame_pixel)
if r > 0 or g > 0 or b > 0:
leds[i] = frame_pixel
# Remove keys that are complete
self._animations = [x for x in self._animations if not x.is_complete()]
return leds
def is_complete(self):
return False