Remove a ton of dict lookups for minor perf gains
This commit is contained in:
parent
a089675aa8
commit
784f8885bd
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
from micropython import const
|
from micropython import const
|
||||||
|
|
||||||
@ -16,31 +17,46 @@ MACRO_COMPLETE_EVENT = const(8)
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
InitFirmware = namedtuple('InitFirmware', (
|
||||||
|
'type',
|
||||||
|
'keymap',
|
||||||
|
'row_pins',
|
||||||
|
'col_pins',
|
||||||
|
'diode_orientation',
|
||||||
|
'unicode_mode',
|
||||||
|
))
|
||||||
|
|
||||||
|
KeyUpDown = namedtuple('KeyUpDown', ('type', 'row', 'col'))
|
||||||
|
KeycodeUpDown = namedtuple('KeycodeUpDown', ('type', 'keycode'))
|
||||||
|
NewMatrix = namedtuple('NewMatrix', ('type', 'matrix'))
|
||||||
|
BareEvent = namedtuple('BareEvent', ('type',))
|
||||||
|
|
||||||
|
|
||||||
def init_firmware(keymap, row_pins, col_pins, diode_orientation, unicode_mode):
|
def init_firmware(keymap, row_pins, col_pins, diode_orientation, unicode_mode):
|
||||||
return {
|
return InitFirmware(
|
||||||
'type': INIT_FIRMWARE_EVENT,
|
type=INIT_FIRMWARE_EVENT,
|
||||||
'keymap': keymap,
|
keymap=keymap,
|
||||||
'row_pins': row_pins,
|
row_pins=row_pins,
|
||||||
'col_pins': col_pins,
|
col_pins=col_pins,
|
||||||
'diode_orientation': diode_orientation,
|
diode_orientation=diode_orientation,
|
||||||
'unicode_mode': unicode_mode,
|
unicode_mode=unicode_mode,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def key_up_event(row, col):
|
def key_up_event(row, col):
|
||||||
return {
|
return KeyUpDown(
|
||||||
'type': KEY_UP_EVENT,
|
type=KEY_UP_EVENT,
|
||||||
'row': row,
|
row=row,
|
||||||
'col': col,
|
col=col,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def key_down_event(row, col):
|
def key_down_event(row, col):
|
||||||
return {
|
return KeyUpDown(
|
||||||
'type': KEY_DOWN_EVENT,
|
type=KEY_DOWN_EVENT,
|
||||||
'row': row,
|
row=row,
|
||||||
'col': col,
|
col=col,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def keycode_up_event(keycode):
|
def keycode_up_event(keycode):
|
||||||
@ -48,10 +64,10 @@ def keycode_up_event(keycode):
|
|||||||
Press a key by Keycode object, bypassing the keymap. Used mostly for
|
Press a key by Keycode object, bypassing the keymap. Used mostly for
|
||||||
macros.
|
macros.
|
||||||
'''
|
'''
|
||||||
return {
|
return KeycodeUpDown(
|
||||||
'type': KEYCODE_UP_EVENT,
|
type=KEYCODE_UP_EVENT,
|
||||||
'keycode': keycode,
|
keycode=keycode,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def keycode_down_event(keycode):
|
def keycode_down_event(keycode):
|
||||||
@ -59,29 +75,29 @@ def keycode_down_event(keycode):
|
|||||||
Release a key by Keycode object, bypassing the keymap. Used mostly for
|
Release a key by Keycode object, bypassing the keymap. Used mostly for
|
||||||
macros.
|
macros.
|
||||||
'''
|
'''
|
||||||
return {
|
return KeycodeUpDown(
|
||||||
'type': KEYCODE_DOWN_EVENT,
|
type=KEYCODE_DOWN_EVENT,
|
||||||
'keycode': keycode,
|
keycode=keycode,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def new_matrix_event(matrix):
|
def new_matrix_event(matrix):
|
||||||
return {
|
return NewMatrix(
|
||||||
'type': NEW_MATRIX_EVENT,
|
type=NEW_MATRIX_EVENT,
|
||||||
'matrix': matrix,
|
matrix=matrix,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def hid_report_event():
|
def hid_report_event():
|
||||||
return {
|
return BareEvent(
|
||||||
'type': HID_REPORT_EVENT,
|
type=HID_REPORT_EVENT,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def macro_complete_event():
|
def macro_complete_event():
|
||||||
return {
|
return BareEvent(
|
||||||
'type': MACRO_COMPLETE_EVENT,
|
type=MACRO_COMPLETE_EVENT,
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
def matrix_changed(new_matrix):
|
def matrix_changed(new_matrix):
|
||||||
|
@ -30,7 +30,7 @@ def process_internal_key_event(state, action, changed_key, logger=None):
|
|||||||
|
|
||||||
|
|
||||||
def grave_escape(state, action, logger):
|
def grave_escape(state, action, logger):
|
||||||
if action['type'] == KEY_DOWN_EVENT:
|
if action[0] == KEY_DOWN_EVENT:
|
||||||
for key in state.keys_pressed:
|
for key in state.keys_pressed:
|
||||||
if key in {Keycodes.Modifiers.KC_LSHIFT, Keycodes.Modifiers.KC_RSHIFT}:
|
if key in {Keycodes.Modifiers.KC_LSHIFT, Keycodes.Modifiers.KC_RSHIFT}:
|
||||||
# if Shift is held, return KC_GRAVE which will become KC_TILDE on OS level
|
# if Shift is held, return KC_GRAVE which will become KC_TILDE on OS level
|
||||||
@ -54,7 +54,7 @@ def grave_escape(state, action, logger):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
elif action['type'] == KEY_UP_EVENT:
|
elif action[0] == KEY_UP_EVENT:
|
||||||
return state.update(
|
return state.update(
|
||||||
keys_pressed=frozenset(
|
keys_pressed=frozenset(
|
||||||
key for key in state.keys_pressed
|
key for key in state.keys_pressed
|
||||||
@ -65,7 +65,7 @@ def grave_escape(state, action, logger):
|
|||||||
|
|
||||||
def df(state, action, changed_key, logger):
|
def df(state, action, changed_key, logger):
|
||||||
"""Switches the default layer"""
|
"""Switches the default layer"""
|
||||||
if action['type'] == KEY_DOWN_EVENT:
|
if action[0] == KEY_DOWN_EVENT:
|
||||||
state.active_layers[0] = changed_key.layer
|
state.active_layers[0] = changed_key.layer
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@ -73,12 +73,12 @@ def df(state, action, changed_key, logger):
|
|||||||
|
|
||||||
def mo(state, action, changed_key, logger):
|
def mo(state, action, changed_key, 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[0] == KEY_UP_EVENT:
|
||||||
state.active_layers = [
|
state.active_layers = [
|
||||||
layer for layer in state.active_layers
|
layer for layer in state.active_layers
|
||||||
if layer != changed_key.layer
|
if layer != changed_key.layer
|
||||||
]
|
]
|
||||||
elif action['type'] == KEY_DOWN_EVENT:
|
elif action[0] == KEY_DOWN_EVENT:
|
||||||
state.active_layers.append(changed_key.layer)
|
state.active_layers.append(changed_key.layer)
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@ -94,7 +94,7 @@ def lt(layer, kc):
|
|||||||
|
|
||||||
def tg(state, action, changed_key, logger):
|
def tg(state, action, changed_key, logger):
|
||||||
"""Toggles the layer (enables it if not active, and vise versa)"""
|
"""Toggles the layer (enables it if not active, and vise versa)"""
|
||||||
if action['type'] == KEY_DOWN_EVENT:
|
if action[0] == KEY_DOWN_EVENT:
|
||||||
if changed_key.layer in state.active_layers:
|
if changed_key.layer in state.active_layers:
|
||||||
state.active_layers = [
|
state.active_layers = [
|
||||||
layer for layer in state.active_layers
|
layer for layer in state.active_layers
|
||||||
@ -108,7 +108,7 @@ def tg(state, action, changed_key, logger):
|
|||||||
|
|
||||||
def to(state, action, changed_key, logger):
|
def to(state, action, changed_key, logger):
|
||||||
"""Activates layer and deactivates all other layers"""
|
"""Activates layer and deactivates all other layers"""
|
||||||
if action['type'] == KEY_DOWN_EVENT:
|
if action[0] == KEY_DOWN_EVENT:
|
||||||
state.active_layers = [changed_key.layer]
|
state.active_layers = [changed_key.layer]
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@ -119,7 +119,7 @@ def tt(layer):
|
|||||||
|
|
||||||
|
|
||||||
def unicode_mode(state, action, changed_key, logger):
|
def unicode_mode(state, action, changed_key, logger):
|
||||||
if action['type'] == KEY_DOWN_EVENT:
|
if action[0] == KEY_DOWN_EVENT:
|
||||||
state.unicode_mode = changed_key.mode
|
state.unicode_mode = changed_key.mode
|
||||||
|
|
||||||
return state
|
return state
|
||||||
|
@ -26,9 +26,9 @@ class ReduxStore:
|
|||||||
self.logger.debug('Finished thunk')
|
self.logger.debug('Finished thunk')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.logger.debug('Dispatching action: Type {} >> {}'.format(action['type'], action))
|
self.logger.debug('Dispatching action: Type {} >> {}'.format(action[0], action))
|
||||||
self.state = self.reducer(self.state, action, logger=self.logger)
|
self.state = self.reducer(self.state, action, logger=self.logger)
|
||||||
self.logger.debug('Dispatching complete: Type {}'.format(action['type']))
|
self.logger.debug('Dispatching complete: Type {}'.format(action[0]))
|
||||||
|
|
||||||
self.logger.debug('New state: {}'.format(self.state))
|
self.logger.debug('New state: {}'.format(self.state))
|
||||||
|
|
||||||
@ -133,28 +133,28 @@ def kmk_reducer(state=None, action=None, logger=None):
|
|||||||
|
|
||||||
return state
|
return state
|
||||||
|
|
||||||
if action['type'] == NEW_MATRIX_EVENT:
|
if action[0] == NEW_MATRIX_EVENT:
|
||||||
return state.update(
|
return state.update(
|
||||||
matrix=action['matrix'],
|
matrix=action[1],
|
||||||
)
|
)
|
||||||
|
|
||||||
if action['type'] == KEYCODE_UP_EVENT:
|
if action[0] == KEYCODE_UP_EVENT:
|
||||||
return state.update(
|
return 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[1]
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
if action['type'] == KEYCODE_DOWN_EVENT:
|
if action[0] == KEYCODE_DOWN_EVENT:
|
||||||
return state.update(
|
return state.update(
|
||||||
keys_pressed=(
|
keys_pressed=(
|
||||||
state.keys_pressed | {action['keycode']}
|
state.keys_pressed | {action[1]}
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
if action['type'] == KEY_UP_EVENT:
|
if action[0] == KEY_UP_EVENT:
|
||||||
row = action['row']
|
row = action[1]
|
||||||
col = action['col']
|
col = action[2]
|
||||||
|
|
||||||
changed_key = find_key_in_map(state, row, col)
|
changed_key = find_key_in_map(state, row, col)
|
||||||
|
|
||||||
@ -182,9 +182,9 @@ def kmk_reducer(state=None, action=None, logger=None):
|
|||||||
|
|
||||||
return newstate
|
return newstate
|
||||||
|
|
||||||
if action['type'] == KEY_DOWN_EVENT:
|
if action[0] == KEY_DOWN_EVENT:
|
||||||
row = action['row']
|
row = action[1]
|
||||||
col = action['col']
|
col = action[2]
|
||||||
|
|
||||||
changed_key = find_key_in_map(state, row, col)
|
changed_key = find_key_in_map(state, row, col)
|
||||||
|
|
||||||
@ -212,16 +212,16 @@ def kmk_reducer(state=None, action=None, logger=None):
|
|||||||
|
|
||||||
return newstate
|
return newstate
|
||||||
|
|
||||||
if action['type'] == INIT_FIRMWARE_EVENT:
|
if action[0] == INIT_FIRMWARE_EVENT:
|
||||||
return state.update(
|
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,
|
||||||
diode_orientation=action['diode_orientation'],
|
diode_orientation=action.diode_orientation,
|
||||||
unicode_mode=action['unicode_mode'],
|
unicode_mode=action.unicode_mode,
|
||||||
matrix=[
|
matrix=[
|
||||||
[False for c in action['col_pins']]
|
[False for c in action.col_pins]
|
||||||
for r in action['row_pins']
|
for r in action.row_pins
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -229,10 +229,10 @@ def kmk_reducer(state=None, action=None, logger=None):
|
|||||||
# they should be doing things. This could/should arguably be folded back
|
# 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
|
# into KEY_UP_EVENT and KEY_DOWN_EVENT, but for now it's nice to separate
|
||||||
# this out for debugging's sake.
|
# this out for debugging's sake.
|
||||||
if action['type'] == HID_REPORT_EVENT:
|
if action[0] == HID_REPORT_EVENT:
|
||||||
return state
|
return state
|
||||||
|
|
||||||
if action['type'] == MACRO_COMPLETE_EVENT:
|
if action[0] == MACRO_COMPLETE_EVENT:
|
||||||
return state.update(macro_pending=None)
|
return state.update(macro_pending=None)
|
||||||
|
|
||||||
# On unhandled events, log and do not mutate state
|
# On unhandled events, log and do not mutate state
|
||||||
|
@ -43,7 +43,7 @@ class HIDHelper:
|
|||||||
self.report_non_mods = memoryview(self._evt)[3:]
|
self.report_non_mods = memoryview(self._evt)[3:]
|
||||||
|
|
||||||
def _subscription(self, state, action):
|
def _subscription(self, state, action):
|
||||||
if action['type'] == HID_REPORT_EVENT:
|
if action[0] == HID_REPORT_EVENT:
|
||||||
self.clear_all()
|
self.clear_all()
|
||||||
|
|
||||||
consumer_key = None
|
consumer_key = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user