-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmissile.py
executable file
·452 lines (412 loc) · 13.5 KB
/
missile.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
#!/usr/bin/env python
#
# Written by: Scott Weston <[email protected]>
# Edited by: Zakaria ElQotbi <[email protected]>
#
# - Version --------------------------------------------------------------
#
# $Id: missile.py,v 2.0 2011/12/02 15:45:24 scott Exp $
#
# - License --------------------------------------------------------------
#
# Copyright (c) 2006, Scott Weston
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * The name of the contributors may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import usb
import exceptions
import urwid
import urwid.curses_display
import sys
import getopt
import random
import re
import os
from time import sleep, time
from socket import *
vendor_product_ids = [(0x1130,0x0202),(0x0416, 0x9391)]
class centerMissileDevice:
dev = None
handle = None
STOP = 0x0
LEFT = 0x8
RIGHT = 0x4
UP = 0x2
DOWN = 0x1
LEFTUP = LEFT + UP
RIGHTUP = RIGHT + UP
LEFTDOWN = LEFT + DOWN
RIGHTDOWN = RIGHT + DOWN
FIRE = 0x10
def __init__(self,usbdevice):
try:
self.handle = usbdevice.open()
self.handle.reset()
return
except NoMissilesError, e:
raise NoMissilesError()
def move(self, direction):
self.handle.controlMsg(0x21, 0x09, [0x5f, direction, 0xe0, 0xff, 0xfe], 0x0300, 0x00)
class legacyMissileDevice:
dev = None
INITA = (85, 83, 66, 67, 0, 0, 4, 0)
INITB = (85, 83, 66, 67, 0, 64, 2, 0)
CMDFILL = ( 8, 8,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)
STOP = ( 0, 0, 0, 0, 0, 0)
LEFT = ( 0, 1, 0, 0, 0, 0)
RIGHT = ( 0, 0, 1, 0, 0, 0)
UP = ( 0, 0, 0, 1, 0, 0)
DOWN = ( 0, 0, 0, 0, 1, 0)
LEFTUP = ( 0, 1, 0, 1, 0, 0)
RIGHTUP = ( 0, 0, 1, 1, 0, 0)
LEFTDOWN = ( 0, 1, 0, 0, 1, 0)
RIGHTDOWN = ( 0, 0, 1, 0, 1, 0)
FIRE = ( 0, 0, 0, 0, 0, 1)
def __init__(self, usbdevice):
try:
self.handle = usbdevice.open()
self.handle.reset()
except NoMissilesError, e:
raise NoMissilesError()
def move(self, direction):
self.handle.controlMsg(0x21, 0x09, self.INITA, 0x02, 0x01)
self.handle.controlMsg(0x21, 0x09, self.INITB, 0x02, 0x01)
self.handle.controlMsg(0x21, 0x09, direction+self.CMDFILL, 0x02, 0x01)
class NoMissilesError(Exception): pass
class UsbDevice:
def __init__(self):
self.handle = None
self.launcher = None
self.dev = None
self.busses = usb.busses()
def probe(self):
count = 0
for bus in self.busses:
devices = bus.devices
for dev in devices:
for i, (vendor_id, product_id) in enumerate(vendor_product_ids):
if dev.idVendor==vendor_id and dev.idProduct==product_id:
if count==0:
self.dev = dev
self.conf = self.dev.configurations[0]
self.intf = self.conf.interfaces[0][0]
self.endpoints = []
for endpoint in self.intf.endpoints:
self.endpoints.append(endpoint)
if i == 0:
self.launcher = legacyMissileDevice
return self.launcher
elif i == 1:
self.launcher = centerMissileDevice
return self.launcher
else:
count=count+1
raise NoMissilesError()
def open(self):
if self.handle:
self.handle = None
self.handle = self.dev.open()
try:
self.handle.detachKernelDriver(0)
self.handle.detachKernelDriver(1)
except usb.USBError, err:
print >> sys.stderr, err
self.handle.setConfiguration(self.conf)
self.handle.claimInterface(self.intf)
self.handle.setAltInterface(self.intf)
return self.handle
class MissileNoDisplay:
def run(self):
try:
usbdevice = UsbDevice()
MissileDevice = usbdevice.probe()
m = MissileDevice(usbdevice)
except NoMissilesError, e:
raise NoMissilesError
while 1:
keys = None
while not keys:
keys = raw_input("Enter something: ")
for k in keys:
if k == 'window resize':
size = self.ui.get_cols_rows()
elif k in ('w', 'up'):
m.move(MissileDevice.UP)
elif k in ('x', 'down'):
m.move(MissileDevice.DOWN)
elif k in ('a', 'left'):
m.move(MissileDevice.LEFT)
elif k in ('d', 'right'):
m.move(MissileDevice.RIGHT)
elif k in ('f', 'space'):
m.move(MissileDevice.FIRE)
elif k in ('s'):
m.move(MissileDevice.STOP)
elif k in ('q'):
m.move(MissileDevice.LEFTUP)
elif k in ('e'):
m.move(MissileDevice.RIGHTUP)
elif k in ('z'):
m.move(MissileDevice.LEFTDOWN)
elif k in ('c'):
m.move(MissileDevice.RIGHTDOWN)
elif k in ('r'):
for n in range(3):
sleep(0.5)
elif k in ('v'):
if random.random() > 0.8:
m.move(MissileDevice.FIRE)
elif k in ('esc'):
return
class MissileDisplay:
palette = [ ('body', 'black', 'dark cyan', 'standout'),
('footer','light gray', 'dark blue'),
('header', 'white', 'dark blue', 'underline'),
('important','white', 'dark red', 'bold'),
('key', 'light cyan', 'black', 'underline'),
('title', 'white', 'black',), ]
BLANK = urwid.Text("")
WARNING = urwid.Text([('important',"CAUTION: AIM AWAY FROM FACE")],
align='center')
HEADER = urwid.AttrWrap(urwid.Text("MISSILE COMMAND!", align='center'),
'header')
FOOTER = urwid.AttrWrap(urwid.Text("[email protected]", align='center'),
'footer')
CONTENT = [ BLANK,
urwid.Text(["Use the following\n"
"keys to move\n\n"
"Q W E \n"
" \ | / \n"
"A -(S)- D \n"
" / | \ \n"
"Z X C \n\n"
"F to fire all guns once\n"
"R for rapid sequentual fire of all missiles\n"
"S to Stop\n"
"ESC to Exit\n\n",], align='center'),
WARNING,
BLANK, ]
def __init__(self):
self.listbox = urwid.ListBox(self.CONTENT)
view = urwid.AttrWrap(self.listbox, 'body')
self.view = urwid.Frame(view, header=self.HEADER, footer=self.FOOTER)
def main(self):
self.ui = urwid.curses_display.Screen()
self.ui.register_palette(self.palette)
self.ui.run_wrapper(self.run)
def run(self):
try:
usbdevice = UsbDevice()
MissileDevice = usbdevice.probe()
m = MissileDevice(usbdevice)
except NoMissilesError, e:
raise NoMissilesError
size = self.ui.get_cols_rows()
while 1:
canvas = self.view.render(size, focus=1)
self.ui.draw_screen(size, canvas)
keys = None
while not keys:
keys = self.ui.get_input()
for k in keys:
if k == 'window resize':
size = self.ui.get_cols_rows()
elif k in ('w', 'up'):
m.move(MissileDevice.UP)
elif k in ('x', 'down'):
m.move(MissileDevice.DOWN)
elif k in ('a', 'left'):
m.move(MissileDevice.LEFT)
elif k in ('d', 'right'):
m.move(MissileDevice.RIGHT)
elif k in ('f', 'space'):
m.move(MissileDevice.FIRE)
elif k in ('s'):
m.move(MissileDevice.STOP)
elif k in ('q'):
m.move(MissileDevice.LEFTUP)
elif k in ('e'):
m.move(MissileDevice.RIGHTUP)
elif k in ('z'):
m.move(MissileDevice.LEFTDOWN)
elif k in ('c'):
m.move(MissileDevice.RIGHTDOWN)
elif k in ('r'):
for n in range(3):
m.move(MissileDevice.FIRE)
sleep(0.5)
elif k in ('v'):
if random.random() > 0.8:
m.move(MissileDevice.FIRE)
elif k in ('esc'):
return
self.view.keypress(size, k)
class MissileNetwork:
def main(self):
host = "localhost"
port = 20000
buf = 1024
addr = (host,port)
md = []
lt = 0
mc = 0
af = 0
lpid = 1
for missiles in range(10):
try:
md.append(MissileDevice(missiles))
except NoMissilesError, e:
break
if missiles==0:
raise NoMissilesError
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
cmd,addr = UDPSock.recvfrom(buf)
cmd = cmd.strip()
spa = re.split(r':', cmd)
k = spa[0]
ppid = spa[1]
if not k:
continue
else:
if k == "s":
if ppid != lpid:
print "Received a STOP for order %s but last order was %s, ignored" % (ppid, lpid)
continue
if abs(time()-lt) < 60:
mc = mc + 1
else:
mc = 0
af = 0
# you can make it noisey here if you wanted to
# os.system("aplay sounds/firstwarning.wav &")
if mc > 60 and af == 0:
k = "v"
# you can make it noisey here if you wanted to
# os.system("aplay sounds/secondwarning.wav &")
print "Received via network at %2.2f command %s/%s (move count: %d, %d)" % (time(), k, ppid, mc, af)
lt = time()
lpid = ppid
if k in ('w', 'up'):
for m in md:
m.move(MissileDevice.UP)
elif k in ('x', 'down'):
for m in md:
m.move(MissileDevice.DOWN)
elif k in ('a', 'left'):
for m in md:
m.move(MissileDevice.LEFT)
elif k in ('d', 'right'):
for m in md:
m.move(MissileDevice.RIGHT)
elif k in ('f', 'space'):
for m in md:
m.move(MissileDevice.FIRE)
elif k in ('s', 'S'):
for m in md:
m.move(MissileDevice.STOP)
elif k in ('q'):
for m in md:
m.move(MissileDevice.LEFTUP)
elif k in ('e'):
for m in md:
m.move(MissileDevice.RIGHTUP)
elif k in ('z'):
for m in md:
m.move(MissileDevice.LEFTDOWN)
elif k in ('c'):
for m in md:
m.move(MissileDevice.RIGHTDOWN)
elif k in ('r'):
for n in range(3):
for m in md:
m.move(MissileDevice.FIRE)
sleep(0.5)
elif k in ('v'):
for m in md:
if random.random() > 0.9:
m.move(MissileDevice.FIRE)
af = 1
elif k in ('esc'):
UDPSock.close()
return
def usage():
print "Usage:"
print " -h | --help : this help"
print " -n | --network: simple network listener mode (Read the source Luke!)"
print " -c | --console: read command directly from console (dont use urwid interface)"
print " -v | --version: version"
sys.exit(2)
def version():
print "$Id: missile.py,v 1.13 2006/07/25 17:01:24 scott Exp $"
sys.exit(0)
def main(argv):
try:
opts, args = getopt.getopt(argv, "hvnc", ["help", "version", "network", "console"])
except getopt.GetoptError:
print "Sorry, bad option."
usage()
if opts:
for o, a in opts:
if o in ("-h", "--help"):
usage()
elif o in ("-v", "--version"):
version()
elif o in ("-n", "--network"):
try:
MissileNetwork().main()
except NoMissilesError, e:
print "No WMDs found."
return
elif o in ("-c", "--console"):
try:
MissileNoDisplay().run()
except NoMissilesError, e:
print "No WMDs found."
return
else:
try:
MissileDisplay().main()
except NoMissilesError, e:
print "No WMDs found."
return
else:
try:
MissileDisplay().main()
except NoMissilesError, e:
print "No WMDs found."
return
if __name__=="__main__":
main(sys.argv[1:])