๐Ÿ“ฆ karalabe / usb

๐Ÿ“„ demo.go ยท 77 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77// usb - Self contained USB and HID library for Go
// Copyright 2019 The library Authors
//
// This library is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// The library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with the library. If not, see <http://www.gnu.org/licenses/>.

// +build none

package main

import (
	"fmt"
	"strings"

	"github.com/karalabe/usb"
)

func main() {
	// Enumerate all the HID devices in alphabetical path order
	hids, err := usb.EnumerateHid(0, 0)
	if err != nil {
		panic(err)
	}
	for i := 0; i < len(hids); i++ {
		for j := i + 1; j < len(hids); j++ {
			if hids[i].Path > hids[j].Path {
				hids[i], hids[j] = hids[j], hids[i]
			}
		}
	}
	for i, hid := range hids {
		fmt.Println(strings.Repeat("-", 128))
		fmt.Printf("HID #%d\n", i)
		fmt.Printf("  OS Path:      %s\n", hid.Path)
		fmt.Printf("  Vendor ID:    %#04x\n", hid.VendorID)
		fmt.Printf("  Product ID:   %#04x\n", hid.ProductID)
		fmt.Printf("  Release:      %d\n", hid.Release)
		fmt.Printf("  Serial:       %s\n", hid.Serial)
		fmt.Printf("  Manufacturer: %s\n", hid.Manufacturer)
		fmt.Printf("  Product:      %s\n", hid.Product)
		fmt.Printf("  Usage Page:   %d\n", hid.UsagePage)
		fmt.Printf("  Usage:        %d\n", hid.Usage)
		fmt.Printf("  Interface:    %d\n", hid.Interface)
	}
	fmt.Println(strings.Repeat("=", 128))

	// Enumerate all the non-HID devices in alphabetical path order
	raws, err := usb.EnumerateRaw(0, 0)
	if err != nil {
		panic(err)
	}
	for i := 0; i < len(raws); i++ {
		for j := i + 1; j < len(raws); j++ {
			if raws[i].Path > raws[j].Path {
				raws[i], raws[j] = raws[j], raws[i]
			}
		}
	}
	for i, raw := range raws {
		fmt.Printf("RAW #%d\n", i)
		fmt.Printf("  OS Path:    %s\n", raw.Path)
		fmt.Printf("  Vendor ID:  %#04x\n", raw.VendorID)
		fmt.Printf("  Product ID: %#04x\n", raw.ProductID)
		fmt.Printf("  Interface:  %d\n", raw.Interface)
		fmt.Println(strings.Repeat("-", 128))
	}
}