2018-09-22 21:49:58 -07:00
|
|
|
import logging
|
2018-09-30 22:50:04 -07:00
|
|
|
from collections import namedtuple
|
2018-09-22 21:49:58 -07:00
|
|
|
|
2018-09-03 13:50:12 -07:00
|
|
|
from micropython import const
|
|
|
|
|
2018-09-22 21:49:58 -07:00
|
|
|
from kmk.common.keycodes import Keycodes
|
2018-10-07 00:44:04 -07:00
|
|
|
from kmk.common.util import reset_bootloader
|
2018-09-22 21:49:58 -07:00
|
|
|
|
2018-09-03 13:50:12 -07:00
|
|
|
KEY_UP_EVENT = const(1)
|
|
|
|
KEY_DOWN_EVENT = const(2)
|
2018-09-03 15:21:34 -07:00
|
|
|
INIT_FIRMWARE_EVENT = const(3)
|
2018-09-22 21:49:58 -07:00
|
|
|
NEW_MATRIX_EVENT = const(4)
|
|
|
|
HID_REPORT_EVENT = const(5)
|
2018-09-30 18:03:43 -07:00
|
|
|
KEYCODE_UP_EVENT = const(6)
|
|
|
|
KEYCODE_DOWN_EVENT = const(7)
|
|
|
|
MACRO_COMPLETE_EVENT = const(8)
|
2018-09-28 14:35:52 -07:00
|
|
|
PENDING_KEYCODE_POP_EVENT = const(9)
|
2018-09-22 21:49:58 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2018-09-03 13:50:12 -07:00
|
|
|
|
|
|
|
|
2018-09-30 22:50:04 -07:00
|
|
|
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',))
|
|
|
|
|
|
|
|
|
Massive refactor largely to support Unicode on Mac
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.
2018-09-30 19:33:23 -07:00
|
|
|
def init_firmware(keymap, row_pins, col_pins, diode_orientation, unicode_mode):
|
2018-09-30 22:50:04 -07:00
|
|
|
return InitFirmware(
|
|
|
|
type=INIT_FIRMWARE_EVENT,
|
|
|
|
keymap=keymap,
|
|
|
|
row_pins=row_pins,
|
|
|
|
col_pins=col_pins,
|
|
|
|
diode_orientation=diode_orientation,
|
|
|
|
unicode_mode=unicode_mode,
|
|
|
|
)
|
2018-09-03 15:21:34 -07:00
|
|
|
|
|
|
|
|
2018-09-22 21:49:58 -07:00
|
|
|
def key_up_event(row, col):
|
2018-09-30 22:50:04 -07:00
|
|
|
return KeyUpDown(
|
|
|
|
type=KEY_UP_EVENT,
|
|
|
|
row=row,
|
|
|
|
col=col,
|
|
|
|
)
|
2018-09-03 13:50:12 -07:00
|
|
|
|
|
|
|
|
2018-09-22 21:49:58 -07:00
|
|
|
def key_down_event(row, col):
|
2018-09-30 22:50:04 -07:00
|
|
|
return KeyUpDown(
|
|
|
|
type=KEY_DOWN_EVENT,
|
|
|
|
row=row,
|
|
|
|
col=col,
|
|
|
|
)
|
2018-09-22 21:49:58 -07:00
|
|
|
|
|
|
|
|
2018-09-30 18:03:43 -07:00
|
|
|
def keycode_up_event(keycode):
|
|
|
|
'''
|
|
|
|
Press a key by Keycode object, bypassing the keymap. Used mostly for
|
|
|
|
macros.
|
|
|
|
'''
|
2018-09-30 22:50:04 -07:00
|
|
|
return KeycodeUpDown(
|
|
|
|
type=KEYCODE_UP_EVENT,
|
|
|
|
keycode=keycode,
|
|
|
|
)
|
2018-09-30 18:03:43 -07:00
|
|
|
|
|
|
|
|
|
|
|
def keycode_down_event(keycode):
|
|
|
|
'''
|
|
|
|
Release a key by Keycode object, bypassing the keymap. Used mostly for
|
|
|
|
macros.
|
|
|
|
'''
|
2018-09-30 22:50:04 -07:00
|
|
|
return KeycodeUpDown(
|
|
|
|
type=KEYCODE_DOWN_EVENT,
|
|
|
|
keycode=keycode,
|
|
|
|
)
|
2018-09-30 18:03:43 -07:00
|
|
|
|
|
|
|
|
2018-09-22 21:49:58 -07:00
|
|
|
def new_matrix_event(matrix):
|
2018-09-30 22:50:04 -07:00
|
|
|
return NewMatrix(
|
|
|
|
type=NEW_MATRIX_EVENT,
|
|
|
|
matrix=matrix,
|
|
|
|
)
|
2018-09-22 21:49:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
def hid_report_event():
|
2018-09-30 22:50:04 -07:00
|
|
|
return BareEvent(
|
|
|
|
type=HID_REPORT_EVENT,
|
|
|
|
)
|
2018-09-22 21:49:58 -07:00
|
|
|
|
|
|
|
|
Massive refactor largely to support Unicode on Mac
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.
2018-09-30 19:33:23 -07:00
|
|
|
def macro_complete_event():
|
2018-09-30 22:50:04 -07:00
|
|
|
return BareEvent(
|
|
|
|
type=MACRO_COMPLETE_EVENT,
|
|
|
|
)
|
2018-09-30 18:03:43 -07:00
|
|
|
|
|
|
|
|
2018-09-28 14:35:52 -07:00
|
|
|
def pending_keycode_pop_event():
|
|
|
|
return BareEvent(
|
|
|
|
type=PENDING_KEYCODE_POP_EVENT,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-01 00:31:45 -07:00
|
|
|
def matrix_changed(new_pressed):
|
2018-09-22 21:49:58 -07:00
|
|
|
def _key_pressed(dispatch, get_state):
|
2018-10-01 00:31:45 -07:00
|
|
|
dispatch(new_matrix_event(new_pressed))
|
|
|
|
|
2018-09-22 21:49:58 -07:00
|
|
|
state = get_state()
|
2018-10-01 00:31:45 -07:00
|
|
|
|
|
|
|
if state.hid_pending:
|
|
|
|
dispatch(hid_report_event())
|
|
|
|
|
|
|
|
if Keycodes.KMK.KC_RESET in state.keys_pressed:
|
2018-10-07 00:44:04 -07:00
|
|
|
reset_bootloader()
|
2018-10-01 00:31:45 -07:00
|
|
|
|
2018-09-28 14:35:52 -07:00
|
|
|
if state.pending_keys:
|
|
|
|
for key in state.pending_keys:
|
|
|
|
if not key.no_press:
|
|
|
|
dispatch(keycode_down_event(key))
|
|
|
|
dispatch(hid_report_event())
|
|
|
|
|
|
|
|
if not key.no_release:
|
|
|
|
dispatch(keycode_up_event(key))
|
|
|
|
dispatch(hid_report_event())
|
|
|
|
|
|
|
|
dispatch(pending_keycode_pop_event())
|
|
|
|
|
2018-10-01 00:31:45 -07:00
|
|
|
if state.macro_pending:
|
|
|
|
macro = state.macro_pending
|
|
|
|
|
|
|
|
for event in macro(state):
|
|
|
|
dispatch(event)
|
|
|
|
|
|
|
|
dispatch(macro_complete_event())
|
2018-09-30 18:03:43 -07:00
|
|
|
|
2018-09-22 21:49:58 -07:00
|
|
|
return _key_pressed
|