Skip to content

Instantly share code, notes, and snippets.

@codingfoo
Last active December 29, 2016 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingfoo/6137307 to your computer and use it in GitHub Desktop.
Save codingfoo/6137307 to your computer and use it in GitHub Desktop.
Foam Missile Launcher Control in Ruby
#!/usr/bin/env ruby
Version = '0.1'
abort("Usage: #{__FILE__} idVendor:idProduct") if ARGV.empty?
require 'curses'
require 'libusb'
# Add your own idVendor, idProduct, and movement codes here!
ActionData = {
{
:idVendor => 0x0a81,
:idProduct => 0x0701
} => {
:up => 0x02,
:down => 0x01,
:left => 0x04,
:right => 0x08,
:fire => 0x10,
:stop => 0x20,
},
}
class RocketLauncher
attr_accessor :device
def move(symbol)
@usb.control_transfer(
:bmRequestType => 0x21,
:bRequest => 0x09,
:wValue => 0x0000,
:wIndex => 0x0000,
:dataOut => ActionData[{
:idVendor => device[:idVendor],
:idProduct => device[:idProduct]
}][symbol].chr
)
end
def start
begin
@usb = LIBUSB::Context.new.devices(
:idVendor => device[:idVendor],
:idProduct => device[:idProduct]
).first.open.claim_interface(0)
rescue LIBUSB::ERROR_ACCESS
abort("No permission to access USB device!")
rescue LIBUSB::ERROR_BUSY
abort("The USB device is busy!")
rescue NoMethodError
abort("Could not find USB device!")
end
Curses.noecho
Curses.init_screen
Curses.stdscr.keypad(true)
Curses.timeout = 150
Curses.addstr("USB Rocket Launcher Controller v#{Version} by Maxwell Pray")
loop do
Curses.setpos(2,0)
case Curses.getch
when Curses::Key::UP, 'w', 'W'
Curses.addstr("Movement: Up!")
self.move(:up)
when Curses::Key::DOWN, 's', 'S'
Curses.addstr("Movement: Down!")
self.move(:down)
when Curses::Key::LEFT, 'a', 'A'
Curses.addstr("Movement: Left!")
self.move(:left)
when Curses::Key::RIGHT, 'd', 'D'
Curses.addstr("Movement: Right!")
self.move(:right)
when Curses::Key::ENTER, 10, ' '
Curses.addstr("Movement: Fire!")
self.move(:fire)
when 27, 'q', 'Q'
exit
else
Curses.addstr("Waiting for key...")
self.move(:stop)
end
Curses.clrtoeol
end
end
end
launcher = RocketLauncher.new
device = ARGV[0].split(':').map { |id| id.to_i(base=16) }
launcher.device = {
:idVendor => device[0],
:idProduct => device[1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment