class Lignite::Connection::Usb
A {Connection} over a USB cable
Constants
- CONFIGURATION_EV3
2) Configuration, 1-based
- ENDPOINT_EV3
5) Endpoint, 0-based
- INTERFACE_EV3
3) Interface, 0-based
- PRODUCT_EV3
- SETTING_EV3
4) Alternate setting, 0-based
- VENDOR_LEGO
To get to the endpoint we need to descend down the hierarchy of 1) Device
Attributes
device[R]
in_ep[R]
interface[R]
out_ep[R]
Public Class Methods
new()
click to toggle source
Calls superclass method
Lignite::Connection::new
# File lib/lignite/connection/usb.rb, line 29 def initialize super usb = LIBUSB::Context.new @device = usb.devices(idVendor: VENDOR_LEGO, idProduct: PRODUCT_EV3).first raise Lignite::NoUsbDevice if @device.nil? ## Because multiple configs are rare, the library allows to omit this: ## device.set_configuration(CONFIGURATION_EV3) @interface = @device.interfaces[INTERFACE_EV3] eps = @interface.endpoints @out_ep = eps.find { |e| e.direction == :out } @in_ep = eps.find { |e| e.direction == :in } end
Public Instance Methods
close()
click to toggle source
Calls superclass method
Lignite::Connection#close
# File lib/lignite/connection/usb.rb, line 73 def close super # do nothing: read and write open and close the handle each time end
read(bytes = nil)
click to toggle source
@return [String]
# File lib/lignite/connection/usb.rb, line 56 def read(bytes = nil) got = nil @device.open do |devh| devh.auto_detach_kernel_driver = true devh.claim_interface(@interface) do begin got = devh.interrupt_transfer(endpoint: @in_ep, dataIn: bytes) rescue LIBUSB::Error => e got = e.transferred raise unless got.is_a? String end end end logger.debug "Read returning #{got.bytesize} bytes" got end
write(data)
click to toggle source
@return [Integer] number of bytes written
# File lib/lignite/connection/usb.rb, line 44 def write(data) written = nil @device.open do |devh| devh.auto_detach_kernel_driver = true devh.claim_interface(@interface) do written = devh.interrupt_transfer(endpoint: @out_ep, dataOut: data) end end written end