Class: Lignite::Connection::Usb
- Inherits:
-
Lignite::Connection
- Object
- Lignite::Connection
- Lignite::Connection::Usb
- Includes:
- Logger
- Defined in:
- lib/lignite/connection/usb.rb
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
-
#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
Constructor Details
#initialize ⇒ Usb
Returns a new instance of Usb.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lignite/connection/usb.rb', line 27 def initialize 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.
25 26 27 |
# File 'lib/lignite/connection/usb.rb', line 25 def device @device end |
#in_ep ⇒ Object (readonly)
Returns the value of attribute in_ep.
25 26 27 |
# File 'lib/lignite/connection/usb.rb', line 25 def in_ep @in_ep end |
#interface ⇒ Object (readonly)
Returns the value of attribute interface.
25 26 27 |
# File 'lib/lignite/connection/usb.rb', line 25 def interface @interface end |
#out_ep ⇒ Object (readonly)
Returns the value of attribute out_ep.
25 26 27 |
# File 'lib/lignite/connection/usb.rb', line 25 def out_ep @out_ep end |
Instance Method Details
#read(bytes = nil) ⇒ String
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lignite/connection/usb.rb', line 53 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 if e.transferred.is_a? String got = e.transferred else raise end end end end logger.debug "Read returning #{got.bytesize} bytes" got end |
#write(data) ⇒ Integer
Returns number of bytes written.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lignite/connection/usb.rb', line 41 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 |