Skip to content

Commit

Permalink
Add an interface to configure Combos
Browse files Browse the repository at this point in the history
  • Loading branch information
sago35 committed Dec 24, 2024
1 parent 64eaa97 commit 5c4efb3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
16 changes: 16 additions & 0 deletions combo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build tinygo

package keyboard

type Combo struct {
Keys [4]Keycode
OutputKey Keycode
}

func (d *Device) SetCombo(index int, c Combo) {
d.Combos[index][0] = c.Keys[0]
d.Combos[index][1] = c.Keys[1]
d.Combos[index][2] = c.Keys[2]
d.Combos[index][3] = c.Keys[3]
d.Combos[index][4] = c.OutputKey
}
23 changes: 20 additions & 3 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ func (d *Device) Init() error {
offset += macroSize

for idx := range device.Combos {
skip := true
for i := 0; i < 10; i++ {
if rbuf[offset+i] != 0xFF {
skip = false
}
}
if skip {
continue
}
device.Combos[idx][0] = Keycode(rbuf[offset+0]) + Keycode(rbuf[offset+1])<<8 // key 1
device.Combos[idx][1] = Keycode(rbuf[offset+2]) + Keycode(rbuf[offset+3])<<8 // key 2
device.Combos[idx][2] = Keycode(rbuf[offset+4]) + Keycode(rbuf[offset+5])<<8 // key 3
Expand Down Expand Up @@ -261,7 +270,7 @@ func (d *Device) Tick() error {
x := d.kb[kbidx].Key(layer, index)
for _, combo := range d.Combos {
for _, ckey := range combo[:4] {
if keycodeViaToTGK(ckey) == x {
if ckey == x {
uniq := true
for _, f := range d.combosFounds {
if f == ckey {
Expand Down Expand Up @@ -325,7 +334,7 @@ func (d *Device) Tick() error {
for xx := range d.combosPressed {
kbidx, layer, index := decKey(xx)
x := d.kb[kbidx].Key(layer, index)
if keycodeViaToTGK(ckey) == x {
if ckey == x {
matchCnt++
}
}
Expand All @@ -334,7 +343,7 @@ func (d *Device) Tick() error {
if matchCnt >= 2 && zero+matchCnt == 4 && matchCnt > matchMax && len(d.combosPressed) == matchCnt {
matched = true
matchMax = matchCnt
d.combosKey = 0xFF000000 | uint32(keycodeViaToTGK(combo[4]))
d.combosKey = 0xFF000000 | uint32(combo[4])
}
}

Expand Down Expand Up @@ -746,7 +755,13 @@ func (d *Device) KeyVia(layer, kbIndex, index int) Keycode {
return 0
}
kc := d.kb[kbIndex].Key(layer, index)
return keycodeTGKtoVia(kc)
}

func keycodeTGKtoVia(kc Keycode) Keycode {
switch kc {
case 0x0000:
kc = 0x0000
case keycodes.MouseLeft:
kc = 0x00D1
case keycodes.MouseRight:
Expand Down Expand Up @@ -825,6 +840,8 @@ func keycodeViaToTGK(key Keycode) Keycode {
kc := key | 0xF000

switch key {
case 0x0000:
kc = 0x0000
case 0x00D1:
kc = keycodes.MouseLeft
case 0x00D2:
Expand Down
30 changes: 15 additions & 15 deletions via.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,28 @@ func rxHandler2(b []byte) bool {
case dynamicVialComboGet:
txb[0] = 0x00
idx := b[3]
txb[1] = byte(device.Combos[idx][0])
txb[2] = byte(device.Combos[idx][0] >> 8)
txb[3] = byte(device.Combos[idx][1])
txb[4] = byte(device.Combos[idx][1] >> 8)
txb[5] = byte(device.Combos[idx][2])
txb[6] = byte(device.Combos[idx][2] >> 8)
txb[7] = byte(device.Combos[idx][3])
txb[8] = byte(device.Combos[idx][3] >> 8)
txb[9] = byte(device.Combos[idx][4])
txb[10] = byte(device.Combos[idx][4] >> 8)
txb[1] = byte(keycodeTGKtoVia(device.Combos[idx][0]))
txb[2] = byte(keycodeTGKtoVia(device.Combos[idx][0]) >> 8)
txb[3] = byte(keycodeTGKtoVia(device.Combos[idx][1]))
txb[4] = byte(keycodeTGKtoVia(device.Combos[idx][1]) >> 8)
txb[5] = byte(keycodeTGKtoVia(device.Combos[idx][2]))
txb[6] = byte(keycodeTGKtoVia(device.Combos[idx][2]) >> 8)
txb[7] = byte(keycodeTGKtoVia(device.Combos[idx][3]))
txb[8] = byte(keycodeTGKtoVia(device.Combos[idx][3]) >> 8)
txb[9] = byte(keycodeTGKtoVia(device.Combos[idx][4]))
txb[10] = byte(keycodeTGKtoVia(device.Combos[idx][4]) >> 8)
// 00 0400 0500 0000 0000 0700 000000000000000000000000000000000000000000
// 0 1 3 5 7 9
case dynamicVialComboSet:
txb[0] = 0x00
idx := b[3]
// fe0d04 00 0400 0500 0000 0000 0700 000000000000000000000000000000000000
// 0 1 2 3 4 6 8 10 12
device.Combos[idx][0] = Keycode(b[4]) + Keycode(b[5])<<8 // key 1
device.Combos[idx][1] = Keycode(b[6]) + Keycode(b[7])<<8 // key 2
device.Combos[idx][2] = Keycode(b[8]) + Keycode(b[9])<<8 // key 3
device.Combos[idx][3] = Keycode(b[10]) + Keycode(b[11])<<8 // key 4
device.Combos[idx][4] = Keycode(b[12]) + Keycode(b[13])<<8 // Output key
device.Combos[idx][0] = keycodeViaToTGK(Keycode(b[4]) + Keycode(b[5])<<8) // key 1
device.Combos[idx][1] = keycodeViaToTGK(Keycode(b[6]) + Keycode(b[7])<<8) // key 2
device.Combos[idx][2] = keycodeViaToTGK(Keycode(b[8]) + Keycode(b[9])<<8) // key 3
device.Combos[idx][3] = keycodeViaToTGK(Keycode(b[10]) + Keycode(b[11])<<8) // key 4
device.Combos[idx][4] = keycodeViaToTGK(Keycode(b[12]) + Keycode(b[13])<<8) // Output key
device.flashCh <- true
default:
txb[0] = 0x00
Expand Down

0 comments on commit 5c4efb3

Please sign in to comment.