-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimationController.py
executable file
·227 lines (185 loc) · 7.68 KB
/
animationController.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
#!/usr/local/bin/python3.4
# -*- coding: utf-8 -*-
# definde import paths
import sys
sys.path.append('animations')
sys.path.append('system')
# import python libs
from threading import Thread
# import project libs
from ambiGridController import DeviceController
from colorController import ColorController
# import animations
from randomGlow import RandomGlowAnimation
from pulsingCircle import PulsingCircleAnimation
from monoColor import MonoColor
from monoPixel import MonoPixel
from colorChange import ColorChange
from fadeOut import FadeOutAnimation
# constants
AUTO_STATUS_UPDATE_RATE = 5 # updates per second
class LightAnimation(Thread):
def __init__(self, beVerbose, showFPS):
# initializations
self.beVerbose = beVerbose
self.device = DeviceController(beVerbose, showFPS)
self.webSocketHandler = []
self.colors = ColorController
self.colors.setDeviceReference(self.device)
self.currentAnimation = ''
self.autoStatusCounter = 0
self.autoUpdateTreshold = 10
self.sentAutoUpdates = 0
# initialize animations
self.fadeOutAnimation = FadeOutAnimation(self)
self.monoColorAnimation = MonoColor(self.device)
self.monoPixelAnimation = MonoPixel(self.device)
self.colorChangeAnimation = ColorChange(self.device)
self.randomGlowAnimation = RandomGlowAnimation(self.device)
self.pulsingCircleAnimation = PulsingCircleAnimation(self.device)
# set animation mode
self.showFadeOut = False
self.showMonoColor = False
self.showMonoPixel = False
self.showColorChange = False
self.showRandomGlow = False
self.showPulsingCircle = False
# start the pulsing circle animation
self.showAnimation({'name': 'colorChange'})
# instantiate as thread
Thread.__init__(self)
def run(self):
# run light animations
try:
while True:
# modify frame buffer with animation
if self.showFadeOut:
self.fadeOutAnimation.renderNextFrame()
if self.showMonoColor:
self.monoColorAnimation.renderNextFrame()
if self.showMonoPixel:
self.monoPixelAnimation.renderNextFrame()
if self.showColorChange:
self.colorChangeAnimation.renderNextFrame()
if self.showRandomGlow:
self.randomGlowAnimation.renderNextFrame()
if self.showPulsingCircle:
self.pulsingCircleAnimation.renderNextFrame()
# apply buffer to AmbiGrid
self.device.writeBuffer()
# update status at connected clients
self.autoUpdateStatusDetails()
except (KeyboardInterrupt):
print('\nreceived KeyboardInterrupt; closing connection')
self.device.closeConnection()
def prepareAnimations(self):
# prepare color change
if self.showColorChange:
self.colorChangeAnimation.start()
# prepare random glow
if self.showRandomGlow:
self.randomGlowAnimation.start()
# perpare pulsing circle
if self.showPulsingCircle:
self.pulsingCircleAnimation.start()
def stopAnimation(self, animationObj):
# stop showing fadeOut-animation
if isinstance(animationObj, FadeOutAnimation):
self.showFadeOut = False
def autoUpdateStatusDetails(self):
if self.autoStatusCounter < self.autoUpdateTreshold:
self.autoStatusCounter = self.autoStatusCounter + 1
return
self.autoStatusCounter = 0
framesPerUpdate = self.device.getCurrentFps() / AUTO_STATUS_UPDATE_RATE
self.autoUpdateTreshold = int(framesPerUpdate)
if len(self.webSocketHandler) > 0:
if self.sentAutoUpdates >= AUTO_STATUS_UPDATE_RATE:
self.sentAutoUpdates = 0
currentStatus = self.getAllStati()
else:
self.sentAutoUpdates = self.sentAutoUpdates + 1
currentStatus = self.getStatusDetails()
for wsHandler in self.webSocketHandler:
wsHandler.sendDictionary(currentStatus)
# ***** animation control **********************************
def stopFadeOut(self):
self.showFadeOut = False
return self.getStatus()
# ***** getter **********************************
def getDevice(self):
return self.device
def getStatus(self):
hue, saturation, lightness = self.colors.getBasisColorAsHsl()
redChannel, greenChannel, blueChannel = self.colors.getBasisColorAsRgb()
statusDictionary = {
'status': self.currentAnimation,
'baseHexColor': self.colors.getBasisColorAsHex(),
'baseColorRed': redChannel,
'baseColorGreen': greenChannel,
'baseColorBlue': blueChannel,
'baseColorHue': hue,
'baseColorSaturation': saturation,
'baseColorLightness': lightness,
'update': 'status'
}
secondsToFadeOut = self.fadeOutAnimation.getSecondsToFadeOut()
if secondsToFadeOut >= 0:
statusDictionary['fadeOutIn'] = secondsToFadeOut
return statusDictionary
def getAllStati(self):
statusDictionary = self.getStatus()
statusDictionary.update(self.getStatusDetails())
statusDictionary['animations'] = {
'colorChange': self.colorChangeAnimation.getAttributes(),
'pulsingCircle': self.pulsingCircleAnimation.getAttributes(),
'randomGlow': self.randomGlowAnimation.getAttributes()
}
statusDictionary['update'] = 'all'
return statusDictionary
def getStatusDetails(self):
statusDictionary = {
'currentLightness': self.colors.getTotalLightness(),
'currentFPS': self.device.getCurrentFps(),
'update': 'details'
}
secondsToFadeOut = self.fadeOutAnimation.getSecondsToFadeOut()
if secondsToFadeOut >= 0:
statusDictionary['fadeOutIn'] = secondsToFadeOut
return statusDictionary
# ***** setters **********************************
def setFadeOut(self, seconds = 10):
self.fadeOutAnimation.secondsToFadeOut = seconds
self.showFadeOut = True
self.fadeOutAnimation.start()
def setWebSocketHandler(self, wsHandler):
self.webSocketHandler.append(wsHandler)
def unsetWebSocketHandler(self, wsHandler):
try:
self.webSocketHandler.remove(wsHandler)
except ValueError:
if self.verbose:
print('failed to unset web socket handler; not registered')
def unsetAnimation(self):
self.showMonoColor = False
self.showColorChange = False
self.showRandomGlow = False
self.showPulsingCircle = False
self.showMonoPixel = False
def showAnimation(self, attributes):
self.unsetAnimation()
if attributes['name'] == 'monoColor':
self.showMonoColor = True
elif attributes['name'] == 'monoPixel':
self.showMonoPixel = True
elif attributes['name'] == 'colorChange':
self.showColorChange = True
self.colorChangeAnimation.setAttributes(attributes)
elif attributes['name'] == 'pulsingCircle':
self.showPulsingCircle = True
self.pulsingCircleAnimation.setAttributes(attributes)
elif attributes['name'] == 'randomGlow':
self.showRandomGlow = True
self.randomGlowAnimation.setAttributes(attributes)
self.prepareAnimations()
self.currentAnimation = attributes['name']