Skip to content

Allow opening device by Serial #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hid_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "ffi", "~> 1.9"
spec.add_dependency "ffi_wide_char", "~> 1.1"

spec.add_development_dependency "bundler", "~> 1.8"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
7 changes: 2 additions & 5 deletions lib/hid_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HidError < StandardError; end
typedef Device, :device
typedef :int, :vendor_id
typedef :int, :product_id
typedef :int, :serial_number
typedef WideString, :serial_number
typedef :int, :length
typedef :int, :timeout

Expand Down Expand Up @@ -49,7 +49,7 @@ class << self
alias enumerate hid_enumerate
alias free_enumeration hid_free_enumeration
def open(vendor_id, product_id, serial_number = nil)
device = hid_open(vendor_id, product_id, serial_number || 0)
device = hid_open(vendor_id, product_id, serial_number)
if device.null?
error = format(
"Unable to open %s",
Expand All @@ -67,6 +67,3 @@ def open_path(path)
end
end
end

# Attempts to extend some core FFI classes with platform-aware string-handling
FFI::AbstractMemory.include(HidApi::Util::WCHAR)
15 changes: 6 additions & 9 deletions lib/hid_api/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def serial_number_string
def read(length)
buffer = clear_buffer length
with_hid_error_handling do
HidApi.hid_read self, buffer, buffer.length
HidApi.hid_read self, buffer, buffer.size
end
buffer
end

def read_timeout(length, timeout)
buffer = clear_buffer length
with_hid_error_handling do
HidApi.hid_read_timeout self, buffer, buffer.length, timeout
HidApi.hid_read_timeout self, buffer, buffer.size, timeout
end
buffer
end
Expand All @@ -59,7 +59,7 @@ def write(data)
end

with_hid_error_handling do
HidApi.hid_write self, buffer, buffer.length
HidApi.hid_write self, buffer, buffer.size
end
end

Expand All @@ -80,16 +80,13 @@ def error
private

def clear_buffer(length)
b = FFI::Buffer.new(1, length)
# FFI::Buffer doesn't clear the first byte if length < 8
b.put_char 0, 0
b
return FFI::MemoryPointer.new(:char, length)
end

def get_buffered_string(field)
buffer = clear_buffer 255
HidApi.send "hid_get_#{field}_string", self, buffer, buffer.length
buffer.read_wchar_string
HidApi.send "hid_get_#{field}_string", self, buffer, buffer.size
WideString.from_native(buffer)
end

def with_hid_error_handling
Expand Down
2 changes: 1 addition & 1 deletion lib/hid_api/device_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.release(pointer)
:next, DeviceInfo.auto_ptr # struct hid_device_info * next

# Makes the struct members available as methods
layout.members.each do |f|
members.each do |f|
define_method(f) do
self[f]
end
Expand Down
5 changes: 0 additions & 5 deletions lib/hid_api/util.rb

This file was deleted.

40 changes: 0 additions & 40 deletions lib/hid_api/util/wchar.rb

This file was deleted.

9 changes: 7 additions & 2 deletions lib/hid_api/wide_string.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "ffi"
require 'ffi_wide_char'

module HidApi
# An FFI data converter that marshalls data from specific device fields via
Expand All @@ -8,9 +9,13 @@ class WideString
native_type FFI::Type::POINTER

class << self
def from_native(value, _context)
def from_native(value, _ctx = nil)
return nil if value.null?
value.read_wchar_string
FfiWideChar.read_wide_string(value).encode('utf-8')
end

def to_native(value, _ctx = nil)
FfiWideChar.to_wide_string value
end
end
end
Expand Down