hid_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7a83252db8cfeddaede2f200665db20a8184d4f
4
+ data.tar.gz: 815203c00e14784e31db522e2c68820f818f6623
5
+ SHA512:
6
+ metadata.gz: af6be07a04225a86cbe005fb7df1b27db1099b361e1284dbd0175910d4213dc3da73ad75e71c3f93ecb31237c7771ba6cd07dbc52d6c276d1d43a6b185c67c90
7
+ data.tar.gz: 6538313c51e2add16a1d1353437a32b806a32b7bd93bc1c6c45bf78925510c785a9377ea6747ba1cd723ceec3e2420eafa146fd930573c79860f9360395f7c87
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hid_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Gareth Adams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # HidApi
2
+
3
+ HidApi is an FFI wrapper around the C library '[hidapi][1]' provided by Signal11.
4
+
5
+ [1]: http://www.signal11.us/oss/hidapi/
6
+
7
+ This gem is my first attempt at writing any FFI code, and my first attempt to use USB. It's very possible that conventions will be broken, edge cases missed and that nothing will work if you're not using it exactly how I am. But let me know (open a Github issue, or even better see the Contributing section below) and I might be able to do something about it.
8
+
9
+ ## Dependencies
10
+
11
+ The gem requires the hidapi library to be installed and available to Ruby. On Mac with homebrew installed, this can be done with the command:
12
+
13
+ $ brew install hidapi
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'hid_api'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install hid_api
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/gareth/ruby_hid_api/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hid_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/hid_api.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hid_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hid_api"
8
+ spec.version = HidApi::VERSION
9
+ spec.licenses = ["MIT"]
10
+ spec.authors = ["Gareth Adams"]
11
+ spec.email = ["g@rethada.ms"]
12
+
13
+ spec.summary = %q{A Ruby FFI wrapper around the System11 hidapi C library}
14
+ spec.description = %q{}
15
+ spec.homepage = "https://github.com/gareth/ruby_hid_api"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "ffi", "~> 1.9"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+
27
+ spec.add_development_dependency "rspec", "> 0"
28
+
29
+ spec.add_development_dependency "pry", "> 0"
30
+ spec.add_development_dependency "awesome_print", "> 0"
31
+ end
@@ -0,0 +1,88 @@
1
+ require 'ffi'
2
+
3
+ module HidApi
4
+ class Device < FFI::Pointer
5
+ extend FFI::DataConverter
6
+ native_type FFI::Type::POINTER
7
+
8
+ def self.from_native(value, ctx)
9
+ new(value)
10
+ end
11
+
12
+ def close
13
+ HidApi.close(self)
14
+ end
15
+
16
+ def set_nonblocking(int)
17
+ HidApi.hid_set_nonblocking self, int
18
+ end
19
+
20
+ def get_manufacturer_string
21
+ get_buffered_string :manufacturer
22
+ end
23
+
24
+ def get_product_string
25
+ get_buffered_string :product
26
+ end
27
+
28
+ def get_serial_number_string
29
+ get_buffered_string :serial_number
30
+ end
31
+
32
+ def read(length)
33
+ buffer = FFI::Buffer.new(1, length, true)
34
+ with_hid_error_handling do
35
+ HidApi.hid_read self, buffer, buffer.length
36
+ end
37
+ buffer
38
+ end
39
+
40
+ def read_timeout(length, timeout)
41
+ buffer = FFI::Buffer.new(1, length, true)
42
+ with_hid_error_handling do
43
+ HidApi.hid_read_timeout self, buffer, buffer.length, timeout
44
+ end
45
+ buffer
46
+ end
47
+
48
+ def write(data)
49
+ buffer = FFI::Buffer.new(1, data.length, true)
50
+ case data
51
+ when String then buffer.put_bytes 0, data
52
+ when Array then buffer.put_array_of_char 0, data
53
+ end
54
+
55
+ with_hid_error_handling do
56
+ HidApi.hid_write self, buffer, buffer.length
57
+ end
58
+ end
59
+
60
+ def get_feature_report(data)
61
+ raise NotImplementedError
62
+ end
63
+
64
+ def send_feature_report(data)
65
+ raise NotImplementedError
66
+ end
67
+
68
+ def error
69
+ # NOTE: hid_error is only implemented on Windows systems and returns nil
70
+ # on other platforms
71
+ HidApi.hid_error(self)
72
+ end
73
+
74
+ private
75
+ def get_buffered_string field
76
+ buffer = FFI::Buffer.new(1, 255, true)
77
+ HidApi.send "hid_get_#{field}_string", self, buffer, buffer.length
78
+ buffer.read_wchar_string
79
+ end
80
+
81
+ def with_hid_error_handling
82
+ raise ArgumentError, "Block required" unless block_given?
83
+ yield.tap do |result|
84
+ raise HidError, error if result == -1
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,55 @@
1
+ require 'ffi'
2
+
3
+ module HidApi
4
+ # Represents the hid_device_info struct returned as part of
5
+ # the HidApi::hid_enumerate operation
6
+ #
7
+ # Because the struct acts as a linked list, every item represents both a
8
+ # device in the enumeration AND an Enumerable representing the rest of the
9
+ # list.
10
+ class DeviceInfo < FFI::Struct
11
+ include Enumerable
12
+
13
+ def self.release(pointer)
14
+ HidApi.hid_free_enumeration(pointer) unless pointer.null?
15
+ end
16
+
17
+ # Struct layout from http://www.signal11.us/oss/hidapi/hidapi/doxygen/html/structhid__device__info.html
18
+ layout :path, :string, # char * path
19
+ :vendor_id, :ushort, # unsigned short vendor_id
20
+ :product_id, :ushort, # unsigned short product_id
21
+ :serial_number, WideString, # wchar_t * serial_number
22
+ :release_number, :ushort, # unsigned short release_number
23
+ :manufacturer_string, WideString, # wchar_t * manufacturer_string
24
+ :product_string, WideString, # wchar_t * product_string
25
+ :usage_page, :ushort, # unsigned short usage_page
26
+ :usage, :ushort, # unsigned short usage
27
+ :interface_number, :int, # int interface_number
28
+ :next, DeviceInfo.auto_ptr # struct hid_device_info * next
29
+
30
+ # Makes the struct members available as methods
31
+ layout.members.each do |f|
32
+ define_method(f) do
33
+ self[f]
34
+ end
35
+ end
36
+
37
+ def inspect
38
+ product_string.tap do |s|
39
+ s << " (%s)" % path unless path.empty?
40
+ end
41
+ end
42
+
43
+ # Exposes the linked list structure in an Enumerable-compatible format
44
+ def each
45
+ return enum_for(:each) unless block_given?
46
+
47
+ pointer = self
48
+ loop do
49
+ break if pointer.null?
50
+ yield pointer
51
+ pointer = pointer.next
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,32 @@
1
+ module HidApi
2
+ module Util
3
+ module WCHAR
4
+ WCHAR_T_WIDTH = RUBY_PLATFORM =~ /mswin/i ? 2 : 4
5
+
6
+ # Maps a WCHAR_T_WIDTH to the corresponding Array#pack character width
7
+ FORMATS = {
8
+ 2 => 'S',
9
+ 4 => 'L'
10
+ }
11
+
12
+ # Maps a WCHAR_T_WIDTH to the corresponding string encoding
13
+ ENCODINGS = {
14
+ 2 => 'utf-16le',
15
+ 4 => 'utf-32le'
16
+ }
17
+
18
+ def read_wchar_string max_chars=nil
19
+ buffer = []
20
+ offset = 0
21
+ loop do
22
+ pointer = self + (offset * WCHAR_T_WIDTH)
23
+ char = pointer.send("read_uint#{WCHAR_T_WIDTH * 8}")
24
+ break if char.zero?
25
+ buffer << char
26
+ offset += 1
27
+ end
28
+ buffer.pack("#{FORMATS[WCHAR_T_WIDTH]}*").force_encoding(ENCODINGS[WCHAR_T_WIDTH]).encode('utf-8')
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module HidApi
2
+ module Util
3
+ autoload :WCHAR, 'hid_api/util/wchar'
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module HidApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'ffi'
2
+
3
+ module HidApi
4
+ class WideString
5
+ extend FFI::DataConverter
6
+ native_type FFI::Type::POINTER
7
+
8
+ class << self
9
+ def from_native value, context
10
+ return nil if value.null?
11
+ value.read_wchar_string
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/hid_api.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'ffi'
2
+
3
+ module HidApi
4
+ class HidError < StandardError; end
5
+
6
+ extend FFI::Library
7
+ ffi_lib 'hidapi'
8
+
9
+ autoload :Device, 'hid_api/device'
10
+ autoload :DeviceInfo, 'hid_api/device_info'
11
+ autoload :Util, 'hid_api/util'
12
+ autoload :VERSION, 'hid_api/version'
13
+ autoload :WideString, 'hid_api/wide_string'
14
+
15
+ typedef DeviceInfo.auto_ptr, :hid_device_info
16
+ typedef WideString, :wide_string
17
+ typedef Device, :device
18
+ typedef :int, :vendor_id
19
+ typedef :int, :product_id
20
+ typedef :int, :serial_number
21
+ typedef :int, :length
22
+ typedef :int, :timeout
23
+
24
+ attach_function :hid_init, [], :int
25
+ attach_function :hid_exit, [], :int
26
+ attach_function :hid_enumerate, [:int, :int], :hid_device_info
27
+ attach_function :hid_free_enumeration, [:hid_device_info], :void
28
+ attach_function :hid_open, [:vendor_id, :product_id, :serial_number], :device
29
+ attach_function :hid_open_path, [:string], :device
30
+ attach_function :hid_write, [:device, :buffer_in, :length], :int
31
+ attach_function :hid_read_timeout, [:device, :buffer_out, :length, :timeout], :int
32
+ attach_function :hid_read, [:device, :buffer_out, :length], :int
33
+ attach_function :hid_set_nonblocking, [:device, :int], :int
34
+ attach_function :hid_send_feature_report, [:device, :buffer_in, :int], :int
35
+ attach_function :hid_get_feature_report, [:device, :buffer_out, :int], :int
36
+ attach_function :hid_close, [:device], :void
37
+ attach_function :hid_get_manufacturer_string, [:device, :buffer_out, :length], :int
38
+ attach_function :hid_get_product_string, [:device, :buffer_out, :length], :int
39
+ attach_function :hid_get_serial_number_string, [:device, :buffer_out, :length], :int
40
+ attach_function :hid_get_indexed_string, [:device, :int, :buffer_out, :length], :int
41
+ # hid_error will always return a nil except on Windows
42
+ attach_function :hid_error, [:device], WideString
43
+
44
+ class << self
45
+ alias init hid_init
46
+ alias exit hid_exit
47
+ alias enumerate hid_enumerate
48
+ alias free_enumeration hid_free_enumeration
49
+ def open vendor_id, product_id, serial_number=nil
50
+ device = hid_open(vendor_id, product_id, serial_number || 0)
51
+ raise HidError, "Unable to open #{[vendor_id, product_id, serial_number].inspect}" if device.null?
52
+ device
53
+ end
54
+
55
+ def open_path path
56
+ device = hid_open_path(path)
57
+ raise HidError, "Unable to open #{path.inspect}" if device.null?
58
+ device
59
+ end
60
+ end
61
+ end
62
+
63
+ # Attempts to extend some core FFI classes with platform-aware string-handling
64
+ FFI::AbstractMemory.include(HidApi::Util::WCHAR)
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hid_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gareth Adams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ''
98
+ email:
99
+ - g@rethada.ms
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - hid_api.gemspec
114
+ - lib/hid_api.rb
115
+ - lib/hid_api/device.rb
116
+ - lib/hid_api/device_info.rb
117
+ - lib/hid_api/util.rb
118
+ - lib/hid_api/util/wchar.rb
119
+ - lib/hid_api/version.rb
120
+ - lib/hid_api/wide_string.rb
121
+ homepage: https://github.com/gareth/ruby_hid_api
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.3
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A Ruby FFI wrapper around the System11 hidapi C library
145
+ test_files: []