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-10-06 12:58:19 +02:00
|
|
|
from kmk.common.internal_state import Store, kmk_reducer
|
2018-10-08 11:41:43 +02:00
|
|
|
from kmk.common.leader_mode import LeaderHelper
|
2018-09-03 12:22:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
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-10-08 11:31:30 +02:00
|
|
|
diode_orientation, unicode_mode=None,
|
2018-10-08 11:41:43 +02:00
|
|
|
hid=None,
|
2018-10-08 11:31:30 +02:00
|
|
|
log_level=logging.NOTSET,
|
|
|
|
matrix_scanner=None,
|
2018-09-03 22:50:12 +02:00
|
|
|
):
|
2018-10-06 10:32:17 +02:00
|
|
|
assert matrix_scanner is not None
|
|
|
|
self.matrix_scanner = matrix_scanner
|
|
|
|
|
2018-09-17 08:20:16 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(log_level)
|
|
|
|
|
2018-10-01 09:31:45 +02:00
|
|
|
self.hydrated = False
|
|
|
|
|
2018-10-06 12:58:19 +02:00
|
|
|
self.store = Store(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
|
|
|
|
2018-10-06 10:32:17 +02:00
|
|
|
if hid:
|
|
|
|
self.hid = hid(store=self.store, log_level=log_level)
|
|
|
|
else:
|
2018-09-17 08:20:16 +02:00
|
|
|
logger.warning(
|
|
|
|
"Must provide a HIDHelper (arg: hid), disabling HID\n"
|
|
|
|
"Board will run in debug mode",
|
|
|
|
)
|
|
|
|
|
2018-10-08 11:41:43 +02:00
|
|
|
self.leader_helper = LeaderHelper(store=self.store, log_level=log_level)
|
2018-10-08 11:31:30 +02:00
|
|
|
|
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):
|
2018-10-01 09:31:45 +02:00
|
|
|
if not self.hydrated:
|
2018-10-06 10:32:17 +02:00
|
|
|
self.matrix = self.matrix_scanner(
|
2018-09-04 00:21:34 +02:00
|
|
|
state.col_pins,
|
|
|
|
state.row_pins,
|
|
|
|
state.diode_orientation,
|
|
|
|
)
|
2018-10-01 09:31:45 +02:00
|
|
|
self.hydrated = True
|
2018-09-03 12:22:11 +02:00
|
|
|
|
|
|
|
def go(self):
|
|
|
|
while True:
|
2018-10-01 09:31:45 +02:00
|
|
|
update = self.matrix.scan_for_pressed()
|
2018-09-23 06:49:58 +02:00
|
|
|
|
|
|
|
if update:
|
|
|
|
self.store.dispatch(update)
|