From f80873fdd0beafa8cf3c3e96d6a3d146627090be Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Mon, 1 Oct 2018 01:07:57 -0700 Subject: [PATCH] Roll back the tuple indexing change (now access by dot notation), optimize GESC and fix a bug in how it interacts with the reducer --- kmk/common/internal_keycodes.py | 23 ++++++++++++----------- kmk/common/internal_state.py | 28 ++++++++++++---------------- kmk/micropython/matrix.py | 1 - kmk/micropython/pyb_hid.py | 2 +- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/kmk/common/internal_keycodes.py b/kmk/common/internal_keycodes.py index 54ef00f..ec34ebc 100644 --- a/kmk/common/internal_keycodes.py +++ b/kmk/common/internal_keycodes.py @@ -3,6 +3,11 @@ import logging from kmk.common.event_defs import KEY_DOWN_EVENT, KEY_UP_EVENT from kmk.common.keycodes import Keycodes, RawKeycodes +GESC_TRIGGERS = { + Keycodes.Modifiers.KC_LSHIFT, Keycodes.Modifiers.KC_RSHIFT, + Keycodes.Modifiers.KC_LGUI, Keycodes.Modifiers.KC_RGUI, +} + def process_internal_key_event(state, action_type, changed_key, logger=None): if logger is None: @@ -31,19 +36,15 @@ def process_internal_key_event(state, action_type, changed_key, logger=None): def grave_escape(state, action_type, logger): if action_type == KEY_DOWN_EVENT: - for key in state.keys_pressed: - if key in { - Keycodes.Modifiers.KC_LSHIFT, Keycodes.Modifiers.KC_RSHIFT, - Keycodes.Modifiers.KC_LGUI, Keycodes.Modifiers.KC_RGUI, - }: - # if Shift is held, KC_GRAVE will become KC_TILDE on OS level - state.keys_pressed.add(Keycodes.Common.KC_GRAVE) - return state - - # else return KC_ESC - state.keys_pressed.add(Keycodes.Common.KC_ESCAPE) + if any(key in GESC_TRIGGERS for key in state.keys_pressed): + # if Shift is held, KC_GRAVE will become KC_TILDE on OS level + state.keys_pressed.add(Keycodes.Common.KC_GRAVE) return state + # else return KC_ESC + state.keys_pressed.add(Keycodes.Common.KC_ESCAPE) + return state + elif action_type == KEY_UP_EVENT: state.keys_pressed.discard(Keycodes.Common.KC_ESCAPE) state.keys_pressed.discard(Keycodes.Common.KC_GRAVE) diff --git a/kmk/common/internal_state.py b/kmk/common/internal_state.py index 36c5601..47972de 100644 --- a/kmk/common/internal_state.py +++ b/kmk/common/internal_state.py @@ -26,9 +26,9 @@ class ReduxStore: self.logger.debug('Finished thunk') return None - self.logger.debug('Dispatching action: Type {} >> {}'.format(action[0], action)) + self.logger.debug('Dispatching action: Type {} >> {}'.format(action.type, action)) self.state = self.reducer(self.state, action, logger=self.logger) - self.logger.debug('Dispatching complete: Type {}'.format(action[0])) + self.logger.debug('Dispatching complete: Type {}'.format(action.type)) self.logger.debug('New state: {}'.format(self.state)) @@ -132,10 +132,10 @@ def kmk_reducer(state=None, action=None, logger=None): return state - if action[0] == NEW_MATRIX_EVENT: + if action.type == NEW_MATRIX_EVENT: matrix_keys_pressed = { find_key_in_map(state, row, col) - for row, col in action[1] + for row, col in action.matrix } pressed = matrix_keys_pressed - state.keys_pressed @@ -171,45 +171,41 @@ def kmk_reducer(state=None, action=None, logger=None): logger=logger, ) - state.matrix = action[1] + state.matrix = action.matrix state.keys_pressed |= pressed state.keys_pressed -= released state.hid_pending = True return state - if action[0] == KEYCODE_UP_EVENT: - state.keys_pressed.discard(action[1]) + if action.type == KEYCODE_UP_EVENT: + state.keys_pressed.discard(action.keycode) state.hid_pending = True return state - if action[0] == KEYCODE_DOWN_EVENT: - state.keys_pressed.add(action[1]) + if action.type == KEYCODE_DOWN_EVENT: + state.keys_pressed.add(action.keycode) state.hid_pending = True return state - if action[0] == INIT_FIRMWARE_EVENT: + if action.type == INIT_FIRMWARE_EVENT: return state.update( keymap=action.keymap, row_pins=action.row_pins, col_pins=action.col_pins, diode_orientation=action.diode_orientation, unicode_mode=action.unicode_mode, - matrix=[ - [False for c in action.col_pins] - for r in action.row_pins - ], ) # HID events are non-mutating, used exclusively for listeners to know # they should be doing things. This could/should arguably be folded back # into KEY_UP_EVENT and KEY_DOWN_EVENT, but for now it's nice to separate # this out for debugging's sake. - if action[0] == HID_REPORT_EVENT: + if action.type == HID_REPORT_EVENT: state.hid_pending = False return state - if action[0] == MACRO_COMPLETE_EVENT: + if action.type == MACRO_COMPLETE_EVENT: return state.update(macro_pending=None) # On unhandled events, log and do not mutate state diff --git a/kmk/micropython/matrix.py b/kmk/micropython/matrix.py index 6f69105..a89f6dd 100644 --- a/kmk/micropython/matrix.py +++ b/kmk/micropython/matrix.py @@ -57,7 +57,6 @@ class MatrixScanner(AbstractMatrixScanner): opin.value(0) if len(pressed) != self.last_pressed_len: - print('LEN PRESSED {} LEN LAST PRESSED {}'.format(len(pressed), self.last_pressed_len)) self.last_pressed_len = len(pressed) return matrix_changed(pressed) diff --git a/kmk/micropython/pyb_hid.py b/kmk/micropython/pyb_hid.py index 4299814..315fac4 100644 --- a/kmk/micropython/pyb_hid.py +++ b/kmk/micropython/pyb_hid.py @@ -44,7 +44,7 @@ class HIDHelper: self.report_non_mods = memoryview(self._evt)[3:] def _subscription(self, state, action): - if action[0] == HID_REPORT_EVENT: + if action.type == HID_REPORT_EVENT: self.clear_all() consumer_key = None