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