class NXT::Interface::Usb

Implements USB connectivity to the NXT 2.0 module.

Constants

ID_PRODUCT_NXT
ID_VENDOR_LEGO
INTERFACE
IN_ENDPOINT
OUT_ENDPOINT
READSIZE
TIMEOUT

Public Instance Methods

connect() click to toggle source
# File lib/nxt/interface/usb.rb, line 19
def connect
  @usb_context = LIBUSB::Context.new
  @dev = @usb_context.devices(idVendor: ID_VENDOR_LEGO, idProduct: ID_PRODUCT_NXT).first

  raise UsbConnectionError, 'Could not find NXT attached as USB device' if @dev.nil?

  @connection = @dev.open
  @connection.claim_interface(INTERFACE)

  @connection
end
connected?() click to toggle source
# File lib/nxt/interface/usb.rb, line 38
def connected?
  # FIXME: How do we check if the device is connected?
  @connection
end
disconnect() click to toggle source
# File lib/nxt/interface/usb.rb, line 31
def disconnect
  return unless connected?

  @connection.release_interface(INTERFACE)
  @connection.close
end
receive() click to toggle source
# File lib/nxt/interface/usb.rb, line 47
def receive
  @connection.bulk_transfer(endpoint: IN_ENDPOINT, dataIn: READSIZE, timeout: TIMEOUT).from_hex_str
end
send(msg) click to toggle source
# File lib/nxt/interface/usb.rb, line 43
def send(msg)
  @connection.bulk_transfer(endpoint: OUT_ENDPOINT, dataOut: msg.pack('C*'), timeout: TIMEOUT)
end