2021-09-13 17:18:01 +02:00
|
|
|
from supervisor import ticks_ms
|
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
from kmk.consts import KMK_RELEASE, UnicodeMode
|
|
|
|
from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes
|
2019-07-25 09:03:35 +02:00
|
|
|
from kmk.keys import KC
|
2019-07-29 02:09:58 +02:00
|
|
|
from kmk.matrix import MatrixScanner, intify_coordinate
|
|
|
|
from kmk.types import TapDanceKeyMeta
|
2019-07-25 08:43:00 +02:00
|
|
|
|
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
|
2018-10-16 13:04:39 +02:00
|
|
|
debug_enabled = False
|
|
|
|
|
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
|
2019-05-13 06:30:09 +02:00
|
|
|
matrix_scanner = MatrixScanner
|
2019-07-13 02:11:36 +02:00
|
|
|
uart_buffer = []
|
2018-10-16 13:04:39 +02:00
|
|
|
|
2018-12-05 02:03:13 +01:00
|
|
|
unicode_mode = UnicodeMode.NOOP
|
2018-10-16 13:04:39 +02:00
|
|
|
tap_time = 300
|
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
|
|
|
|
hid_pending = False
|
2021-09-19 04:24:37 +02:00
|
|
|
current_key = None
|
2020-10-21 21:19:42 +02:00
|
|
|
matrix_update = None
|
|
|
|
secondary_matrix_update = None
|
|
|
|
_matrix_modify = None
|
|
|
|
state_changed = False
|
|
|
|
_old_timeouts_len = None
|
|
|
|
_new_timeouts_len = None
|
|
|
|
_trigger_powersave_enable = False
|
|
|
|
_trigger_powersave_disable = False
|
|
|
|
i2c_deinit_count = 0
|
2021-09-17 15:47:41 +02:00
|
|
|
_go_args = None
|
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 = {}
|
|
|
|
_tapping = False
|
|
|
|
_tap_dance_counts = {}
|
|
|
|
_tap_side_effects = {}
|
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....
|
2019-07-13 00:16:33 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return (
|
2019-07-25 10:30:55 +02:00
|
|
|
'KMKKeyboard('
|
2019-07-13 00:16:33 +02:00
|
|
|
'debug_enabled={} '
|
|
|
|
'diode_orientation={} '
|
|
|
|
'matrix_scanner={} '
|
|
|
|
'unicode_mode={} '
|
|
|
|
'tap_time={} '
|
2020-10-21 21:19:42 +02:00
|
|
|
'_hid_helper={} '
|
2019-07-29 02:09:58 +02:00
|
|
|
'keys_pressed={} '
|
2020-10-21 21:19:42 +02:00
|
|
|
'coordkeys_pressed={} '
|
2019-07-29 02:09:58 +02:00
|
|
|
'hid_pending={} '
|
|
|
|
'active_layers={} '
|
|
|
|
'timeouts={} '
|
|
|
|
'tapping={} '
|
|
|
|
'tap_dance_counts={} '
|
|
|
|
'tap_side_effects={}'
|
2019-07-13 00:16:33 +02:00
|
|
|
')'
|
|
|
|
).format(
|
|
|
|
self.debug_enabled,
|
|
|
|
self.diode_orientation,
|
|
|
|
self.matrix_scanner,
|
|
|
|
self.unicode_mode,
|
|
|
|
self.tap_time,
|
2020-10-21 21:19:42 +02:00
|
|
|
self._hid_helper,
|
2019-07-29 02:09:58 +02:00
|
|
|
# internal state
|
2020-10-21 21:19:42 +02:00
|
|
|
self.keys_pressed,
|
|
|
|
self._coordkeys_pressed,
|
|
|
|
self.hid_pending,
|
|
|
|
self.active_layers,
|
2019-07-29 02:09:58 +02:00
|
|
|
self._timeouts,
|
|
|
|
self._tapping,
|
|
|
|
self._tap_dance_counts,
|
|
|
|
self._tap_side_effects,
|
2019-07-13 00:16:33 +02:00
|
|
|
)
|
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
def _print_debug_cycle(self, init=False):
|
|
|
|
if self.debug_enabled:
|
|
|
|
if init:
|
|
|
|
print('KMKInit(release={})'.format(KMK_RELEASE))
|
|
|
|
print(self)
|
2018-10-17 07:50:41 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
def _send_hid(self):
|
2020-10-21 21:19:42 +02:00
|
|
|
self._hid_helper.create_report(self.keys_pressed).send()
|
|
|
|
self.hid_pending = False
|
2018-10-17 07:50:41 +02:00
|
|
|
|
2018-11-06 06:14:20 +01:00
|
|
|
def _handle_matrix_report(self, update=None):
|
2018-10-19 11:22:22 +02:00
|
|
|
if update is not None:
|
2019-07-29 02:09:58 +02:00
|
|
|
self._on_matrix_changed(update[0], update[1], update[2])
|
|
|
|
self.state_changed = True
|
2018-10-19 11:22:22 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def _find_key_in_map(self, int_coord, row, col):
|
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:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print(
|
2020-10-21 21:19:42 +02:00
|
|
|
'CoordMappingNotFound(ic={}, row={}, col={})'.format(
|
|
|
|
int_coord, row, col
|
|
|
|
)
|
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-09-19 04:24:37 +02:00
|
|
|
layer_key = self.keymap[layer][idx]
|
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
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
if self.debug_enabled:
|
2021-09-19 04:24:37 +02:00
|
|
|
print('KeyResolution(key={})'.format(layer_key))
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2021-09-19 04:24:37 +02:00
|
|
|
return layer_key
|
2019-07-29 02:09:58 +02:00
|
|
|
|
|
|
|
def _on_matrix_changed(self, row, col, is_pressed):
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('MatrixChange(col={} row={} pressed={})'.format(col, row, is_pressed))
|
|
|
|
|
|
|
|
int_coord = intify_coordinate(row, col)
|
2021-09-17 15:59:57 +02:00
|
|
|
if not is_pressed:
|
2021-09-19 04:24:37 +02:00
|
|
|
self.current_key = self._coordkeys_pressed[int_coord]
|
2021-09-17 15:59:57 +02:00
|
|
|
if self.debug_enabled:
|
2021-09-19 04:24:37 +02:00
|
|
|
print('PressedKeyResolution(key={})'.format(self.current_key))
|
2021-09-17 15:59:57 +02:00
|
|
|
|
2021-09-19 04:24:37 +02:00
|
|
|
if self.current_key is None:
|
|
|
|
self.current_key = self._find_key_in_map(int_coord, row, col)
|
2021-09-17 15:59:57 +02:00
|
|
|
|
|
|
|
if is_pressed:
|
2021-09-19 04:24:37 +02:00
|
|
|
self._coordkeys_pressed[int_coord] = self.current_key
|
2021-09-17 15:59:57 +02:00
|
|
|
else:
|
|
|
|
self._coordkeys_pressed[int_coord] = None
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2021-09-19 04:24:37 +02:00
|
|
|
if self.current_key is None:
|
2019-07-29 02:09:58 +02:00
|
|
|
print('MatrixUndefinedCoordinate(col={} row={})'.format(col, row))
|
|
|
|
return self
|
|
|
|
|
2021-09-19 04:24:37 +02:00
|
|
|
return self.process_key(self.current_key, is_pressed, int_coord, (row, col))
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def process_key(self, key, is_pressed, coord_int=None, coord_raw=None):
|
2019-07-29 02:09:58 +02:00
|
|
|
if self._tapping and not isinstance(key.meta, TapDanceKeyMeta):
|
|
|
|
self._process_tap_dance(key, is_pressed)
|
|
|
|
else:
|
|
|
|
if is_pressed:
|
2020-10-21 21:19:42 +02:00
|
|
|
key.on_press(self, coord_int, coord_raw)
|
2019-07-29 02:09:58 +02:00
|
|
|
else:
|
2020-10-21 21:19:42 +02:00
|
|
|
key.on_release(self, coord_int, coord_raw)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
|
|
|
return self
|
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def remove_key(self, keycode):
|
|
|
|
self.keys_pressed.discard(keycode)
|
|
|
|
return self.process_key(keycode, False)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def add_key(self, keycode):
|
|
|
|
self.keys_pressed.add(keycode)
|
|
|
|
return self.process_key(keycode, True)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def tap_key(self, keycode):
|
|
|
|
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
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _process_tap_dance(self, changed_key, is_pressed):
|
|
|
|
if is_pressed:
|
|
|
|
if not isinstance(changed_key.meta, TapDanceKeyMeta):
|
|
|
|
# If we get here, changed_key is not a TapDanceKey and thus
|
|
|
|
# the user kept typing elsewhere (presumably). End ALL of the
|
|
|
|
# currently outstanding tap dance runs.
|
|
|
|
for k, v in self._tap_dance_counts.items():
|
|
|
|
if v:
|
|
|
|
self._end_tap_dance(k)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
if (
|
|
|
|
changed_key not in self._tap_dance_counts
|
|
|
|
or not self._tap_dance_counts[changed_key]
|
|
|
|
):
|
|
|
|
self._tap_dance_counts[changed_key] = 1
|
2020-10-21 21:19:42 +02:00
|
|
|
self.set_timeout(
|
2019-07-29 02:09:58 +02:00
|
|
|
self.tap_time, lambda: self._end_tap_dance(changed_key)
|
|
|
|
)
|
|
|
|
self._tapping = True
|
|
|
|
else:
|
|
|
|
self._tap_dance_counts[changed_key] += 1
|
|
|
|
|
|
|
|
if changed_key not in self._tap_side_effects:
|
|
|
|
self._tap_side_effects[changed_key] = None
|
|
|
|
else:
|
|
|
|
has_side_effects = self._tap_side_effects[changed_key] is not None
|
|
|
|
hit_max_defined_taps = self._tap_dance_counts[changed_key] == len(
|
|
|
|
changed_key.codes
|
|
|
|
)
|
|
|
|
|
|
|
|
if has_side_effects or hit_max_defined_taps:
|
|
|
|
self._end_tap_dance(changed_key)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _end_tap_dance(self, td_key):
|
|
|
|
v = self._tap_dance_counts[td_key] - 1
|
|
|
|
|
|
|
|
if v >= 0:
|
2020-10-21 21:19:42 +02:00
|
|
|
if td_key in self.keys_pressed:
|
2019-07-29 02:09:58 +02:00
|
|
|
key_to_press = td_key.codes[v]
|
2020-10-21 21:19:42 +02:00
|
|
|
self.add_key(key_to_press)
|
2019-07-29 02:09:58 +02:00
|
|
|
self._tap_side_effects[td_key] = key_to_press
|
2020-10-21 21:19:42 +02:00
|
|
|
self.hid_pending = True
|
2019-07-29 02:09:58 +02:00
|
|
|
else:
|
|
|
|
if self._tap_side_effects[td_key]:
|
2020-10-21 21:19:42 +02:00
|
|
|
self.remove_key(self._tap_side_effects[td_key])
|
2019-07-29 02:09:58 +02:00
|
|
|
self._tap_side_effects[td_key] = None
|
2020-10-21 21:19:42 +02:00
|
|
|
self.hid_pending = True
|
2019-07-29 02:09:58 +02:00
|
|
|
self._cleanup_tap_dance(td_key)
|
|
|
|
else:
|
2020-10-21 21:19:42 +02:00
|
|
|
self.tap_key(td_key.codes[v])
|
2019-07-29 02:09:58 +02:00
|
|
|
self._cleanup_tap_dance(td_key)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _cleanup_tap_dance(self, td_key):
|
|
|
|
self._tap_dance_counts[td_key] = 0
|
|
|
|
self._tapping = any(count > 0 for count in self._tap_dance_counts.values())
|
|
|
|
return self
|
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def set_timeout(self, after_ticks, callback):
|
2019-07-29 02:09:58 +02:00
|
|
|
if after_ticks is False:
|
|
|
|
# We allow passing False as an implicit "run this on the next process timeouts cycle"
|
|
|
|
timeout_key = ticks_ms()
|
2018-10-20 04:01:39 +02:00
|
|
|
else:
|
2019-07-29 02:09:58 +02:00
|
|
|
timeout_key = ticks_ms() + after_ticks
|
2018-10-20 04:01:39 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
while timeout_key in self._timeouts:
|
|
|
|
timeout_key += 1
|
|
|
|
|
|
|
|
self._timeouts[timeout_key] = callback
|
|
|
|
return timeout_key
|
|
|
|
|
|
|
|
def _cancel_timeout(self, timeout_key):
|
|
|
|
if timeout_key in self._timeouts:
|
|
|
|
del self._timeouts[timeout_key]
|
|
|
|
|
|
|
|
def _process_timeouts(self):
|
|
|
|
if not self._timeouts:
|
|
|
|
return self
|
|
|
|
|
|
|
|
current_time = ticks_ms()
|
|
|
|
|
|
|
|
# cast this to a tuple to ensure that if a callback itself sets
|
|
|
|
# timeouts, we do not handle them on the current cycle
|
|
|
|
timeouts = tuple(self._timeouts.items())
|
|
|
|
|
|
|
|
for k, v in timeouts:
|
|
|
|
if k <= current_time:
|
|
|
|
v()
|
|
|
|
del self._timeouts[k]
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _init_sanity_check(self):
|
|
|
|
'''
|
|
|
|
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'
|
|
|
|
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'
|
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'
|
2018-10-16 13:04:39 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
def _init_coord_mapping(self):
|
|
|
|
'''
|
|
|
|
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:
|
|
|
|
self.coord_mapping = []
|
|
|
|
|
|
|
|
rows_to_calc = len(self.row_pins)
|
|
|
|
cols_to_calc = len(self.col_pins)
|
|
|
|
|
|
|
|
for ridx in range(rows_to_calc):
|
|
|
|
for cidx in range(cols_to_calc):
|
2019-07-29 02:09:58 +02:00
|
|
|
self.coord_mapping.append(intify_coordinate(ridx, cidx))
|
2019-07-17 08:30:14 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
def _init_hid(self):
|
|
|
|
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)
|
2019-05-12 23:13:41 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
def _init_matrix(self):
|
|
|
|
self.matrix = MatrixScanner(
|
2018-10-17 07:30:33 +02:00
|
|
|
cols=self.col_pins,
|
|
|
|
rows=self.row_pins,
|
|
|
|
diode_orientation=self.diode_orientation,
|
|
|
|
rollover_cols_every_rows=getattr(self, 'rollover_cols_every_rows', None),
|
|
|
|
)
|
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
return self
|
2018-12-29 15:58:08 +01:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def before_matrix_scan(self):
|
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.before_matrix_scan(self)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run pre matrix function in module: ', err, module)
|
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.before_matrix_scan(self.sandbox)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run pre matrix function in extension: ', err, ext)
|
2018-12-29 15:58:08 +01:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
def after_matrix_scan(self):
|
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.after_matrix_scan(self)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post matrix function in module: ', err, module)
|
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.after_matrix_scan(self.sandbox)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post matrix function in extension: ', err, ext)
|
|
|
|
|
|
|
|
def before_hid_send(self):
|
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.before_hid_send(self)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run pre hid function in module: ', err, module)
|
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.before_hid_send(self.sandbox)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run pre hid function in extension: ', err, ext)
|
|
|
|
|
|
|
|
def after_hid_send(self):
|
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.after_hid_send(self)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post hid function in module: ', err, module)
|
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.after_hid_send(self.sandbox)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post hid function in extension: ', err, ext)
|
|
|
|
|
|
|
|
def powersave_enable(self):
|
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.on_powersave_enable(self)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post hid function in module: ', err, module)
|
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:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post hid function in extension: ', err, ext)
|
|
|
|
|
|
|
|
def powersave_disable(self):
|
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.on_powersave_disable(self)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post hid function in module: ', err, module)
|
|
|
|
for ext in self.extensions:
|
|
|
|
try:
|
|
|
|
ext.on_powersave_disable(self.sandbox)
|
|
|
|
except Exception as err:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to run post hid function in extension: ', err, ext)
|
|
|
|
|
|
|
|
def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs):
|
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_coord_mapping()
|
|
|
|
self._init_hid()
|
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
for module in self.modules:
|
|
|
|
try:
|
|
|
|
module.during_bootup(self)
|
|
|
|
except Exception:
|
|
|
|
if self.debug_enabled:
|
|
|
|
print('Failed to load module', module)
|
|
|
|
for ext in self.extensions:
|
2019-07-29 02:09:58 +02:00
|
|
|
try:
|
|
|
|
ext.during_bootup(self)
|
|
|
|
except Exception:
|
2020-10-21 21:19:42 +02:00
|
|
|
if self.debug_enabled:
|
2021-09-17 16:06:32 +02:00
|
|
|
print('Failed to load extension', ext)
|
2019-07-29 02:09:58 +02:00
|
|
|
|
|
|
|
self._init_matrix()
|
2018-10-19 08:24:19 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
self._print_debug_cycle(init=True)
|
|
|
|
|
|
|
|
while True:
|
2021-09-19 04:24:37 +02:00
|
|
|
self.current_key = None
|
2019-07-29 02:09:58 +02:00
|
|
|
self.state_changed = False
|
2020-10-21 21:19:42 +02:00
|
|
|
self.sandbox.active_layers = self.active_layers.copy()
|
|
|
|
|
|
|
|
self.before_matrix_scan()
|
2019-07-29 02:09:58 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
self.matrix_update = (
|
|
|
|
self.sandbox.matrix_update
|
|
|
|
) = self.matrix.scan_for_changes()
|
|
|
|
self.sandbox.secondary_matrix_update = self.secondary_matrix_update
|
|
|
|
|
|
|
|
self.after_matrix_scan()
|
|
|
|
|
|
|
|
self._handle_matrix_report(self.secondary_matrix_update)
|
|
|
|
self.secondary_matrix_update = None
|
|
|
|
self._handle_matrix_report(self.matrix_update)
|
|
|
|
self.matrix_update = None
|
|
|
|
|
|
|
|
self.before_hid_send()
|
|
|
|
|
|
|
|
if self.hid_pending:
|
2018-10-21 01:55:17 +02:00
|
|
|
self._send_hid()
|
2018-10-16 13:04:39 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
self._old_timeouts_len = len(self._timeouts)
|
2019-07-29 02:09:58 +02:00
|
|
|
self._process_timeouts()
|
2020-10-21 21:19:42 +02:00
|
|
|
self._new_timeouts_len = len(self._timeouts)
|
2018-10-21 01:55:17 +02:00
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
if self._old_timeouts_len != self._new_timeouts_len:
|
2019-07-29 02:09:58 +02:00
|
|
|
self.state_changed = True
|
2020-10-21 21:19:42 +02:00
|
|
|
if self.hid_pending:
|
2018-12-30 01:52:06 +01:00
|
|
|
self._send_hid()
|
|
|
|
|
2020-10-21 21:19:42 +02:00
|
|
|
self.after_hid_send()
|
|
|
|
|
|
|
|
if self._trigger_powersave_enable:
|
|
|
|
self.powersave_enable()
|
|
|
|
|
|
|
|
if self._trigger_powersave_disable:
|
|
|
|
self.powersave_disable()
|
2019-07-13 02:11:36 +02:00
|
|
|
|
2019-07-29 02:09:58 +02:00
|
|
|
if self.state_changed:
|
|
|
|
self._print_debug_cycle()
|