-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
202 lines (175 loc) · 4.15 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
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
package main
import (
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/sstallion/go-hid"
)
func checksum(msg []byte) byte {
var checksumLower uint8 = 0x6
for _, b := range msg {
checksumLower ^= b
}
checksumLower &= 0xf
return checksumLower + (msg[len(msg)-1]^0xc0)&0xf0
}
type Property struct {
Name string
Description string
Min byte
Max byte
Value uint16
}
func main() {
properties := []Property{
{
Name: "brightness",
Min: 0,
Max: 100,
Value: 0x10,
},
{
Name: "contrast",
Min: 0,
Max: 100,
Value: 0x12,
},
{
Name: "sharpness",
Min: 0,
Max: 10,
Value: 0x87,
},
{
Name: "volume",
Min: 0,
Max: 100,
Value: 0x62,
},
{
Name: "low-blue-light",
Description: "Blue light reduction. 0 means no reduction.",
Min: 0,
Max: 10,
Value: 0xe00b,
},
{
Name: "kvm-switch",
Description: "Switch KVM to device 0 or 1",
Min: 0,
Max: 1,
Value: 0xe069,
},
{
Name: "colour-mode",
Description: "0 is cool, 1 is normal, 2 is warm, 3 is user-defined.",
Min: 0,
Max: 3,
Value: 0xe003,
},
{
Name: "rgb-red",
Description: "Red value -- only works if colour-mode is set to 3",
Min: 0,
Max: 100,
Value: 0xe004,
},
{
Name: "rgb-green",
Description: "Green value -- only works if colour-mode is set to 3",
Min: 0,
Max: 100,
Value: 0xe005,
},
{
Name: "rgb-blue",
Description: "Blue value -- only works if colour-mode is set to 3",
Min: 0,
Max: 100,
Value: 0xe006,
},
}
propMap := make(map[string]Property)
propHelp := []string{}
for _, p := range properties {
propMap[p.Name] = p
propText := fmt.Sprintf("\t%s (%d-%d)", p.Name, p.Min, p.Max)
if p.Description != "" {
propText = propText + "\n\t\t" + p.Description
}
propHelp = append(propHelp, propText)
}
prop := flag.String("prop", "", "Property to set. Available properties: \n"+strings.Join(propHelp, "\n"))
propNum := flag.Uint("propNum", 0, "Property number to set instead of -prop")
val := flag.Int("val", -1, "Value to set property to")
dryrun := flag.Bool("n", false, "Dry run: test commands and print instead")
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()
errExit := func(str string) {
fmt.Printf("ERROR: %s\n\n", str)
flag.Usage()
os.Exit(1)
}
if *prop == "" && *propNum == 0 {
// TODO: launch a repl or gui with tab completion instead here, or
// something like fish_config
errExit("-prop or -propNum is required")
}
if *val == -1 {
errExit("-val is required")
}
if *propNum != 0 && *prop != "" {
errExit("Specify only one of -prop or -propNum")
}
var prop16 uint16
if *propNum == 0 {
found, ok := propMap[*prop]
if !ok {
errExit(fmt.Sprintf("Unknown property: %s", *prop))
}
if *val > int(found.Max) || *val < int(found.Min) {
errExit(fmt.Sprintf("Value %d for property %s is not within range: %d-%d", *val, found.Name, found.Min, found.Max))
}
prop16 = found.Value
} else {
prop16 = uint16(*propNum)
}
// Buf is actually 192 bytes, but we need one for the report id
buf := make([]byte, 193)
buf[0] = 0
copy(buf[1:], []byte{0x40, 0xc6})
copy(buf[1+6:], []byte{0x20, 0, 0x6e, 0, 0x80})
var preamble []byte
msg := []byte{}
if prop16 > 0xff {
msg = append(msg, byte(prop16>>8))
prop16 &= 0xff
}
msg = append(msg, []byte{byte(prop16), 0, byte(*val)}...)
// TODO: 0x01 is read, 0x03 is write
preamble = []byte{0x51, byte(0x81 + len(msg)), 0x03}
copy(buf[1+0x40:], append(preamble, msg...))
if *dryrun {
log.Println("Would have sent:\n" + hex.Dump(buf))
return
}
err := hid.Init()
if err != nil {
log.Fatal(err)
}
dev, err := hid.OpenFirst(0x0bda, 0x1100)
if err != nil {
log.Fatal(err)
}
// TODO: get current value and nicely transition to the expected value like in
// TODO: read a value if "v" not specified, I think the value is in the byte
// 0xa of the response if we do a read
_, err = dev.Write(buf)
if err != nil {
log.Fatal(err)
}
log.Print("Property set.")
}