-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
103 lines (87 loc) · 2.53 KB
/
find.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package blink1
import (
"fmt"
"sort"
"sync"
hid "github.com/b1ug/gid"
)
var (
devInfoMu sync.RWMutex
devInfoList []*hid.DeviceInfo
)
// ListDeviceInfo returns all HID device info of all blink(1) devices which are connected to the system. The returned slice is sorted by serial number.
func ListDeviceInfo() []*hid.DeviceInfo {
infos := hid.ListAllDevices(IsBlink1Device)
sort.SliceStable(infos, func(i, j int) bool {
return infos[i].SerialNumber < infos[j].SerialNumber
})
return infos
}
// FindDeviceInfoBySerialNumber finds a connected blink(1) device with serial number and returns its HID device info.
func FindDeviceInfoBySerialNumber(sn string) (*hid.DeviceInfo, error) {
dev := hid.ListFirstDevice(func(di *hid.DeviceInfo) bool {
return IsBlink1Device(di) && di.SerialNumber == sn
})
// not found
if dev == nil {
return nil, fmt.Errorf("%w for %q", errDeviceNotFound, sn)
}
return dev, nil
}
// FindNextDeviceInfo returns the next HID device info of a blink(1) device which is connected to the system.
func FindNextDeviceInfo() (di *hid.DeviceInfo, err error) {
devInfoMu.Lock()
defer devInfoMu.Unlock()
// nil list: need init or reset
if devInfoList == nil {
devInfoList = ListDeviceInfo()
}
// empty list: last device already returned
if len(devInfoList) == 0 {
devInfoList = nil
return nil, errDeviceNotFound
}
di = devInfoList[0]
devInfoList = devInfoList[1:]
return di, nil
}
// OpenNextDevice opens the next blink(1) device which is connected to the system and returns as device.
func OpenNextDevice() (*Device, error) {
// find
di, err := FindNextDeviceInfo()
if err != nil {
return nil, err
}
// open
return OpenDevice(di)
}
// OpenNextController opens the next blink(1) device which is connected to the system and returns as controller.
func OpenNextController() (*Controller, error) {
// find
di, err := FindNextDeviceInfo()
if err != nil {
return nil, err
}
// open
return OpenController(di)
}
// OpenDeviceBySerialNumber finds a connected blink(1) device with serial number and opens it as device.
func OpenDeviceBySerialNumber(sn string) (*Device, error) {
// find
di, err := FindDeviceInfoBySerialNumber(sn)
if err != nil {
return nil, err
}
// open
return OpenDevice(di)
}
// OpenControllerBySerialNumber finds a connected blink(1) device with serial number and opens it as controller.
func OpenControllerBySerialNumber(sn string) (*Controller, error) {
// find
di, err := FindDeviceInfoBySerialNumber(sn)
if err != nil {
return nil, err
}
// open
return OpenController(di)
}