Class: Lignite::Connection::Usb
- Inherits:
-
Lignite::Connection
- Object
- Lignite::Connection
- Lignite::Connection::Usb
- Includes:
- Logger
- Defined in:
- lib/lignite/connection/usb.rb
Overview
A Lignite::Connection over a USB cable
Constant Summary collapse
- VENDOR_LEGO =
To get to the endpoint we need to descend down the hierarchy of 1) Device
0x0694
- PRODUCT_EV3 =
5
- CONFIGURATION_EV3 =
2) Configuration, 1-based
1
- INTERFACE_EV3 =
3) Interface, 0-based
0
- SETTING_EV3 =
4) Alternate setting, 0-based
0
- ENDPOINT_EV3 =
5) Endpoint, 0-based
1
Instance Attribute Summary collapse
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#in_ep ⇒ Object
readonly
Returns the value of attribute in_ep.
-
#interface ⇒ Object
readonly
Returns the value of attribute interface.
-
#out_ep ⇒ Object
readonly
Returns the value of attribute out_ep.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize ⇒ Usb
constructor
A new instance of Usb.
- #read(bytes = nil) ⇒ String
-
#write(data) ⇒ Integer
Number of bytes written.
Methods included from Logger
Methods inherited from Lignite::Connection
create, #receive, reset, #send
Methods included from Bytes
#bin_to_hex, #f32, #hex_to_bin, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8
Constructor Details
#initialize ⇒ Usb
Returns a new instance of Usb.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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 |
Instance Attribute Details
#device ⇒ Object (readonly)
Returns the value of attribute device.
27 28 29 |
# File 'lib/lignite/connection/usb.rb', line 27 def device @device end |
#in_ep ⇒ Object (readonly)
Returns the value of attribute in_ep.
27 28 29 |
# File 'lib/lignite/connection/usb.rb', line 27 def in_ep @in_ep end |
#interface ⇒ Object (readonly)
Returns the value of attribute interface.
27 28 29 |
# File 'lib/lignite/connection/usb.rb', line 27 def interface @interface end |
#out_ep ⇒ Object (readonly)
Returns the value of attribute out_ep.
27 28 29 |
# File 'lib/lignite/connection/usb.rb', line 27 def out_ep @out_ep end |
Instance Method Details
#close ⇒ Object
73 74 75 76 |
# 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) ⇒ String
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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) ⇒ Integer
Returns number of bytes written.
44 45 46 47 48 49 50 51 52 53 |
# 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 |