2022-08-05 21:24:01 +02:00
|
|
|
try:
|
2022-09-22 21:02:14 +02:00
|
|
|
from typing import Callable, Optional, Tuple
|
2022-08-05 21:24:01 +02:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2021-09-13 17:18:01 +02:00
|
|
|
from supervisor import ticks_ms
|
|
|
|
|
2022-10-08 21:56:22 +02:00
|
|
|
from collections import namedtuple
|
2022-09-22 21:02:14 +02:00
|
|
|
from keypad import Event as KeyEvent
|
|
|
|
|
2022-08-03 18:34:22 +02:00
|
|
|
from kmk.consts import UnicodeMode
|
2019-07-29 02:09:58 +02:00
|
|
|
from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes
|
2022-08-05 21:24:01 +02:00
|
|
|
from kmk.keys import KC, Key
|
2022-03-09 02:22:09 +01:00
|
|
|
from kmk.kmktime import ticks_add, ticks_diff
|
2022-08-05 21:24:01 +02:00
|
|
|
from kmk.modules import Module
|
2022-04-10 14:46:40 +02:00
|
|
|
from kmk.scanners.keypad import MatrixScanner
|
2022-07-20 15:35:56 +02:00
|
|
|
from kmk.utils import Debug
|
|
|
|
|
|
|
|
debug = Debug(__name__)
|
2019-07-25 08:43:00 +02:00
|
|
|
|
2022-10-08 21:56:22 +02:00
|
|
|
KeyBufferFrame = namedtuple(
|
|
|
|
'KeyBufferFrame', ('key', 'is_pressed', 'int_coord', 'index')
|
|
|
|
)
|
|
|
|
|
2018-09-03 12:22:11 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
class Sandbox:
|
|
|
|
matrix_update = None
|
|
|
|
secondary_matrix_update = None
|
|
|
|
active_layers = None
|
|
|
|
|
|
|
|
|
2019-07-25 10:30:55 +02:00
|
|
|
class KMKKeyboard:
|
2019-07-29 02:09:58 +02:00
|
|
|
#####
|
|
|
|
# User-configurable
|
2020-10-21 21:19:42 +02:00
|
|
|
keymap = []
|
2019-05-13 02:04:23 +02:00
|
|
|
coord_mapping = None
|
2018-10-16 13:04:39 +02:00
|
|
|
|
|
|
|
row_pins = None
|
|
|
|
col_pins = None
|
|
|
|
diode_orientation = None
|
2020-10-21 21:19:42 +02:00
|
|
|
matrix = None
|
2018-10-16 13:04:39 +02:00
|
|
|
|
2018-12-05 02:03:13 +01:00
|
|
|
unicode_mode = UnicodeMode.NOOP
|
2019-02-23 04:50:56 +01:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
modules = []
|
|
|
|
extensions = []
|
|
|
|
sandbox = Sandbox()
|
2019-07-13 02:11:36 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
#####
|
|
|
|
# Internal State
|
2020-10-21 21:19:42 +02:00
|
|
|
keys_pressed = set()
|
|
|
|
_coordkeys_pressed = {}
|
|
|
|
hid_type = HIDModes.USB
|
|
|
|
secondary_hid_type = None
|
|
|
|
_hid_helper = None
|
2022-02-03 23:04:58 +01:00
|
|
|
_hid_send_enabled = False
|
2020-10-21 21:19:42 +02:00
|
|
|
hid_pending = False
|
|
|
|
matrix_update = None
|
|
|
|
secondary_matrix_update = None
|
2022-02-01 04:51:22 +01:00
|
|
|
matrix_update_queue = []
|
2020-10-21 21:19:42 +02:00
|
|
|
state_changed = False
|
|
|
|
_trigger_powersave_enable = False
|
|
|
|
_trigger_powersave_disable = False
|
|
|
|
i2c_deinit_count = 0
|
2021-09-17 15:47:41 +02:00
|
|
|
_go_args = None
|
2022-04-01 19:55:40 +02:00
|
|
|
_processing_timeouts = False
|
2022-10-08 21:56:22 +02:00
|
|
|
_resume_buffer = []
|
|
|
|
_resume_buffer_x = []
|
2019-07-29 02:09:58 +02:00
|
|
|
|
|
|
|
# this should almost always be PREpended to, replaces
|
|
|
|
# former use of reversed_active_layers which had pointless
|
|
|
|
# overhead (the underlying list was never used anyway)
|
2020-10-21 21:19:42 +02:00
|
|
|
active_layers = [0]
|
2019-07-29 02:09:58 +02:00
|
|
|
|
|
|
|
_timeouts = {}
|
2019-07-13 02:11:36 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
# on some M4 setups (such as klardotsh/klarank_feather_m4, CircuitPython
|
|
|
|
# 6.0rc1) this runs out of RAM every cycle and takes down the board. no
|
|
|
|
# real known fix yet other than turning off debug, but M4s have always been
|
|
|
|
# tight on RAM so....
|
2022-09-24 06:56:10 +02:00
|
|
|
def __repr__(self) -> str:
|
2022-06-11 23:54:01 +02:00
|
|
|
return ''.join(
|
|
|
|
[
|
|
|
|
'KMKKeyboard(\n',
|
|
|
|
f' debug_enabled={self.debug_enabled}, ',
|
|
|
|
f'diode_orientation={self.diode_orientation}, ',
|
|
|
|
f'matrix={self.matrix},\n',
|
|
|
|
f' unicode_mode={self.unicode_mode}, ',
|
|
|
|
f'_hid_helper={self._hid_helper},\n',
|
|
|
|
f' keys_pressed={self.keys_pressed},\n',
|
|
|
|
f' _coordkeys_pressed={self._coordkeys_pressed},\n',
|
|
|
|
f' hid_pending={self.hid_pending}, ',
|
|
|
|
f'active_layers={self.active_layers}, ',
|
|
|
|
f'_timeouts={self._timeouts}\n',
|
|
|
|
')',
|
|
|
|
]
|
2019-07-13 00:16:33 +02:00
|
|
|
)
|
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _print_debug_cycle(self, init: bool = False) -> None:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'coordkeys_pressed={self._coordkeys_pressed}')
|
|
|
|
debug(f'keys_pressed={self.keys_pressed}')
|
2018-10-17 07:50:41 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _send_hid(self) -> None:
|
2022-02-03 23:04:58 +01:00
|
|
|
if self._hid_send_enabled:
|
|
|
|
hid_report = self._hid_helper.create_report(self.keys_pressed)
|
|
|
|
try:
|
|
|
|
hid_report.send()
|
|
|
|
except KeyError as e:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'HidNotFound(HIDReportType={e})')
|
2020-10-21 21:19:42 +02:00
|
|
|
self.hid_pending = False
|
2018-10-17 07:50:41 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _handle_matrix_report(self, kevent: KeyEvent) -> None:
|
|
|
|
if kevent is not None:
|
|
|
|
self._on_matrix_changed(kevent)
|
2019-07-29 02:09:58 +02:00
|
|
|
self.state_changed = True
|
2018-10-19 11:22:22 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _find_key_in_map(self, int_coord: int) -> Key:
|
2019-07-29 02:09:58 +02:00
|
|
|
try:
|
2020-10-21 21:19:42 +02:00
|
|
|
idx = self.coord_mapping.index(int_coord)
|
2019-07-29 02:09:58 +02:00
|
|
|
except ValueError:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'CoordMappingNotFound(ic={int_coord})')
|
2019-07-29 02:09:58 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
for layer in self.active_layers:
|
2021-12-12 11:11:35 +01:00
|
|
|
try:
|
|
|
|
layer_key = self.keymap[layer][idx]
|
|
|
|
except IndexError:
|
|
|
|
layer_key = None
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'KeymapIndexError(idx={idx}, layer={layer})')
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2021-09-19 04:24:37 +02:00
|
|
|
if not layer_key or layer_key == KC.TRNS:
|
2019-07-29 02:09:58 +02:00
|
|
|
continue
|
2018-10-26 23:26:15 +02:00
|
|
|
|
2021-09-19 04:24:37 +02:00
|
|
|
return layer_key
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _on_matrix_changed(self, kevent: KeyEvent) -> None:
|
2022-01-31 01:02:51 +01:00
|
|
|
int_coord = kevent.key_number
|
|
|
|
is_pressed = kevent.pressed
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'MatrixChange(ic={int_coord}, pressed={is_pressed})')
|
2021-09-17 15:59:57 +02:00
|
|
|
|
2022-04-23 22:12:31 +02:00
|
|
|
key = None
|
2022-02-01 04:51:22 +01:00
|
|
|
if not is_pressed:
|
2022-01-31 01:02:51 +01:00
|
|
|
try:
|
2022-04-23 22:12:31 +02:00
|
|
|
key = self._coordkeys_pressed[int_coord]
|
2022-01-31 01:02:51 +01:00
|
|
|
except KeyError:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'KeyNotPressed(ic={int_coord})')
|
2022-02-01 04:51:22 +01:00
|
|
|
|
2022-04-23 22:12:31 +02:00
|
|
|
if key is None:
|
|
|
|
key = self._find_key_in_map(int_coord)
|
2021-09-17 15:59:57 +02:00
|
|
|
|
2022-04-23 22:12:31 +02:00
|
|
|
if key is None:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'MatrixUndefinedCoordinate(ic={int_coord})')
|
2022-02-03 22:09:39 +01:00
|
|
|
return self
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'KeyResolution(key={key})')
|
2022-04-23 23:16:47 +02:00
|
|
|
|
2022-04-23 22:12:31 +02:00
|
|
|
self.pre_process_key(key, is_pressed, int_coord)
|
2022-04-23 21:48:55 +02:00
|
|
|
|
2022-10-08 21:56:22 +02:00
|
|
|
def _process_resume_buffer(self):
|
|
|
|
'''
|
|
|
|
Resume the processing of buffered, delayed, deferred, etc. key events
|
|
|
|
emitted by modules.
|
|
|
|
|
|
|
|
We use a copy of the `_resume_buffer` as a working buffer. The working
|
|
|
|
buffer holds all key events in the correct order for processing. If
|
|
|
|
during processing new events are pushed to the `_resume_buffer`, they
|
|
|
|
are prepended to the working buffer (which may not be emptied), in
|
|
|
|
order to preserve key event order.
|
|
|
|
We also double-buffer `_resume_buffer` with `_resume_buffer_x`, only
|
|
|
|
copying the reference to hopefully safe some time on allocations.
|
|
|
|
'''
|
|
|
|
|
|
|
|
buffer, self._resume_buffer = self._resume_buffer, self._resume_buffer_x
|
|
|
|
|
|
|
|
while buffer:
|
|
|
|
ksf = buffer.pop(0)
|
|
|
|
key = ksf.key
|
|
|
|
|
|
|
|
# Handle any unaccounted-for layer shifts by looking up the key resolution again.
|
|
|
|
if ksf.int_coord in self._coordkeys_pressed.keys():
|
|
|
|
key = self._find_key_in_map(ksf.int_coord)
|
|
|
|
|
|
|
|
# Resume the processing of the key event and update the HID report
|
|
|
|
# when applicable.
|
|
|
|
self.pre_process_key(key, ksf.is_pressed, ksf.int_coord, ksf.index)
|
|
|
|
|
|
|
|
if self.hid_pending:
|
|
|
|
self._send_hid()
|
|
|
|
self.hid_pending = False
|
|
|
|
|
|
|
|
# Any newly buffered key events must be prepended to the working
|
|
|
|
# buffer.
|
|
|
|
if self._resume_buffer:
|
|
|
|
self._resume_buffer.extend(buffer)
|
|
|
|
buffer.clear()
|
|
|
|
buffer, self._resume_buffer = self._resume_buffer, buffer
|
|
|
|
|
|
|
|
self._resume_buffer_x = buffer
|
|
|
|
|
2022-07-20 15:35:56 +02:00
|
|
|
@property
|
2022-09-22 21:02:14 +02:00
|
|
|
def debug_enabled(self) -> bool:
|
2022-07-20 15:35:56 +02:00
|
|
|
return debug.enabled
|
|
|
|
|
|
|
|
@debug_enabled.setter
|
2022-09-22 21:02:14 +02:00
|
|
|
def debug_enabled(self, enabled: bool):
|
2022-07-20 15:35:56 +02:00
|
|
|
debug.enabled = enabled
|
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def pre_process_key(
|
|
|
|
self,
|
|
|
|
key: Key,
|
|
|
|
is_pressed: bool,
|
|
|
|
int_coord: Optional[int] = None,
|
|
|
|
index: int = 0,
|
|
|
|
) -> None:
|
2022-07-27 17:17:50 +02:00
|
|
|
for module in self.modules[index:]:
|
2022-01-18 06:21:05 +01:00
|
|
|
try:
|
2022-04-23 22:12:31 +02:00
|
|
|
key = module.process_key(self, key, is_pressed, int_coord)
|
|
|
|
if key is None:
|
2022-01-18 06:21:05 +01:00
|
|
|
break
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.process_key: {err}')
|
2022-02-03 22:09:39 +01:00
|
|
|
|
2022-04-23 22:12:31 +02:00
|
|
|
if int_coord is not None:
|
|
|
|
if is_pressed:
|
|
|
|
self._coordkeys_pressed[int_coord] = key
|
|
|
|
else:
|
2022-05-23 22:08:05 +02:00
|
|
|
try:
|
|
|
|
del self._coordkeys_pressed[int_coord]
|
|
|
|
except KeyError:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'ReleaseKeyError(ic={int_coord})')
|
2022-02-03 22:09:39 +01:00
|
|
|
|
2022-04-23 22:12:31 +02:00
|
|
|
if key:
|
|
|
|
self.process_key(key, is_pressed, int_coord)
|
2022-02-03 22:09:39 +01:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def process_key(
|
|
|
|
self, key: Key, is_pressed: bool, coord_int: Optional[int] = None
|
|
|
|
) -> None:
|
2022-01-18 06:21:05 +01:00
|
|
|
if is_pressed:
|
2022-01-31 01:02:51 +01:00
|
|
|
key.on_press(self, coord_int)
|
2019-07-29 02:09:58 +02:00
|
|
|
else:
|
2022-01-31 01:02:51 +01:00
|
|
|
key.on_release(self, coord_int)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-08-05 21:24:01 +02:00
|
|
|
def resume_process_key(
|
|
|
|
self,
|
2022-08-10 19:28:12 +02:00
|
|
|
module: Module,
|
2022-08-05 21:24:01 +02:00
|
|
|
key: Key,
|
|
|
|
is_pressed: bool,
|
2022-08-10 19:28:12 +02:00
|
|
|
int_coord: Optional[int] = None,
|
2022-08-05 21:24:01 +02:00
|
|
|
) -> None:
|
2022-08-05 21:22:04 +02:00
|
|
|
index = self.modules.index(module) + 1
|
2022-10-08 21:56:22 +02:00
|
|
|
ksf = KeyBufferFrame(
|
|
|
|
key=key, is_pressed=is_pressed, int_coord=int_coord, index=index
|
|
|
|
)
|
|
|
|
self._resume_buffer.append(ksf)
|
2022-08-05 21:22:04 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def remove_key(self, keycode: Key) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
self.keys_pressed.discard(keycode)
|
2022-09-22 21:02:14 +02:00
|
|
|
self.process_key(keycode, False)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def add_key(self, keycode: Key) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
self.keys_pressed.add(keycode)
|
2022-09-22 21:02:14 +02:00
|
|
|
self.process_key(keycode, True)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def tap_key(self, keycode: Key) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
self.add_key(keycode)
|
2019-07-29 02:09:58 +02:00
|
|
|
# On the next cycle, we'll remove the key.
|
2020-10-21 21:19:42 +02:00
|
|
|
self.set_timeout(False, lambda: self.remove_key(keycode))
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def set_timeout(
|
|
|
|
self, after_ticks: int, callback: Callable[[None], None]
|
|
|
|
) -> Tuple[int, int]:
|
2022-04-01 19:55:40 +02:00
|
|
|
# We allow passing False as an implicit "run this on the next process timeouts cycle"
|
2019-07-29 02:09:58 +02:00
|
|
|
if after_ticks is False:
|
2022-04-01 19:55:40 +02:00
|
|
|
after_ticks = 0
|
|
|
|
|
|
|
|
if after_ticks == 0 and self._processing_timeouts:
|
|
|
|
after_ticks += 1
|
|
|
|
|
|
|
|
timeout_key = ticks_add(ticks_ms(), after_ticks)
|
2018-10-20 04:01:39 +02:00
|
|
|
|
2022-03-09 02:22:09 +01:00
|
|
|
if timeout_key not in self._timeouts:
|
|
|
|
self._timeouts[timeout_key] = []
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-03-09 02:22:09 +01:00
|
|
|
idx = len(self._timeouts[timeout_key])
|
|
|
|
self._timeouts[timeout_key].append(callback)
|
|
|
|
|
|
|
|
return (timeout_key, idx)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def cancel_timeout(self, timeout_key: int) -> None:
|
2022-03-09 02:22:09 +01:00
|
|
|
try:
|
|
|
|
self._timeouts[timeout_key[0]][timeout_key[1]] = None
|
|
|
|
except (KeyError, IndexError):
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'no such timeout: {timeout_key}')
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _process_timeouts(self) -> None:
|
2019-07-29 02:09:58 +02:00
|
|
|
if not self._timeouts:
|
2022-09-22 21:02:14 +02:00
|
|
|
return
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-04-01 19:55:40 +02:00
|
|
|
# Copy timeout keys to a temporary list to allow sorting.
|
|
|
|
# Prevent net timeouts set during handling from running on the current
|
|
|
|
# cycle by setting a flag `_processing_timeouts`.
|
2019-07-29 02:09:58 +02:00
|
|
|
current_time = ticks_ms()
|
2022-04-01 19:55:40 +02:00
|
|
|
timeout_keys = []
|
|
|
|
self._processing_timeouts = True
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-04-01 19:55:40 +02:00
|
|
|
for k in self._timeouts.keys():
|
2022-03-09 02:22:09 +01:00
|
|
|
if ticks_diff(k, current_time) <= 0:
|
2022-04-01 19:55:40 +02:00
|
|
|
timeout_keys.append(k)
|
2022-03-09 02:22:09 +01:00
|
|
|
|
2022-07-20 15:35:56 +02:00
|
|
|
if timeout_keys and debug.enabled:
|
|
|
|
debug('processing timeouts')
|
|
|
|
|
2022-04-01 19:55:40 +02:00
|
|
|
for k in sorted(timeout_keys):
|
|
|
|
for callback in self._timeouts[k]:
|
2022-03-09 02:22:09 +01:00
|
|
|
if callback:
|
|
|
|
callback()
|
2022-04-01 19:55:40 +02:00
|
|
|
del self._timeouts[k]
|
|
|
|
|
|
|
|
self._processing_timeouts = False
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _init_sanity_check(self) -> None:
|
2019-07-29 02:09:58 +02:00
|
|
|
'''
|
|
|
|
Ensure the provided configuration is *probably* bootable
|
|
|
|
'''
|
2018-10-16 13:04:39 +02:00
|
|
|
assert self.keymap, 'must define a keymap with at least one row'
|
Continue to shuffle and burn stuff
- Remove the concept of "mcus". With only one target platform
(CircuitPython), it no longer makes a bunch of sense and has been kept
around for "what if" reasons, complicating our import chains and eating
up RAM for pointless subclasses. If you're a `board`, you derive from
`KeyboardConfig`. If you're a handwire, the user will derive from
`KeyboardConfig`. The end. As part of this, `kmk.hid` was refactored
heavily to emphasize that CircuitPython is our only supported HID stack,
with stubs for future HID implementations (`USB_HID` becomes
`AbstractHID`, probably only usable for testing purposes,
`CircuitPython_USB_HID` becomes `USBHID`, and `BLEHID` is added with an
immediate `NotImplementedError` on instantiation)
- `KeyboardConfig` can now take a HID type at runtime. The NRF52840
boards will happily run in either configuration once CircuitPython
support is in place, and a completely separate `mcu` subclass for each
mode made no sense. This also potentially allows runtime *swaps* of HID
driver down the line, but no code has been added to this effect. The
default, and only functional value, for this is `HIDModes.USB`
- Most consts have been moved to more logical homes - often, the main
or, often only, component that uses them. `DiodeOrientation` moved to
`kmk.matrix`, and anything HID-related moved to `kmk.hid`
2019-07-25 09:58:23 +02:00
|
|
|
assert (
|
2019-07-29 02:09:58 +02:00
|
|
|
self.hid_type in HIDModes.ALL_MODES
|
Continue to shuffle and burn stuff
- Remove the concept of "mcus". With only one target platform
(CircuitPython), it no longer makes a bunch of sense and has been kept
around for "what if" reasons, complicating our import chains and eating
up RAM for pointless subclasses. If you're a `board`, you derive from
`KeyboardConfig`. If you're a handwire, the user will derive from
`KeyboardConfig`. The end. As part of this, `kmk.hid` was refactored
heavily to emphasize that CircuitPython is our only supported HID stack,
with stubs for future HID implementations (`USB_HID` becomes
`AbstractHID`, probably only usable for testing purposes,
`CircuitPython_USB_HID` becomes `USBHID`, and `BLEHID` is added with an
immediate `NotImplementedError` on instantiation)
- `KeyboardConfig` can now take a HID type at runtime. The NRF52840
boards will happily run in either configuration once CircuitPython
support is in place, and a completely separate `mcu` subclass for each
mode made no sense. This also potentially allows runtime *swaps* of HID
driver down the line, but no code has been added to this effect. The
default, and only functional value, for this is `HIDModes.USB`
- Most consts have been moved to more logical homes - often, the main
or, often only, component that uses them. `DiodeOrientation` moved to
`kmk.matrix`, and anything HID-related moved to `kmk.hid`
2019-07-25 09:58:23 +02:00
|
|
|
), 'hid_type must be a value from kmk.consts.HIDModes'
|
2022-02-21 02:34:44 +01:00
|
|
|
if not self.matrix:
|
|
|
|
assert self.row_pins, 'no GPIO pins defined for matrix rows'
|
|
|
|
assert self.col_pins, 'no GPIO pins defined for matrix columns'
|
|
|
|
assert (
|
|
|
|
self.diode_orientation is not None
|
|
|
|
), 'diode orientation must be defined'
|
2018-10-16 13:04:39 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _init_coord_mapping(self) -> None:
|
2019-07-29 02:09:58 +02:00
|
|
|
'''
|
|
|
|
Attempt to sanely guess a coord_mapping if one is not provided. No-op
|
|
|
|
if `kmk.extensions.split.Split` is used, it provides equivalent
|
|
|
|
functionality in `on_bootup`
|
|
|
|
|
|
|
|
To save RAM on boards that don't use Split, we don't import Split
|
|
|
|
and do an isinstance check, but instead do string detection
|
|
|
|
'''
|
2020-10-21 21:19:42 +02:00
|
|
|
if any(x.__class__.__module__ == 'kmk.modules.split' for x in self.modules):
|
2019-07-29 02:09:58 +02:00
|
|
|
return
|
2019-07-17 08:30:14 +02:00
|
|
|
|
|
|
|
if not self.coord_mapping:
|
2022-04-16 22:04:42 +02:00
|
|
|
cm = []
|
|
|
|
for m in self.matrix:
|
|
|
|
cm.extend(m.coord_mapping)
|
|
|
|
self.coord_mapping = tuple(cm)
|
2019-07-17 08:30:14 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _init_hid(self) -> None:
|
2019-07-29 02:09:58 +02:00
|
|
|
if self.hid_type == HIDModes.NOOP:
|
2020-10-21 21:19:42 +02:00
|
|
|
self._hid_helper = AbstractHID
|
2019-07-29 02:09:58 +02:00
|
|
|
elif self.hid_type == HIDModes.USB:
|
2020-10-21 21:19:42 +02:00
|
|
|
self._hid_helper = USBHID
|
2019-07-29 02:09:58 +02:00
|
|
|
elif self.hid_type == HIDModes.BLE:
|
2020-10-21 21:19:42 +02:00
|
|
|
self._hid_helper = BLEHID
|
|
|
|
else:
|
|
|
|
self._hid_helper = AbstractHID
|
2021-09-17 15:47:41 +02:00
|
|
|
self._hid_helper = self._hid_helper(**self._go_args)
|
2022-02-03 23:04:58 +01:00
|
|
|
self._hid_send_enabled = True
|
2019-05-12 23:13:41 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _init_matrix(self) -> None:
|
2021-10-03 16:40:18 +02:00
|
|
|
if self.matrix is None:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug('Initialising default matrix scanner.')
|
2022-04-10 14:46:40 +02:00
|
|
|
self.matrix = MatrixScanner(
|
2022-04-10 21:28:19 +02:00
|
|
|
column_pins=self.col_pins,
|
2022-04-09 21:34:06 +02:00
|
|
|
row_pins=self.row_pins,
|
2022-04-10 21:28:19 +02:00
|
|
|
columns_to_anodes=self.diode_orientation,
|
2021-10-03 16:40:18 +02:00
|
|
|
)
|
2018-10-17 07:30:33 +02:00
|
|
|
|
2022-04-16 22:04:42 +02:00
|
|
|
try:
|
|
|
|
self.matrix = tuple(iter(self.matrix))
|
|
|
|
offset = 0
|
|
|
|
for matrix in self.matrix:
|
|
|
|
matrix.offset = offset
|
|
|
|
offset += matrix.key_count
|
|
|
|
except TypeError:
|
|
|
|
self.matrix = (self.matrix,)
|
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def before_matrix_scan(self) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.before_matrix_scan(self)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.before_matrix_scan: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.before_matrix_scan(self.sandbox)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {ext}.before_matrix_scan: {err}')
|
2018-12-29 15:58:08 +01:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def after_matrix_scan(self) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.after_matrix_scan(self)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.after_matrix_scan: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.after_matrix_scan(self.sandbox)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {ext}.after_matrix_scan: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def before_hid_send(self) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.before_hid_send(self)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.before_hid_send: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.before_hid_send(self.sandbox)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(
|
|
|
|
f'Error in {ext}.before_hid_send: {err}',
|
2022-05-01 11:21:29 +02:00
|
|
|
)
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def after_hid_send(self) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.after_hid_send(self)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.after_hid_send: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.after_hid_send(self.sandbox)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {ext}.after_hid_send: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def powersave_enable(self) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.on_powersave_enable(self)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.on_powersave: {err}')
|
2018-10-19 08:24:19 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.on_powersave_enable(self.sandbox)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {ext}.powersave_enable: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def powersave_disable(self) -> None:
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.on_powersave_disable(self)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {module}.powersave_disable: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.on_powersave_disable(self.sandbox)
|
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Error in {ext}.powersave_disable: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs) -> None:
|
2021-12-05 15:24:51 +01:00
|
|
|
self._init(hid_type=hid_type, secondary_hid_type=secondary_hid_type, **kwargs)
|
|
|
|
while True:
|
|
|
|
self._main_loop()
|
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _init(
|
|
|
|
self,
|
|
|
|
hid_type: HIDModes = HIDModes.USB,
|
|
|
|
secondary_hid_type: Optional[HIDModes] = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> None:
|
2021-09-17 15:47:41 +02:00
|
|
|
self._go_args = kwargs
|
2019-07-29 02:09:58 +02:00
|
|
|
self.hid_type = hid_type
|
2020-10-21 21:19:42 +02:00
|
|
|
self.secondary_hid_type = secondary_hid_type
|
2018-10-26 08:00:11 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
self._init_sanity_check()
|
|
|
|
self._init_hid()
|
2022-01-31 01:02:51 +01:00
|
|
|
self._init_matrix()
|
2022-04-10 01:54:23 +02:00
|
|
|
self._init_coord_mapping()
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.during_bootup(self)
|
2022-01-18 17:26:00 +01:00
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Failed to load module {module}: {err}')
|
2020-10-21 21:19:42 +02:00
|
|
|
for ext in self.extensions:
|
2019-07-29 02:09:58 +02:00
|
|
|
try:
|
|
|
|
ext.during_bootup(self)
|
2022-01-18 17:26:00 +01:00
|
|
|
except Exception as err:
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
|
|
|
debug(f'Failed to load extensions {module}: {err}')
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-07-20 15:35:56 +02:00
|
|
|
if debug.enabled:
|
2022-08-03 18:34:22 +02:00
|
|
|
debug(f'init: {self}')
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-09-22 21:02:14 +02:00
|
|
|
def _main_loop(self) -> None:
|
2021-12-05 15:24:51 +01:00
|
|
|
self.state_changed = False
|
|
|
|
self.sandbox.active_layers = self.active_layers.copy()
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
self.before_matrix_scan()
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2022-10-08 21:56:22 +02:00
|
|
|
self._process_resume_buffer()
|
|
|
|
|
2022-04-16 22:04:42 +02:00
|
|
|
for matrix in self.matrix:
|
|
|
|
update = matrix.scan_for_changes()
|
|
|
|
if update:
|
2022-05-03 01:36:19 +02:00
|
|
|
self.matrix_update = update
|
2022-04-16 22:04:42 +02:00
|
|
|
break
|
2022-09-18 20:35:46 +02:00
|
|
|
self.sandbox.matrix_update = self.matrix_update
|
2021-12-05 15:24:51 +01:00
|
|
|
self.sandbox.secondary_matrix_update = self.secondary_matrix_update
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
self.after_matrix_scan()
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2022-02-01 04:51:22 +01:00
|
|
|
if self.secondary_matrix_update:
|
2022-01-31 01:02:51 +01:00
|
|
|
self.matrix_update_queue.append(self.secondary_matrix_update)
|
2022-02-01 04:51:22 +01:00
|
|
|
self.secondary_matrix_update = None
|
|
|
|
|
|
|
|
if self.matrix_update:
|
2022-01-31 01:02:51 +01:00
|
|
|
self.matrix_update_queue.append(self.matrix_update)
|
2022-02-01 04:51:22 +01:00
|
|
|
self.matrix_update = None
|
|
|
|
|
|
|
|
# only handle one key per cycle.
|
|
|
|
if self.matrix_update_queue:
|
|
|
|
self._handle_matrix_report(self.matrix_update_queue.pop(0))
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
self.before_hid_send()
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
if self.hid_pending:
|
|
|
|
self._send_hid()
|
2018-10-16 13:04:39 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
self._process_timeouts()
|
2018-10-21 01:55:17 +02:00
|
|
|
|
2022-07-20 15:35:56 +02:00
|
|
|
if self.hid_pending:
|
|
|
|
self._send_hid()
|
2021-12-05 15:24:51 +01:00
|
|
|
self.state_changed = True
|
2018-12-30 01:52:06 +01:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
self.after_hid_send()
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
if self._trigger_powersave_enable:
|
|
|
|
self.powersave_enable()
|
2020-10-21 21:19:42 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
if self._trigger_powersave_disable:
|
|
|
|
self.powersave_disable()
|
2019-07-13 02:11:36 +02:00
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
if self.state_changed:
|
|
|
|
self._print_debug_cycle()
|