This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
forked from steveh/usblauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.rb
98 lines (76 loc) · 1.87 KB
/
launcher.rb
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
require "usb"
$USERS = YAML.load_file('users.yml')
class Launcher
VENDOR = 0x2123
PRODUCT = 0x1010
DOWN = 0x01
UP = 0x02
LEFT = 0x04
RIGHT = 0x08
FIRE = 0x10
STOP = 0x20
def initialize
find_device_by_vendor_and_product(VENDOR, PRODUCT)
end
def down(duration = 1000)
send_move(DOWN, duration)
end
def up(duration = 1000)
send_move(UP, duration)
end
def left(duration = 1000)
send_move(LEFT, duration)
end
def right(duration = 1000)
send_move(RIGHT, duration)
end
def fire(num_projectiles = 1)
# Stabilise prior to the shot
sleep 0.5
num_projectiles.times do
send_command(FIRE)
# Allow for reloading
sleep 4.5
end
end
def zero
send_move(DOWN, 2000)
send_move(LEFT, 8000)
end
def pause(duration)
sleep(duration / 1000.0)
end
def attack(user)
if $USERS[user]
zero
$USERS[user].each do |direction, distance|
send_move(self.class.const_get(direction.upcase), distance)
end
fire(2)
else
print "WARNING: No target command set defined for user "+user
end
end
private
def find_device_by_vendor_and_product(vendor, product)
@device = USB.devices.detect{ |device| device.idVendor == vendor && device.idProduct == product }
raise "Launcher not found" unless @device
end
def send_command(command)
@device.open do |handle|
request_type = 0x21
request = 0x09
value = 0
index = 0
bytes = [0x02, command, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00].pack("c*")
timeout = 1000
result = handle.usb_control_msg(request_type, request, value, index, bytes, timeout)
result
end
end
def send_move(direction, duration)
send_command(direction)
pause(duration)
send_command(STOP)
end
end