-
Notifications
You must be signed in to change notification settings - Fork 18
/
hardware.go
315 lines (273 loc) · 5.81 KB
/
hardware.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
package main
import (
"fmt"
"time"
"github.com/achushu/hid"
)
// manu: wch.cn, product: CH57x, vendorID 4489, productID: 34960
const (
VENDOR_ID = 4489
PRODUCT_ID = 34960
INTERFACE = 1 // the programmable interface
)
type MacroType uint8
const (
MACRONONE MacroType = 0x00
MACROKEYS = 0x01
MACROPLAY = 0x02
MACROMOUSE = 0x03
)
type Layer uint8
const (
LAYER1 Layer = 0x10
LAYER2 = 0x20
LAYER3 = 0x30
)
type Macro struct {
Type MacroType
Layer Layer
Key Key
Combo []Sequence
}
func NewMacro(key Key) *Macro {
return &Macro{
Layer: LAYER1,
Key: key,
Combo: make([]Sequence, 0),
}
}
func NewMacroSequence(key Key, seq Sequence) *Macro {
m := &Macro{
Layer: LAYER1,
Key: key,
Combo: []Sequence{seq},
}
switch seq.Key.(type) {
case Keycode:
m.Type = MACROKEYS
case Mediacode:
m.Type = MACROPLAY
case Mousecode:
m.Type = MACROMOUSE
case Wheelcode:
m.Type = MACROMOUSE
}
return m
}
func (m *Macro) Add(mod Modifier, key Code) error {
switch key.(type) {
case Keycode:
if m.Type == MACRONONE {
m.Type = MACROKEYS
} else if m.Type != MACROKEYS {
return ErrTypeMixing
}
case Mediacode:
if m.Type == MACRONONE {
m.Type = MACROPLAY
} else if m.Type != MACROPLAY {
return ErrTypeMixing
}
case Mousecode:
if m.Type == MACRONONE {
m.Type = MACROMOUSE
} else if m.Type != MACROMOUSE {
return ErrTypeMixing
}
}
m.Combo = append(m.Combo, Sequence{mod, key})
return nil
}
func (m *Macro) AddKey(key Code) error {
return m.Add(NOMOD, key)
}
func (m *Macro) Len() int {
return len(m.Combo)
}
type Sequence struct {
Mod Modifier
Key Code
}
var EmptySequence = Sequence{NOMOD, NOKEY}
type Key uint8
const (
KEY1 Key = iota + 1
KEY2
KEY3
KEY4
KEY5
KEY6
KEY7
KEY8
KEY9
KEY10
KEY11
KEY12
ROT1CCW
ROT1
ROT1CW
ROT2CCW
ROT2
ROT2CW
)
type Keyboard struct {
dev *hid.Device
}
func NewKeyboard(info hid.DeviceInfo) (*Keyboard, error) {
d, err := info.Open()
if err != nil {
return nil, err
}
return &Keyboard{d}, nil
}
func (k *Keyboard) Close() {
k.dev.Close()
}
func (k *Keyboard) Send(data []byte) error {
_, err := k.dev.Write(append([]byte{3}, data...))
time.Sleep(15 * time.Millisecond)
return err
}
func (k *Keyboard) SendHello() error {
req := make([]byte, 64)
return k.Send(req)
}
func (k *Keyboard) sendKeybindStart() error {
req := make([]byte, 64)
req[0] = 0xa1
req[1] = 0x01
return k.Send(req)
}
func (k *Keyboard) sendKeybindEnd() error {
req := make([]byte, 64)
req[0] = 0xaa
req[1] = 0xaa
return k.Send(req)
}
func (k *Keyboard) BindKeyMacro(macro *Macro) error {
var err error
err = k.sendKeybindStart()
if err != nil {
return err
}
// header
req := make([]byte, 64)
req[0] = byte(macro.Key) // key ID
req[1] = byte(macro.Layer) + byte(macro.Type) // layer and macro type
req[2] = byte(macro.Len()) // length
var combo []Sequence
if macro.Type == MACROKEYS {
// start key sequences with a blank (for some reason... bug?)
combo = append([]Sequence{EmptySequence}, macro.Combo...)
} else {
combo = macro.Combo
}
// bind the key sequence
for i, seq := range combo {
req[3] = byte(i)
req[4] = byte(seq.Mod)
req[5] = byte(seq.Key.Code())
err = k.Send(req)
if err != nil {
return err
}
}
return k.sendKeybindEnd()
}
func (k *Keyboard) BindMediaMacro(macro *Macro) error {
var err error
err = k.sendKeybindStart()
if err != nil {
return err
}
// header
req := make([]byte, 64)
req[0] = byte(macro.Key) // key ID
req[1] = byte(macro.Layer) + byte(macro.Type) // layer and macro type
var combo []Sequence
combo = macro.Combo
if (len(combo) > 1) {
//the rest of the keys will be ignored for now
fmt.Println("can't bind a media key macro larger then one key")
}
req[2] = byte(combo[0].Key.Code()) // media key code
//idk if there is anything you can do with the next bytes...
req[3] = byte(0x00)
req[4] = byte(0x00)
req[5] = byte(0x00)
err = k.Send(req)
if err != nil {
return err
}
return k.sendKeybindEnd()
}
func (k *Keyboard) BindMouseMacro(macro *Macro) error {
var err error
err = k.sendKeybindStart()
if err != nil {
return err
}
// header
req := make([]byte, 64)
req[0] = byte(macro.Key) // key ID
req[1] = byte(macro.Layer) + byte(macro.Type) // layer and macro type
var combo []Sequence
combo = macro.Combo
if (len(combo) > 1) {
//the rest of the buttons will be ignored for now
fmt.Println("can't bind a mouse macro larger then one key")
}
switch combo[0].Key.(type) {
case Mousecode:
req[2] = byte(combo[0].Key.Code()) // mouse button code
//idk if there is anything you can do with the next bytes...
req[3] = byte(0x00)
req[4] = byte(0x00)
req[5] = byte(0x00)
case Wheelcode:
req[2] = byte(0x00) // length?
req[3] = byte(0x00) // seq
req[4] = byte(0x00) // ??
req[5] = byte(combo[0].Key.Code()) // mouse wheel code
req[6] = byte(combo[0].Mod)
default:
fmt.Println("unknown mouse key type", combo[0].Key)
}
err = k.Send(req)
if err != nil {
return err
}
return k.sendKeybindEnd()
}
func (k *Keyboard) BindMacro(macro *Macro) error {
var err error
switch macro.Type {
case MACROKEYS:
err = k.BindKeyMacro(macro)
case MACROPLAY:
err = k.BindMediaMacro(macro)
case MACROMOUSE:
err = k.BindMouseMacro(macro)
default:
fmt.Println("binding a macro of type", macro.Type, "is unsupported")
err = ErrUnsupported
}
return err
}
func (k *Keyboard) BindMapping(mapping []*Macro) {
for _, m := range mapping {
err := k.BindMacro(m)
if err != nil {
fmt.Println("error binding key", m.Key, err)
} else {
fmt.Println("bound key", m.Key)
}
}
}
func MapKeys(seqs []Sequence) []*Macro {
mapping := make([]*Macro, len(seqs))
for i, s := range seqs {
mapping[i] = NewMacroSequence(Key(i+1), s)
}
return mapping
}