Class: SimpleHID::Device

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

Constant Summary collapse

MAX_STR_LEN =
255

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Device

Returns a new instance of Device.



107
108
109
110
# File 'lib/simple_hid.rb', line 107

def initialize(path)
  @handle = SimpleHID.hid_open_path(path)
  raise "Unable to open HID device at path: #{path}" if @handle.null?
end

Instance Method Details

#closeObject



119
120
121
122
123
# File 'lib/simple_hid.rb', line 119

def close
  return if @handle.nil? 
  SimpleHID.hid_close(@handle)
  @handle = nil
end

#manufacturer_stringObject



125
126
127
# File 'lib/simple_hid.rb', line 125

def manufacturer_string
  read_wide_string_with(:hid_get_manufacturer_string)
end

#product_stringObject



129
130
131
# File 'lib/simple_hid.rb', line 129

def product_string
  read_wide_string_with(:hid_get_product_string)
end

#serial_number_stringObject



133
134
135
# File 'lib/simple_hid.rb', line 133

def serial_number_string
  read_wide_string_with(:hid_get_serial_number_string)
end

#write(byte_array) ⇒ Object



112
113
114
115
116
117
# File 'lib/simple_hid.rb', line 112

def write(byte_array)
  raise "Device is closed." if @handle.nil?
  data_string = byte_array.pack('C*')
  data_ptr = FFI::MemoryPointer.from_string(data_string)
  SimpleHID.hid_write(@handle, data_ptr, data_string.bytesize)
end