This repository has been archived by the owner on May 29, 2023. It is now read-only.
forked from codedance/Retaliation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretaliation.py
executable file
·147 lines (120 loc) · 3.81 KB
/
retaliation.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
#!/usr/bin/python
import sys
import platform
import time
import itertools
import usb.core
import usb.util
# Protocol command bytes
DOWN = 0x01
UP = 0x02
LEFT = 0x04
RIGHT = 0x08
FIRE = 0x10
STOP = 0x20
DEVICE = None
DEVICE_TYPE = None
def usage():
print "Usage: retaliation.py [command] [value]"
print ""
print " commands:"
print ""
print " up - move up <value> milliseconds"
print " down - move down <value> milliseconds"
print " right - move right <value> milliseconds"
print " left - move left <value> milliseconds"
print " fire - fire <value> times (between 1-4)"
print " zero - park at zero position (bottom-left)"
print " pause - pause <value> milliseconds"
print " led - turn the led on or of (1 or 0)"
print ""
print ""
def setup_usb():
# Tested only with the Cheeky Dream Thunder
# and original USB Launcher
global DEVICE
global DEVICE_TYPE
DEVICE = usb.core.find(idVendor=0x2123, idProduct=0x1010)
if DEVICE is None:
DEVICE = usb.core.find(idVendor=0x0a81, idProduct=0x0701)
if DEVICE is None:
raise ValueError('Missile device not found')
else:
DEVICE_TYPE = "Original"
else:
DEVICE_TYPE = "Thunder"
# On Linux we need to detach usb HID first
if "Linux" == platform.system():
try:
DEVICE.detach_kernel_driver(0)
except Exception, e:
pass # already unregistered
DEVICE.set_configuration()
def send_cmd(cmd):
if "Thunder" == DEVICE_TYPE:
DEVICE.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, cmd, 0x00,0x00,0x00,0x00,0x00,0x00])
elif "Original" == DEVICE_TYPE:
DEVICE.ctrl_transfer(0x21, 0x09, 0x0200, 0, [cmd])
def led(cmd):
if "Thunder" == DEVICE_TYPE:
DEVICE.ctrl_transfer(0x21, 0x09, 0, 0, [0x03, cmd, 0x00,0x00,0x00,0x00,0x00,0x00])
elif "Original" == DEVICE_TYPE:
print("There is no LED on this device")
def send_move(cmd, duration_ms):
send_cmd(cmd)
time.sleep(duration_ms / 1000.0)
send_cmd(STOP)
def run_command(command, value):
command = command.lower()
if command == "right":
send_move(RIGHT, value)
elif command == "left":
send_move(LEFT, value)
elif command == "up":
send_move(UP, value)
elif command == "down":
send_move(DOWN, value)
elif command == "zero" or command == "park" or command == "reset":
# Move to bottom-left
send_move(DOWN, 2000)
send_move(LEFT, 8000)
elif command == "pause" or command == "sleep":
time.sleep(value / 1000.0)
elif command == "led":
if value == 0:
led(0x00)
else:
led(0x01)
elif command == "fire" or command == "shoot":
if value < 1 or value > 4:
value = 1
# Stabilize prior to the shot, then allow for reload time after.
time.sleep(0.5)
for i in range(value):
send_cmd(FIRE)
time.sleep(4.5)
else:
print "Error: Unknown command: '%s'" % command
def run_command_set(commands):
for cmd, value in commands:
run_command(cmd, value)
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
def main(args):
if len(args) < 2:
usage()
sys.exit(1)
setup_usb()
commandList = grouper(args[1:], 2)
for (command,value) in commandList:
# Process any passed commands or command_sets
if value is not None:
intValue = int(value)
else:
intValue = 0
run_command(command, intValue)
if __name__ == '__main__':
main(sys.argv)