Add support for changing to N layers as needed
This commit is contained in:
		| @@ -17,8 +17,8 @@ def main(): | |||||||
|     diode_orientation = DiodeOrientation.COLUMNS |     diode_orientation = DiodeOrientation.COLUMNS | ||||||
|  |  | ||||||
|     keymap = [ |     keymap = [ | ||||||
|         [KC.DF, KC.H, KC.RESET], |         [KC.DF(1), KC.H, KC.RESET], | ||||||
|         [KC.MO, KC.I, KC.ENTER], |         [KC.MO(2), KC.I, KC.ENTER], | ||||||
|         [KC.CTRL, KC.SPACE, KC.SHIFT], |         [KC.CTRL, KC.SPACE, KC.SHIFT], | ||||||
|     ] |     ] | ||||||
|  |  | ||||||
|   | |||||||
| @@ -13,32 +13,37 @@ def process(state, action, logger=None): | |||||||
|  |  | ||||||
|     logger.warning(action['keycode']) |     logger.warning(action['keycode']) | ||||||
|     if action['keycode'] == Keycodes.KMK.KC_RESET: |     if action['keycode'] == Keycodes.KMK.KC_RESET: | ||||||
|         return reset(logger) |         return reset(state, action, logger=logger) | ||||||
|     elif action['keycode'] == Keycodes.Layers.KC_DF: |     elif action['keycode'].code == Keycodes.Layers._KC_DF: | ||||||
|         return df(state, "Filler", action, logger=logger) |         return df(state, action, logger=logger) | ||||||
|     elif action['keycode'] == Keycodes.Layers.KC_MO: |     elif action['keycode'].code == Keycodes.Layers._KC_MO: | ||||||
|         return mo(state, "Filler", action, logger=logger) |         return mo(state, action, logger=logger) | ||||||
|  |     else: | ||||||
|  |         return state | ||||||
|  |  | ||||||
|  |  | ||||||
| def reset(logger): | def reset(state, action, logger): | ||||||
|     logger.debug('Rebooting to bootloader') |     logger.debug('Rebooting to bootloader') | ||||||
|     import machine |     import machine | ||||||
|     machine.bootloader() |     machine.bootloader() | ||||||
|  |  | ||||||
|  |  | ||||||
| def df(state, layer, action, logger): | def df(state, action, logger): | ||||||
|     """Switches the default layer""" |     """Switches the default layer""" | ||||||
|     state.active_layers = [1] |     state.active_layers[0] = action['keycode'].layer | ||||||
|  |  | ||||||
|     return state |     return state | ||||||
|  |  | ||||||
|  |  | ||||||
| def mo(state, layer, action, logger): | def mo(state, action, logger): | ||||||
|     """Momentarily activates layer, switches off when you let go""" |     """Momentarily activates layer, switches off when you let go""" | ||||||
|     if action['type'] == KEY_UP_EVENT: |     if action['type'] == KEY_UP_EVENT: | ||||||
|         state.active_layers = [0] |         state.active_layers = [ | ||||||
|  |             layer for layer in state.active_layers | ||||||
|  |             if layer != action['keycode'].layer | ||||||
|  |         ] | ||||||
|     elif action['type'] == KEY_DOWN_EVENT: |     elif action['type'] == KEY_DOWN_EVENT: | ||||||
|         state.active_layers = [0, 1] |         state.active_layers.append(action['keycode'].layer) | ||||||
|  |  | ||||||
|     return state |     return state | ||||||
|  |  | ||||||
|   | |||||||
| @@ -50,34 +50,40 @@ class InternalState: | |||||||
|     matrix = [] |     matrix = [] | ||||||
|     diode_orientation = DiodeOrientation.COLUMNS |     diode_orientation = DiodeOrientation.COLUMNS | ||||||
|     active_layers = [0] |     active_layers = [0] | ||||||
|  |     _oldstates = [] | ||||||
|  |  | ||||||
|     @property |     def __init__(self, preserve_intermediate_states=False): | ||||||
|     def __dict__(self): |         self.preserve_intermediate_states = preserve_intermediate_states | ||||||
|         return { |  | ||||||
|  |     def to_dict(self, verbose=False): | ||||||
|  |         ret = { | ||||||
|             'keys_pressed': self.keys_pressed, |             'keys_pressed': self.keys_pressed, | ||||||
|             'modifiers_pressed': self.modifiers_pressed, |             'modifiers_pressed': self.modifiers_pressed, | ||||||
|             'keymap': self.keymap, |  | ||||||
|             'col_pins': self.col_pins, |  | ||||||
|             'row_pins': self.row_pins, |  | ||||||
|             'diode_orientation': self.diode_orientation, |  | ||||||
|             'active_layers': self.active_layers, |             'active_layers': self.active_layers, | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         if verbose: | ||||||
|  |             ret.update({ | ||||||
|  |                 'keymap': self.keymap, | ||||||
|  |                 'matrix': self.matrix, | ||||||
|  |                 'col_pins': self.col_pins, | ||||||
|  |                 'row_pins': self.row_pins, | ||||||
|  |                 'diode_orientation': self.diode_orientation, | ||||||
|  |             }) | ||||||
|  |  | ||||||
|  |         return ret | ||||||
|  |  | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return 'InternalState({})'.format(self.__dict__) |         return 'InternalState({})'.format(self.to_dict()) | ||||||
|  |  | ||||||
|     def copy(self, **kwargs): |     def update(self, **kwargs): | ||||||
|         new_state = InternalState() |         if self.preserve_intermediate_states: | ||||||
|  |             self._oldstates.append(repr(self.to_dict(verbose=True))) | ||||||
|         for k, v in self.__dict__.items(): |  | ||||||
|             if hasattr(new_state, k): |  | ||||||
|                 setattr(new_state, k, v) |  | ||||||
|  |  | ||||||
|         for k, v in kwargs.items(): |         for k, v in kwargs.items(): | ||||||
|             if hasattr(new_state, k): |             setattr(self, k, v) | ||||||
|                 setattr(new_state, k, v) |  | ||||||
|  |  | ||||||
|         return new_state |         return self | ||||||
|  |  | ||||||
|  |  | ||||||
| def kmk_reducer(state=None, action=None, logger=None): | def kmk_reducer(state=None, action=None, logger=None): | ||||||
| @@ -94,7 +100,7 @@ def kmk_reducer(state=None, action=None, logger=None): | |||||||
|         return state |         return state | ||||||
|  |  | ||||||
|     if action['type'] == KEY_UP_EVENT: |     if action['type'] == KEY_UP_EVENT: | ||||||
|         newstate = state.copy( |         newstate = state.update( | ||||||
|             keys_pressed=frozenset( |             keys_pressed=frozenset( | ||||||
|                 key for key in state.keys_pressed if key != action['keycode'] |                 key for key in state.keys_pressed if key != action['keycode'] | ||||||
|             ), |             ), | ||||||
| @@ -113,7 +119,7 @@ def kmk_reducer(state=None, action=None, logger=None): | |||||||
|         return newstate |         return newstate | ||||||
|  |  | ||||||
|     if action['type'] == KEY_DOWN_EVENT: |     if action['type'] == KEY_DOWN_EVENT: | ||||||
|         newstate = state.copy( |         newstate = state.update( | ||||||
|             keys_pressed=( |             keys_pressed=( | ||||||
|                 state.keys_pressed | {action['keycode']} |                 state.keys_pressed | {action['keycode']} | ||||||
|             ), |             ), | ||||||
| @@ -132,7 +138,7 @@ def kmk_reducer(state=None, action=None, logger=None): | |||||||
|         return newstate |         return newstate | ||||||
|  |  | ||||||
|     if action['type'] == INIT_FIRMWARE_EVENT: |     if action['type'] == INIT_FIRMWARE_EVENT: | ||||||
|         return state.copy( |         return state.update( | ||||||
|             keymap=action['keymap'], |             keymap=action['keymap'], | ||||||
|             row_pins=action['row_pins'], |             row_pins=action['row_pins'], | ||||||
|             col_pins=action['col_pins'], |             col_pins=action['col_pins'], | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ from kmk.common.types import AttrDict | |||||||
| from kmk.common.util import flatten_dict | from kmk.common.util import flatten_dict | ||||||
|  |  | ||||||
| Keycode = namedtuple('Keycode', ('code', 'is_modifier')) | Keycode = namedtuple('Keycode', ('code', 'is_modifier')) | ||||||
|  | LayerKeycode = namedtuple('LayerKeycode', ('code', 'layer')) | ||||||
|  |  | ||||||
|  |  | ||||||
| class KeycodeCategory(type): | class KeycodeCategory(type): | ||||||
| @@ -322,13 +323,41 @@ class Keycodes(KeycodeCategory): | |||||||
|         KC_LOCK = Keycode(1006, False) |         KC_LOCK = Keycode(1006, False) | ||||||
|  |  | ||||||
|     class Layers(KeycodeCategory): |     class Layers(KeycodeCategory): | ||||||
|         KC_DF = Keycode(1050, False) |         _KC_DF = 1050 | ||||||
|         KC_MO = Keycode(1051, False) |         _KC_MO = 1051 | ||||||
|         KC_LM = Keycode(1052, False) |         _KC_LM = 1052 | ||||||
|         KC_LT = Keycode(1053, False) |         _KC_LT = 1053 | ||||||
|         KC_TG = Keycode(1054, False) |         _KC_TG = 1054 | ||||||
|         KC_TO = Keycode(1055, False) |         _KC_TO = 1055 | ||||||
|         KC_TT = Keycode(1056, False) |         _KC_TT = 1056 | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_DF(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_DF, layer) | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_MO(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_MO, layer) | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_LM(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_LM, layer) | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_LT(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_LT, layer) | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_TG(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_TG, layer) | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_TO(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_TO, layer) | ||||||
|  |  | ||||||
|  |         @staticmethod | ||||||
|  |         def KC_TT(layer): | ||||||
|  |             return LayerKeycode(Keycodes.Layers._KC_TT, layer) | ||||||
|  |  | ||||||
|  |  | ||||||
| ALL_KEYS = KC = AttrDict({ | ALL_KEYS = KC = AttrDict({ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user