diff --git a/src/main/python/keyboard_widget.py b/src/main/python/keyboard_widget.py index 75e837a..94968e6 100644 --- a/src/main/python/keyboard_widget.py +++ b/src/main/python/keyboard_widget.py @@ -12,6 +12,7 @@ class KeyWidget: def __init__(self, desc, scale, shift_x=0, shift_y=0): self.active = False self.masked = False + self.pressed = False self.desc = desc self.text = "" self.mask_text = "" @@ -109,6 +110,9 @@ class KeyWidget: def setActive(self, active): self.active = active + def setPressed(self, pressed): + self.pressed = pressed + def setColor(self, color): self.color = color @@ -263,6 +267,16 @@ class KeyboardWidget(QWidget): active_brush.setColor(QApplication.palette().color(QPalette.Highlight)) active_brush.setStyle(Qt.SolidPattern) + # for pressed keycaps + pressed_pen = qp.pen() + pressed_pen_color = QApplication.palette().color(QPalette.HighlightedText).lighter(75) + pressed_pen.setColor(pressed_pen_color) + + pressed_brush = QBrush() + pressed_brush_color = QApplication.palette().color(QPalette.Highlight).lighter(75) + pressed_brush.setColor(pressed_brush_color) + pressed_brush.setStyle(Qt.SolidPattern) + mask_font = qp.font() mask_font.setPointSize(mask_font.pointSize() * 0.8) @@ -280,6 +294,12 @@ class KeyboardWidget(QWidget): qp.setPen(active_pen) qp.setBrush(active_brush) + if key.pressed: + # move key slightly down when pressed + qp.translate(0, 5) + qp.setPen(pressed_pen) + qp.setBrush(pressed_brush) + # draw the keycap qp.drawPath(key.draw_path) qp.strokePath(key.draw_path2, regular_pen) diff --git a/src/main/python/matrix_test.py b/src/main/python/matrix_test.py index c97ea59..28ac550 100644 --- a/src/main/python/matrix_test.py +++ b/src/main/python/matrix_test.py @@ -18,12 +18,14 @@ class MatrixTest(BasicEditor): self.keyboardWidget.set_enabled(False) self.startButtonWidget = QPushButton("Start testing") + self.resetButtonWidget = QPushButton("Reset") layout = QVBoxLayout() layout.addWidget(self.keyboardWidget) layout.setAlignment(self.keyboardWidget, Qt.AlignCenter) self.addLayout(layout) + self.addWidget(self.resetButtonWidget) self.addWidget(self.startButtonWidget) self.keyboard = None @@ -32,7 +34,9 @@ class MatrixTest(BasicEditor): self.timer = QTimer() self.timer.timeout.connect(self.matrix_poller) + self.startButtonWidget.clicked.connect(self.start_poller) + self.resetButtonWidget.clicked.connect(self.reset_keyboard_widget) def rebuild(self, device): super().rebuild(device) @@ -42,33 +46,56 @@ class MatrixTest(BasicEditor): self.keyboardWidget.set_keys(self.keyboard.keys, self.keyboard.encoders) def valid(self): - return isinstance(self.device, VialKeyboard) + # Check if vial protocol is v3 or later + return isinstance(self.device, VialKeyboard) and (self.device.keyboard and self.device.keyboard.vial_protocol >= 3) + + def reset_keyboard_widget(self): + # reset keyboard widget + for w in self.keyboardWidget.widgets: + w.setPressed(False) + w.setActive(False) + + self.keyboardWidget.update_layout() + self.keyboardWidget.update() + self.keyboardWidget.updateGeometry() def matrix_poller(self): + # Get size for matrix rows = self.keyboard.rows cols = self.keyboard.cols + # Generate 2d array of matrix matrix = [ [None for y in range(cols)] for x in range(rows) ] + # Get matrix data from keyboard data = self.keyboard.matrix_poll() - + # Calculate the amount of bytes belong to 1 row, each bit is 1 key, so per 8 keys in a row, a byte is needed for the row. row_size = math.ceil(cols / 8) + for row in range(rows): + # Make slice of bytes for the row (skip first 2 bytes, they're for VIAL) row_data_start = 2 + (row * row_size) row_data_end = row_data_start + row_size row_data = data[row_data_start:row_data_end] + #Get each bit representing pressed state for col for col in range(cols): - col_byte = math.floor(col / 8) - state = (row_data[col_byte] >> col) & 1 - matrix[row][col] = state + # row_data is array of bytes, calculate in which byte the col is located + col_byte = len(row_data) - 1 - math.floor(col / 8) + # since we select a single byte as slice of byte, mod 8 to get nth pos of byte + col_mod = (col % 8) + # write to matrix array + matrix[row][col] = (row_data[col_byte] >> col_mod) & 1 + # write matrix state to keyboard widget for w in self.keyboardWidget.widgets: row = w.desc.row col = w.desc.col if row < len(matrix) and col < len(matrix[row]): - w.setActive(matrix[row][col]) + w.setPressed(matrix[row][col]) + if matrix[row][col]: + w.setActive(True) self.keyboardWidget.update_layout() self.keyboardWidget.update() @@ -78,7 +105,7 @@ class MatrixTest(BasicEditor): if not self.polling: Unlocker.unlock(self.keyboard) self.startButtonWidget.setText("Stop testing") - self.timer.start(100) + self.timer.start(20) self.polling = True else: self.timer.stop()