Skip to content

Instantly share code, notes, and snippets.

@aycabta
Last active December 26, 2016 19:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
require 'libusb'
VENDOR_ID = 0x0922
PRODUCT_ID = 0x8003
SCALING_FACTOR = 0.1
usb_context = LIBUSB::Context.new
device = usb_context.devices(idVendor: VENDOR_ID, idProduct: PRODUCT_ID).first
unless device
puts 'Device not found'
exit 1
end
begin
handle = device.open
handle.detach_kernel_driver(0)
rescue
ensure
handle.close
end
endpoint = device.endpoints.first
handle = device.open
loop do
data = handle.interrupt_transfer(endpoint: endpoint, dataIn: 0x0006, timeout: 100)
# data.bytes[1]: 4 is negative, 2 is zero, 5 is positive
case data.bytes[3]
when 0 # kg mode
grams = data.bytes[4] + (256 * data.bytes[5])
puts "weight: #{data.bytes[1] == 5 ? '-' : ''}#{grams}g"
when 255 # lb mode
ounces = SCALING_FACTOR * (data.bytes[4] + (256 * data.bytes[5]))
puts "weight: #{data.bytes[1] == 5 ? '-' : ''}#{'%.1f' % ounces}oz"
end
sleep 1
end
handle.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment