From d4e3d1c22566200a1704b4fc65aaee02f9044553 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Fri, 25 Dec 2020 00:00:20 -0500 Subject: [PATCH] macro_recorder: read macro memory/count from the keyboard --- src/main/python/keyboard_comm.py | 12 ++++++++++++ src/main/python/macro_recorder.py | 22 ++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/python/keyboard_comm.py b/src/main/python/keyboard_comm.py index 8fe9e58..8803540 100644 --- a/src/main/python/keyboard_comm.py +++ b/src/main/python/keyboard_comm.py @@ -12,6 +12,8 @@ CMD_VIA_GET_KEYBOARD_VALUE = 0x02 CMD_VIA_SET_KEYBOARD_VALUE = 0x03 CMD_VIA_GET_KEYCODE = 0x04 CMD_VIA_SET_KEYCODE = 0x05 +CMD_VIA_MACRO_GET_COUNT = 0x0C +CMD_VIA_MACRO_GET_BUFFER_SIZE = 0x0D CMD_VIA_GET_LAYER_COUNT = 0x11 CMD_VIA_VIAL_PREFIX = 0xFE @@ -43,6 +45,8 @@ class Keyboard: self.keys = [] self.encoders = [] self.sideload = False + self.macro_count = 0 + self.macro_memory = 0 self.vial_protocol = self.keyboard_id = -1 @@ -57,6 +61,7 @@ class Keyboard: self.reload_layout(sideload_json) self.reload_layers() self.reload_keymap() + self.reload_macros() def reload_layers(self): """ Get how many layers the keyboard has """ @@ -161,6 +166,13 @@ class Keyboard: data = self.usb_send(self.dev, struct.pack("BB", CMD_VIA_GET_KEYBOARD_VALUE, VIA_LAYOUT_OPTIONS)) self.layout_options = struct.unpack(">I", data[2:6])[0] + def reload_macros(self): + """ Loads macro information from the keyboard """ + data = self.usb_send(self.dev, struct.pack("B", CMD_VIA_MACRO_GET_COUNT)) + self.macro_count = data[1] + data = self.usb_send(self.dev, struct.pack(">H", CMD_VIA_MACRO_GET_BUFFER_SIZE)) + self.macro_memory = struct.unpack(">H", data[1:3])[0] + def set_key(self, layer, row, col, code): key = (layer, row, col) if self.layout[key] != code: diff --git a/src/main/python/macro_recorder.py b/src/main/python/macro_recorder.py index b17d39c..728eb87 100644 --- a/src/main/python/macro_recorder.py +++ b/src/main/python/macro_recorder.py @@ -132,8 +132,11 @@ class MacroRecorder(BasicEditor): def __init__(self): super().__init__() + self.keyboard = None + self.keystrokes = [] self.macro_tabs = [] + self.macro_tab_w = [] self.recorder = LinuxRecorder() self.recorder.keystroke.connect(self.on_keystroke) @@ -152,7 +155,7 @@ class MacroRecorder(BasicEditor): self.macro_tabs.append(tab) w = QWidget() w.setLayout(tab) - self.tabs.addTab(w, "Macro {}".format(x + 1)) + self.macro_tab_w.append(w) self.lbl_memory = QLabel() @@ -165,8 +168,6 @@ class MacroRecorder(BasicEditor): self.addWidget(self.tabs) self.addLayout(buttons) - self.on_change() - def valid(self): return isinstance(self.device, VialKeyboard) @@ -174,6 +175,15 @@ class MacroRecorder(BasicEditor): super().rebuild(device) if not self.valid(): return + self.keyboard = self.device.keyboard + + # only show the number of macro editors that keyboard supports + for x in range(self.tabs.count()): + self.tabs.removeTab(x) + for x, w in enumerate(self.macro_tab_w[:self.keyboard.macro_count]): + self.tabs.addTab(w, "Macro {}".format(x + 1)) + + self.on_change() def on_record(self, tab, append): self.recording_tab = tab @@ -181,7 +191,7 @@ class MacroRecorder(BasicEditor): self.recording_tab.pre_record() - for x, w in enumerate(self.macro_tabs): + for x, w in enumerate(self.macro_tabs[:self.keyboard.macro_count]): if tab != w: self.tabs.tabBar().setTabEnabled(x, False) @@ -193,7 +203,7 @@ class MacroRecorder(BasicEditor): self.recorder.stop() def on_stop(self): - for x in range(len(self.macro_tabs)): + for x in range(self.keyboard.macro_count): self.tabs.tabBar().setTabEnabled(x, True) if not self.recording_append: @@ -216,4 +226,4 @@ class MacroRecorder(BasicEditor): memory = 0 for x, macro in enumerate(self.macro_tabs): memory += len(macro.serialize()) - self.lbl_memory.setText("Memory used by macros: {}/345".format(memory)) + self.lbl_memory.setText("Memory used by macros: {}/{}".format(memory, self.keyboard.macro_memory))