Holy refactor, Batman: full layer support (MO/DF)

Wow, what a trip this was. Layer support is now fully implemented. Other
changes here mostly revolve around the event dispatching model: more
floating state (hidden in clases wherever) has been purged, with the
reducer (now mutable, comments inline) serving, as it should, as the
sole source of truth. Thunk support has been added to our fake Redux
clone, allowing Action Creators to handle sequences of events (which is
arguably a cleaner way of handling matrix changes when not all matrix
changes should result in a new HID report - in the case of internal
keys). A whole class has been deprecated (Keymap) which only served as
another arbitor of state: instead, the MatrixScanner has been made
smarter and handles diffing internally, dispatching an Action when
needed (and allowing the reducer to parse the keymap and figure out what
key is pressed - this is the infinitely cleaner solution when layers
come into play).
This commit is contained in:
Josh Klar
2018-09-22 21:49:58 -07:00
parent 0ae3adcc84
commit 9bec905fce
9 changed files with 214 additions and 111 deletions

View File

@@ -2,6 +2,7 @@ import machine
from kmk.common.abstract.matrix_scanner import AbstractMatrixScanner
from kmk.common.consts import DiodeOrientation
from kmk.common.event_defs import matrix_changed
class MatrixScanner(AbstractMatrixScanner):
@@ -52,3 +53,17 @@ class MatrixScanner(AbstractMatrixScanner):
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)
return None # The default, but for explicitness

View File

@@ -3,8 +3,9 @@ import string
from pyb import USB_HID, delay
from kmk.common.event_defs import KEY_DOWN_EVENT, KEY_UP_EVENT
from kmk.common.keycodes import Keycodes, char_lookup
from kmk.common.event_defs import HID_REPORT_EVENT
from kmk.common.keycodes import (FIRST_KMK_INTERNAL_KEYCODE, Keycodes,
char_lookup)
class HIDHelper:
@@ -48,26 +49,19 @@ class HIDHelper:
self.clear_all()
def _subscription(self, state, action):
if action['type'] == KEY_DOWN_EVENT:
if action['keycode'].code >= 1000:
return
if action['type'] == HID_REPORT_EVENT:
self.clear_all()
if action['keycode'].is_modifier:
self.add_modifier(action['keycode'])
self.send()
else:
self.add_key(action['keycode'])
self.send()
elif action['type'] == KEY_UP_EVENT:
if action['keycode'].code >= 1000:
return
for key in state.keys_pressed:
if key.code >= FIRST_KMK_INTERNAL_KEYCODE:
continue
if action['keycode'].is_modifier:
self.remove_modifier(action['keycode'])
self.send()
else:
self.remove_key(action['keycode'])
self.send()
if key.is_modifier:
self.add_modifier(key)
else:
self.add_key(key)
self.send()
def send(self):
self.logger.debug('Sending HID report: {}'.format(self._evt))