demo.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // usb - Self contained USB and HID library for Go
  2. // Copyright 2019 The library Authors
  3. //
  4. // This library is free software: you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation, either version 3 of the License, or (at your option) any
  7. // later version.
  8. //
  9. // The library is distributed in the hope that it will be useful, but WITHOUT ANY
  10. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. // A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public License along
  14. // with the library. If not, see <http://www.gnu.org/licenses/>.
  15. // +build none
  16. package main
  17. import (
  18. "fmt"
  19. "strings"
  20. "github.com/karalabe/usb"
  21. )
  22. func main() {
  23. // Enumerate all the HID devices in alphabetical path order
  24. hids, err := usb.EnumerateHid(0, 0)
  25. if err != nil {
  26. panic(err)
  27. }
  28. for i := 0; i < len(hids); i++ {
  29. for j := i + 1; j < len(hids); j++ {
  30. if hids[i].Path > hids[j].Path {
  31. hids[i], hids[j] = hids[j], hids[i]
  32. }
  33. }
  34. }
  35. for i, hid := range hids {
  36. fmt.Println(strings.Repeat("-", 128))
  37. fmt.Printf("HID #%d\n", i)
  38. fmt.Printf(" OS Path: %s\n", hid.Path)
  39. fmt.Printf(" Vendor ID: %#04x\n", hid.VendorID)
  40. fmt.Printf(" Product ID: %#04x\n", hid.ProductID)
  41. fmt.Printf(" Release: %d\n", hid.Release)
  42. fmt.Printf(" Serial: %s\n", hid.Serial)
  43. fmt.Printf(" Manufacturer: %s\n", hid.Manufacturer)
  44. fmt.Printf(" Product: %s\n", hid.Product)
  45. fmt.Printf(" Usage Page: %d\n", hid.UsagePage)
  46. fmt.Printf(" Usage: %d\n", hid.Usage)
  47. fmt.Printf(" Interface: %d\n", hid.Interface)
  48. }
  49. fmt.Println(strings.Repeat("=", 128))
  50. // Enumerate all the non-HID devices in alphabetical path order
  51. raws, err := usb.EnumerateRaw(0, 0)
  52. if err != nil {
  53. panic(err)
  54. }
  55. for i := 0; i < len(raws); i++ {
  56. for j := i + 1; j < len(raws); j++ {
  57. if raws[i].Path > raws[j].Path {
  58. raws[i], raws[j] = raws[j], raws[i]
  59. }
  60. }
  61. }
  62. for i, raw := range raws {
  63. fmt.Printf("RAW #%d\n", i)
  64. fmt.Printf(" OS Path: %s\n", raw.Path)
  65. fmt.Printf(" Vendor ID: %#04x\n", raw.VendorID)
  66. fmt.Printf(" Product ID: %#04x\n", raw.ProductID)
  67. fmt.Printf(" Interface: %d\n", raw.Interface)
  68. fmt.Println(strings.Repeat("-", 128))
  69. }
  70. }