Thanks to visit codestin.com
Credit goes to github.com

Skip to content

alexbatalov/crystal-usb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crystal-usb

libusb for Crystal.

Installation

  1. Install libusb-1.0:

In macOS:

$ brew install libusb

In Debian/Ubuntu:

$ sudo apt-get install libusb-1.0
  1. Add the dependency to your shard.yml:
dependencies:
  usb:
    github: alexbatalov/crystal-usb
  1. Run shards install

Usage

At this time the library exposes everything libusb has to offer through LibUSB bindings. These bindings are (hopefully) stable and will follow libusb releases.

require "usb"

r = LibUSB.init(out ctx)
raise "libusb_init" if r < 0

cnt = LibUSB.get_device_list(ctx, out devices)
raise "libusb_get_device_list" if cnt < 0

cnt.times do |i|
  device = devices[i]

  LibUSB.get_device_descriptor(device, out device_descriptor)
  next unless device_descriptor.vendor_id == 0x0001 && device_descriptor.product_id == 0x0002

  r = LibUSB.open(device, out device_handle)
  raise "libusb_open" if r < 0

  r = LibUSB.claim_interface(device_handle, 0x00)
  raise "libusb_claim_interface" if r < 0

  data = Bytes.new(2)
  r = LibUSB.interrupt_transfer(device_handle, 0x81, data, data.size, out transferred, 0)
  raise "libusb_interrupt_transfer" if r < 0

  r = LibUSB.release_interface(device_handle, 0x00)
  raise "libusb_release_interface" if r < 0

  LibUSB.close(device_handle)
end

LibUSB.free_device_list(devices, 1)

LibUSB.exit(ctx)

I'm working on wrapping this C-like mess into high-level objects.

Roadmap

  • Bindings
  • Core API
  • Examples

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published