From 5c4efb3bfabfb266b9defe699c80cc5883748dac Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 24 Dec 2024 20:25:58 +0900 Subject: [PATCH] Add an interface to configure Combos --- combo.go | 16 ++++++++++++++++ keyboard.go | 23 ++++++++++++++++++++--- via.go | 30 +++++++++++++++--------------- 3 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 combo.go diff --git a/combo.go b/combo.go new file mode 100644 index 0000000..7c824a0 --- /dev/null +++ b/combo.go @@ -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 +} diff --git a/keyboard.go b/keyboard.go index cfa5243..ae6af79 100644 --- a/keyboard.go +++ b/keyboard.go @@ -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 @@ -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 { @@ -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++ } } @@ -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]) } } @@ -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: @@ -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: diff --git a/via.go b/via.go index d4e2cb3..89d67ae 100644 --- a/via.go +++ b/via.go @@ -241,16 +241,16 @@ 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: @@ -258,11 +258,11 @@ func rxHandler2(b []byte) bool { 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