Skip to content

Instantly share code, notes, and snippets.

@t33m
Created October 29, 2017 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t33m/5ecbd754f4b5d62acee60c5bf6c46438 to your computer and use it in GitHub Desktop.
Save t33m/5ecbd754f4b5d62acee60c5bf6c46438 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/google/gousb"
"github.com/google/gousb/usbid"
)
func main() {
ctx := gousb.NewContext()
defer ctx.Close()
ctx.Debug(3)
// var thePrinter *usb.Device
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
// The usbid package can be used to print out human readable information.
// fmt.Printf("Port: %d\n", desc.Port)
// fmt.Printf("Bus: %d\n", desc.Bus)
// fmt.Printf("Address: %d\n", desc.Address)
// fmt.Printf("Vendor: %s\n", desc.Vendor)
// fmt.Printf("Product: %s\n", desc.Product)
// fmt.Printf("Class: %s\n", desc.Class)
// fmt.Printf("Describe: %s\n", usbid.Describe(desc))
fmt.Printf("Classify: %s\n", usbid.Classify(desc))
for _, cfg := range desc.Configs {
// This loop just uses more of the built-in and usbid pretty printing to list
// the USB devices.
fmt.Printf(" %s:\n", cfg)
for _, intf := range cfg.Interfaces {
fmt.Printf(" --------------\n")
for _, ifSetting := range intf.AltSettings {
fmt.Printf(" %s\n", ifSetting)
fmt.Printf(" %s\n", usbid.Classify(ifSetting))
for _, end := range ifSetting.Endpoints {
fmt.Printf(" %s\n", end)
}
}
}
fmt.Printf(" --------------\n")
}
return false
})
defer func() {
for _, d := range devs {
d.Close()
}
}()
// OpenDevices can occaionally fail, so be sure to check its return value.
if err != nil {
log.Fatalf("list: %s", err)
}
for _, dev := range devs {
// Once the device has been selected from OpenDevices, it is opened
// and can be interacted with.
_ = dev
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment