-
Notifications
You must be signed in to change notification settings - Fork 0
/
glorious.go
380 lines (306 loc) · 7.62 KB
/
glorious.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"github.com/sstallion/go-hid"
)
type Device struct {
hid *hid.Device
mouse *GloriousMouse
conf *GloriousConfig
dbt int
}
const CFG_SIZE_USED = 131
type RGBEffect uint8
type DPI uint8
type GloriousMouse struct {
vid uint16
pid uint16
name string
path string
}
type RGB8 struct {
R, G, B uint8
}
type GloriousConfig struct {
ReportID uint8
CommandID uint8
Unk1 uint8
ConfigWrite uint8
Unk2 [6]uint8
Config1 uint8
DpiCount uint8
ActiveDpi uint8
DpiEnabled uint8
Dpi [16]uint8
DpiColour [8]RGB8
RgbEffect RGBEffect
GloriousMode uint8
GloriousDirection uint8
SingleMode uint8
SingleColour RGB8
Breathing7Mode uint8
Breathing7ColourCount uint8
Breathing7Colours [7]RGB8
TailMode uint8
Unk4 [33]uint8
RaveMode uint8
RaveColours [2]RGB8
WaveMode uint8
Breathing1Mode uint8
Breathing1Colour RGB8
Unk5 uint8
LiftOffDistance uint8
}
var SupportedDevices = []*GloriousMouse{
{vid: 0x258a, pid: 0x27, name: "Dream Machines DM5"},
{vid: 0x258a, pid: 0x33, name: "Glorious Model D"},
{vid: 0x258a, pid: 0x36, name: "Glorious Model O/O-"},
}
//RGB Effect Enums
const (
RGB_OFF RGBEffect = 0
RGB_GLORIOUS RGBEffect = 0x1
RGB_SINGLE RGBEffect = 0x2
RGB_BREATHING RGBEffect = 0x5
RGB_BREATHING7 RGBEffect = 0x3
RGB_BREATHING1 RGBEffect = 0xa
RGB_TAIL RGBEffect = 0x4
RGB_RAVE RGBEffect = 0x7
RGB_WAVE RGBEffect = 0x9
)
func (r RGBEffect) Name() (string, bool) {
switch r {
case RGB_OFF:
return "Off", true
case RGB_GLORIOUS:
return "Glorious Mode", true
case RGB_SINGLE:
return "Single Color", true
case RGB_BREATHING:
return "RGB Breathing", true
case RGB_BREATHING7:
return "Seven-color Breathing", true
case RGB_BREATHING1:
return "Single color Breathing", true
case RGB_TAIL:
return "Tail Effect", true
case RGB_RAVE:
return "Two-color Rave", true
case RGB_WAVE:
return "Wave Effect", true
default:
return "", false
}
}
func (c *GloriousConfig) Mode(effect RGBEffect) (uint8, bool) {
switch effect {
case RGB_GLORIOUS:
return c.GloriousMode, true
case RGB_SINGLE:
return c.SingleMode, true
case RGB_BREATHING7:
return c.Breathing7Mode, true
case RGB_BREATHING1:
return c.Breathing1Mode, true
case RGB_TAIL:
return c.TailMode, true
case RGB_RAVE:
return c.RaveMode, true
case RGB_WAVE:
return c.WaveMode, true
default:
return 0x0, false
}
}
func NameToRGBEffect(n string) (RGBEffect, bool) {
switch n {
case "Off":
return RGB_OFF, true
case "Glorious Mode":
return RGB_GLORIOUS, true
case "Single Color":
return RGB_SINGLE, true
case "RGB Breathing":
return RGB_BREATHING, true
case "Seven-color Breathing":
return RGB_BREATHING7, true
case "Single color Breathing":
return RGB_BREATHING1, true
case "Tail Effect":
return RGB_TAIL, true
case "Two-color Rave":
return RGB_RAVE, true
case "Wave Effect":
return RGB_WAVE, true
default:
return RGB_OFF, false
}
}
func (m *GloriousMouse) Path() (string, bool) {
if m.path == "" {
return "", false
}
return m.path, true
}
func (m *GloriousMouse) enumFunc(info *hid.DeviceInfo) error {
if info.InterfaceNbr == 1 {
m.path = info.Path
}
return nil
}
func GetDevice(m *GloriousMouse) {
hid.Enumerate(m.vid, m.pid, m.enumFunc)
}
// Commands start here
func (dev *Device) GetConfig() *GloriousConfig {
conf := [6]byte{0x5, 0x11}
res, err := dev.hid.SendFeatureReport(conf[:])
if err != nil || res != len(conf) {
log.Fatalln("res:", res, "in get config cmd, go err:", err, "hid err:", dev.hid.Error())
}
cfg := GloriousConfig{
ReportID: 0x4,
}
emptyBytes := 520 - binary.Size(cfg)
var buf bytes.Buffer
err = binary.Write(&buf, binary.BigEndian, cfg)
if err != nil {
log.Fatalln(err)
}
err = binary.Write(&buf, binary.BigEndian, make([]byte, emptyBytes))
if err != nil {
log.Fatalln(err)
}
d := buf.Bytes()
res, err = dev.hid.GetFeatureReport(d)
if err != nil || res < 1 {
log.Fatalln("res:", res, "in read config, go err:", err, "hid err:", dev.hid.Error())
}
dev.conf = Read(d)
return dev.conf
}
func (dev *Device) SetConfig() error {
if dev.conf == nil {
return fmt.Errorf("conf is nil")
}
dev.conf.ConfigWrite = CFG_SIZE_USED - 8
confBytes, err := dev.conf.Write()
if err != nil {
return err
}
res, err := dev.hid.SendFeatureReport(confBytes)
if res == -1 || err != nil {
return fmt.Errorf("error writing config: (%v) res: %v", err, res)
}
return nil
}
func (dev *Device) GetFirmwareVersion() string {
version := [6]byte{0x5, 0x1}
res, err := dev.hid.SendFeatureReport(version[:])
if err != nil || res != len(version) {
log.Fatalln("res:", res, "in get firmware version cmd, go err:", err, "hid err:", dev.hid.Error())
}
res, err = dev.hid.GetFeatureReport(version[:])
if err != nil || res != len(version) {
log.Fatalln("res:", res, "in read firmware version, go err:", err, "hid err:", dev.hid.Error())
}
return fmt.Sprintf("%c", version)
}
func (dev *Device) GetDebounceTime() int {
debounce := [6]byte{0x5, 0x1a}
res, err := dev.hid.SendFeatureReport(debounce[:])
if err != nil || res != len(debounce) {
log.Fatalln("res:", res, "in get debounce time cmd, go err:", err, "hid err:", dev.hid.Error())
}
res, err = dev.hid.GetFeatureReport(debounce[:])
if err != nil || res != len(debounce) {
log.Fatalln("res:", res, "in read debounce time, go err:", err, "hid err:", dev.hid.Error())
}
dev.dbt = int(debounce[2] * 2)
return dev.dbt
}
func (dev *Device) SetDebounceTime(dbt int) error {
debounce := [6]byte{0x5, 0x1a, byte(dbt / 2)}
res, err := dev.hid.SendFeatureReport(debounce[:])
if err != nil || res != len(debounce) {
log.Fatalln("res:", res, "in set debounce time, go err:", err, "hid err:", dev.hid.Error())
}
return err
}
func (c *GloriousConfig) SetActiveDPI(opt int) error {
if opt < 1 || opt > 6 {
return fmt.Errorf("opt too high or low")
}
c.ActiveDpi = uint8(opt)
return nil
}
func (c *GloriousConfig) SetDPI(opt int, dpi int) error {
opt--
if dpi < 200 {
return fmt.Errorf("dpi is too low")
}
if opt < 0 || opt > 5 {
return fmt.Errorf("opt too high or low")
}
c.Dpi[opt] = uint8(dpi/100 - 1)
return nil
}
func (c *GloriousConfig) GetRGBMode() (int, int, error) {
mode, ok := c.Mode(c.RgbEffect)
if !ok {
return 0, 0, fmt.Errorf("no fitting rgb mode")
}
brightness := int(mode >> 4)
speed := int(mode & 0x0f)
return brightness, speed, nil
}
func (c *GloriousConfig) SetRGBMode(brightness int, speed int) error {
newModeSetting := uint8(speed) | uint8(brightness)<<4
switch c.RgbEffect {
case RGB_GLORIOUS:
c.GloriousMode = newModeSetting
case RGB_SINGLE:
c.SingleMode = newModeSetting
case RGB_BREATHING7:
c.Breathing7Mode = newModeSetting
case RGB_BREATHING1:
c.Breathing1Mode = newModeSetting
case RGB_TAIL:
c.TailMode = newModeSetting
case RGB_RAVE:
c.RaveMode = newModeSetting
case RGB_WAVE:
c.WaveMode = newModeSetting
}
return nil
}
func (c *GloriousConfig) SetRGBEffect(effect RGBEffect) {
c.RgbEffect = effect
}
func (c *GloriousConfig) SetRGBBrightness(brightness int) error {
if brightness < 0 || brightness > 4 {
return fmt.Errorf("brightness level too high or low")
}
switch c.RgbEffect {
case RGB_GLORIOUS:
return fmt.Errorf("brightness can not be set on RGB_GLORIOUS")
}
_, speed, err := c.GetRGBMode()
if err != nil {
return err
}
return c.SetRGBMode(brightness, speed)
}
func (c *GloriousConfig) SetRGBSpeed(speed int) error {
if speed < 0 || speed > 3 {
return fmt.Errorf("speed level too high or low")
}
brightness, _, err := c.GetRGBMode()
if err != nil {
return err
}
return c.SetRGBMode(brightness, speed)
}