Skip to content

Instantly share code, notes, and snippets.

@jhgorse
Last active May 30, 2020 15:07
Show Gist options
  • Save jhgorse/a584724153ad75aff3d6fdd81c5c9242 to your computer and use it in GitHub Desktop.
Save jhgorse/a584724153ad75aff3d6fdd81c5c9242 to your computer and use it in GitHub Desktop.
A simple push/pull USB HID API script for raw devices
#!/usr/bin/env ruby
# rvm use default
#### install for macOS
# \curl -sSL https://get.rvm.io | bash -s stable --ruby
# source $HOME/.rvm/scripts/rvm
# rvm use default
# gem install hidapi
require "hidapi"
STDOUT.sync = true # allow us to use pipes on the output
vendor_id = 0x10c4
product_id = 0x63cd
kext_handle = HIDAPI::SetupTaskHelper.new(
vendor_id, # vendor_id
product_id, # product_id
"irradiance", # simple_name
0 # interface
).run
handle = HIDAPI::open(0x10c4, 0x63cd)
# printf "%04X:%04X %02x Mfg: %-12s P: %s\n", 0x10c4, 0x63cd, 0, handle.get_manufacturer_string, handle.get_product_string
buf = Array.new(61) { |i| (0) };
buf[0] = 0x2
while 1 do
handle.write(buf)
rbuf = handle.read_timeout(buf.length,1000)
print rbuf.read_bytes(buf.length).unpack("C*"), "\n"
#rbuf.read_bytes(61).unpack("C*") # full packet
sleep(0.2)
end
HIDAPI::hid_close(handle)
@jhgorse
Copy link
Author

jhgorse commented Aug 5, 2016

if you want to use the HIDAPI layer via FFI, we need buffers like this:

# ffi hacking https://github.com/ffi/ffi/wiki/Examples

buffer = FFI::Buffer.new(:char, 61)
res = HidApi.hid_read_timeout handle, buffer, 61, 1000

@jhgorse
Copy link
Author

jhgorse commented May 30, 2020

still works 2020. must use rvm on macOS, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment