keyboard: add backwards-compat for VIA keymap retrival

main
Ilya Zhuravlev 2020-12-21 02:30:43 -05:00
parent b9c10095e5
commit 03d853012e
2 changed files with 23 additions and 9 deletions

View File

@ -42,6 +42,7 @@ class Keyboard:
self.layout_options = -1 self.layout_options = -1
self.keys = [] self.keys = []
self.encoders = [] self.encoders = []
self.sideload = False
self.vial_protocol = self.keyboard_id = -1 self.vial_protocol = self.keyboard_id = -1
@ -67,6 +68,7 @@ class Keyboard:
if sideload_json is not None: if sideload_json is not None:
payload = sideload_json payload = sideload_json
self.sideload = True
else: else:
# get keyboard identification # get keyboard identification
data = self.usb_send(self.dev, struct.pack("BB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_GET_KEYBOARD_ID)) data = self.usb_send(self.dev, struct.pack("BB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_GET_KEYBOARD_ID))
@ -130,16 +132,24 @@ class Keyboard:
for layer in range(self.layers): for layer in range(self.layers):
for row, cols in self.rowcol.items(): for row, cols in self.rowcol.items():
for chunk in chunks(cols, 16): # if this is a sideload, we have to assume it's a VIA keyboard
req = struct.pack("BBBB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_GET_KEYMAP_FAST, layer, row) # and does not support fast keymap retrieval
for col in chunk: if self.sideload:
req += struct.pack("B", col) for col in cols:
req += b"\xFF" * (MSG_LEN - len(req)) data = self.usb_send(self.dev, struct.pack("BBBB", CMD_VIA_GET_KEYCODE, layer, row, col))
keycode = struct.unpack(">H", data[4:6])[0]
data = self.usb_send(self.dev, req)
for x, col in enumerate(chunk):
keycode = struct.unpack(">H", data[x*2:x*2+2])[0]
self.layout[(layer, row, col)] = keycode self.layout[(layer, row, col)] = keycode
else:
for chunk in chunks(cols, 16):
req = struct.pack("BBBB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_GET_KEYMAP_FAST, layer, row)
for col in chunk:
req += struct.pack("B", col)
req += b"\xFF" * (MSG_LEN - len(req))
data = self.usb_send(self.dev, req)
for x, col in enumerate(chunk):
keycode = struct.unpack(">H", data[x*2:x*2+2])[0]
self.layout[(layer, row, col)] = keycode
for layer in range(self.layers): for layer in range(self.layers):
for idx in self.encoderpos: for idx in self.encoderpos:

View File

@ -93,8 +93,12 @@ class TestKeyboard(unittest.TestCase):
dev.expect_keymap(keymap) dev.expect_keymap(keymap)
if encoders is not None: if encoders is not None:
dev.expect_encoders(encoders) dev.expect_encoders(encoders)
# layout options
dev.expect("0202", "020200000000")
kb = Keyboard(dev, dev.sim_send) kb = Keyboard(dev, dev.sim_send)
# simulate old VIA keymap retrieval in tests for now
kb.sideload = True
kb.reload() kb.reload()
return kb, dev return kb, dev