/
mario_server.py
380 lines (320 loc) · 11.6 KB
/
mario_server.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
import sys
sys.path.append('/Users/qdot/git-projects/library/usr_darwin_10.5_x86/lib/python2.6/site-packages')
import os
os.environ["DYLD_LIBRARY_PATH"] = "/Users/qdot/git-projects/library/usr_darwin_10.5_x86/lib/"
import operator
import time
import osc
import trancevibe
import threading
import serial
import traceback
from ambx import ambx
################################################################################
class ArduinoDrinkControl():
WATER = 2
COKE = 0
RUM = 1
SERVO_DOWN_POS = 150
SERVO_UP_POS = 180
def __init__(self):
self.SERVO_DOWN_LIST = [self.SERVO_DOWN_POS, self.SERVO_DOWN_POS,
self.SERVO_DOWN_POS]
self.current_servo_pos = self.SERVO_DOWN_LIST
self.serial = serial.Serial(port="/dev/tty.usbserial-A6004oBL",
baudrate=38400)
def checksum(self, msg):
return reduce(operator.add, map(ord, msg)) % 256
def sendCommand(self):
command = ''.join(['a'] + map(chr, self.current_servo_pos))
command += chr(self.checksum(command))
self.serial.write(command)
def isOpen(self):
return self.serial.isOpen()
################################################################################
class BaseThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.should_run = False
################################################################################
class ArduinoDrinkControlThread(BaseThread):
current_time = [0.0, 0.0, 0.0]
start_time = [0,0,0]
l = threading.Lock()
def __init__(self):
BaseThread.__init__(self)
self._drink_control = None
try:
self._drink_control = ArduinoDrinkControl()
except Exception, e:
print "SERIAL PORT NOT FOUND, NOT USING ARDUINO"
pass
def add_to_time(self, index, time):
self.l.acquire()
self.current_time[index] += time
self.l.release()
def get_time(self, index):
return self.current_time[index]
def run(self):
if self._drink_control is None:
return
self.should_run = True
while self.should_run:
should_send = False
for i in range(0, 3):
self.l.acquire()
if self.current_time[i] > 0:
if (time.time() - self.start_time[i]) > self.current_time[i] and self._drink_control.current_servo_pos[i] == ArduinoDrinkControl.SERVO_UP_POS:
self._drink_control.current_servo_pos[i] = ArduinoDrinkControl.SERVO_DOWN_POS
self.current_time[i] = 0.0
should_send = True
elif self._drink_control.current_servo_pos[i] == ArduinoDrinkControl.SERVO_DOWN_POS and self.current_time[i] > 0:
self._drink_control.current_servo_pos[i] = ArduinoDrinkControl.SERVO_UP_POS
self.start_time[i] = time.time()
should_send = True
self.l.release()
if should_send:
self._drink_control.sendCommand()
else:
time.sleep(0.005)
g_drinkControlThread = ArduinoDrinkControlThread()
################################################################################
class TranceVibratorThread(BaseThread):
current_time = 0
run_time = -1
t = threading.Lock()
def __init__(self):
BaseThread.__init__(self)
try:
self.tv = trancevibe.TranceVibrator()
self.tv.open(0)
except Exception, e:
print "TRANCEVIBE EXCEPTION"
print e
self.tv = None
def set_time(self, s = 0):
try:
self.t.acquire()
self.run_time = s
self.current_time = time.time()
self.tv.set_speed(255)
except Exception, e:
print traceback.print_exc()
print e
print "EXCEPTION?"
finally:
self.t.release()
def run(self):
if self.tv is None:
return
self.should_run = True
try:
while self.should_run:
if self.run_time > 0:
if time.time() - self.current_time > self.run_time:
self.t.acquire()
self.tv.set_speed(0)
self.run_time = 0
self.t.release()
time.sleep(.005)
self.tv.set_speed(0)
self.tv.close()
except Exception, e:
print traceback.print_exc()
print e
print "EXCEPTION?"
finally:
self.t.release()
g_tranceVibratorThread = TranceVibratorThread()
################################################################################
class ambxThread(BaseThread):
lights = [ambx.LEFT_SP_LIGHT, ambx.LEFT_WW_LIGHT, ambx.CENTER_WW_LIGHT, ambx.RIGHT_WW_LIGHT, ambx.RIGHT_SP_LIGHT]
colors = [(0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0)]
fans = [ambx.RIGHT_FAN, ambx.LEFT_FAN]
fan_speed = 0
_need_light_update = False
_need_fan_update = False
color_time = 0
last_time = 0
old_colors = []
def __init__(self):
BaseThread.__init__(self)
self._ambx = ambx()
if not self._ambx.open():
print "No ambx found, not using ambx system"
self._ambx = None
def clear_timed_color(self):
if self.color_time > 0:
print "clearing timed color"
self.colors = self.old_colors
self._need_light_update = True
self.color_time = 0
def set_all_colors(self, color):
self.clear_timed_color()
for i in range(0, 5):
self.colors[i] = color
print self.colors
self._need_light_update = True
def set_color(self, index, color):
self.clear_timed_color()
self.color_time = 0
self.colors[i] = color
self._need_light_update = True
def set_fan_speed(self, speed):
self.fan_speed = speed
self._need_fan_update = True
def set_timed_color(self, color, t):
self.clear_timed_color()
self.old_colors = list(self.colors)
self.set_all_colors(color)
self.color_time = t
self.last_time = time.time()
def run(self):
if self._ambx is None:
print "No ambx found, not running ambx thread"
return
try:
print "Starting ambx thread"
self.should_run = True
while self.should_run:
if self.color_time > 0 and (time.time() - self.last_time) > self.color_time:
self.clear_timed_color()
if self._need_light_update is True:
for i in range(0, 5):
self._ambx.setLightColor(self.lights[i], self.colors[i])
self._need_light_update = False
if self._need_fan_update is True:
for i in range(0, 2):
self._ambx.setFanSpeed(self.fans[i], self.fan_speed)
self._need_fan_update = False
time.sleep(.005)
except Exception, e:
print e
g_ambxThread = ambxThread()
################################################################################
class OSCServer():
_handlers = []
def __init__(self):
osc.init()
osc.listen('localhost', 9001)
osc.listen('10.211.55.2', 9001)
self._threads = []
self._threads.append(g_drinkControlThread)
self._threads.append(g_ambxThread)
self._threads.append(g_tranceVibratorThread)
[thread.start() for thread in self._threads]
def add_handler(self, h):
self._handlers.append(h)
h.bind_osc(osc)
def close(self):
osc.dontListen()
for thread in self._threads:
thread.should_run = False
################################################################################
class OSCBinder():
def bind_osc(self, osc):
return
################################################################################
class MarioHandler(OSCBinder):
def __init__(self):
return
def on_coin(self, *msg):
try:
print "Coin!"
if g_drinkControlThread.get_time(ArduinoDrinkControl.COKE) > 0:
g_drinkControlThread.add_to_time(ArduinoDrinkControl.COKE, .2)
else:
g_drinkControlThread.add_to_time(ArduinoDrinkControl.COKE, .4)
g_ambxThread.set_timed_color((0xf3, 0xff, 0x39), .5)
except Exception, e:
print e
def on_die(self, *msg):
try:
g_drinkControlThread.add_to_time(ArduinoDrinkControl.WATER, 2.0)
except Exception, e:
print e
def on_enemy(self, *msg):
try:
print "Enemy!"
if g_drinkControlThread.get_time(ArduinoDrinkControl.RUM) > 0:
g_drinkControlThread.add_to_time(ArduinoDrinkControl.RUM, .4)
else:
g_drinkControlThread.add_to_time(ArduinoDrinkControl.RUM, .2)
g_ambxThread.set_timed_color((255, 0, 0), .5)
except Exception, e:
print e
def on_flag(self, *msg):
try:
g_tranceVibratorThread.set_time((255 - msg[0][2]) * .005)
g_drinkControlThread.add_to_time(ArduinoDrinkControl.RUM, .75 * (255.0-msg[0][2]) / 255.0)
except Exception, e:
print e
def bind_osc(self, osc):
print "Binding Mario functions"
osc.bind(self.on_coin, "/mario/coin")
osc.bind(self.on_flag, "/mario/flag")
osc.bind(self.on_speed, "/mario/speed")
osc.bind(self.on_sky, "/mario/sky")
osc.bind(self.on_die, "/mario/die")
osc.bind(self.on_enemy, "/mario/enemy")
def on_speed(self, *msg):
try:
s = 0
# Going backwards
if msg[0][2] > 200:
s = (int)(((256 - msg[0][2]) / 40.0) * 255)
# Going forwards
else:
s = (int)(((msg[0][2]) / 40.0) * 255)
g_ambxThread.set_fan_speed(s)
return
except Exception, e:
print msg
print e
def on_sky(self, *msg):
sky_color = [(0, 0, 20), (0, 0, 180), (0, 0, 180), (255,0,0), (0,0,0)]
color = sky_color[msg[0][2]]
g_ambxThread.set_all_colors(color)
return
################################################################################
class TetrisHandler():
def __init__(self):
return
def bind_osc(self, osc):
print "Binding Tetris Functions"
osc.bind(self.on_line, "/tetris/line")
osc.bind(self.on_level, "/tetris/level")
osc.bind(self.on_piece_down, "/tetris/piece_down")
def on_piece_down(self, *msg):
try:
g_tranceVibratorThread.set_time(.2)
except Exception, e:
print traceback.print_exc()
print "EX"
print e
def on_line(self, *msg):
color = msg[0][2:5]
g_ambxThread.set_all_colors(color)
return
def on_level(self, *msg):
#print msg
g_drinkControlThread.add_to_time(msg[0][2], msg[0][3])
return
################################################################################
def main():
s = OSCServer()
s.add_handler(MarioHandler())
s.add_handler(TetrisHandler())
try:
while 1:
time.sleep(0.01)
except KeyboardInterrupt, e:
pass
except Exception, ex:
print "EXCEPTION!"
print ex
finally:
print "CLOSING CONNECTION"
s.close()
if __name__ == '__main__':
sys.exit(main())