kmk_firmware/user_keymaps/ZFR_KBD/RP2.65-F.py

214 lines
8.4 KiB
Python
Raw Normal View History

2022-03-31 02:49:12 +02:00
import board
2022-04-10 19:23:49 +02:00
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.consumer_control import ConsumerControl
import usb_hid
from kb import KMKKeyboard
from kmk.keys import KC
2022-04-10 21:44:12 +02:00
from kmk.extensions.RGB import RGB, AnimationModes
2022-03-31 02:49:12 +02:00
from kmk.modules.potentiometer import PotentiometerHandler
from kmk.modules.layers import Layers
2022-04-10 21:44:12 +02:00
from kmk.modules.midi import MidiKeys
keyboard = KMKKeyboard()
keyboard.modules.append(Layers())
2022-04-10 21:44:12 +02:00
keyboard.modules.append(MidiKeys())
2022-04-10 19:23:49 +02:00
rgb_ext = RGB(
val_default=10, val_limit=100, # out of 255
pixel_pin=keyboard.rgb_pixel_pin, num_pixels=keyboard.rgb_num_pixels,
refresh_rate=30, animation_speed=3,
animation_mode=AnimationModes.STATIC)
2022-03-31 00:29:38 +02:00
keyboard.extensions.append(rgb_ext)
2022-03-29 03:31:52 +02:00
_______ = KC.TRNS
XXXXXXX = KC.NO
2022-04-10 21:44:12 +02:00
BASE_LAYER_IDX = 0
FN1_LAYER_IDX = 1
FN2_LAYER_IDX = 2
FN3_LAYER_IDX = 3
MIDI_LAYER_IDX = 4
FN1 = KC.MO(FN1_LAYER_IDX)
FN2 = KC.MO(FN2_LAYER_IDX)
FN3 = KC.MO(FN3_LAYER_IDX)
MIDI = KC.TG(MIDI_LAYER_IDX)
2022-03-29 03:31:52 +02:00
2022-03-31 02:49:12 +02:00
RGB_TOG = KC.RGB_TOG
RAINBOW = KC.RGB_MODE_RAINBOW
2022-04-10 21:44:12 +02:00
def get_kb_rgb_obj(keyboard):
rgb = None
for ext in keyboard.extensions:
if type(ext) is RGB:
rgb = ext
break
return rgb
2022-04-10 19:23:49 +02:00
cc = ConsumerControl(usb_hid.devices)
# keyboard.level_lock = 0
keyboard.last_level = 0
level_steps = 17
2022-04-10 21:44:12 +02:00
def set_sys_vol(state):
# print(f"slider 1 state: {state}")
2022-04-10 19:23:49 +02:00
# if keyboard.level_lock == 1:
# return
2022-03-31 02:49:12 +02:00
2022-04-10 19:23:49 +02:00
# convert to 0-100
level = int((state['position'] / 127) * level_steps)
last_level = keyboard.last_level
# print(f"new vol level: {level}")
# print(f"last: {keyboard.last_level}")
level_diff = abs(last_level - level)
2022-04-10 21:44:12 +02:00
2022-04-10 19:23:49 +02:00
if level_diff > 1:
# keyboard.level_lock = 1
2022-04-10 21:44:12 +02:00
2022-04-10 19:23:49 +02:00
# print(f"Volume change: {level_diff}")
# set volume to new level
vol_direction = "unknown"
if state['direction'] == 1:
vol_direction = "up"
cmd = ConsumerControlCode.VOLUME_INCREMENT
else:
vol_direction = "down"
cmd = ConsumerControlCode.VOLUME_DECREMENT
2022-04-10 21:44:12 +02:00
2022-04-10 19:23:49 +02:00
# print(f"Setting system volume {vol_direction} by {level_diff}")
for i in range(level_diff):
cc.send(cmd)
keyboard.last_level = level
# keyboard.level_lock = 0
return
2022-04-10 21:44:12 +02:00
2022-04-10 19:23:49 +02:00
# LEDs Color or animation speed
2022-04-10 21:44:12 +02:00
def set_led_var(state):
rgb = get_kb_rgb_obj(keyboard)
2022-04-10 19:23:49 +02:00
if rgb is None:
return
2022-04-10 21:44:12 +02:00
2022-04-10 19:23:49 +02:00
if rgb.animation_mode == AnimationModes.STATIC:
rgb.hue = int((state['position'] / 127) * 359)
else:
rgb.animation_speed = int((state['position'] / 127) * 5)
rgb._do_update()
return
2022-04-10 21:44:12 +02:00
def set_led_brightness(state):
rgb = get_kb_rgb_obj(keyboard)
2022-04-10 19:23:49 +02:00
if rgb is None:
return
2022-03-31 02:49:12 +02:00
2022-04-10 19:23:49 +02:00
rgb.val = int((state['position'] / 127) * rgb.val_limit)
rgb._do_update()
return
2022-04-10 21:44:12 +02:00
def slider_1_handler(state):
set_sys_vol(state)
pb_lut = []
# reserve middle four values - software detent
pb_lut[00:29] = [x for x in range(0, 8192, int(8192/30))]
pb_lut[30:33] = [8192 for x in range(4)]
pb_lut[34:63] = [x for x in range(8192, 8192*2, int(8192/30))]
2022-04-10 21:44:12 +02:00
def slider_2_handler(state):
if keyboard.active_layers[0] == MIDI_LAYER_IDX:
# use as MIDI Pitch wheel
# use 64 values
bend_idx = int((state['position'] / 127) * 64)
# scale to 8192 midpoint for no bends
# bend *= int(8192 * 2 / 64)
bend = pb_lut[bend_idx]
2022-04-10 22:18:09 +02:00
key = KC.MIDI_PB(bend)
2022-04-10 21:44:12 +02:00
keyboard.tap_key(key)
2022-04-10 21:44:12 +02:00
else:
set_led_var(state)
2022-04-10 22:18:09 +02:00
keyboard.__midi_velocity = 0
2022-04-10 21:44:12 +02:00
def slider_3_handler(state):
if keyboard.active_layers[0] == MIDI_LAYER_IDX:
# use as MIDI note velocity
keyboard.__midi_velocity = int((state['position'] / 127) * 127)
2022-04-10 21:44:12 +02:00
else:
set_led_brightness(state)
2022-04-10 19:23:49 +02:00
faders = PotentiometerHandler()
faders.pins = (
2022-03-31 02:49:12 +02:00
(board.RV1, slider_1_handler, False),
2022-04-10 19:23:49 +02:00
(board.RV2, slider_2_handler),
(board.RV3, slider_3_handler),
2022-03-31 02:49:12 +02:00
)
keyboard.modules.append(faders)
def set_midi_vel(key, keyboard, *args):
key.meta.velocity = keyboard.__midi_velocity
return True
2022-03-31 02:49:12 +02:00
def MN(note : str):
midi_keypress = KC.MIDI_NOTE(note, 1)
midi_keypress.before_press_handler(set_midi_vel)
return midi_keypress
keyboard.keymap = [
2022-03-29 03:31:52 +02:00
[ # Base Layer
2022-04-10 19:23:49 +02:00
KC.ESC, KC.GRAVE, KC.N1, KC.N2, KC.N3, KC.N4, KC.N5, KC.N6, KC.N7, KC.N8, KC.N9, KC.N0, KC.BSLS, KC.DEL, KC.MINS, KC.EQUAL,
2022-03-29 03:31:52 +02:00
KC.TAB, KC.Q, KC.W, KC.E, KC.R, KC.T, KC.Y, KC.U, KC.I, KC.O, KC.P, KC.BACKSPACE, KC.LBRC, KC.RBRC,
KC.LCTRL, KC.A, KC.S, KC.D, KC.F, KC.G, KC.H, KC.J, KC.K, KC.L, KC.SCLN, KC.QUOT, KC.ENTER, KC.QUOT, KC.HOME,
KC.LSFT, KC.Z, KC.X, KC.C, KC.V, KC.B, KC.N, KC.M, KC.COMM, KC.DOT, KC.SLSH, KC.RSFT, KC.UP, KC.END,
FN1, KC.LGUI, KC.LALT, KC.SPC, KC.ENTER, KC.RGUI, KC.RALT, FN2, KC.LEFT, KC.DOWN, KC.RIGHT,
],
2022-04-10 21:44:12 +02:00
2022-03-29 03:31:52 +02:00
[ # FN1 Layer
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
2022-03-31 02:49:12 +02:00
_______, _______, _______, RGB_TOG, RAINBOW, _______, _______, _______, _______, _______, _______, _______, _______, _______,
2022-03-29 03:31:52 +02:00
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
2022-04-10 22:18:09 +02:00
XXXXXXX, _______, _______, _______, _______, _______, _______, FN3, _______, _______, _______,
2022-03-29 03:31:52 +02:00
],
2022-04-10 21:44:12 +02:00
2022-03-29 03:31:52 +02:00
[ # FN2 Layer
2022-04-10 19:23:49 +02:00
_______, _______, KC.F1 , KC.F2 , KC.F3 , KC.F4 , KC.F5 , KC.F6 , KC.F7 , KC.F8 , KC.F9 , KC.F10 , KC.F11 , KC.F12 , _______, _______,
2022-03-29 03:31:52 +02:00
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
FN3, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______,
],
2022-04-10 21:44:12 +02:00
2022-03-29 03:31:52 +02:00
[ # FN3 Layer
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, MIDI, _______, _______, _______, _______, _______, _______,
XXXXXXX, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______,
],
2022-04-10 21:44:12 +02:00
2022-03-29 03:31:52 +02:00
[ # MIDI Layer
2022-04-10 21:44:12 +02:00
MIDI, MN('E3'), MN('F#3'),MN('G#3'),MN('A#3'),XXXXXXX, MN('C#4'),MN('D#4'),XXXXXXX, MN('F#4'),MN('G#4'),MN('A#4'),MN('C5'),MN('C#5'), _______, _______,
_______, MN('F3'), MN('G3'), MN('A3'), MN('B3'), MN('C4'), MN('D4'), MN('E4'), MN('F4'), MN('G4'), MN('A4'), MN('B4'), _______, _______,
2022-04-10 22:18:09 +02:00
_______, XXXXXXX, MN('C#2'),MN('D#2'),XXXXXXX, MN('F#2'),MN('G#2'),MN('A#2'),XXXXXXX, MN('C#3'),MN('D#3'),XXXXXXX, _______, _______, _______,
2022-04-10 21:44:12 +02:00
_______, MN('B1'), MN('C2'), MN('D2'), MN('E2'), MN('F2'), MN('G2'), MN('A2'), MN('B2'), MN('C3'), MN('D3'), MN('E3'), _______, _______,
2022-03-29 03:31:52 +02:00
XXXXXXX, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______,
],
]
2022-04-10 19:23:49 +02:00
# keyboard.debug_enabled = True
2022-03-31 00:29:38 +02:00
if __name__ == '__main__':
keyboard.go()