Update uses of List/OpenDevices

This commit is contained in:
Sebastian Zagrodzki
2017-06-13 10:56:35 +02:00
parent 67ddfbabc2
commit f87a74e39f
2 changed files with 10 additions and 10 deletions

View File

@@ -39,8 +39,8 @@ func main() {
// Debugging can be turned on; this shows some of the inner workings of the libusb package. // Debugging can be turned on; this shows some of the inner workings of the libusb package.
ctx.Debug(*debug) ctx.Debug(*debug)
// ListDevices is used to find the devices to open. // OpenDevices is used to find the devices to open.
devs, err := ctx.ListDevices(func(desc *gousb.DeviceDesc) bool { devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
// The usbid package can be used to print out human readable information. // The usbid package can be used to print out human readable information.
fmt.Printf("%03d.%03d %s:%s %s\n", desc.Bus, desc.Address, desc.Vendor, desc.Product, usbid.Describe(desc)) fmt.Printf("%03d.%03d %s:%s %s\n", desc.Bus, desc.Address, desc.Vendor, desc.Product, usbid.Describe(desc))
fmt.Printf(" Protocol: %s\n", usbid.Classify(desc)) fmt.Printf(" Protocol: %s\n", usbid.Classify(desc))
@@ -71,20 +71,20 @@ func main() {
return false return false
}) })
// All Devices returned from ListDevices must be closed. // All Devices returned from OpenDevices must be closed.
defer func() { defer func() {
for _, d := range devs { for _, d := range devs {
d.Close() d.Close()
} }
}() }()
// ListDevices can occaionally fail, so be sure to check its return value. // OpenDevices can occaionally fail, so be sure to check its return value.
if err != nil { if err != nil {
log.Fatalf("list: %s", err) log.Fatalf("list: %s", err)
} }
for _, dev := range devs { for _, dev := range devs {
// Once the device has been selected from ListDevices, it is opened // Once the device has been selected from OpenDevices, it is opened
// and can be interacted with. // and can be interacted with.
_ = dev _ = dev
} }

View File

@@ -107,8 +107,8 @@ func main() {
} }
log.Printf("Scanning for device %q...", devName) log.Printf("Scanning for device %q...", devName)
// ListDevices is used to find the devices to open. // OpenDevices is used to find the devices to open.
devs, err := ctx.ListDevices(func(desc *gousb.DeviceDesc) bool { devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
switch { switch {
case vid == desc.Vendor && pid == desc.Product: case vid == desc.Vendor && pid == desc.Product:
return true return true
@@ -117,16 +117,16 @@ func main() {
} }
return false return false
}) })
// All Devices returned from ListDevices must be closed. // All Devices returned from OpenDevices must be closed.
defer func() { defer func() {
for _, d := range devs { for _, d := range devs {
d.Close() d.Close()
} }
}() }()
// ListDevices can occasionally fail, so be sure to check its return value. // OpenDevices can occasionally fail, so be sure to check its return value.
if err != nil { if err != nil {
log.Printf("Warning: ListDevices: %s.", err) log.Printf("Warning: OpenDevices: %s.", err)
} }
switch { switch {
case len(devs) == 0: case len(devs) == 0: