Roll back the tuple indexing change (now access by dot notation), optimize GESC and fix a bug in how it interacts with the reducer

This commit is contained in:
Josh Klar 2018-10-01 01:07:57 -07:00
parent b5457534bf
commit f80873fdd0
No known key found for this signature in database
GPG Key ID: 220F99BD7DB7A99E
4 changed files with 25 additions and 29 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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