Class: HIDAPI::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/hidapi/engine.rb

Overview

A wrapper around the USB context that makes it easy to locate HID devices.

Constant Summary collapse

HID_CLASS =

Contains the class code for HID devices from LIBUSB.

LIBUSB::CLASS_HID

Instance Method Summary collapse

Constructor Details

#initializeEngine

Creates a new engine.



14
15
16
# File 'lib/hidapi/engine.rb', line 14

def initialize
  @context = LIBUSB::Context.new
end

Instance Method Details

#enumerate(vendor_id = 0, product_id = 0, options = {}) ⇒ Object

Enumerates the HID devices matching the vendor and product IDs.

Both vendor_id and product_id are optional. They will act as a wild card if set to 0 (the default).



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hidapi/engine.rb', line 23

def enumerate(vendor_id = 0, product_id = 0, options = {})
  raise HIDAPI::HidApiError, 'not initialized' unless @context

  if options.is_a?(String) || options.is_a?(Symbol)
    options = { as: options }
  end

  unless options.nil? || options.is_a?(Hash)
    raise ArgumentError, 'options hash is invalid'
  end

  klass = (options || {}).delete(:as) || 'HIDAPI::Device'
  klass = Object.const_get(klass) unless klass == :no_mapping

  filters = { bClass: HID_CLASS }

  unless vendor_id.nil? || vendor_id.to_i == 0
    filters[:idVendor] = vendor_id.to_i
  end
  unless product_id.nil? || product_id.to_i == 0
    filters[:idProduct] = product_id.to_i
  end

  list = @context.devices(filters)

  if klass != :no_mapping
    list.to_a.map{ |dev| klass.new(dev) }
  else
    list.to_a
  end
end

#get_device(vendor_id, product_id, serial_number = nil, options = {}) ⇒ Object

Gets the first device with the specified vendor_id, product_id, and optionally serial_number.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hidapi/engine.rb', line 57

def get_device(vendor_id, product_id, serial_number = nil, options = {})
  raise ArgumentError, 'vendor_id must be provided' if vendor_id.to_i == 0
  raise ArgumentError, 'product_id must be provided' if product_id.to_i == 0

  klass = (options || {}).delete(:as) || 'HIDAPI::Device'
  klass = Object.const_get(klass) unless klass == :no_mapping

  list = enumerate(vendor_id, product_id, as: :no_mapping)
  return nil unless list && list.count > 0
  if serial_number.to_s == ''
    if klass != :no_mapping
      return klass.new(list.first)
    else
      return list.first
    end
  end
  list.each do |dev|
    if dev.serial_number == serial_number
      if klass != :no_mapping
        return klass.new(dev)
      else
        return dev
      end
    end
  end
  nil
end

#get_device_by_path(path, options = {}) ⇒ Object

Gets the device with the specified path.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hidapi/engine.rb', line 94

def get_device_by_path(path, options = {})
  klass = (options || {}).delete(:as) || 'HIDAPI::Device'
  klass = Object.const_get(klass) unless klass == :no_mapping

  enumerate(as: :no_mapping).each do |usb_dev|
    usb_dev.settings.each do |intf_desc|
      if intf_desc.bInterfaceClass == HID_CLASS
        dev_path = HIDAPI::make_path(usb_dev, intf_desc.bInterfaceNumber)
        if dev_path == path
          if klass != :no_mapping
            return klass.new(usb_dev, intf_desc.bInterfaceNumber)
          else
            return usb_dev
          end
        end
      end
    end
  end
end

#inspectObject

:nodoc:



142
143
144
# File 'lib/hidapi/engine.rb', line 142

def inspect   # :nodoc:
  "#<#{self.class.name}:#{self.object_id.to_hex(16)} context=0x#{@context.object_id.to_hex(16)}>"
end

#open(vendor_id, product_id, serial_number = nil) ⇒ Object

Opens the first device with the specified vendor_id, product_id, and optionally serial_number.



87
88
89
90
# File 'lib/hidapi/engine.rb', line 87

def open(vendor_id, product_id, serial_number = nil)
  dev = get_device(vendor_id, product_id, serial_number)
  dev.open if dev
end

#open_path(path) ⇒ Object

Opens the device with the specified path.



116
117
118
119
# File 'lib/hidapi/engine.rb', line 116

def open_path(path)
  dev = get_device_by_path(path)
  dev.open if dev
end

#to_sObject

:nodoc:



146
147
148
# File 'lib/hidapi/engine.rb', line 146

def to_s      # :nodoc:
  inspect
end

#usb_code_for_current_localeObject

Gets the USB code for the current locale.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hidapi/engine.rb', line 123

def usb_code_for_current_locale
  @usb_code_for_current_locale ||=
      begin
        locale = I18n.locale
        if locale
          locale = locale.to_s.partition('.')[0]  # remove encoding
          result = HIDAPI::Language.get_by_code(locale)
          unless result
            locale = locale.partition('_')[0]     # chop off extra specification
            result = HIDAPI::Language.get_by_code(locale)
          end
          result ? result[:usb_code] : 0
        else
          0
        end
      end
end