Last active
December 26, 2016 19:44
-
-
Save aycabta/93cd33a1e86dfd11a492c1782ff64faa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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