### Install usbhid Package Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md Install the usbhid package using go get. The module requires Go 1.19 or later. ```bash go get rafaelmartins.com/p/usbhid ``` -------------------------------- ### Get a Specific USB HID Device with Lock Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md Use Get to retrieve a single device matching a filter function. The 'open' and 'lock' arguments control immediate opening and exclusive access. ```go device, err := usbhid.Get(func(d *usbhid.Device) bool { return d.VendorId() == 0x16c0 && d.ProductId() == 0x05df }, true, true) if err != nil { log.Fatal(err) } defer device.Close() ``` -------------------------------- ### Import usbhid Package Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md Import the usbhid package into your Go project to use its functionalities. ```go import "rafaelmartins.com/p/usbhid" ``` -------------------------------- ### Enumerate All Connected USB HID Devices Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/00_index.md Iterates through all connected USB HID devices and prints their basic information. This is useful for discovering available devices. ```go devices, err := usbhid.Enumerate(nil) if err != nil { log.Fatal(err) } for _, device := range devices { fmt.Printf("Device: 0x%04x:0x%04x\n", device.VendorId(), device.ProductId()) fmt.Printf("\tManufacturer: %s\n", device.Manufacturer()) fmt.Printf("\tProduct: %s\n", device.Product()) fmt.Printf("\tSerial Number: %s\n", device.SerialNumber()) fmt.Printf("\tUsage: 0x%04x/0x%04x\n", device.UsagePage(), device.Usage()) } ``` -------------------------------- ### Send Output Report Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md Use SetOutputReport to send data to a device. Ensure the data does not exceed the report size to avoid ErrReportBufferOverflow. ```go err := device.SetOutputReport(0x01, []byte{0x00, 0x01, 0x02}) if err != nil { log.Fatal(err) } ``` -------------------------------- ### Open Device with Exclusive Lock Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md Open a device for I/O, requesting an exclusive lock to prevent concurrent access. Ensure to defer Close() to release the device and lock. ```go if err := device.Open(true); err != nil { log.Fatal(err) } defer device.Close() ``` -------------------------------- ### Find and Open a Specific USB HID Device Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/00_index.md Opens a specific USB HID device based on a filter function that matches Vendor ID and Product ID. It also demonstrates exclusive locking and the defer close pattern. ```go device, err := usbhid.Get(func(d *usbhid.Device) bool { return d.VendorId() == 0x16c0 && d.ProductId() == 0x05df }, true, true) if err != nil { log.Fatal(err) } deffer device.Close() ``` -------------------------------- ### Read Input Report Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md Read an input report from the device. This call blocks until a report is available. Results are returned as report ID and data. ```go reportId, data, err := device.GetInputReport() if err != nil { log.Fatal(err) } fmt.Printf("Report ID: %d, Data: %x\n", reportId, data) ``` -------------------------------- ### Read and Write Feature Reports Source: https://github.com/rafaelmartins/usbhid/blob/main/docs/10_development-guide.md GetFeatureReport reads a feature report by ID, and SetFeatureReport writes one. Be mindful of report buffer overflow rules similar to output reports. ```go data, err := device.GetFeatureReport(0x01) if err != nil { log.Fatal(err) } err = device.SetFeatureReport(0x01, []byte{0x00, 0x01}) if err != nil { log.Fatal(err) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.