ffa81bcf43
This does a bunch of crazy stuff: - The ability to set a unicode mode (right now only Linux+ibus or MacOS-RALT) in the keymap. This will be changeable at runtime soon, to allow a single keyboard to be able to send table flips and whatever other crazy stuff on any OS the board is plugged into (something that's not currently doable on QMK, so yay us?) - As part of the above, there is now just one user-facing macro for unicode codepoint submission, `kmk.common.macros.unicode.unicode_sequence`. Users should never use the platform-specific macros, partly because they just outright won't work. There's all sorts of fun stuff in these methods now, thank goodness MicroPython supports the `yield from` construct. - Keycode (these should really be renamed Keysym or something) objects that are intended to not be pressed, or not be released. Right now these properties are completely ignored if not part of a macro, and it's probably sane to keep it that way. This was necessary to support MacOS's "hold RALT while typing the codepoint characters" flow. - Other refactor-y bits, like moving macro support to `kmk/common` rather than sitting at the top level of the tree. One day `kmk/common` may make sense to surface at top level `kmk/`, but that's a discussion for another day.
139 lines
3.6 KiB
Python
139 lines
3.6 KiB
Python
import logging
|
|
|
|
from micropython import const
|
|
|
|
from kmk.common.keycodes import Keycodes
|
|
|
|
KEY_UP_EVENT = const(1)
|
|
KEY_DOWN_EVENT = const(2)
|
|
INIT_FIRMWARE_EVENT = const(3)
|
|
NEW_MATRIX_EVENT = const(4)
|
|
HID_REPORT_EVENT = const(5)
|
|
KEYCODE_UP_EVENT = const(6)
|
|
KEYCODE_DOWN_EVENT = const(7)
|
|
MACRO_COMPLETE_EVENT = const(8)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def init_firmware(keymap, row_pins, col_pins, diode_orientation, unicode_mode):
|
|
return {
|
|
'type': INIT_FIRMWARE_EVENT,
|
|
'keymap': keymap,
|
|
'row_pins': row_pins,
|
|
'col_pins': col_pins,
|
|
'diode_orientation': diode_orientation,
|
|
'unicode_mode': unicode_mode,
|
|
}
|
|
|
|
|
|
def key_up_event(row, col):
|
|
return {
|
|
'type': KEY_UP_EVENT,
|
|
'row': row,
|
|
'col': col,
|
|
}
|
|
|
|
|
|
def key_down_event(row, col):
|
|
return {
|
|
'type': KEY_DOWN_EVENT,
|
|
'row': row,
|
|
'col': col,
|
|
}
|
|
|
|
|
|
def keycode_up_event(keycode):
|
|
'''
|
|
Press a key by Keycode object, bypassing the keymap. Used mostly for
|
|
macros.
|
|
'''
|
|
return {
|
|
'type': KEYCODE_UP_EVENT,
|
|
'keycode': keycode,
|
|
}
|
|
|
|
|
|
def keycode_down_event(keycode):
|
|
'''
|
|
Release a key by Keycode object, bypassing the keymap. Used mostly for
|
|
macros.
|
|
'''
|
|
return {
|
|
'type': KEYCODE_DOWN_EVENT,
|
|
'keycode': keycode,
|
|
}
|
|
|
|
|
|
def new_matrix_event(matrix):
|
|
return {
|
|
'type': NEW_MATRIX_EVENT,
|
|
'matrix': matrix,
|
|
}
|
|
|
|
|
|
def hid_report_event():
|
|
return {
|
|
'type': HID_REPORT_EVENT,
|
|
}
|
|
|
|
|
|
def macro_complete_event():
|
|
return {
|
|
'type': MACRO_COMPLETE_EVENT,
|
|
}
|
|
|
|
|
|
def matrix_changed(new_matrix):
|
|
def _key_pressed(dispatch, get_state):
|
|
state = get_state()
|
|
# Temporarily preserve a reference to the old event
|
|
# We do fake Redux around here because microcontrollers
|
|
# aren't exactly RAM or CPU powerhouses - the state does
|
|
# mutate in place. Unfortunately this makes reasoning
|
|
# about code a bit messier and really hurts one of the
|
|
# selling points of Redux. Former development versions
|
|
# of KMK created new InternalState copies every single
|
|
# time the state changed, but it was sometimes slow.
|
|
old_matrix = state.matrix
|
|
old_keys_pressed = state.keys_pressed
|
|
|
|
dispatch(new_matrix_event(new_matrix))
|
|
|
|
with get_state() as new_state:
|
|
for ridx, row in enumerate(new_state.matrix):
|
|
for cidx, col in enumerate(row):
|
|
if col != old_matrix[ridx][cidx]:
|
|
if col:
|
|
dispatch(key_down_event(
|
|
row=ridx,
|
|
col=cidx,
|
|
))
|
|
else:
|
|
dispatch(key_up_event(
|
|
row=ridx,
|
|
col=cidx,
|
|
))
|
|
|
|
with get_state() as new_state:
|
|
if old_keys_pressed != new_state.keys_pressed:
|
|
dispatch(hid_report_event())
|
|
|
|
if Keycodes.KMK.KC_RESET in new_state.keys_pressed:
|
|
try:
|
|
import machine
|
|
machine.bootloader()
|
|
except ImportError:
|
|
logger.warning('Tried to reset to bootloader, but not supported on this chip?')
|
|
|
|
with get_state() as new_state:
|
|
if new_state.macro_pending:
|
|
macro = new_state.macro_pending
|
|
|
|
for event in macro(new_state):
|
|
dispatch(event)
|
|
|
|
dispatch(macro_complete_event())
|
|
|
|
return _key_pressed
|