forked from antage/ssv2leds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
159 lines (134 loc) · 3.22 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"os"
"github.com/hanwen/usb"
)
const (
VendorId = 0x1038
ProductId = 0x1211
InterfaceNum = 3
Timeout = 1000
)
func frame(b1 byte, b2 byte) []byte {
result := [37]byte{
0x04, 0x40, 0x01, 0x11, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
}
result[4] = b1
result[5] = b2
return result[:]
}
func send(h *usb.DeviceHandle, b1 byte, b2 byte) error {
return h.ControlTransfer(
usb.ENDPOINT_OUT|usb.REQUEST_TYPE_CLASS|usb.RECIPIENT_INTERFACE,
0x09, // SET_REPORT
0x0204, // Output, ID: 4
InterfaceNum,
frame(b1, b2),
Timeout,
)
}
func process(h *usb.DeviceHandle) error {
err := h.ClaimInterface(InterfaceNum)
if err != nil {
fmt.Fprintf(os.Stderr, "ClaimInterface failed: %s\n", err)
return err
}
defer h.ReleaseInterface(InterfaceNum)
if *intensity >= 0 {
err = send(h, 0x89, byte(*intensity))
if err != nil {
fmt.Fprintf(os.Stderr, "ControlTransfer failed: %s\n", err)
return err
}
}
if *pulse != "" {
switch *pulse {
case "steady":
err = send(h, 0x87, 0x02)
case "slow":
err = send(h, 0x87, 0x22)
case "medium":
err = send(h, 0x87, 0x26)
case "fast":
err = send(h, 0x87, 0x2A)
case "trigger":
err = send(h, 0x87, 0x12)
}
if err != nil {
fmt.Fprintf(os.Stderr, "ControlTransfer failed: %s\n", err)
return err
}
}
return nil
}
var intensity *int = flag.Int("i", -1, "LEDs intensity (0-255)")
var pulse *string = flag.String("p", "", "LEDs pulsating mode (steady, slow, medium, fast, trigger)")
func main() {
flag.Parse()
if *intensity > 255 {
fmt.Fprintf(os.Stderr, "Intensity should be in range 0-255\n")
os.Exit(2)
}
if *pulse != "" && *pulse != "steady" && *pulse != "slow" && *pulse != "medium" && *pulse != "fast" && *pulse != "trigger" {
fmt.Fprintf(os.Stderr, "Pulse mode should be steady, slow, medium, fast or trigger")
os.Exit(2)
}
ctx := usb.NewContext()
defer ctx.Exit()
devices, err := ctx.GetDeviceList()
if err != nil {
fmt.Fprintf(os.Stderr, "GetDeviceList failed: %s\n", err)
os.Exit(1)
}
var device *usb.Device
func() {
defer devices.Done()
for _, d := range devices {
dd, err := d.GetDeviceDescriptor()
if err != nil {
fmt.Fprintf(os.Stderr, "GetDeviceDescriptor failed: %s\n", err)
continue
}
if dd.IdVendor == VendorId && dd.IdProduct == ProductId {
fmt.Printf("Found SteelSeries v2 headset\n")
device = d.Ref()
break
}
}
}()
if device == nil {
fmt.Fprintf(os.Stderr, "Can't find supported device\n")
os.Exit(1)
}
defer device.Unref()
h, err := device.Open()
if err != nil {
fmt.Fprintf(os.Stderr, "Open device failed: %s\n", err)
os.Exit(1)
}
defer h.Close()
active, err := h.KernelDriverActive(InterfaceNum)
if err != nil {
fmt.Fprintf(os.Stderr, "KernelDriverActive filed: %s\n", err)
os.Exit(1)
}
if active {
func() {
err := h.DetachKernelDriver(InterfaceNum)
if err != nil {
fmt.Fprintf(os.Stderr, "DetachKernelDriver failed: %s\n", err)
os.Exit(1)
}
defer h.AttachKernelDriver(InterfaceNum)
process(h)
}()
} else {
process(h)
}
}