Module: SimpleHID

Extended by:
FFI::Library
Defined in:
lib/simple_hid.rb,
lib/simple_hid/version.rb

Defined Under Namespace

Classes: Device, DeviceInfo

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.enumerate(vid: 0, pid: 0) ⇒ Object

Enumeration method



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/simple_hid.rb', line 61

def self.enumerate(vid: 0, pid: 0)
  devices = []
  head = hid_enumerate(vid, pid)

  begin
    return [] if head.null?
    current_ptr = head
    until current_ptr.null?
      info_struct = DeviceInfo.new(current_ptr)
      
      # Convert the FFI::Struct to a plain Ruby Hash
      device_info = {
        path: info_struct[:path].dup, 
        vendor_id: info_struct[:vendor_id],
        product_id: info_struct[:product_id],
        release_number: info_struct[:release_number],
        usage_page: info_struct[:usage_page],
        usage: info_struct[:usage],
        interface_number: info_struct[:interface_number],
        bus_type: info_struct[:bus_type]
      }
      devices << device_info
      current_ptr = info_struct[:next]
    end
  ensure
    hid_free_enumeration(head) unless head.null?
  end

  devices
end

.find_path(vid: 0, pid: 0, usage_page: nil, usage: nil) ⇒ Object

Helper method



93
94
95
96
97
98
99
100
101
# File 'lib/simple_hid.rb', line 93

def self.find_path(vid: 0, pid: 0, usage_page: nil, usage: nil)
  all_devices = self.enumerate(vid: vid, pid: pid)
  found_device = all_devices.find do |dev|
    match_usage_page = usage_page.nil? || dev[:usage_page] == usage_page
    match_usage = usage.nil? || dev[:usage] == usage
    match_usage_page && match_usage
  end
  found_device ? found_device[:path] : nil
end