-
Notifications
You must be signed in to change notification settings - Fork 9
/
ctrl.rb
executable file
·60 lines (53 loc) · 1.23 KB
/
ctrl.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
#!/Users/aaron/.rubies/arm64/ruby-trunk/bin/ruby
require "myhidapi"
class Handle
NONE = 0
BACKLIGHT = 1
RESET = 2
DRIVE = 3
NEUTRAL = 4
REVERSE = 5
PARK = 6
def initialize
devices = MyHIDAPI.enumerate 0x0, 0x0
dev = devices.find { |dev|
if dev.product_string =~ /Initial/i
p dev
end
dev.product_string =~ /Initial/i && dev.usage == 0x61
}
@handle = dev.open
100.times do
break if @handle
@handle = dev.open
end
raise "Couldn't connect" unless @handle
end
def reset!; send_command RESET; end
def drive!; send_command DRIVE; end
def neutral!; send_command NEUTRAL; end
def reverse!; send_command REVERSE; end
def park!; send_command PARK; end
def backlight!; send_command BACKLIGHT; end
private
def send_command cmd, param = nil
buf = [0x0, cmd, param].compact
loop do
break if @handle.write buf.pack('C*')
end
end
end
if __FILE__ == $0
handle = Handle.new
cmd = ARGV[0] || "reset"
case cmd
when "reset"
handle.reset!
sleep 1
handle.backlight!
when "drive" then handle.drive!
when "neutral" then handle.neutral!
when "park" then handle.park!
when "reverse" then handle.reverse!
end
end