implement global debug interface.
This commit is contained in:
parent
3f826267b4
commit
3f3bd93109
10
kmk/keys.py
10
kmk/keys.py
@ -4,8 +4,7 @@ import kmk.handlers.stock as handlers
|
||||
from kmk.consts import UnicodeMode
|
||||
from kmk.key_validators import key_seq_sleep_validator, unicode_mode_key_validator
|
||||
from kmk.types import UnicodeModeKeyMeta
|
||||
|
||||
DEBUG_OUTPUT = False
|
||||
from kmk.utils import Debug
|
||||
|
||||
FIRST_KMK_INTERNAL_KEY = const(1000)
|
||||
NEXT_AVAILABLE_KEY = 1000
|
||||
@ -19,6 +18,8 @@ ALL_NUMBERS = '1234567890'
|
||||
# since KC.1 isn't valid Python, alias to KC.N1
|
||||
ALL_NUMBER_ALIASES = tuple(f'N{x}' for x in ALL_NUMBERS)
|
||||
|
||||
debug = Debug(__name__)
|
||||
|
||||
|
||||
def maybe_make_key(code, names, *args, **kwargs):
|
||||
def closure(candidate):
|
||||
@ -412,12 +413,11 @@ class KeyAttrDict:
|
||||
maybe_key = func(key)
|
||||
if maybe_key:
|
||||
break
|
||||
|
||||
else:
|
||||
raise ValueError(f'Invalid key: {key}')
|
||||
|
||||
if DEBUG_OUTPUT:
|
||||
print(f'{key}: {maybe_key}')
|
||||
if debug.enabled:
|
||||
debug(f'{key}: {maybe_key}')
|
||||
|
||||
return self.__cache[key]
|
||||
|
||||
|
@ -5,6 +5,9 @@ from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes
|
||||
from kmk.keys import KC
|
||||
from kmk.kmktime import ticks_add, ticks_diff
|
||||
from kmk.scanners.keypad import MatrixScanner
|
||||
from kmk.utils import Debug
|
||||
|
||||
debug = Debug(__name__)
|
||||
|
||||
|
||||
class Sandbox:
|
||||
@ -16,8 +19,6 @@ class Sandbox:
|
||||
class KMKKeyboard:
|
||||
#####
|
||||
# User-configurable
|
||||
debug_enabled = False
|
||||
|
||||
keymap = []
|
||||
coord_mapping = None
|
||||
|
||||
@ -85,10 +86,9 @@ class KMKKeyboard:
|
||||
)
|
||||
|
||||
def _print_debug_cycle(self, init=False):
|
||||
if self.debug_enabled:
|
||||
if init:
|
||||
print(f'KMKInit(release={KMK_RELEASE})')
|
||||
print(self)
|
||||
if debug.enabled:
|
||||
debug(f'coordkeys_pressed={self._coordkeys_pressed}')
|
||||
debug(f'keys_pressed={self.keys_pressed}')
|
||||
|
||||
def _send_hid(self):
|
||||
if self._hid_send_enabled:
|
||||
@ -96,8 +96,8 @@ class KMKKeyboard:
|
||||
try:
|
||||
hid_report.send()
|
||||
except KeyError as e:
|
||||
if self.debug_enabled:
|
||||
print(f'HidNotFound(HIDReportType={e})')
|
||||
if debug.enabled:
|
||||
debug(f'HidNotFound(HIDReportType={e})')
|
||||
self.hid_pending = False
|
||||
|
||||
def _handle_matrix_report(self, update=None):
|
||||
@ -109,8 +109,8 @@ class KMKKeyboard:
|
||||
try:
|
||||
idx = self.coord_mapping.index(int_coord)
|
||||
except ValueError:
|
||||
if self.debug_enabled:
|
||||
print(f'CoordMappingNotFound(ic={int_coord})')
|
||||
if debug.enabled:
|
||||
debug(f'CoordMappingNotFound(ic={int_coord})')
|
||||
|
||||
return None
|
||||
|
||||
@ -119,8 +119,8 @@ class KMKKeyboard:
|
||||
layer_key = self.keymap[layer][idx]
|
||||
except IndexError:
|
||||
layer_key = None
|
||||
if self.debug_enabled:
|
||||
print(f'KeymapIndexError(idx={idx}, layer={layer})')
|
||||
if debug.enabled:
|
||||
debug(f'KeymapIndexError(idx={idx}, layer={layer})')
|
||||
|
||||
if not layer_key or layer_key == KC.TRNS:
|
||||
continue
|
||||
@ -130,30 +130,38 @@ class KMKKeyboard:
|
||||
def _on_matrix_changed(self, kevent):
|
||||
int_coord = kevent.key_number
|
||||
is_pressed = kevent.pressed
|
||||
if self.debug_enabled:
|
||||
print(f'\nMatrixChange(ic={int_coord}, pressed={is_pressed})')
|
||||
if debug.enabled:
|
||||
debug(f'MatrixChange(ic={int_coord}, pressed={is_pressed})')
|
||||
|
||||
key = None
|
||||
if not is_pressed:
|
||||
try:
|
||||
key = self._coordkeys_pressed[int_coord]
|
||||
except KeyError:
|
||||
if self.debug_enabled:
|
||||
print(f'KeyNotPressed(ic={int_coord})')
|
||||
if debug.enabled:
|
||||
debug(f'KeyNotPressed(ic={int_coord})')
|
||||
|
||||
if key is None:
|
||||
key = self._find_key_in_map(int_coord)
|
||||
|
||||
if key is None:
|
||||
if self.debug_enabled:
|
||||
print(f'MatrixUndefinedCoordinate(ic={int_coord})')
|
||||
if debug.enabled:
|
||||
debug(f'MatrixUndefinedCoordinate(ic={int_coord})')
|
||||
return self
|
||||
|
||||
if self.debug_enabled:
|
||||
print(f'KeyResolution(key={key})')
|
||||
if debug.enabled:
|
||||
debug(f'KeyResolution(key={key})')
|
||||
|
||||
self.pre_process_key(key, is_pressed, int_coord)
|
||||
|
||||
@property
|
||||
def debug_enabled(self):
|
||||
return debug.enabled
|
||||
|
||||
@debug_enabled.setter
|
||||
def debug_enabled(self, enabled):
|
||||
debug.enabled = enabled
|
||||
|
||||
def pre_process_key(self, key, is_pressed, int_coord=None):
|
||||
for module in self.modules:
|
||||
try:
|
||||
@ -161,8 +169,8 @@ class KMKKeyboard:
|
||||
if key is None:
|
||||
break
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print('Failed to run process_key function in module: ', err, module)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.process_key: {err}')
|
||||
|
||||
if int_coord is not None:
|
||||
if is_pressed:
|
||||
@ -171,8 +179,8 @@ class KMKKeyboard:
|
||||
try:
|
||||
del self._coordkeys_pressed[int_coord]
|
||||
except KeyError:
|
||||
if self.debug_enabled:
|
||||
print(f'ReleaseKeyError(ic={int_coord})')
|
||||
if debug.enabled:
|
||||
debug(f'ReleaseKeyError(ic={int_coord})')
|
||||
|
||||
if key:
|
||||
self.process_key(key, is_pressed, int_coord)
|
||||
@ -224,8 +232,8 @@ class KMKKeyboard:
|
||||
try:
|
||||
self._timeouts[timeout_key[0]][timeout_key[1]] = None
|
||||
except (KeyError, IndexError):
|
||||
if self.debug_enabled:
|
||||
print(f'no such timeout: {timeout_key}')
|
||||
if debug.enabled:
|
||||
debug(f'no such timeout: {timeout_key}')
|
||||
|
||||
def _process_timeouts(self):
|
||||
if not self._timeouts:
|
||||
@ -242,6 +250,9 @@ class KMKKeyboard:
|
||||
if ticks_diff(k, current_time) <= 0:
|
||||
timeout_keys.append(k)
|
||||
|
||||
if timeout_keys and debug.enabled:
|
||||
debug('processing timeouts')
|
||||
|
||||
for k in sorted(timeout_keys):
|
||||
for callback in self._timeouts[k]:
|
||||
if callback:
|
||||
@ -301,16 +312,13 @@ class KMKKeyboard:
|
||||
|
||||
def _init_matrix(self):
|
||||
if self.matrix is None:
|
||||
if self.debug_enabled:
|
||||
print('Initialising default matrix scanner.')
|
||||
if debug.enabled:
|
||||
debug('Initialising default matrix scanner.')
|
||||
self.matrix = MatrixScanner(
|
||||
column_pins=self.col_pins,
|
||||
row_pins=self.row_pins,
|
||||
columns_to_anodes=self.diode_orientation,
|
||||
)
|
||||
else:
|
||||
if self.debug_enabled:
|
||||
print('Matrix scanner already set, not overwriting.')
|
||||
|
||||
try:
|
||||
self.matrix = tuple(iter(self.matrix))
|
||||
@ -328,68 +336,46 @@ class KMKKeyboard:
|
||||
try:
|
||||
module.before_matrix_scan(self)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run before matrix scan function in module: ',
|
||||
err,
|
||||
module,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.before_matrix_scan: {err}')
|
||||
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.before_matrix_scan(self.sandbox)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run before matrix scan function in extension: ',
|
||||
err,
|
||||
ext,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {ext}.before_matrix_scan: {err}')
|
||||
|
||||
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 after matrix scan function in module: ',
|
||||
err,
|
||||
module,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.after_matrix_scan: {err}')
|
||||
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.after_matrix_scan(self.sandbox)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run after matrix scan function in extension: ',
|
||||
err,
|
||||
ext,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {ext}.after_matrix_scan: {err}')
|
||||
|
||||
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 before hid send function in module: ',
|
||||
err,
|
||||
module,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.before_hid_send: {err}')
|
||||
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.before_hid_send(self.sandbox)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run before hid send function in extension: ',
|
||||
err,
|
||||
ext,
|
||||
if debug.enabled:
|
||||
debug(
|
||||
f'Error in {ext}.before_hid_send: {err}',
|
||||
)
|
||||
|
||||
def after_hid_send(self):
|
||||
@ -397,64 +383,44 @@ class KMKKeyboard:
|
||||
try:
|
||||
module.after_hid_send(self)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run after hid send function in module: ', err, module
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.after_hid_send: {err}')
|
||||
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.after_hid_send(self.sandbox)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run after hid send function in extension: ', err, ext
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {ext}.after_hid_send: {err}')
|
||||
|
||||
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 on powersave enable function in module: ',
|
||||
err,
|
||||
module,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.on_powersave: {err}')
|
||||
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.on_powersave_enable(self.sandbox)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run on powersave enable function in extension: ',
|
||||
err,
|
||||
ext,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {ext}.powersave_enable: {err}')
|
||||
|
||||
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 on powersave disable function in module: ',
|
||||
err,
|
||||
module,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {module}.powersave_disable: {err}')
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.on_powersave_disable(self.sandbox)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print(
|
||||
'Failed to run on powersave disable function in extension: ',
|
||||
err,
|
||||
ext,
|
||||
)
|
||||
if debug.enabled:
|
||||
debug(f'Error in {ext}.powersave_disable: {err}')
|
||||
|
||||
def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs):
|
||||
self._init(hid_type=hid_type, secondary_hid_type=secondary_hid_type, **kwargs)
|
||||
@ -475,17 +441,18 @@ class KMKKeyboard:
|
||||
try:
|
||||
module.during_bootup(self)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print('Failed to load module', err, module)
|
||||
print()
|
||||
if debug.enabled:
|
||||
debug(f'Failed to load module {module}: {err}')
|
||||
for ext in self.extensions:
|
||||
try:
|
||||
ext.during_bootup(self)
|
||||
except Exception as err:
|
||||
if self.debug_enabled:
|
||||
print('Failed to load extension', err, ext)
|
||||
if debug.enabled:
|
||||
debug(f'Failed to load extensions {module}: {err}')
|
||||
|
||||
self._print_debug_cycle(init=True)
|
||||
if debug.enabled:
|
||||
debug(f'KMKInit(release={KMK_RELEASE})')
|
||||
debug(self)
|
||||
|
||||
def _main_loop(self):
|
||||
self.state_changed = False
|
||||
@ -523,10 +490,9 @@ class KMKKeyboard:
|
||||
self._process_timeouts()
|
||||
self._new_timeouts_len = len(self._timeouts)
|
||||
|
||||
if self._old_timeouts_len != self._new_timeouts_len:
|
||||
if self.hid_pending:
|
||||
self._send_hid()
|
||||
self.state_changed = True
|
||||
if self.hid_pending:
|
||||
self._send_hid()
|
||||
|
||||
self.after_hid_send()
|
||||
|
||||
|
@ -3,6 +3,9 @@ from kmk.key_validators import layer_key_validator
|
||||
from kmk.keys import KC, make_argumented_key
|
||||
from kmk.modules.holdtap import ActivationType, HoldTap
|
||||
from kmk.types import HoldTapKeyMeta
|
||||
from kmk.utils import Debug
|
||||
|
||||
debug = Debug(__name__)
|
||||
|
||||
|
||||
def layer_key_validator_lt(layer, kc, prefer_hold=False, **kwargs):
|
||||
@ -88,12 +91,14 @@ class Layers(HoldTap):
|
||||
Switches the default layer
|
||||
'''
|
||||
keyboard.active_layers[-1] = key.meta.layer
|
||||
self._print_debug(keyboard)
|
||||
|
||||
def _mo_pressed(self, key, keyboard, *args, **kwargs):
|
||||
'''
|
||||
Momentarily activates layer, switches off when you let go
|
||||
'''
|
||||
keyboard.active_layers.insert(0, key.meta.layer)
|
||||
self._print_debug(keyboard)
|
||||
|
||||
@staticmethod
|
||||
def _mo_released(key, keyboard, *args, **kwargs):
|
||||
@ -110,6 +115,7 @@ class Layers(HoldTap):
|
||||
del keyboard.active_layers[del_idx]
|
||||
except ValueError:
|
||||
pass
|
||||
__class__._print_debug(__class__, keyboard)
|
||||
|
||||
def _lm_pressed(self, key, keyboard, *args, **kwargs):
|
||||
'''
|
||||
@ -145,3 +151,8 @@ class Layers(HoldTap):
|
||||
'''
|
||||
keyboard.active_layers.clear()
|
||||
keyboard.active_layers.insert(0, key.meta.layer)
|
||||
|
||||
def _print_debug(self, keyboard):
|
||||
# debug(f'__getitem__ {key}')
|
||||
if debug.enabled:
|
||||
debug(f'active_layers={keyboard.active_layers}')
|
||||
|
28
kmk/utils.py
28
kmk/utils.py
@ -1,2 +1,30 @@
|
||||
from supervisor import ticks_ms
|
||||
|
||||
|
||||
def clamp(x, bottom=0, top=100):
|
||||
return min(max(bottom, x), top)
|
||||
|
||||
|
||||
_debug_enabled = False
|
||||
|
||||
|
||||
class Debug:
|
||||
'''default usage:
|
||||
debug = Debug(__name__)
|
||||
'''
|
||||
|
||||
def __init__(self, name=__name__):
|
||||
self.name = name
|
||||
|
||||
def __call__(self, message):
|
||||
print(f'{ticks_ms()} {self.name}: {message}')
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
global _debug_enabled
|
||||
return _debug_enabled
|
||||
|
||||
@enabled.setter
|
||||
def enabled(self, enabled):
|
||||
global _debug_enabled
|
||||
_debug_enabled = enabled
|
||||
|
Loading…
Reference in New Issue
Block a user