Skip to content

Instantly share code, notes, and snippets.

@mortyr45
Created July 8, 2024 10:09
Show Gist options
  • Select an option

  • Save mortyr45/1ebe891ee5d8d8319fdcc7d4b5768c51 to your computer and use it in GitHub Desktop.

Select an option

Save mortyr45/1ebe891ee5d8d8319fdcc7d4b5768c51 to your computer and use it in GitHub Desktop.
Detect raw hid reports from a device.
// https://docs.qmk.fm/features/rawhid
package main
import (
"fmt"
"os"
"github.com/sstallion/go-hid"
)
func main() {
// vendorID, err := strconv.ParseUint("0xbeeb", 0, 16)
// if err != nil {
// fmt.Printf("could not parse vendorID value: %s", err.Error())
// os.Exit(1)
// }
// productID, err := strconv.ParseUint("0x0002", 0, 16)
// if err != nil {
// fmt.Printf("could not parse productID value: %s", err.Error())
// os.Exit(1)
// }
// fmt.Printf("vendorID: %v, productID: %v", uint16(vendorID), uint16(productID))
// dev, err := hid.OpenFirst(uint16(productID), uint16(vendorID))
hid.Init()
dev, err := hid.OpenPath("/dev/hidraw2")
if err != nil {
fmt.Printf("could not open hid device: %s", err.Error())
os.Exit(1)
}
defer func() {
defer func() {
err := hid.Exit()
if err != nil {
fmt.Printf("could not finalize hid: %s", err.Error())
}
}()
err := dev.Close()
if err != nil {
fmt.Printf("could not close device: %s", err.Error())
os.Exit(1)
}
}()
info, err := dev.GetDeviceInfo()
if err != nil {
fmt.Printf("could not get device info: %s", err.Error())
os.Exit(1)
}
fmt.Println(info)
inputReport := make([]byte, 8)
numberOfBytes, err := dev.Read(inputReport)
if err != nil {
fmt.Printf("could not get input report: %s", err.Error())
os.Exit(1)
}
fmt.Printf("number of bytes: %v, bytes: %v", numberOfBytes, inputReport)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment