Skip to content

Commit 8312259

Browse files
authored
Add an interface to configure Combos (#65)
1 parent 64eaa97 commit 8312259

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

combo.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build tinygo
2+
3+
package keyboard
4+
5+
type Combo struct {
6+
Keys [4]Keycode
7+
OutputKey Keycode
8+
}
9+
10+
func (d *Device) SetCombo(index int, c Combo) {
11+
d.Combos[index][0] = c.Keys[0]
12+
d.Combos[index][1] = c.Keys[1]
13+
d.Combos[index][2] = c.Keys[2]
14+
d.Combos[index][3] = c.Keys[3]
15+
d.Combos[index][4] = c.OutputKey
16+
}

keyboard.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ func (d *Device) Init() error {
157157
offset += macroSize
158158

159159
for idx := range device.Combos {
160+
skip := true
161+
for i := 0; i < 10; i++ {
162+
if rbuf[offset+i] != 0xFF {
163+
skip = false
164+
}
165+
}
166+
if skip {
167+
continue
168+
}
160169
device.Combos[idx][0] = Keycode(rbuf[offset+0]) + Keycode(rbuf[offset+1])<<8 // key 1
161170
device.Combos[idx][1] = Keycode(rbuf[offset+2]) + Keycode(rbuf[offset+3])<<8 // key 2
162171
device.Combos[idx][2] = Keycode(rbuf[offset+4]) + Keycode(rbuf[offset+5])<<8 // key 3
@@ -261,7 +270,7 @@ func (d *Device) Tick() error {
261270
x := d.kb[kbidx].Key(layer, index)
262271
for _, combo := range d.Combos {
263272
for _, ckey := range combo[:4] {
264-
if keycodeViaToTGK(ckey) == x {
273+
if ckey == x {
265274
uniq := true
266275
for _, f := range d.combosFounds {
267276
if f == ckey {
@@ -325,7 +334,7 @@ func (d *Device) Tick() error {
325334
for xx := range d.combosPressed {
326335
kbidx, layer, index := decKey(xx)
327336
x := d.kb[kbidx].Key(layer, index)
328-
if keycodeViaToTGK(ckey) == x {
337+
if ckey == x {
329338
matchCnt++
330339
}
331340
}
@@ -334,7 +343,7 @@ func (d *Device) Tick() error {
334343
if matchCnt >= 2 && zero+matchCnt == 4 && matchCnt > matchMax && len(d.combosPressed) == matchCnt {
335344
matched = true
336345
matchMax = matchCnt
337-
d.combosKey = 0xFF000000 | uint32(keycodeViaToTGK(combo[4]))
346+
d.combosKey = 0xFF000000 | uint32(combo[4])
338347
}
339348
}
340349

@@ -746,7 +755,13 @@ func (d *Device) KeyVia(layer, kbIndex, index int) Keycode {
746755
return 0
747756
}
748757
kc := d.kb[kbIndex].Key(layer, index)
758+
return keycodeTGKtoVia(kc)
759+
}
760+
761+
func keycodeTGKtoVia(kc Keycode) Keycode {
749762
switch kc {
763+
case 0x0000:
764+
kc = 0x0000
750765
case keycodes.MouseLeft:
751766
kc = 0x00D1
752767
case keycodes.MouseRight:
@@ -825,6 +840,8 @@ func keycodeViaToTGK(key Keycode) Keycode {
825840
kc := key | 0xF000
826841

827842
switch key {
843+
case 0x0000:
844+
kc = 0x0000
828845
case 0x00D1:
829846
kc = keycodes.MouseLeft
830847
case 0x00D2:

via.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,28 +241,28 @@ func rxHandler2(b []byte) bool {
241241
case dynamicVialComboGet:
242242
txb[0] = 0x00
243243
idx := b[3]
244-
txb[1] = byte(device.Combos[idx][0])
245-
txb[2] = byte(device.Combos[idx][0] >> 8)
246-
txb[3] = byte(device.Combos[idx][1])
247-
txb[4] = byte(device.Combos[idx][1] >> 8)
248-
txb[5] = byte(device.Combos[idx][2])
249-
txb[6] = byte(device.Combos[idx][2] >> 8)
250-
txb[7] = byte(device.Combos[idx][3])
251-
txb[8] = byte(device.Combos[idx][3] >> 8)
252-
txb[9] = byte(device.Combos[idx][4])
253-
txb[10] = byte(device.Combos[idx][4] >> 8)
244+
txb[1] = byte(keycodeTGKtoVia(device.Combos[idx][0]))
245+
txb[2] = byte(keycodeTGKtoVia(device.Combos[idx][0]) >> 8)
246+
txb[3] = byte(keycodeTGKtoVia(device.Combos[idx][1]))
247+
txb[4] = byte(keycodeTGKtoVia(device.Combos[idx][1]) >> 8)
248+
txb[5] = byte(keycodeTGKtoVia(device.Combos[idx][2]))
249+
txb[6] = byte(keycodeTGKtoVia(device.Combos[idx][2]) >> 8)
250+
txb[7] = byte(keycodeTGKtoVia(device.Combos[idx][3]))
251+
txb[8] = byte(keycodeTGKtoVia(device.Combos[idx][3]) >> 8)
252+
txb[9] = byte(keycodeTGKtoVia(device.Combos[idx][4]))
253+
txb[10] = byte(keycodeTGKtoVia(device.Combos[idx][4]) >> 8)
254254
// 00 0400 0500 0000 0000 0700 000000000000000000000000000000000000000000
255255
// 0 1 3 5 7 9
256256
case dynamicVialComboSet:
257257
txb[0] = 0x00
258258
idx := b[3]
259259
// fe0d04 00 0400 0500 0000 0000 0700 000000000000000000000000000000000000
260260
// 0 1 2 3 4 6 8 10 12
261-
device.Combos[idx][0] = Keycode(b[4]) + Keycode(b[5])<<8 // key 1
262-
device.Combos[idx][1] = Keycode(b[6]) + Keycode(b[7])<<8 // key 2
263-
device.Combos[idx][2] = Keycode(b[8]) + Keycode(b[9])<<8 // key 3
264-
device.Combos[idx][3] = Keycode(b[10]) + Keycode(b[11])<<8 // key 4
265-
device.Combos[idx][4] = Keycode(b[12]) + Keycode(b[13])<<8 // Output key
261+
device.Combos[idx][0] = keycodeViaToTGK(Keycode(b[4]) + Keycode(b[5])<<8) // key 1
262+
device.Combos[idx][1] = keycodeViaToTGK(Keycode(b[6]) + Keycode(b[7])<<8) // key 2
263+
device.Combos[idx][2] = keycodeViaToTGK(Keycode(b[8]) + Keycode(b[9])<<8) // key 3
264+
device.Combos[idx][3] = keycodeViaToTGK(Keycode(b[10]) + Keycode(b[11])<<8) // key 4
265+
device.Combos[idx][4] = keycodeViaToTGK(Keycode(b[12]) + Keycode(b[13])<<8) // Output key
266266
device.flashCh <- true
267267
default:
268268
txb[0] = 0x00

0 commit comments

Comments
 (0)