You are not logged in.

#1 2012-06-11 01:41:58

synthead
Member
Registered: 2006-05-09
Posts: 1,337

Thought I would share: Ruby script to control USB dart launchers

Been hacking away at this for a bit and I'm calling this good for now smile  This script controls USB dart launchers like this guy:

usb-missle-launcher.jpg

0a81:0701 Chesen Electronics Corp. USB Missile Launcher

At the moment, it's coded to only control the above, but add your idVendor, idProduct, and movement codes to the ActionData hash and it should pick right up and work.

Requires the libusb and curses gems.  Codepad: http://codepad.org/v9h3ziYw

Enjoy! big_smile

#!/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]
}

launcher.start

Offline

#2 2012-07-22 02:16:59

archlinuxsagi
Member
Registered: 2008-09-12
Posts: 259

Re: Thought I would share: Ruby script to control USB dart launchers

Cool.. I wish FluxBB provide a like button

Offline

#3 2012-11-01 13:52:18

neverfoldaq
Member
Registered: 2012-11-01
Posts: 2

Re: Thought I would share: Ruby script to control USB dart launchers

The problem with LIBUSB :

var/lib/gems/1.8/gems/libusb-0.2.2/lib/libusb/constants.rb:46:in `raise_error': LIBUSB::ERROR_BUSY in libusb_claim_interface (LIBUSB::ERROR_BUSY)

do you how it might be fixed? why is it?

thanks anyway

Offline

#4 2012-11-01 15:02:13

neverfoldaq
Member
Registered: 2012-11-01
Posts: 2

Re: Thought I would share: Ruby script to control USB dart launchers

problem fixed with method

detach_kernel_driver

thx ^^

but now there is no FIRE action with enter big_smile Action is active, but:

:fire  => 0x10

does nothing.

Where i can find any info about this? Maybe it must be another for my device?

Offline

Board footer

Powered by FluxBB