Allow super simple keymap keycode definitions (very QMK-ish)
This commit is contained in:
parent
88807837d5
commit
6933d9c484
@ -2,7 +2,7 @@ from logging import DEBUG
|
|||||||
|
|
||||||
import machine
|
import machine
|
||||||
|
|
||||||
from kmk.common.consts import DiodeOrientation
|
from kmk.common.consts import KC, DiodeOrientation
|
||||||
from kmk.firmware import Firmware
|
from kmk.firmware import Firmware
|
||||||
|
|
||||||
|
|
||||||
@ -15,9 +15,9 @@ def main():
|
|||||||
diode_orientation = DiodeOrientation.COLUMNS
|
diode_orientation = DiodeOrientation.COLUMNS
|
||||||
|
|
||||||
keymap = [
|
keymap = [
|
||||||
['A', 'B', 'C'],
|
[KC.ESC, KC.H, KC.BACKSPACE],
|
||||||
['D', 'E', 'F'],
|
[KC.TAB, KC.I, KC.ENTER],
|
||||||
['G', 'H', 'I'],
|
[KC.CTRL, KC.SPACE, KC.SHIFT],
|
||||||
]
|
]
|
||||||
|
|
||||||
firmware = Firmware(
|
firmware = Firmware(
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
from kmk.common.types import AttrDict
|
||||||
|
from kmk.common.util import flatten_dict
|
||||||
|
|
||||||
|
|
||||||
class DiodeOrientation:
|
class DiodeOrientation:
|
||||||
'''
|
'''
|
||||||
Orientation of diodes on handwired boards. You can think of:
|
Orientation of diodes on handwired boards. You can think of:
|
||||||
@ -19,11 +23,30 @@ class KeycodeCategory(type):
|
|||||||
__dict__, limited to just keys we're likely to care about (though this
|
__dict__, limited to just keys we're likely to care about (though this
|
||||||
could be opened up further later).
|
could be opened up further later).
|
||||||
'''
|
'''
|
||||||
return {
|
|
||||||
|
hidden = ('to_dict', 'recursive_dict', 'contains')
|
||||||
|
return AttrDict({
|
||||||
key: getattr(cls, key)
|
key: getattr(cls, key)
|
||||||
for key in dir(cls)
|
for key in dir(cls)
|
||||||
if not key.startswith('_')
|
if not key.startswith('_') and key not in hidden
|
||||||
}
|
})
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def recursive_dict(cls):
|
||||||
|
'''
|
||||||
|
to_dict() executed recursively all the way down a tree
|
||||||
|
'''
|
||||||
|
ret = cls.to_dict()
|
||||||
|
|
||||||
|
for key, val in ret.items():
|
||||||
|
try:
|
||||||
|
nested_ret = val.recursive_dict()
|
||||||
|
except (AttributeError, NameError):
|
||||||
|
continue
|
||||||
|
|
||||||
|
ret[key] = nested_ret
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def contains(cls, kc):
|
def contains(cls, kc):
|
||||||
@ -115,16 +138,19 @@ class Keycodes(KeycodeCategory):
|
|||||||
KC_X = 27
|
KC_X = 27
|
||||||
KC_Y = 28
|
KC_Y = 28
|
||||||
KC_Z = 29
|
KC_Z = 29
|
||||||
KC_1 = 30
|
|
||||||
KC_2 = 31
|
# Aliases to play nicely with AttrDict, since KC.1 isn't a valid
|
||||||
KC_3 = 32
|
# attribute key in Python, but KC.N1 is
|
||||||
KC_4 = 33
|
KC_1 = KC_N1 = 30
|
||||||
KC_5 = 34
|
KC_2 = KC_N2 = 31
|
||||||
KC_6 = 35
|
KC_3 = KC_N3 = 32
|
||||||
KC_7 = 36
|
KC_4 = KC_N4 = 33
|
||||||
KC_8 = 37
|
KC_5 = KC_N5 = 34
|
||||||
KC_9 = 38
|
KC_6 = KC_N6 = 35
|
||||||
KC_0 = 39
|
KC_7 = KC_N7 = 36
|
||||||
|
KC_8 = KC_N8 = 37
|
||||||
|
KC_9 = KC_N9 = 38
|
||||||
|
KC_0 = KC_N0 = 39
|
||||||
|
|
||||||
KC_ENTER = 40
|
KC_ENTER = 40
|
||||||
KC_ESC = 41
|
KC_ESC = 41
|
||||||
@ -194,6 +220,11 @@ class Keycodes(KeycodeCategory):
|
|||||||
KC_KP_PERIOD = 99
|
KC_KP_PERIOD = 99
|
||||||
|
|
||||||
|
|
||||||
|
ALL_KEYS = KC = AttrDict({
|
||||||
|
k.replace('KC_', ''): v
|
||||||
|
for k, v in flatten_dict(Keycodes.recursive_dict()).items()
|
||||||
|
})
|
||||||
|
|
||||||
char_lookup = {
|
char_lookup = {
|
||||||
"\n": (Keycodes.Common.KC_ENTER,),
|
"\n": (Keycodes.Common.KC_ENTER,),
|
||||||
"\t": (Keycodes.Common.KC_TAB,),
|
"\t": (Keycodes.Common.KC_TAB,),
|
||||||
|
12
kmk/common/types.py
Normal file
12
kmk/common/types.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
class AttrDict(dict):
|
||||||
|
'''
|
||||||
|
Primitive support for accessing dictionary entries in dot notation.
|
||||||
|
Mostly for user-facing stuff (allows for `k.KC_ESC` rather than
|
||||||
|
`k['KC_ESC']`, which gets a bit obnoxious).
|
||||||
|
|
||||||
|
This is read-only on purpose.
|
||||||
|
'''
|
||||||
|
def __getattr__(self, key):
|
||||||
|
return self[key]
|
||||||
|
|
||||||
|
|
10
kmk/common/util.py
Normal file
10
kmk/common/util.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
def flatten_dict(d):
|
||||||
|
items = {}
|
||||||
|
|
||||||
|
for k, v in d.items():
|
||||||
|
if isinstance(v, dict):
|
||||||
|
items.update(flatten_dict(v))
|
||||||
|
else:
|
||||||
|
items[k] = v
|
||||||
|
|
||||||
|
return items
|
Loading…
x
Reference in New Issue
Block a user