Completely overhaul the entire MatrixScanner and KEY_UP/DOWN_EVENT system for efficiency. Less delay() on HID sends. Speed is only BARELY slower than QMK now.

This commit is contained in:
Josh Klar
2018-10-01 00:31:45 -07:00
parent 784f8885bd
commit b5457534bf
7 changed files with 126 additions and 211 deletions

View File

@@ -21,6 +21,7 @@ class MatrixScanner(AbstractMatrixScanner):
self.rows = [machine.Pin(pin) for pin in rows]
self.diode_orientation = diode_orientation
self.active_layers = active_layers
self.last_pressed_len = 0
if self.diode_orientation == DiodeOrientation.COLUMNS:
self.outputs = self.cols
@@ -41,29 +42,23 @@ class MatrixScanner(AbstractMatrixScanner):
pin.init(machine.Pin.IN, machine.Pin.PULL_DOWN)
pin.off()
def _normalize_matrix(self, matrix):
return super()._normalize_matrix(matrix)
def scan_for_pressed(self):
pressed = []
def raw_scan(self):
matrix = []
for opin in self.outputs:
for oidx, opin in enumerate(self.outputs):
opin.value(1)
matrix.append([bool(ipin.value()) for ipin in self.inputs])
for iidx, ipin in enumerate(self.inputs):
if ipin.value():
pressed.append(
(oidx, iidx) if self.diode_orientation == DiodeOrientation.ROWS else (iidx, oidx) # noqa
)
opin.value(0)
return self._normalize_matrix(matrix)
def scan_for_changes(self, old_matrix):
matrix = self.raw_scan()
if any(
any(
col != old_matrix[ridx][cidx]
for cidx, col in enumerate(row)
)
for ridx, row in enumerate(matrix)
):
return matrix_changed(matrix)
if len(pressed) != self.last_pressed_len:
print('LEN PRESSED {} LEN LAST PRESSED {}'.format(len(pressed), self.last_pressed_len))
self.last_pressed_len = len(pressed)
return matrix_changed(pressed)
return None # The default, but for explicitness

View File

@@ -6,6 +6,7 @@ from kmk.common.consts import HID_REPORT_STRUCTURE, HIDReportTypes
from kmk.common.event_defs import HID_REPORT_EVENT
from kmk.common.keycodes import (FIRST_KMK_INTERNAL_KEYCODE, ConsumerKeycode,
ModifierKeycode)
from kmk.common.macros import KMKMacro
def generate_pyb_hid_descriptor():
@@ -71,7 +72,7 @@ class HIDHelper:
self.add_key(consumer_key)
else:
for key in state.keys_pressed:
if key.code >= FIRST_KMK_INTERNAL_KEYCODE:
if isinstance(key, KMKMacro) or key.code >= FIRST_KMK_INTERNAL_KEYCODE:
continue
if isinstance(key, ModifierKeycode):
@@ -98,7 +99,7 @@ class HIDHelper:
# It'd be real awesome if pyb.USB_HID.send/recv would support
# uselect.poll or uselect.select to more safely determine when
# it is safe to write to the host again...
delay(10)
delay(5)
return self