Event dispatching, super simply

This commit is contained in:
Josh Klar 2018-09-03 04:22:52 -07:00
parent c641903d61
commit d9b909d841
6 changed files with 102 additions and 18 deletions

4
.gitmodules vendored
View File

@ -2,10 +2,6 @@
path = vendor/circuitpython
url = https://github.com/adafruit/circuitpython.git
branch = "9b98ad779468676c3d5f1efdc06b454aaed7c407"
[submodule "pydux"]
path = vendor/pydux
url = https://github.com/usrlocalben/pydux.git
branch = "943ca1c75357b9289f55f17ff2d997a66a3313a4"
[submodule "upy-lib"]
path = vendor/upy-lib
url = https://github.com/micropython/micropython-lib.git

View File

@ -4,6 +4,7 @@ verify_ssl = true
name = "pypi"
[packages]
pydux = "*"
[dev-packages]
adafruit-ampy = "*"

13
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "13025d2428b2f3830c77567460f41ae840f254378a26bd01c3832d20a6239bf0"
"sha256": "adeed87e2092ae72a6de1ba825163f5e9fd803059aea3cc42a461aa729914b23"
},
"pipfile-spec": 6,
"requires": {
@ -15,7 +15,16 @@
}
]
},
"default": {},
"default": {
"pydux": {
"hashes": [
"sha256:5cb9217be9d8c7ff79b028f6f02597bbb055b107ce8eecbe5f631f3fc76d793f",
"sha256:bed123b5255d566f792b9ceebad87e3f9c1d2d85abed4b9a9475ffc831035879"
],
"index": "pypi",
"version": "==0.2.2"
}
},
"develop": {
"adafruit-ampy": {
"hashes": [

View File

@ -1,3 +1,21 @@
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,
}
class Keymap:
def __init__(self, map):
self.map = map
@ -6,13 +24,13 @@ class Keymap:
for row in self.map
]
def parse(self, matrix):
def parse(self, matrix, store):
for ridx, row in enumerate(matrix):
for cidx, col in enumerate(row):
if col != self.state[ridx][cidx]:
yield '{}: {}'.format(
'KEYDOWN' if col else 'KEYUP',
self.map[ridx][cidx],
)
if col:
store.dispatch(key_down_event(self.map[ridx][cidx]))
else:
store.dispatch(key_up_event(self.map[ridx][cidx]))
self.state = matrix

View File

@ -1,4 +1,4 @@
from kmk.common.keymap import Keymap
from kmk.common.keymap import KEY_DOWN_EVENT, KEY_UP_EVENT, Keymap
try:
from kmk.circuitpython.matrix import MatrixScanner
@ -6,13 +6,77 @@ 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):
self.raw_keymap = keymap
self.keymap = Keymap(keymap)
self.matrix = MatrixScanner(col_pins, row_pins, diode_orientation)
self.store = ReduxStore(reducer)
def go(self):
while True:
for event in self.keymap.parse(self.matrix.raw_scan()):
print(event)
self.keymap.parse(self.matrix.raw_scan(), store=self.store)

View File

@ -1,4 +0,0 @@
vendor/upy-lib/__future__/__future__.py
vendor/upy-lib/functools/functools.py
vendor/upy-lib/string/string.py
vendor/pydux/pydux