forked from achushu/CH57x-keyboard-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardware.go
214 lines (183 loc) · 3.5 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
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 Mousecode:
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 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) BindMapping(mapping []*Macro) {
for _, m := range mapping {
err := k.BindKeyMacro(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
}