From 7ae2d18e4523ec982123f95a6f3a75aa5f2e6cfe Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 21 Sep 2018 17:22:03 -0700 Subject: [PATCH] Very broken, but some work done probably --- boards/kdb424/handwire_planck_pyboard.py | 3 +- kmk/common/abstract/matrix_scanner.py | 2 +- kmk/common/event_defs.py | 9 ++++-- kmk/common/internal_keycodes.py | 37 ++++++++++++++++++++++-- kmk/common/internal_state.py | 3 ++ kmk/common/keycodes.py | 9 ++++++ kmk/firmware.py | 6 ++-- kmk/micropython/matrix.py | 3 +- kmk/micropython/pyb_hid.py | 4 +-- 9 files changed, 63 insertions(+), 13 deletions(-) diff --git a/boards/kdb424/handwire_planck_pyboard.py b/boards/kdb424/handwire_planck_pyboard.py index 9c88ed9..ae5a55a 100644 --- a/boards/kdb424/handwire_planck_pyboard.py +++ b/boards/kdb424/handwire_planck_pyboard.py @@ -21,7 +21,7 @@ def main(): KC.BACKSPACE], [KC.TAB, KC.A, KC.O, KC.E, KC.U, KC.I, KC.D, KC.H, KC.T, KC.N, KC.S, KC.ENTER], [KC.SHIFT, KC.SEMICOLON, KC.Q, KC.J, KC.K, KC.X, KC.B, KC.M, KC.W, KC.V, KC.Z, KC.SLASH], - [KC.CTRL, KC.GUI, KC.ALT, KC.RESET, KC.A, KC.SPACE, KC.SPACE, KC.A, KC.LEFT, KC.DOWN, + [KC.CTRL, KC.GUI, KC.ALT, KC.RESET, KC.DF, KC.SPACE, KC.SPACE, KC.A, KC.LEFT, KC.DOWN, KC.UP, KC.RIGHT], ] @@ -31,6 +31,7 @@ def main(): col_pins=cols, diode_orientation=diode_orientation, hid=HIDHelper, + active_layers=[0], log_level=DEBUG, ) diff --git a/kmk/common/abstract/matrix_scanner.py b/kmk/common/abstract/matrix_scanner.py index 4ef71dc..28561c9 100644 --- a/kmk/common/abstract/matrix_scanner.py +++ b/kmk/common/abstract/matrix_scanner.py @@ -2,7 +2,7 @@ from kmk.common.consts import DiodeOrientation class AbstractMatrixScanner(): - def __init__(self, cols, rows, diode_orientation=DiodeOrientation.COLUMNS): + def __init__(self, cols, rows, active_layers, diode_orientation=DiodeOrientation.COLUMNS): raise NotImplementedError('Abstract implementation') def _normalize_matrix(self, matrix): diff --git a/kmk/common/event_defs.py b/kmk/common/event_defs.py index 04a1f7a..71b496f 100644 --- a/kmk/common/event_defs.py +++ b/kmk/common/event_defs.py @@ -5,29 +5,32 @@ KEY_DOWN_EVENT = const(2) INIT_FIRMWARE_EVENT = const(3) -def init_firmware(keymap, row_pins, col_pins, diode_orientation): +def init_firmware(keymap, row_pins, col_pins, diode_orientation, active_layers): return { 'type': INIT_FIRMWARE_EVENT, 'keymap': keymap, 'row_pins': row_pins, 'col_pins': col_pins, 'diode_orientation': diode_orientation, + 'active_layers': active_layers, } -def key_up_event(keycode, row, col): +def key_up_event(keycode, row, col, active_layers): return { 'type': KEY_UP_EVENT, 'keycode': keycode, 'row': row, 'col': col, + 'active_layers': active_layers, } -def key_down_event(keycode, row, col): +def key_down_event(keycode, row, col, active_layers): return { 'type': KEY_DOWN_EVENT, 'keycode': keycode, 'row': row, 'col': col, + 'active_layers': active_layers, } diff --git a/kmk/common/internal_keycodes.py b/kmk/common/internal_keycodes.py index c390454..4982d3c 100644 --- a/kmk/common/internal_keycodes.py +++ b/kmk/common/internal_keycodes.py @@ -1,7 +1,9 @@ -def process(self, state, key): - self.logger.warning(key) - if key.code == 1000: +def process(self, state, action): + self.logger.warning(action['keycode']) + if action['keycode'].code == 1000: reset(self) + elif action['keycode'].code == 1050: + df(self, "Filler", action) def reset(self): @@ -9,3 +11,32 @@ def reset(self): import machine machine.bootloader() return self + + +def df(self, layer, action): + """Switches the default layer""" + self.logger.warning(action['active_layers']) + + +def mo(layer): + """Momentarily activates layer, switches off when you let go""" + + +def lm(layer, mod): + """As MO(layer) but with mod active""" + + +def lt(layer, kc): + """Momentarily activates layer if held, sends kc if tapped""" + + +def tg(layer): + """Toggles the layer (enables it if no active, and vise versa)""" + + +def to(layer): + """Activates layer and deactivates all other layers""" + + +def tt(layer): + """Momentarily activates layer if held, toggles it if tapped repeatedly""" diff --git a/kmk/common/internal_state.py b/kmk/common/internal_state.py index 8c54e63..4c75f0a 100644 --- a/kmk/common/internal_state.py +++ b/kmk/common/internal_state.py @@ -48,6 +48,7 @@ class InternalState: col_pins = [] matrix = [] diode_orientation = DiodeOrientation.COLUMNS + active_layers = [0] @property def __dict__(self): @@ -58,6 +59,7 @@ class InternalState: 'col_pins': self.col_pins, 'row_pins': self.row_pins, 'diode_orientation': self.diode_orientation, + 'active_layers': self.active_layers, } def __repr__(self): @@ -124,6 +126,7 @@ def kmk_reducer(state=None, action=None, logger=None): row_pins=action['row_pins'], col_pins=action['col_pins'], diode_orientation=action['diode_orientation'], + active_layers=action['active_layers'], matrix=[ [False for c in action['col_pins']] for r in action['row_pins'] diff --git a/kmk/common/keycodes.py b/kmk/common/keycodes.py index 681eeec..2d6ae12 100644 --- a/kmk/common/keycodes.py +++ b/kmk/common/keycodes.py @@ -321,6 +321,15 @@ class Keycodes(KeycodeCategory): KC_LEAD = Keycode(1005, False) KC_LOCK = Keycode(1006, False) + class Layers(KeycodeCategory): + KC_DF = Keycode(1050, False) + KC_MO = Keycode(1051, False) + KC_LM = Keycode(1052, False) + KC_LT = Keycode(1053, False) + KC_TG = Keycode(1054, False) + KC_TO = Keycode(1055, False) + KC_TT = Keycode(1056, False) + ALL_KEYS = KC = AttrDict({ k.replace('KC_', ''): v diff --git a/kmk/firmware.py b/kmk/firmware.py index 6af51ac..32ec2ba 100644 --- a/kmk/firmware.py +++ b/kmk/firmware.py @@ -12,8 +12,8 @@ except ImportError: class Firmware: def __init__( - self, keymap, row_pins, col_pins, diode_orientation, - hid=None, log_level=logging.NOTSET, + self, keymap, row_pins, col_pins, active_layers, + diode_orientation, hid=None, log_level=logging.NOTSET, ): logger = logging.getLogger(__name__) logger.setLevel(log_level) @@ -36,6 +36,7 @@ class Firmware: keymap=keymap, row_pins=row_pins, col_pins=col_pins, + active_layers=active_layers, diode_orientation=diode_orientation, )) @@ -50,6 +51,7 @@ class Firmware: self.matrix = MatrixScanner( state.col_pins, state.row_pins, + state.active_layers, state.diode_orientation, ) diff --git a/kmk/micropython/matrix.py b/kmk/micropython/matrix.py index f775719..584ea97 100644 --- a/kmk/micropython/matrix.py +++ b/kmk/micropython/matrix.py @@ -5,7 +5,7 @@ from kmk.common.consts import DiodeOrientation class MatrixScanner(AbstractMatrixScanner): - def __init__(self, cols, rows, diode_orientation=DiodeOrientation.COLUMNS): + def __init__(self, cols, rows, active_layers, diode_orientation=DiodeOrientation.COLUMNS): # A pin cannot be both a row and column, detect this by combining the # two tuples into a set and validating that the length did not drop # @@ -19,6 +19,7 @@ class MatrixScanner(AbstractMatrixScanner): self.cols = [machine.Pin(pin) for pin in cols] self.rows = [machine.Pin(pin) for pin in rows] self.diode_orientation = diode_orientation + self.active_layers = active_layers if self.diode_orientation == DiodeOrientation.COLUMNS: self.outputs = self.cols diff --git a/kmk/micropython/pyb_hid.py b/kmk/micropython/pyb_hid.py index 9fa0cb5..2f8dfc3 100644 --- a/kmk/micropython/pyb_hid.py +++ b/kmk/micropython/pyb_hid.py @@ -59,8 +59,8 @@ class HIDHelper: self.add_key(action['keycode']) self.send() else: - self.logger.warning('Should be processing') - internal_keycodes.process(self, state, action['keycode']) + self.logger.warning('Triggering KMK keycodes') + internal_keycodes.process(self, state, action) elif action['type'] == KEY_UP_EVENT: # If keycode is 1000 or over, these are internal keys if action['keycode'].code < 1000: