macro_recorder: support saving macros to keyboard
parent
3edc3b89c6
commit
e19b0a5a35
|
|
@ -28,6 +28,9 @@ CMD_VIAL_GET_ENCODER = 0x03
|
|||
CMD_VIAL_SET_ENCODER = 0x04
|
||||
CMD_VIAL_GET_KEYMAP_FAST = 0x05
|
||||
|
||||
# how much of a macro we can read/write per packet
|
||||
MACRO_CHUNK = 28
|
||||
|
||||
|
||||
class Keyboard:
|
||||
""" Low-level communication with a vial-enabled keyboard """
|
||||
|
|
@ -177,10 +180,9 @@ class Keyboard:
|
|||
self.macro_memory = struct.unpack(">H", data[1:3])[0]
|
||||
|
||||
self.macro = b""
|
||||
# now retrieve the entire buffer, 28 bytes at a time, as that is what fits into a packet
|
||||
chunk = 28
|
||||
for x in range(0, self.macro_memory, chunk):
|
||||
sz = min(chunk, self.macro_memory - x)
|
||||
# now retrieve the entire buffer, MACRO_CHUNK bytes at a time, as that is what fits into a packet
|
||||
for x in range(0, self.macro_memory, MACRO_CHUNK):
|
||||
sz = min(MACRO_CHUNK, self.macro_memory - x)
|
||||
data = self.usb_send(self.dev, struct.pack(">BHB", CMD_VIA_MACRO_GET_BUFFER, x, sz))
|
||||
self.macro += data[4:4+sz]
|
||||
|
||||
|
|
@ -202,6 +204,14 @@ class Keyboard:
|
|||
self.layout_options = options
|
||||
self.usb_send(self.dev, struct.pack(">BBI", CMD_VIA_SET_KEYBOARD_VALUE, VIA_LAYOUT_OPTIONS, options))
|
||||
|
||||
def set_macro(self, data):
|
||||
if len(data) > self.macro_memory:
|
||||
raise RuntimeError("the macro is too big: got {} max {}".format(len(data), self.macro_memory))
|
||||
|
||||
for x, chunk in enumerate(chunks(data, MACRO_CHUNK)):
|
||||
off = x * MACRO_CHUNK
|
||||
self.usb_send(self.dev, struct.pack(">BHB", CMD_VIA_MACRO_SET_BUFFER, off, len(chunk)) + chunk)
|
||||
|
||||
def save_layout(self):
|
||||
""" Serializes current layout to a binary """
|
||||
|
||||
|
|
|
|||
|
|
@ -198,10 +198,11 @@ class MacroRecorder(BasicEditor):
|
|||
buttons = QHBoxLayout()
|
||||
buttons.addWidget(self.lbl_memory)
|
||||
buttons.addStretch()
|
||||
btn_save = QPushButton(tr("MacroRecorder", "Save"))
|
||||
self.btn_save = QPushButton(tr("MacroRecorder", "Save"))
|
||||
self.btn_save.clicked.connect(self.on_save)
|
||||
btn_revert = QPushButton(tr("MacroRecorder", "Revert"))
|
||||
btn_revert.clicked.connect(self.on_revert)
|
||||
buttons.addWidget(btn_save)
|
||||
buttons.addWidget(self.btn_save)
|
||||
buttons.addWidget(btn_revert)
|
||||
|
||||
self.addWidget(self.tabs)
|
||||
|
|
@ -267,6 +268,8 @@ class MacroRecorder(BasicEditor):
|
|||
def on_change(self):
|
||||
memory = len(self.serialize())
|
||||
self.lbl_memory.setText("Memory used by macros: {}/{}".format(memory, self.keyboard.macro_memory))
|
||||
self.btn_save.setEnabled(memory <= self.keyboard.macro_memory)
|
||||
self.lbl_memory.setStyleSheet("QLabel { color: red; }" if memory > self.keyboard.macro_memory else "")
|
||||
|
||||
def deserialize(self, data):
|
||||
macros = data.split(b"\x00")
|
||||
|
|
@ -285,3 +288,6 @@ class MacroRecorder(BasicEditor):
|
|||
def on_revert(self):
|
||||
self.keyboard.reload_macros()
|
||||
self.deserialize(self.keyboard.macro)
|
||||
|
||||
def on_save(self):
|
||||
self.keyboard.set_macro(self.serialize())
|
||||
|
|
|
|||
Loading…
Reference in New Issue