From ef639f5292a78e93004e5e917ba08d2e196ce747 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Mon, 3 Sep 2018 13:50:12 -0700 Subject: [PATCH] Add debug hooks to the event dispatcher, remove explicit prints --- kmk/common/event_defs.py | 18 +++++++++ kmk/common/internal_state.py | 69 ++++++++++++++++++++++++++++++++ kmk/common/keymap.py | 17 +------- kmk/firmware.py | 76 +++++------------------------------- upy-freeze.txt | 1 + vendor/pydux | 1 - 6 files changed, 98 insertions(+), 84 deletions(-) create mode 100644 kmk/common/event_defs.py create mode 100644 kmk/common/internal_state.py delete mode 160000 vendor/pydux diff --git a/kmk/common/event_defs.py b/kmk/common/event_defs.py new file mode 100644 index 000000000..12cb1de02 --- /dev/null +++ b/kmk/common/event_defs.py @@ -0,0 +1,18 @@ +from micropython import const + +KEY_UP_EVENT = const(1) +KEY_DOWN_EVENT = const(2) + + +def key_up_event(keycode): + return { + 'type': KEY_UP_EVENT, + 'keycode': keycode, + } + + +def key_down_event(keycode): + return { + 'type': KEY_DOWN_EVENT, + 'keycode': keycode, + } diff --git a/kmk/common/internal_state.py b/kmk/common/internal_state.py new file mode 100644 index 000000000..0a408ede8 --- /dev/null +++ b/kmk/common/internal_state.py @@ -0,0 +1,69 @@ +import logging + +from kmk.common.event_defs import KEY_DOWN_EVENT, KEY_UP_EVENT + + +class ReduxStore: + def __init__(self, reducer, log_level=logging.NOTSET): + self.reducer = reducer + self.logger = logging.getLogger(__name__) + self.logger.setLevel(log_level) + self.state = self.reducer(logger=self.logger) + + def dispatch(self, action): + self.logger.debug('Dispatching action: {}'.format(action)) + self.state = self.reducer(self.state, action) + self.logger.debug('Dispatching complete: {}'.format(action)) + + def get_state(self): + return self.state + + +class InternalState: + modifiers_pressed = frozenset() + keys_pressed = frozenset() + + def __repr__(self): + return 'InternalState(mods={}, keys={})'.format( + self.modifiers_pressed, + self.keys_pressed, + ) + + def copy(self, modifiers_pressed=None, keys_pressed=None): + new_state = InternalState() + + if modifiers_pressed is None: + new_state.modifiers_pressed = self.modifiers_pressed.copy() + else: + new_state.modifiers_pressed = modifiers_pressed + + if keys_pressed is None: + new_state.keys_pressed = self.keys_pressed.copy() + else: + new_state.keys_pressed = keys_pressed + + return new_state + + +def kmk_reducer(state=None, action=None, logger=None): + if state is None: + state = InternalState() + + if logger is not None: + logger.debug('Reducer received state of None, creating new') + + if action is None: + if logger is not None: + logger.debug('No action received, returning state unmodified') + + return state + + if action['type'] == KEY_UP_EVENT: + return state.copy(keys_pressed=frozenset( + key for key in state.keys_pressed if key != action['keycode'] + )) + + if action['type'] == KEY_DOWN_EVENT: + return state.copy(keys_pressed=( + state.keys_pressed | {action['keycode']} + )) diff --git a/kmk/common/keymap.py b/kmk/common/keymap.py index 3f3ba0964..da018eefa 100644 --- a/kmk/common/keymap.py +++ b/kmk/common/keymap.py @@ -1,19 +1,4 @@ -KEY_UP_EVENT = 'KEY_UP' -KEY_DOWN_EVENT = 'KEY_DOWN' - - -def key_up_event(keycode): - return { - 'type': KEY_UP_EVENT, - 'keycode': keycode, - } - - -def key_down_event(keycode): - return { - 'type': KEY_DOWN_EVENT, - 'keycode': keycode, - } +from kmk.common.event_defs import key_down_event, key_up_event class Keymap: diff --git a/kmk/firmware.py b/kmk/firmware.py index 2f6b9abab..b9071fa26 100644 --- a/kmk/firmware.py +++ b/kmk/firmware.py @@ -1,4 +1,7 @@ -from kmk.common.keymap import KEY_DOWN_EVENT, KEY_UP_EVENT, Keymap +import logging + +from kmk.common.internal_state import ReduxStore, kmk_reducer +from kmk.common.keymap import Keymap try: from kmk.circuitpython.matrix import MatrixScanner @@ -6,76 +9,15 @@ from kmk.micropython.matrix import MatrixScanner -class ReduxStore: - def __init__(self, reducer): - self.reducer = reducer - self.state = self.reducer() - - def dispatch(self, action): - self.state = self.reducer(self.state, action) - - def get_state(self): - return self.state - - -class InternalState: - modifiers_pressed = frozenset() - keys_pressed = frozenset() - - def __repr__(self): - return 'InternalState(mods={}, keys={})'.format( - self.modifiers_pressed, - self.keys_pressed, - ) - - def copy(self, modifiers_pressed=None, keys_pressed=None): - new_state = InternalState() - - if modifiers_pressed is None: - new_state.modifiers_pressed = self.modifiers_pressed.copy() - else: - new_state.modifiers_pressed = modifiers_pressed - - if keys_pressed is None: - new_state.keys_pressed = self.keys_pressed.copy() - else: - new_state.keys_pressed = keys_pressed - - return new_state - - -def reducer(state=None, action=None): - if state is None: - state = InternalState() - - if action is None: - return state - - if action['type'] == KEY_UP_EVENT: - new_state = state.copy(keys_pressed=frozenset( - key for key in state.keys_pressed if key != action['keycode'] - )) - - print(new_state) - - return new_state - - if action['type'] == KEY_DOWN_EVENT: - new_state = state.copy(keys_pressed=( - state.keys_pressed | {action['keycode']} - )) - - print(new_state) - - return new_state - - class Firmware: - def __init__(self, keymap, row_pins, col_pins, diode_orientation): + def __init__( + self, keymap, row_pins, col_pins, diode_orientation, + log_level=logging.NOTSET, + ): self.raw_keymap = keymap self.keymap = Keymap(keymap) self.matrix = MatrixScanner(col_pins, row_pins, diode_orientation) - self.store = ReduxStore(reducer) + self.store = ReduxStore(kmk_reducer, log_level=log_level) def go(self): while True: diff --git a/upy-freeze.txt b/upy-freeze.txt index e69de29bb..b06922b4e 100644 --- a/upy-freeze.txt +++ b/upy-freeze.txt @@ -0,0 +1 @@ +vendor/upy-lib/logging/logging.py diff --git a/vendor/pydux b/vendor/pydux deleted file mode 160000 index 943ca1c75..000000000 --- a/vendor/pydux +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 943ca1c75357b9289f55f17ff2d997a66a3313a4