Add debug hooks to the event dispatcher, remove explicit prints

This commit is contained in:
Josh Klar 2018-09-03 13:50:12 -07:00
parent d9b909d841
commit ef639f5292
6 changed files with 98 additions and 84 deletions

18
kmk/common/event_defs.py Normal file
View File

@ -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,
}

View File

@ -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']}
))

View File

@ -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:

View File

@ -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 @@ except ImportError:
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:

View File

@ -0,0 +1 @@
vendor/upy-lib/logging/logging.py

1
vendor/pydux vendored

@ -1 +0,0 @@
Subproject commit 943ca1c75357b9289f55f17ff2d997a66a3313a4