-
Notifications
You must be signed in to change notification settings - Fork 9
/
filtergraphDemo.py
241 lines (215 loc) · 7.09 KB
/
filtergraphDemo.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
import errno
import json
import os
import time
from timeit import default_timer as timer
import jsonpickle
from audioled import configs, devices, filtergraph, audio, runtimeconfiguration, serverconfiguration
num_pixels = 300
device = None
switch_time = 10 # seconds
# define configs (add other configs here)
movingLightConf = 'movingLight'
movingLightsConf = 'movingLights'
spectrumConf = 'spectrum'
vu_peakConf = 'vu_peak'
swimmingConf = 'swimming'
defenceConf = 'defence'
keyboardConf = 'keyboard'
keyboardSpringConf = 'keyboardSpring'
proxyConf = 'proxy'
fallingConf = 'falling'
breathingConf = 'breathing'
heartbeatConf = 'heartbeat'
pendulumConf = 'pendulum'
rpendulumConf = 'rpendulum'
testblobConf = 'testblob'
bonfireConf = 'bonfire'
generatewavesConf = 'generatewaves'
sortingConf = 'sorting'
gifConf = 'gif'
panelConf = 'panel'
oscilloscopeConf = 'oscilloscope'
configChoices = [
movingLightConf,
spectrumConf,
vu_peakConf,
movingLightsConf,
swimmingConf,
defenceConf,
proxyConf,
fallingConf,
breathingConf,
heartbeatConf,
pendulumConf,
rpendulumConf,
keyboardConf,
keyboardSpringConf,
testblobConf,
bonfireConf,
generatewavesConf,
sortingConf,
gifConf,
panelConf,
oscilloscopeConf,
]
deviceRasp = 'RaspberryPi'
deviceCandy = 'FadeCandy'
parser = runtimeconfiguration.commonRuntimeArgumentParser()
# Add specific arguments
deviceChoices = serverconfiguration.ServerConfiguration.getConfigurationParameters().get('device')
parser.add_argument(
'-D',
'--device',
dest='device',
default=devices.FadeCandy.__name__,
choices=deviceChoices,
help='device to send RGB to')
parser.add_argument(
'-C',
'--config',
dest='config',
default='',
choices=configChoices,
help='config to use, default is rolling through all configs')
parser.add_argument(
'-s',
'--save_config',
dest='save_config',
type=bool,
default=False,
help='Save config to config/',
)
args = parser.parse_args()
num_pixels = args.num_pixels
num_rows = args.num_rows
# Initialize device
if args.device == deviceRasp:
device = devices.RaspberryPi(num_pixels, num_rows)
elif args.device == deviceCandy:
device = devices.FadeCandy(num_pixels, num_rows, server=args.device_candy_server)
if args.device_panel_mapping is not None:
mappingFile = args.device_panel_mapping
if os.path.exists(mappingFile):
with open(mappingFile, "r", encoding='utf-8') as f:
content = f.read()
mapping = json.loads(content)
print("Panel mapping loaded")
device = devices.PanelWrapper(device, mapping)
else:
print("Fatal: Cannot find mapping file {}".format(mappingFile))
exit(1)
# Initialize Audio device
if args.audio_device_index is not None:
audio.GlobalAudio.overrideDeviceIndex = args.audio_device_index
# select config to show
config = args.config
print("The following audio devices are available:")
audio.print_audio_devices()
if args.audio_device_index is not None:
globalAudio = audio.GlobalAudio(args.audio_device_index)
else:
globalAudio = audio.GlobalAudio()
def createFilterGraph(config, num_pixels):
print("Creating config {}".format(config))
if config == movingLightConf:
return configs.createMovingLightGraph()
elif config == movingLightsConf:
return configs.createMovingLightsGraph()
elif config == spectrumConf:
return configs.createSpectrumGraph()
elif config == vu_peakConf:
return configs.createVUPeakGraph()
elif config == swimmingConf:
return configs.createSwimmingPoolGraph()
elif config == defenceConf:
return configs.createDefenceGraph()
elif config == keyboardConf:
return configs.createKeyboardGraph()
elif config == keyboardSpringConf:
return configs.createKeyboardSpringGraph()
elif config == proxyConf:
return configs.createProxyServerGraph()
elif config == fallingConf:
return configs.createFallingStarsGraph()
elif config == breathingConf:
return configs.createBreathingGraph()
elif config == heartbeatConf:
return configs.createHeartbeatGraph()
elif config == pendulumConf:
return configs.createPendulumGraph()
elif config == rpendulumConf:
return configs.createRPendulumGraph()
elif config == testblobConf:
return configs.createTestBlobGraph()
elif config == bonfireConf:
return configs.createBonfireGraph()
elif config == generatewavesConf:
return configs.createGenerateWavesGraph()
elif config == sortingConf:
return configs.createSortingGraph()
elif config == gifConf:
return configs.createGifPlayerGraph()
elif config == panelConf:
return configs.createPanelPendulum()
elif config == oscilloscopeConf:
return configs.createOscilloscope()
else:
raise NotImplementedError("Config not implemented")
def saveAndLoad(config, fg):
if (args.save_config):
# save filtergraph to json
filename = "configs/{}.json".format(config)
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
saveJson = jsonpickle.encode(fg)
temp = json.loads(saveJson)
saveJson = json.dumps(temp, sort_keys=True)
with open(filename, "w") as f:
f.write(saveJson)
# load filtergraph from json in case there are any issues with saving/loading
fg = jsonpickle.decode(saveJson)
return fg
current_time = timer()
count = 0
totalTiming = filtergraph.Timing()
config_idx = 0
last_switch_time = current_time
cur_graph = None
if args.config == '':
cur_graph = createFilterGraph(configChoices[config_idx], num_pixels)
else:
cur_graph = createFilterGraph(args.config, num_pixels)
saveAndLoad(args.config, cur_graph)
cur_graph.propagateNumPixels(num_pixels, num_rows)
while True:
last_time = current_time
current_time = timer()
dt = current_time - last_time
if args.config == '' and current_time - last_switch_time > switch_time:
# switch configuration
print('---switching configuration---')
config_idx = (config_idx) % len(configChoices)
cur_graph = createFilterGraph(configChoices[config_idx], num_pixels)
cur_graph = saveAndLoad(configChoices[config_idx], cur_graph)
cur_graph.propagateNumPixels(num_pixels)
config_idx = config_idx + 1
last_switch_time = current_time
cur_graph.update(dt)
cur_graph.process()
totalTiming.update(timer() - current_time)
if cur_graph.getLEDOutput() is not None and cur_graph.getLEDOutput()._outputBuffer[0] is not None:
device.show(cur_graph.getLEDOutput()._outputBuffer[0])
if count == 100:
cur_graph.printProcessTimings()
cur_graph.printUpdateTimings()
print(totalTiming.__dict__)
count = 0
count = count + 1
if dt < 0.015:
sleeptime = 0.015 - dt
time.sleep(sleeptime)