2018-09-03 22:50:12 +02:00
|
|
|
import logging
|
|
|
|
|
2018-09-04 00:21:34 +02:00
|
|
|
from kmk.common.event_defs import init_firmware
|
2018-09-03 22:50:12 +02:00
|
|
|
from kmk.common.internal_state import ReduxStore, kmk_reducer
|
2018-09-03 12:22:11 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
from kmk.circuitpython.matrix import MatrixScanner
|
|
|
|
except ImportError:
|
|
|
|
from kmk.micropython.matrix import MatrixScanner
|
|
|
|
|
|
|
|
|
|
|
|
class Firmware:
|
2018-09-03 22:50:12 +02:00
|
|
|
def __init__(
|
2018-09-22 08:44:30 +02:00
|
|
|
self, keymap, row_pins, col_pins,
|
2018-09-22 02:22:03 +02:00
|
|
|
diode_orientation, hid=None, log_level=logging.NOTSET,
|
2018-09-03 22:50:12 +02:00
|
|
|
):
|
2018-09-17 08:20:16 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(log_level)
|
|
|
|
|
2018-09-04 00:21:34 +02:00
|
|
|
self.cached_state = None
|
2018-09-03 22:50:12 +02:00
|
|
|
self.store = ReduxStore(kmk_reducer, log_level=log_level)
|
2018-09-04 00:21:34 +02:00
|
|
|
self.store.subscribe(
|
|
|
|
lambda state, action: self._subscription(state, action),
|
|
|
|
)
|
2018-09-17 08:20:16 +02:00
|
|
|
|
|
|
|
if not hid:
|
|
|
|
logger.warning(
|
|
|
|
"Must provide a HIDHelper (arg: hid), disabling HID\n"
|
|
|
|
"Board will run in debug mode",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.hid = hid(store=self.store, log_level=log_level)
|
|
|
|
|
2018-09-04 00:21:34 +02:00
|
|
|
self.store.dispatch(init_firmware(
|
|
|
|
keymap=keymap,
|
|
|
|
row_pins=row_pins,
|
|
|
|
col_pins=col_pins,
|
|
|
|
diode_orientation=diode_orientation,
|
|
|
|
))
|
|
|
|
|
|
|
|
def _subscription(self, state, action):
|
|
|
|
if self.cached_state is None or any(
|
|
|
|
getattr(self.cached_state, k) != getattr(state, k)
|
|
|
|
for k in state.__dict__.keys()
|
|
|
|
):
|
|
|
|
self.matrix = MatrixScanner(
|
|
|
|
state.col_pins,
|
|
|
|
state.row_pins,
|
|
|
|
state.diode_orientation,
|
|
|
|
)
|
2018-09-03 12:22:11 +02:00
|
|
|
|
|
|
|
def go(self):
|
|
|
|
while True:
|
2018-09-23 06:49:58 +02:00
|
|
|
state = self.store.get_state()
|
|
|
|
update = self.matrix.scan_for_changes(state.matrix)
|
|
|
|
|
|
|
|
if update:
|
|
|
|
self.store.dispatch(update)
|