-
Notifications
You must be signed in to change notification settings - Fork 2
/
brain.py
executable file
·474 lines (417 loc) · 18.4 KB
/
brain.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/python
import Queue # thread safe
import numpy, select, socket, string, struct
from shows import *
from beats import *
import music
from artCarHandler import ArtCarHandler
from internalAnimationsHandler import InternalAninamtionsHandler
BRAIN_DEBUG = True
artCarHandler = ArtCarHandler(ART_CAR_HELLO_DURATION, ART_CAR_AMPLITUDE_THRESHOLD, ART_CAR_MIN_HELLO_DURATION)
mega_to_node_map = {
5: 1,
6: 2,
3: 3,
2: 4,
4: 5,
7: 0,
}
# close all TCP connections and continue to run
def do_disconnect(ignored, neglected):
global listener, sources # , writable, oops
copy_of_sources = [] + sources # array elements will be removed from sources itself
for s in copy_of_sources:
if s is not sys.stdin and s is not listener:
disconnect(s, 'closing')
# writable = []
# oops = []
def do_list(ignored, neglected):
global listener, sources
for s in sources:
if s is not sys.stdin:
if s is listener:
print 'listening on %s:%d' % s.getsockname()
else:
print 'connected to', remote_name[s]
def do_quit(ignored, neglected):
global running
running = False
# send a message to one remote or all
def do_send(socket, message):
global message_queues, writing
#print 'sending', repr(message)
if socket and socket != 'send':
list = [socket]
else:
list = message_queues
for s in list:
message_queues[s].put(message)
if s not in writing:
writing.append(s)
def do_auto(ignored, show_name):
global auto_show, last_show_change_sec
auto_show = show_name
if auto_show:
auto_show(True)
last_show_change_sec = time.time()
def do_show(cmd, param):
global last_show_change_sec, show_colors, show_parameters
if param:
if cmd == 'SetAnimation':
show_parameters[0] = (ord(param) - ord('0')) % NUM_ANIMATIONS
last_show_change_sec = time.time()
show_colors_list = []
for i in range(0, NUM_COLORS_PER_PALETTE):
show_colors_list += show_colors[i]
do_send(None, struct.pack('>c%uB' % (len(show_parameters) + len(show_colors_list)), 's', *(show_parameters + show_colors_list)))
print_parameters()
next_timesync_sec = 0.0
# almost the same as do_send() above, but send the very latest
# timestamp to each remote or just the specified remote
def do_time(socket, neglected):
global message_queues, next_timesync_sec, writing
if socket == 'time':
list = message_queues
next_timesync_sec = time.time() + 31 # half a minute in the future
else:
list = [socket]
for s in list:
# send microseconds, which is all the precision we have
# network byte order, a character and 64-bit unsigned integer
message_queues[s].put(struct.pack('>cQ', 't', int(time.time() * 1000000.0)))
if s not in writing:
writing.append(s)
# a command and optional 8-bit unsigned integer
def do_simple(cmd, param):
if param:
do_send(None, struct.pack('>cB', cmd[0:1], ord(param) - ord('0')))
else:
do_send(None, cmd[0:1])
#play mediation manually
def do_meditation (ignored, meditation_num):
meditation_play = music.manual_meditation(meditation_num)
if meditation_play:
do_send(None, encapsulated_audio_msg(meditation_play))
def do_gain(ignored, g):
try:
gain = int(g)
if gain < -70 or 10 < gain:
raise ValueError
except (TypeError, ValueError): # non-integer or out of range
print 'Usage: gain num (-70 to 10)'
return
do_send(None, struct.pack('>cb', 'g', gain))
def do_change_palette(ignored, style):
if style == None:
choose_new_playa_palette()
else:
use_test_palette()
def stop_auto(ignore, neglect):
global auto_show
auto_show = None
control_messages = {
# 'SetAllAudio': do_unimplemented,
# 'SetAudioCh': do_unimplemented,
# 'SetVolCh': do_unimplemented,
# 'MuteAllAudio': do_unimplemented,
'SetAnimation': (do_show, None, None),
# 'AllLEDoff': (do_simple, 'program', '0'),
# 'CheckHandStat': do_unimplemented,
# 'CheckAudioIn': do_unimplemented,
'cp': (do_change_palette, None, None),
'day': (do_date, None, None),
'disconnect': (do_disconnect, None, None),
'edm': (do_auto, None, edm_program),
'gain': (do_gain, None, None),
'initialize': (do_simple, None, None),
'Initialize': (do_simple, None, None),
'led': (do_simple, 'program', None),
'list': (do_list, None, None),
'node': (do_simple, None, None),
'og': (do_auto, None, old_globe_program),
'pause': (do_auto, None, None),
'playa': (do_auto, None, playa_program),
'quit': (do_quit, None, None),
'reconnect': (do_simple, None, None),
'send': (do_send, None, None),
'sp': (do_set_show_parameter, None, None),
'time': (do_time, None, None),
'tp': (do_change_palette, None, 'test'),
'meditation': (do_meditation, None, None),
'stopAuto': (stop_auto, None, None)
}
# listen for TCP connections on the specified port
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # disregard TIME_WAIT state
listener.setblocking(0) # don't wait for anything
while listener:
try:
listener.bind(('', 3528)) # any local IPv4 address, port 3528
break
except:
if BRAIN_DEBUG:
print sys.exc_value
time.sleep(10)
listener.listen(6) # maximum connection backlog
sources = []
if len(sys.argv) <= 1: # interactive when no command line parameters
sources += [sys.stdin]
else: # noninteractive, run Old Globe program
do_auto(None, old_globe_program)
do_date(None, ' '.join(sys.argv[1:]))
sources += [listener] # read from these
writing = [] # write to these sockets
message_queues = {} # queues of outgoing messages
remote_name = {} # socket remote name because getpeername() can fail
# print a message and clean up resources associated with an open TCP connection
def disconnect(socket, msg):
print msg, remote_name[socket]
if socket in writing:
writing.remove(socket)
sources.remove(socket)
socket.close() # shutdown() is too abrupt, do a graceful close()
del message_queues[socket]
del remote_name[socket]
# insert a length byte into an audio message
def encapsulated_audio_msg(audio_msg):
size = len(audio_msg) - 1 # don't count the 'a' at the beginning
msg = struct.pack('>cB', audio_msg[0], size) + audio_msg[1:]
return msg
def get_external_amplitude_sum(channel_data):
amplitude = 0
# 7 channels of audio data
for i in range(0, 6):
try:
amplitude += ord(channel_data[i+7])
except:
if BRAIN_DEBUG:
print "channel data format does not have 2 * 7 channels of information " + str(len(channel_data))
return -1
return amplitude
# check art car detection status and change animation accordingly
def check_art_car_status(ring_num, amplitude):
global last_show_change_sec
if ring_num is None or amplitude is None:
if BRAIN_DEBUG:
#print "seems as though the data did not make a plane, ring -> art car detection not possible"
pass
if not artCarHandler.mock:
return
ring_ac_newly_detected = artCarHandler.handle_amplitude_info(ring_num, amplitude)
if BRAIN_DEBUG:
print "ring detected " + str(ring_ac_newly_detected)
print "art car ring " + str(artCarHandler.art_car)
if ring_ac_newly_detected is None:
# artcar total structure animation was running now stop
get_internal_animations_handler().set_do_animations(True)
if BRAIN_DEBUG:
print "stopping art car edm takeover animations"
do_auto(None, playa_program)
do_show(None, None)
elif ring_ac_newly_detected == artCarHandler.art_car and ring_ac_newly_detected != -1:
# return value signaling ART_CAR_HELLO_DURATION exceeded - trigger edm animations
get_internal_animations_handler().set_do_animations(False)
# trigger edm animations on whole structure if time has past time
if time.time() > last_show_change_sec + TIME_LIMIT:
auto_show()
last_show_change_sec = time.time()
do_show(None, None)
do_auto(None, art_car_edm)
do_show(None, None)
elif ring_ac_newly_detected >= 0:
# Utilizing ArtCarHadler to know which is the oldest ring_num.
# Necessary becuase it sounds like the messaging system will be a broadcast
# with every node getting the 'hello' ring. (Not just a response to whoever sent the cahnnel data)
oldest_ring = artCarHandler.get_oldest_ring()
# check if the oldest is past the min hello duration
if artCarHandler.get_detected_duration(oldest_ring) > artCarHandler.get_min_hello_duration():
# send the hello animation broadcast with target ring
show_parameters[ART_CAR_RING_PARAM] = oldest_ring
show_parameters[SEVEN_PAL_BEAT_PARAM_START] = artCarHandler.get_ring_animation(oldest_ring)
do_show(None, None)
else:
# no art car detected past threshold tell any rings playing hello animations to stop
# send hello animation stop message broadcast for target ring
for ring_num_stop in artCarHandler.rings_to_stop_hello_animation:
show_parameters[ART_CAR_RING_PARAM] = ring_num_stop
show_parameters[SEVEN_PAL_BEAT_PARAM_START] = 0
do_show(None, None)
if BRAIN_DEBUG:
do_list(None, None)
print sorted(control_messages.keys())
mega_music = music.Music()
running = True
while running:
try:
if len(message_queues) == 0:
timeout = None # wait indefinitely when there are no remotes
else:
timeout = next_timesync_sec - time.time()
if auto_show:
show_timeout = last_show_change_sec + TIME_LIMIT - time.time()
if show_timeout < timeout:
timeout = show_timeout
if timeout <= 0:
timeout = 0.01
# could generate writing list here from nonempty message_queues
readable, writable, oops = select.select(sources, writing, sources, timeout)
for s in readable:
if s is sys.stdin:
message = s.readline().strip()
try:
command, parameters = string.split(message, None, 1) # one word separated by whitespace from the parameter(s)
except ValueError: # no whitespace
command = message
parameters = None
try:
action = control_messages[command]
function = action[0]
if action[1]:
command = action[1]
if action[2]:
parameters = action[2]
function(command, parameters)
except KeyError:
print sorted(control_messages.keys()) # unrecognized
elif s is listener:
remote, addr = s.accept()
remote.setblocking(0)
sources.append(remote) # remember this connection
message_queues[remote] = Queue.Queue() # create outgoing FIFO queue
remote_name[remote] = '%s:%d' % addr # addr is the same as remote.getpeername()
if BRAIN_DEBUG:
print 'connection from', remote_name[remote]
do_time(remote, None); # synchronize time immediately
else:
try:
message = s.recv(102400)
except:
if BRAIN_DEBUG:
print sys.exc_value
message = None
if message:
while len(message) > 0:
if message[0:1] == 'b':
if len(message) >= 55:
do_send(None, message) # relay to all nodes
if BRAIN_DEBUG:
print 'beat message', repr(message), 'from', remote_name[s]
message = message[55:]
else:
if BRAIN_DEBUG:
print 'beat message expected 55 but has', len(message), 'bytes'
break
elif message[0:1] == 'c':
if len(message) >= 24:
if False:
node, timestamp, channel_data = struct.unpack_from('>BQ14s', message, 1)
timestamp /= 1000.0 # convert from milliseconds
#print 'node', node, 'channel data', repr(channel_data), 'at', timestamp
amplitude_sum = get_external_amplitude_sum(channel_data)
if amplitude_sum > 0: # otherwise error reading channel_data
esitmated_ring_number, mean_intensity = analyze_beat(node, amplitude_sum, timestamp)
show_mode = get_show_mode()
if show_mode == 1 or show_mode == 3: # not during meditaiton
check_art_car_status(esitmated_ring_number, mean_intensity)
message = message[24:]
else:
if BRAIN_DEBUG:
print 'channel message expected 24 but has', len(message), 'bytes'
break
elif message[0:1] == 'm':
if len(message) >= 3:
mega_number = ord(message[1:2])
try:
node_number = mega_to_node_map[mega_number]
except KeyError:
if mega_number >= 100: # mock_mega
node_number = mega_number % 6 + 10
else:
node_number = None
if BRAIN_DEBUG:
print 'mega', mega_number, '( node ', repr(node_number), 'switches', repr(ord(message[2:3])), ') is at', remote_name[s]
if node_number != None:
remote_name[s] = 'node %u' % node_number
do_send(s, struct.pack('>cB', 'n', node_number))
message = message[3:]
else:
if BRAIN_DEBUG:
print 'mega message expected 3 but has', len(message), 'bytes'
break
elif message[0:1] == 's':
msg = message[0:2]
if message[1:2] == 'N':
size = -1 # no length byte
else: # remove the length byte for local processing
size = ord(message[2:3])
msg += message[3:3+size]
# print 'tsunami says:', repr(msg), 'from', remote_name[s]
if music.status_update(msg):
mega_music.need_drone = True
message = message[3+size:]
else:
if BRAIN_DEBUG:
print 'received unknown', repr(message[0:60]), '... (', len(message), 'bytes) from', remote_name[s]
pos = message.find('c')
if pos < 0:
pos = len(message) # discard all
if BRAIN_DEBUG:
print 'discarding', pos, 'bytes of input', repr(message[0:pos]), 'and continuing'
message = message[pos:]
if BRAIN_DEBUG and len(message) > 0:
print 'discarding', len(message), 'bytes of input'
else:
disconnect(s, 'remote closed')
for s in writable:
try:
next_msg = message_queues[s].get_nowait()
except Queue.Empty:
writing.remove(s)
except KeyError: # this happens occasionally
pass
else:
# print 'sending', repr(next_msg), 'to', remote_name[s]
try:
sent = s.send(next_msg)
except socket.error as err:
disconnect(s, err.strerror)
else:
unsent = len(next_msg) - sent
if unsent != 0:
if BRAIN_DEBUG:
print 'failed to send %d bytes to %s' % (unsent, remote_name[s])
# Queue module can't push unsent data back to the front of the queue
for s in oops:
disconnect(s, sys.exc_value)
#syncing time across nodes
if time.time() > next_timesync_sec:
do_time('time', None)
#audio commands
audio_msg = mega_music.tick()
if audio_msg is not None:
if artCarHandler.art_car_takover == False:
do_send(None, encapsulated_audio_msg(audio_msg)) # always send to all nodes
if BRAIN_DEBUG:
print repr(audio_msg)
else:
mega_music.mute()
if BRAIN_DEBUG:
print "in art car takeover mode not sending audio info"
# meditation = mega.meditation
get_internal_animations_handler().interpret_audio_msg(audio_msg)
if auto_show and time.time() > last_show_change_sec + TIME_LIMIT:
auto_show()
last_show_change_sec = time.time()
do_show(None, None)
except KeyboardInterrupt:
running = False
except:
if len(sys.argv) <= 1:
raise
# print sys.exc_value
do_disconnect(None, None) # TODO: be more selective
mute_msg = encapsulated_audio_msg(music.mute())
for s in remote_name:
s.send(mute_msg)
for s in sources:
s.close()