-
Notifications
You must be signed in to change notification settings - Fork 4
/
protocol.go
58 lines (48 loc) · 1.09 KB
/
protocol.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
package openrgb
import (
"bytes"
"encoding/binary"
)
var (
offset8BEBits = 1
offset16LEBits = 2
offset32LEBits = 4
)
const (
commandSetClientName = 50
commandRequestControllerCount = 0
commandRequestControllerData = 1
commandUpdateLEDs = 1050
commandUpdateZoneLEDs = 1051
commandSetCustomMode = 1100
)
type orgbHeader struct {
deviceID uint32
commandID uint32
length uint32
}
func readString(buf []byte, offset int) (string, int) {
length := int(binary.LittleEndian.Uint16(buf[offset:]))
b := buf[offset+2 : offset+length+1]
return string(b), length + 2
}
func encodeHeader(header orgbHeader) *bytes.Buffer {
b := bytes.NewBufferString("ORGB")
for _, v := range []uint32{
header.deviceID,
header.commandID,
header.length,
} {
buf := make([]byte, offset32LEBits)
binary.LittleEndian.PutUint32(buf, v)
b.Write(buf)
}
return b
}
func decodeHeader(buffer []byte) orgbHeader {
return orgbHeader{
binary.LittleEndian.Uint32(buffer[4:]),
binary.LittleEndian.Uint32(buffer[8:]),
binary.LittleEndian.Uint32(buffer[12:]),
}
}