refactor remaining ardux specific code and move to kb.py

This commit is contained in:
KemoNine 2023-04-19 11:50:56 -04:00
parent 507f29ae93
commit 0153acc2e2
2 changed files with 74 additions and 77 deletions

86
kb.py
View file

@ -1,27 +1,77 @@
import os
import board import board
from kmk.kmk_keyboard import KMKKeyboard as _KMKKeyboard
from kmk.quickpin.pro_micro.kb2040 import pinout as pins from kmk.quickpin.pro_micro.kb2040 import pinout as pins
from kmk.kmk_keyboard import KMKKeyboard
from kmk.scanners.keypad import KeysScanner from kmk.scanners.keypad import KeysScanner
from kmk.modules.layers import Layers
from kmk.modules.combos import Combos, Chord
# Direct wire config from kmk.keys import KC
_KEY_CFG = [ @
pins[16], # TODO: fix up pass by reference trick (wrapped in array) thats used below
pins[17], # likely deserves a global but KmN couldnt figure it out
pins[18], class ArduxChord(Chord):
pins[19], # Override default constructor to allow passing of required fields
pins[12], def __init__(
pins[13], self,
pins[14], match: Tuple[Union[Key, int], ...],
pins[15] result: Key,
] fast_reset=None,
per_key_timeout=None,
class KMKKeyboard(_KMKKeyboard): timeout=None,
def __init__(self): match_coord=None,
# create and register the scanner for direct wire ardux_keyboard=[],
self.matrix = KeysScanner(_KEY_CFG) layers=[],
):
super().__init__(match, result, fast_reset, per_key_timeout, match_coord)
self.ardux_keyboard = ardux_keyboard
self.layers = layers
# Override standard kmk match logic to first check active vs allowed layers
def matches(self, key: Key, int_coord: int):
if self.ardux_keyboard is None or len(self.ardux_keyboard) == 0 or len(self.layers) == 0 or any(i in self.ardux_keyboard[0].active_layers for i in self.layers):
return super().matches(key, int_coord)
return False
class ArduxKeyboard(KMKKeyboard):
coord_mapping = [ coord_mapping = [
0, 1, 2, 3, 0, 1, 2, 3,
4, 5, 6, 7, 4, 5, 6, 7,
] ]
keymap = [
[KC.S, KC.T, KC.R, KC.A,
KC.O, KC.I, KC.Y, KC.E]
]
# Init / constructor / setup
def __init__(self):
# Enable debugging if appropriate
if os.getenv('ARDUX_KMK_DEBUGGING'):
self.debug_enabled = True
# setup modules/extensions arrays
self.modules = []
self.extensions = []
# Direct wire & matrix setup
self.matrix = KeysScanner([pins[16], pins[17], pins[18], pins[19], pins[12], pins[13], pins[14], pins[15]])
# Layers
self.modules.append(Layers())
# Combos
self.combo_module = Combos()
self.modules.append(self.combo_module)
self.setup_combos()
# Define combos for ardux
def setup_combos(self):
self.combo_module.combos = []
combo_enter = ArduxChord((KC.A, KC.E), KC.ENTER, ardux_keyboard=[self], layers=[0])
self.combo_module.combos.append(combo_enter)
combo_space = ArduxChord((KC.O, KC.I, KC.Y, KC.E), KC.SPACE, ardux_keyboard=[self], layers=[1])
self.combo_module.combos.append(combo_space)

65
main.py
View file

@ -1,15 +1,10 @@
import board import board
import os import os
from kb import KMKKeyboard
from kmk.keys import KC
keyboard = KMKKeyboard()
##### #####
# Enable debugging # Main keyboard object
if os.getenv('ARDUX_KMK_DEBUGGING'): from kb import ArduxKeyboard
keyboard.debug_enabled = True ardux_keyboard = ArduxKeyboard()
##### #####
# NeoPixel on kb2040 (tune accordingly / remove if different mcu) # NeoPixel on kb2040 (tune accordingly / remove if different mcu)
@ -19,60 +14,12 @@ rgb_ext = RGB(
num_pixels=1, num_pixels=1,
val_limit=100, val_limit=100,
val_default=25, val_default=25,
animation_mode=AnimationModes.RAINBOW animation_mode=AnimationModes.BREATHING_RAINBOW
) )
keyboard.extensions.append(rgb_ext) ardux_keyboard.extensions.append(rgb_ext)
#####
# Layers
from kmk.modules.layers import Layers
keyboard.modules.append(Layers())
#####
# Combos
from kmk.modules.combos import Combos, Chord
combos = Combos()
keyboard.modules.append(combos)
class KmNChord(Chord):
def __init__(
self,
match: Tuple[Union[Key, int], ...],
result: Key,
fast_reset=None,
per_key_timeout=None,
timeout=None,
match_coord=None,
keyboard=None,
layers=[],
):
super().__init__(match, result, fast_reset, per_key_timeout, match_coord)
self.keyboard = keyboard
self.layers = layers
def matches(self, key: Key, int_coord: int):
if keyboard is None or len(self.layers) == 0 or any(i in self.keyboard.active_layers for i in self.layers):
return super().matches(key, int_coord)
return False
combos.combos = []
combo_enter = KmNChord((KC.A, KC.E), KC.ENTER, keyboard=keyboard, layers=[0])
combos.combos.append(combo_enter)
combo_space = KmNChord((KC.O, KC.I, KC.Y, KC.E), KC.SPACE, keyboard=keyboard, layers=[1])
combos.combos.append(combo_space)
#####
# Keymap
keyboard.keymap = [
[KC.S, KC.T, KC.R, KC.A,
KC.O, KC.I, KC.Y, KC.E]
]
##### #####
# Main # Main
if __name__ == '__main__': if __name__ == '__main__':
keyboard.go() ardux_keyboard.go()