add type hinting for kmk_keyboard

This commit is contained in:
xs5871 2022-09-22 19:02:14 +00:00 committed by Kyle Brown
parent 63fd4d9574
commit f2fb4eecf4

View File

@ -1,10 +1,12 @@
try: try:
from typing import Optional from typing import Callable, Optional, Tuple
except ImportError: except ImportError:
pass pass
from supervisor import ticks_ms from supervisor import ticks_ms
from keypad import Event as KeyEvent
from kmk.consts import UnicodeMode from kmk.consts import UnicodeMode
from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes
from kmk.keys import KC, Key from kmk.keys import KC, Key
@ -69,7 +71,7 @@ class KMKKeyboard:
# 6.0rc1) this runs out of RAM every cycle and takes down the board. no # 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 # real known fix yet other than turning off debug, but M4s have always been
# tight on RAM so.... # tight on RAM so....
def __repr__(self): def __repr__(self) -> None:
return ''.join( return ''.join(
[ [
'KMKKeyboard(\n', 'KMKKeyboard(\n',
@ -87,12 +89,12 @@ class KMKKeyboard:
] ]
) )
def _print_debug_cycle(self, init=False): def _print_debug_cycle(self, init: bool = False) -> None:
if debug.enabled: if debug.enabled:
debug(f'coordkeys_pressed={self._coordkeys_pressed}') debug(f'coordkeys_pressed={self._coordkeys_pressed}')
debug(f'keys_pressed={self.keys_pressed}') debug(f'keys_pressed={self.keys_pressed}')
def _send_hid(self): def _send_hid(self) -> None:
if self._hid_send_enabled: if self._hid_send_enabled:
hid_report = self._hid_helper.create_report(self.keys_pressed) hid_report = self._hid_helper.create_report(self.keys_pressed)
try: try:
@ -102,12 +104,12 @@ class KMKKeyboard:
debug(f'HidNotFound(HIDReportType={e})') debug(f'HidNotFound(HIDReportType={e})')
self.hid_pending = False self.hid_pending = False
def _handle_matrix_report(self, update=None): def _handle_matrix_report(self, kevent: KeyEvent) -> None:
if update is not None: if kevent is not None:
self._on_matrix_changed(update) self._on_matrix_changed(kevent)
self.state_changed = True self.state_changed = True
def _find_key_in_map(self, int_coord): def _find_key_in_map(self, int_coord: int) -> Key:
try: try:
idx = self.coord_mapping.index(int_coord) idx = self.coord_mapping.index(int_coord)
except ValueError: except ValueError:
@ -129,7 +131,7 @@ class KMKKeyboard:
return layer_key return layer_key
def _on_matrix_changed(self, kevent): def _on_matrix_changed(self, kevent: KeyEvent) -> None:
int_coord = kevent.key_number int_coord = kevent.key_number
is_pressed = kevent.pressed is_pressed = kevent.pressed
if debug.enabled: if debug.enabled:
@ -157,14 +159,20 @@ class KMKKeyboard:
self.pre_process_key(key, is_pressed, int_coord) self.pre_process_key(key, is_pressed, int_coord)
@property @property
def debug_enabled(self): def debug_enabled(self) -> bool:
return debug.enabled return debug.enabled
@debug_enabled.setter @debug_enabled.setter
def debug_enabled(self, enabled): def debug_enabled(self, enabled: bool):
debug.enabled = enabled debug.enabled = enabled
def pre_process_key(self, key, is_pressed, int_coord=None, index=0): def pre_process_key(
self,
key: Key,
is_pressed: bool,
int_coord: Optional[int] = None,
index: int = 0,
) -> None:
for module in self.modules[index:]: for module in self.modules[index:]:
try: try:
key = module.process_key(self, key, is_pressed, int_coord) key = module.process_key(self, key, is_pressed, int_coord)
@ -187,16 +195,14 @@ class KMKKeyboard:
if key: if key:
self.process_key(key, is_pressed, int_coord) self.process_key(key, is_pressed, int_coord)
return self def process_key(
self, key: Key, is_pressed: bool, coord_int: Optional[int] = None
def process_key(self, key, is_pressed, coord_int=None): ) -> None:
if is_pressed: if is_pressed:
key.on_press(self, coord_int) key.on_press(self, coord_int)
else: else:
key.on_release(self, coord_int) key.on_release(self, coord_int)
return self
def resume_process_key( def resume_process_key(
self, self,
module: Module, module: Module,
@ -207,22 +213,22 @@ class KMKKeyboard:
index = self.modules.index(module) + 1 index = self.modules.index(module) + 1
self.pre_process_key(key, is_pressed, int_coord, index) self.pre_process_key(key, is_pressed, int_coord, index)
def remove_key(self, keycode): def remove_key(self, keycode: Key) -> None:
self.keys_pressed.discard(keycode) self.keys_pressed.discard(keycode)
return self.process_key(keycode, False) self.process_key(keycode, False)
def add_key(self, keycode): def add_key(self, keycode: Key) -> None:
self.keys_pressed.add(keycode) self.keys_pressed.add(keycode)
return self.process_key(keycode, True) self.process_key(keycode, True)
def tap_key(self, keycode): def tap_key(self, keycode: Key) -> None:
self.add_key(keycode) self.add_key(keycode)
# On the next cycle, we'll remove the key. # On the next cycle, we'll remove the key.
self.set_timeout(False, lambda: self.remove_key(keycode)) self.set_timeout(False, lambda: self.remove_key(keycode))
return self def set_timeout(
self, after_ticks: int, callback: Callable[[None], None]
def set_timeout(self, after_ticks, callback): ) -> Tuple[int, int]:
# We allow passing False as an implicit "run this on the next process timeouts cycle" # We allow passing False as an implicit "run this on the next process timeouts cycle"
if after_ticks is False: if after_ticks is False:
after_ticks = 0 after_ticks = 0
@ -240,16 +246,16 @@ class KMKKeyboard:
return (timeout_key, idx) return (timeout_key, idx)
def cancel_timeout(self, timeout_key): def cancel_timeout(self, timeout_key: int) -> None:
try: try:
self._timeouts[timeout_key[0]][timeout_key[1]] = None self._timeouts[timeout_key[0]][timeout_key[1]] = None
except (KeyError, IndexError): except (KeyError, IndexError):
if debug.enabled: if debug.enabled:
debug(f'no such timeout: {timeout_key}') debug(f'no such timeout: {timeout_key}')
def _process_timeouts(self): def _process_timeouts(self) -> None:
if not self._timeouts: if not self._timeouts:
return self return
# Copy timeout keys to a temporary list to allow sorting. # Copy timeout keys to a temporary list to allow sorting.
# Prevent net timeouts set during handling from running on the current # Prevent net timeouts set during handling from running on the current
@ -273,9 +279,7 @@ class KMKKeyboard:
self._processing_timeouts = False self._processing_timeouts = False
return self def _init_sanity_check(self) -> None:
def _init_sanity_check(self):
''' '''
Ensure the provided configuration is *probably* bootable Ensure the provided configuration is *probably* bootable
''' '''
@ -290,9 +294,7 @@ class KMKKeyboard:
self.diode_orientation is not None self.diode_orientation is not None
), 'diode orientation must be defined' ), 'diode orientation must be defined'
return self def _init_coord_mapping(self) -> None:
def _init_coord_mapping(self):
''' '''
Attempt to sanely guess a coord_mapping if one is not provided. No-op Attempt to sanely guess a coord_mapping if one is not provided. No-op
if `kmk.extensions.split.Split` is used, it provides equivalent if `kmk.extensions.split.Split` is used, it provides equivalent
@ -310,7 +312,7 @@ class KMKKeyboard:
cm.extend(m.coord_mapping) cm.extend(m.coord_mapping)
self.coord_mapping = tuple(cm) self.coord_mapping = tuple(cm)
def _init_hid(self): def _init_hid(self) -> None:
if self.hid_type == HIDModes.NOOP: if self.hid_type == HIDModes.NOOP:
self._hid_helper = AbstractHID self._hid_helper = AbstractHID
elif self.hid_type == HIDModes.USB: elif self.hid_type == HIDModes.USB:
@ -322,7 +324,7 @@ class KMKKeyboard:
self._hid_helper = self._hid_helper(**self._go_args) self._hid_helper = self._hid_helper(**self._go_args)
self._hid_send_enabled = True self._hid_send_enabled = True
def _init_matrix(self): def _init_matrix(self) -> None:
if self.matrix is None: if self.matrix is None:
if debug.enabled: if debug.enabled:
debug('Initialising default matrix scanner.') debug('Initialising default matrix scanner.')
@ -341,9 +343,7 @@ class KMKKeyboard:
except TypeError: except TypeError:
self.matrix = (self.matrix,) self.matrix = (self.matrix,)
return self def before_matrix_scan(self) -> None:
def before_matrix_scan(self):
for module in self.modules: for module in self.modules:
try: try:
module.before_matrix_scan(self) module.before_matrix_scan(self)
@ -358,7 +358,7 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'Error in {ext}.before_matrix_scan: {err}') debug(f'Error in {ext}.before_matrix_scan: {err}')
def after_matrix_scan(self): def after_matrix_scan(self) -> None:
for module in self.modules: for module in self.modules:
try: try:
module.after_matrix_scan(self) module.after_matrix_scan(self)
@ -373,7 +373,7 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'Error in {ext}.after_matrix_scan: {err}') debug(f'Error in {ext}.after_matrix_scan: {err}')
def before_hid_send(self): def before_hid_send(self) -> None:
for module in self.modules: for module in self.modules:
try: try:
module.before_hid_send(self) module.before_hid_send(self)
@ -390,7 +390,7 @@ class KMKKeyboard:
f'Error in {ext}.before_hid_send: {err}', f'Error in {ext}.before_hid_send: {err}',
) )
def after_hid_send(self): def after_hid_send(self) -> None:
for module in self.modules: for module in self.modules:
try: try:
module.after_hid_send(self) module.after_hid_send(self)
@ -405,7 +405,7 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'Error in {ext}.after_hid_send: {err}') debug(f'Error in {ext}.after_hid_send: {err}')
def powersave_enable(self): def powersave_enable(self) -> None:
for module in self.modules: for module in self.modules:
try: try:
module.on_powersave_enable(self) module.on_powersave_enable(self)
@ -420,7 +420,7 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'Error in {ext}.powersave_enable: {err}') debug(f'Error in {ext}.powersave_enable: {err}')
def powersave_disable(self): def powersave_disable(self) -> None:
for module in self.modules: for module in self.modules:
try: try:
module.on_powersave_disable(self) module.on_powersave_disable(self)
@ -434,12 +434,17 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'Error in {ext}.powersave_disable: {err}') debug(f'Error in {ext}.powersave_disable: {err}')
def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs): def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs) -> None:
self._init(hid_type=hid_type, secondary_hid_type=secondary_hid_type, **kwargs) self._init(hid_type=hid_type, secondary_hid_type=secondary_hid_type, **kwargs)
while True: while True:
self._main_loop() self._main_loop()
def _init(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs): def _init(
self,
hid_type: HIDModes = HIDModes.USB,
secondary_hid_type: Optional[HIDModes] = None,
**kwargs,
) -> None:
self._go_args = kwargs self._go_args = kwargs
self.hid_type = hid_type self.hid_type = hid_type
self.secondary_hid_type = secondary_hid_type self.secondary_hid_type = secondary_hid_type
@ -465,7 +470,7 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'init: {self}') debug(f'init: {self}')
def _main_loop(self): def _main_loop(self) -> None:
self.state_changed = False self.state_changed = False
self.sandbox.active_layers = self.active_layers.copy() self.sandbox.active_layers = self.active_layers.copy()