Skip to content

Instantly share code, notes, and snippets.

@geoffyoungs
Created January 4, 2011 17:53

Revisions

  1. geoffyoungs revised this gist Sep 21, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stuts-car.rb
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,7 @@ def msg(byte)
    car = StutsCar.new(h)

    car.set_config(1)
    #car.read_descriptor

    car.class_request_out

    # car.forward
  2. geoffyoungs revised this gist Sep 21, 2011. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions stuts-car.rb
    Original file line number Diff line number Diff line change
    @@ -102,6 +102,4 @@ def msg(byte)
    end

    car.handle.usb_release_interface(0)
    # car.handle.usb_close

    end
  3. geoffyoungs revised this gist Sep 21, 2011. 1 changed file with 8 additions and 14 deletions.
    22 changes: 8 additions & 14 deletions stuts-car.rb
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,10 @@ class StutsCar
    attr_reader :handle
    def initialize(handle)
    @handle = handle
    p @handle.public_methods(false)
    p @handle.usb_detach_kernel_driver_np(0, 0) rescue nil
    p @handle.usb_claim_interface(0) rescue nil
    p @handle.usb_claim_interface(0) rescue nil
    #@handle.usb_set_configuration(0)

    #@handle.detach_kernel_driver_np(1)
    @handle.public_methods(false)
    @handle.usb_detach_kernel_driver_np(0, 0) rescue nil
    @handle.usb_claim_interface(0) rescue nil
    @handle.usb_claim_interface(0) rescue nil
    end
    def forward
    msg(0x01)
    @@ -35,23 +32,20 @@ def stop
    msg(0)
    end
    def set_config(num)
    p [:config, @handle.usb_control_msg(0, 9, num, 0, "", 0)]
    @handle.usb_control_msg(0, 9, num, 0, "", 0)
    end
    def read_descriptor
    buf = ("\0"*4)
    p [:read_descriptor, @handle.usb_control_msg(0x81, 0x6, 0x2200, 0, buf, 0)]
    #io = IO.popen('hex -', 'w')
    #io.write(buf)
    p buf
    @handle.usb_control_msg(0x81, 0x6, 0x2200, 0, buf, 0)
    end

    def class_request_out
    p [:class_request_out, @handle.usb_control_msg(0x21, 0xA, 0, 0, "", 0)]
    @handle.usb_control_msg(0x21, 0xA, 0, 0, "", 0)
    end
    private
    def msg(byte)
    b = '' << byte
    p [:msg, @handle.usb_control_msg(0x21, 9, 0x0200, 0, b, 0)]
    @handle.usb_control_msg(0x21, 9, 0x0200, 0, b, 0)
    end
    end

  4. geoffyoungs renamed this gist Jan 4, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. geoffyoungs created this gist Jan 4, 2011.
    113 changes: 113 additions & 0 deletions stuts-car
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    require 'usb'
    require 'pp'

    class StutsCar
    attr_reader :handle
    def initialize(handle)
    @handle = handle
    p @handle.public_methods(false)
    p @handle.usb_detach_kernel_driver_np(0, 0) rescue nil
    p @handle.usb_claim_interface(0) rescue nil
    p @handle.usb_claim_interface(0) rescue nil
    #@handle.usb_set_configuration(0)

    #@handle.detach_kernel_driver_np(1)
    end
    def forward
    msg(0x01)
    end
    def reverse
    msg 0x8
    end
    def forward_and_right
    msg(0x2)
    end
    def forward_and_left
    msg 0x20
    end
    def reverse_and_left
    msg 0x10
    end
    def reverse_and_right
    msg 0x04
    end
    def stop
    msg(0)
    end
    def set_config(num)
    p [:config, @handle.usb_control_msg(0, 9, num, 0, "", 0)]
    end
    def read_descriptor
    buf = ("\0"*4)
    p [:read_descriptor, @handle.usb_control_msg(0x81, 0x6, 0x2200, 0, buf, 0)]
    #io = IO.popen('hex -', 'w')
    #io.write(buf)
    p buf
    end

    def class_request_out
    p [:class_request_out, @handle.usb_control_msg(0x21, 0xA, 0, 0, "", 0)]
    end
    private
    def msg(byte)
    b = '' << byte
    p [:msg, @handle.usb_control_msg(0x21, 9, 0x0200, 0, b, 0)]
    end
    end


    bus = nil
    device = nil

    USB.devices.each do |dev|
    begin
    if '0a81' == ("%04x" % dev.idVendor) &&
    '0702' == ("%04x" % dev.idProduct)
    bus = dev.bus.dirname
    device = dev.filename
    end
    rescue
    end
    end

    dev = nil

    if bus.nil? && device.nil?
    exit -1
    else
    dev = USB.find_bus(bus.to_i).find_device(device.to_i)
    end

    dev.open do |h|

    car = StutsCar.new(h)

    car.set_config(1)
    #car.read_descriptor
    car.class_request_out

    # car.forward
    # sleep 0.5
    # car.stop
    # sleep 0.5
    # car.reverse
    # car.reverse_and_right
    # sleep 0.1
    # car.reverse
    # sleep 0.7
    # car.stop
    5.times do
    car.forward_and_right
    sleep 0.2
    car.stop
    sleep 0.2
    car.reverse_and_left
    sleep 0.2
    car.stop
    sleep 0.2
    end

    car.handle.usb_release_interface(0)
    # car.handle.usb_close

    end