From 016ac7eed553f60caa188ab08ec23ca63f8102ee Mon Sep 17 00:00:00 2001 From: Pieterv24 <9167905+Pieterv24@users.noreply.github.com> Date: Mon, 19 Apr 2021 00:42:39 +0200 Subject: [PATCH] started working on matrix test feature --- src/main/python/keyboard_comm.py | 8 ++++ src/main/python/main_window.py | 6 ++- src/main/python/matrix_test.py | 77 ++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/main/python/matrix_test.py diff --git a/src/main/python/keyboard_comm.py b/src/main/python/keyboard_comm.py index 5397506..e3f2b17 100644 --- a/src/main/python/keyboard_comm.py +++ b/src/main/python/keyboard_comm.py @@ -26,6 +26,7 @@ CMD_VIA_KEYMAP_GET_BUFFER = 0x12 CMD_VIA_VIAL_PREFIX = 0xFE VIA_LAYOUT_OPTIONS = 0x02 +VIA_SWITCH_MATRIX_STATE = 0x03 CMD_VIAL_GET_KEYBOARD_ID = 0x00 CMD_VIAL_GET_SIZE = 0x01 @@ -500,6 +501,13 @@ class Keyboard: self.usb_send(self.dev, struct.pack("BB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_LOCK), retries=20) + def matrix_poll(self): + if self.via_protocol < 0: + return + + data = self.usb_send(self.dev, struct.pack("BB", CMD_VIA_GET_KEYBOARD_VALUE, VIA_SWITCH_MATRIX_STATE), retries=20) + return data + def macro_serialize(self, macro): """ Serialize a single macro, a macro is made out of macro actions (BasicAction) diff --git a/src/main/python/main_window.py b/src/main/python/main_window.py index 387aff0..624c024 100644 --- a/src/main/python/main_window.py +++ b/src/main/python/main_window.py @@ -16,6 +16,7 @@ from macro_recorder import MacroRecorder from unlocker import Unlocker from util import tr, find_vial_devices, EXAMPLE_KEYBOARDS from vial_device import VialKeyboard +from matrix_test import MatrixTest import themes @@ -51,9 +52,10 @@ class MainWindow(QMainWindow): self.keymap_editor = KeymapEditor(self.layout_editor) self.firmware_flasher = FirmwareFlasher(self) self.macro_recorder = MacroRecorder() + self.matrix_tester = MatrixTest(self.layout_editor) self.editors = [(self.keymap_editor, "Keymap"), (self.layout_editor, "Layout"), (self.macro_recorder, "Macros"), - (self.firmware_flasher, "Firmware updater")] + (self.matrix_tester, "Matrix tester"), (self.firmware_flasher, "Firmware updater")] Unlocker.global_layout_editor = self.layout_editor self.tabs = QTabWidget() @@ -234,7 +236,7 @@ class MainWindow(QMainWindow): Unlocker.unlock(self.current_device.keyboard) self.current_device.keyboard.reload() - for e in [self.layout_editor, self.keymap_editor, self.firmware_flasher, self.macro_recorder]: + for e in [self.layout_editor, self.keymap_editor, self.firmware_flasher, self.macro_recorder, self.matrix_tester]: e.rebuild(self.current_device) def refresh_tabs(self): diff --git a/src/main/python/matrix_test.py b/src/main/python/matrix_test.py new file mode 100644 index 0000000..33131c4 --- /dev/null +++ b/src/main/python/matrix_test.py @@ -0,0 +1,77 @@ +from PyQt5.QtWidgets import QVBoxLayout, QPushButton +from PyQt5.QtCore import Qt, QTimer +import struct +import math + +from basic_editor import BasicEditor +from keyboard_widget import KeyboardWidget +from vial_device import VialKeyboard +from unlocker import Unlocker + +class MatrixTest(BasicEditor): + def __init__(self, layout_editor): + super().__init__() + + self.layout_editor = layout_editor + + self.keyboardWidget = KeyboardWidget(layout_editor) + self.startButtonWidget = QPushButton("Start testing") + + layout = QVBoxLayout() + layout.addWidget(self.keyboardWidget) + layout.setAlignment(self.keyboardWidget, Qt.AlignCenter) + + self.addLayout(layout) + self.addWidget(self.startButtonWidget) + + self.keyboard = None + self.device = None + self.polling = False + + self.timer = QTimer() + self.timer.timeout.connect(self.matrix_poller) + self.startButtonWidget.clicked.connect(self.start_poller) + + def rebuild(self, device): + super().rebuild(device) + if self.valid(): + self.keyboard = device.keyboard + + self.keyboardWidget.set_keys(self.keyboard.keys, self.keyboard.encoders) + + def valid(self): + return isinstance(self.device, VialKeyboard) + + def matrix_poller(self): + # print(f"Rows: {self.keyboard.rows}") + # print(f"Cols: {self.keyboard.cols}") + rows = self.keyboard.rows + cols = self.keyboard.cols + matrix = [ [None for y in range(cols)] for x in range(rows) ] + + data = self.keyboard.matrix_poll() + + row_size = math.ceil(cols / 8) + + for row in range(rows): + row_data_start = 2 + (row * row_size) + row_data_end = row_data_start + row_size + row_data = data[row_data_start:row_data_end] + + for col in range(cols): + col_byte = math.floor(col / 8) + state = (row_data[col_byte] >> col) & 1 + matrix[row][col] = state + + def start_poller(self): + if not self.polling: + Unlocker.unlock(self.keyboard) + self.startButtonWidget.setText("Stop testing") + self.timer.start(100) + self.polling = True + else: + self.timer.stop() + self.keyboard.lock() + self.startButtonWidget.setText("Start testing") + self.polling = False +