### Install usbhid Go Module Source: https://rafaelmartins.com/p/usbhid/development-guide Installs the usbhid module using the go get command. Requires Go 1.19 or later. ```bash go get rafaelmartins.com/p/usbhid ``` -------------------------------- ### Get and Set Feature Reports Source: https://rafaelmartins.com/p/usbhid/development-guide Demonstrates how to read a feature report by ID using GetFeatureReport and write a feature report using SetFeatureReport. Note that SetFeatureReport has the same overflow rules as SetOutputReport. ```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) } ``` -------------------------------- ### Filter and Get a Single USB Device Source: https://rafaelmartins.com/p/usbhid/development-guide Retrieves a single USB HID device matching a filter function. Optionally opens the device and acquires an exclusive lock. The second argument ('open') controls immediate opening, and the third ('lock') requests an exclusive lock. ```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) } def device.Close() ``` -------------------------------- ### Import usbhid Package Source: https://rafaelmartins.com/p/usbhid/development-guide Imports the usbhid package for use in Go programs. ```go import "rafaelmartins.com/p/usbhid" ``` -------------------------------- ### Enumerate All Connected USB HID Devices Source: https://rafaelmartins.com/p/usbhid This snippet shows how to enumerate all connected USB HID devices. It iterates through the devices and prints their basic information. ```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()) } ``` -------------------------------- ### Find and Open a Specific USB HID Device Source: https://rafaelmartins.com/p/usbhid This snippet demonstrates how to find and open a specific USB HID device using a filter function based on Vendor ID and Product ID. The device is closed using defer. ```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() ``` -------------------------------- ### Open and Close a USB Device Source: https://rafaelmartins.com/p/usbhid/development-guide Opens a USB device for I/O, optionally requesting an exclusive lock. Close releases the device and any held lock. Open returns ErrDeviceIsOpen if already open, ErrDeviceIsClosed if closed, and ErrDeviceLocked if another application holds the lock. ```go if err := device.Open(true); err != nil { log.Fatal(err) } def device.Close() ``` -------------------------------- ### Write Output Report to USB Device Source: https://rafaelmartins.com/p/usbhid/development-guide Sends an output report to the USB device. Provide the report ID and the data. Returns ErrReportBufferOverflow if the data exceeds the expected report size. ```go err := device.SetOutputReport(0x01, []byte{0x00, 0x01, 0x02}) if err != nil { log.Fatal(err) } ``` -------------------------------- ### Read Input Report from USB Device Source: https://rafaelmartins.com/p/usbhid/development-guide Blocks until an input report is available from the device, then returns the report ID and data. This call blocks the calling goroutine; consider using a dedicated goroutine for non-blocking reads. ```go reportId, data, err := device.GetInputReport() if err != nil { log.Fatal(err) } fmt.Printf("Report ID: %d, Data: %x\n", reportId, data) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.