-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdevice.go
97 lines (79 loc) · 1.83 KB
/
device.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
package openrgb
import (
"encoding/binary"
"fmt"
)
// Device represents a controller registered by the OpenRGB Server.
type Device struct {
Type uint32
Name string
Description string
Version string
Serial string
Location string
ActiveMode uint32
LEDs []LED
Colors []Color
Modes []Mode
Zones []Zone
}
func readDevice(buf []byte) (Device, error) {
var d Device
offset := offset32LEBits
d.Type = binary.LittleEndian.Uint32(buf[4:])
offset += offset32LEBits
for _, st := range []*string{
&d.Name,
&d.Description,
&d.Version,
&d.Serial,
&d.Location,
} {
s, i := readString(buf, offset)
offset += i
*st = s
}
modeCount := binary.LittleEndian.Uint16(buf[offset:])
offset += offset16LEBits
d.ActiveMode = binary.LittleEndian.Uint32(buf[offset:])
offset += offset32LEBits
modes, i, err := readMode(buf, modeCount, offset)
if err != nil {
return Device{}, err
}
offset = i
d.Modes = modes
zoneCount := binary.LittleEndian.Uint16(buf[offset:])
offset += offset16LEBits
zones, i := readZones(buf, zoneCount, offset)
d.Zones = zones
offset = i
ledCount := binary.LittleEndian.Uint16(buf[offset:])
offset += offset16LEBits
leds, i, err := readLEDs(buf, ledCount, offset)
if err != nil {
return Device{}, err
}
offset = i
d.LEDs = leds
colorCount := binary.LittleEndian.Uint16(buf[offset:])
offset += offset16LEBits
d.Colors = make([]Color, 0)
for i := uint16(0); i < colorCount; i++ {
color, err := readColor(buf, offset)
if err != nil {
return Device{}, err
}
d.Colors = append(d.Colors, color)
offset += 4
}
return d, nil
}
func (d Device) String() string {
return fmt.Sprintf(`%s (typ %d; ver %s; ser %s)
Mode - Active: %d; Total: %d
%v
---`,
d.Name, d.Type, d.Version, d.Serial,
d.ActiveMode, len(d.Modes), d.Modes[d.ActiveMode])
}