class BetaBrite::USB

Constants

INTERFACE
PRODUCT_ID

USB Device Codes

READ_ENDPOINT
READ_TIMEOUT
RECV_LENGTH
SEND_LENGTH
VENDOR_ID
WRITE_ENDPOINT
WRITE_TIMEOUT

Public Instance Methods

reset!() click to toggle source
# File lib/betabrite/usb.rb, line 14
def reset!
  write_memory!
end
write!() click to toggle source
# File lib/betabrite/usb.rb, line 26
def write!
  device = usb_device()
  handle = usb_interface(device)
  handle.usb_bulk_write(WRITE_ENDPOINT, message, WRITE_TIMEOUT)
  handle.usb_bulk_write(WRITE_ENDPOINT, message, WRITE_TIMEOUT)
  handle.usb_close
end
write_memory!() click to toggle source
# File lib/betabrite/usb.rb, line 18
def write_memory!
  device = usb_device()
  handle = usb_interface(device)
  handle.usb_bulk_write(WRITE_ENDPOINT, memory_message, WRITE_TIMEOUT)
  handle.usb_bulk_write(WRITE_ENDPOINT, memory_message, WRITE_TIMEOUT)
  handle.usb_close
end

Private Instance Methods

usb_device() click to toggle source
# File lib/betabrite/usb.rb, line 35
def usb_device
  ::USB.devices.find { |d|
    d.idProduct == PRODUCT_ID && d.idVendor == VENDOR_ID
  } || raise("Unable to locate the BETABrite.")
end
usb_interface(device) click to toggle source
# File lib/betabrite/usb.rb, line 41
def usb_interface(device)
  handle = device.usb_open
  raise "Unable to obtain a handle to the device." if handle.nil?

  retries = 0
  begin
    if RUBY_PLATFORM =~ /mswin/
      # http://osdir.com/ml/lib.libusb.devel.windows/2004-06/msg00001.html
      handle.set_configuration(0x01)
    end
    error_code = handle.usb_claim_interface(INTERFACE)
    raise unless error_code.nil?
  rescue
    handle.usb_detach_kernel_driver_np(INTERFACE);
    if retries.zero?
      retries += 1
      retry
    else
      raise "Unable to claim the device interface."
    end
  end

  raise "Unable to set alternative interface." unless handle.set_altinterface(0).nil?

  handle
end