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

231 lines
8.5 KiB
Python
Raw Normal View History

2022-03-31 02:49:12 +02:00
import board
2022-05-03 01:58:47 +02:00
import usb_hid
2022-03-31 02:49:12 +02:00
import ulab.numpy as np
from kb import KMKKeyboard
2022-05-04 04:12:55 +02:00
from kmk.extensions.media_keys import MediaKeys
2022-04-10 21:44:12 +02:00
from kmk.extensions.RGB import RGB, AnimationModes
2022-05-03 01:58:47 +02:00
from kmk.keys import KC
from kmk.modules.layers import Layers
2022-04-10 21:44:12 +02:00
from kmk.modules.midi import MidiKeys
2022-05-03 01:58:47 +02:00
from kmk.modules.potentiometer import PotentiometerHandler
keyboard = KMKKeyboard()
keyboard.modules.append(Layers())
2022-04-10 21:44:12 +02:00
keyboard.modules.append(MidiKeys())
2022-05-04 04:12:55 +02:00
2022-04-10 19:23:49 +02:00
rgb_ext = RGB(
2022-05-03 01:58:47 +02:00
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-05-04 04:12:55 +02:00
keyboard.extensions.append(MediaKeys())
2022-03-31 00:29:38 +02:00
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-05-03 01:58:47 +02:00
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-05-03 01:58:47 +02:00
keyboard.last_level = -1
# Gnome in Linux
2022-04-10 19:23:49 +02:00
level_steps = 17
level_inc_step = 1
2022-04-10 19:23:49 +02:00
# Windows 10
# level_steps = 100
# level_inc_step = 1
2022-04-10 21:44:12 +02:00
level_lut = [int(x) for x in np.linspace(0, level_steps, 64).tolist()]
2022-03-31 02:49:12 +02:00
2022-05-03 01:58:47 +02:00
def set_sys_vol(state):
2022-04-10 19:23:49 +02:00
# convert to 0-100
2022-05-05 02:29:28 +02:00
new_pos = int((state.position / 127) * 64)
level = level_lut[new_pos]
2022-04-10 19:23:49 +02:00
# print(f"new vol level: {level}")
# print(f"last: {keyboard.last_level}")
# check if uninitialized
if keyboard.last_level == -1:
keyboard.last_level = level
return
2022-04-10 19:23:49 +02:00
level_diff = abs(keyboard.last_level - level)
if level_diff > 0:
2022-04-10 19:23:49 +02:00
# set volume to new level
# vol_direction = "unknown"
if level > keyboard.last_level:
# vol_direction = "up"
2022-05-04 04:12:55 +02:00
cmd = KC.VOLU
2022-04-10 19:23:49 +02:00
else:
# vol_direction = "down"
2022-05-04 04:12:55 +02:00
cmd = KC.VOLD
2022-04-10 21:44:12 +02:00
# print(f"Setting system volume {vol_direction} by {level_diff} to reach {level}")
2022-05-03 01:58:47 +02:00
for i in range(int(level_diff / level_inc_step)):
2022-05-04 04:12:55 +02:00
hid_report = keyboard._hid_helper.create_report([cmd])
hid_report.send()
hid_report.clear_all()
hid_report.send()
2022-04-10 19:23:49 +02:00
keyboard.last_level = level
return
2022-05-03 01:58:47 +02:00
2022-04-10 19:23:49 +02:00
# LEDs Color or animation speed
hue_lut = [int(x) for x in np.linspace(0, 360, 127).tolist()]
2022-05-03 01:58:47 +02:00
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:
2022-05-05 02:29:28 +02:00
rgb.hue = hue_lut[state.position]
2022-04-10 19:23:49 +02:00
else:
2022-05-05 02:29:28 +02:00
rgb.animation_speed = int((state.position / 127) * 5)
2022-04-10 19:23:49 +02:00
rgb._do_update()
return
2022-05-03 01:58:47 +02:00
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-05-05 02:29:28 +02:00
rgb.val = int((state.position / 127) * rgb.val_limit)
2022-04-10 19:23:49 +02:00
rgb._do_update()
return
2022-05-03 01:58:47 +02:00
2022-04-10 21:44:12 +02:00
def slider_1_handler(state):
2022-05-03 01:58:47 +02:00
set_sys_vol(state)
# reserve middle four values - software detent
pb_lut = []
pb_lut[00:29] = [int(x) for x in np.linspace(0, 8192, 30).tolist()]
2022-05-03 01:58:47 +02:00
pb_lut[30:33] = [8192 for x in range(4)] # midpoint - no bend
pb_lut[34:63] = [int(x) for x in np.linspace(8192, 8192 * 2, 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
2022-05-05 02:29:28 +02:00
bend_idx = int((state.position / 127) * 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-05-03 01:58:47 +02:00
else:
2022-04-10 21:44:12 +02:00
set_led_var(state)
2022-05-03 01:58:47 +02:00
2022-04-10 22:18:09 +02:00
keyboard.__midi_velocity = 0
2022-05-03 01:58:47 +02:00
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
2022-05-05 02:29:28 +02:00
keyboard.__midi_velocity = int((state.position / 127) * 127)
2022-05-03 01:58:47 +02:00
else:
2022-04-10 21:44:12 +02:00
set_led_brightness(state)
2022-05-03 01:58:47 +02:00
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)
2022-05-03 01:58:47 +02:00
def set_midi_vel(key, keyboard, *args):
key.meta.velocity = keyboard.__midi_velocity
return True
2022-03-31 02:49:12 +02:00
2022-05-03 01:58:47 +02:00
def MN(note: str):
midi_keypress = KC.MIDI_NOTE(note, 1)
midi_keypress.before_press_handler(set_midi_vel)
return midi_keypress
2022-05-03 01:58:47 +02:00
keyboard.keymap = [
2022-05-04 00:26:18 +02:00
[ # Base Layer
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,
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-03-29 03:31:52 +02:00
],
2022-05-04 00:26:18 +02:00
[ # FN1 Layer
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, RGB_TOG, RAINBOW, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
XXXXXXX, _______, _______, _______, _______, _______, _______, FN3, _______, _______, _______,
2022-03-29 03:31:52 +02:00
],
2022-05-04 00:26:18 +02:00
[ # FN2 Layer
_______, _______, KC.F1 , KC.F2 , KC.F3 , KC.F4 , KC.F5 , KC.F6 , KC.F7 , KC.F8 , KC.F9 , KC.F10 , KC.F11 , KC.F12 , _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
FN3, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______,
],
2022-05-04 00:26:18 +02:00
[ # FN3 Layer
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, MIDI, _______, _______, _______, _______, _______, _______,
XXXXXXX, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______,
2022-03-29 03:31:52 +02:00
],
2022-05-04 00:26:18 +02:00
[ # MIDI Layer
2022-05-04 00:55:31 +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'), _______, _______,
_______, 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, _______, _______, _______,
_______, MN('B1'), MN('C2'), MN('D2'), MN('E2'), MN('F2'), MN('G2'), MN('A2'), MN('B2'), MN('C3'), MN('D3'), MN('E3'), _______, _______,
XXXXXXX, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______,
2022-03-29 03:31:52 +02:00
],
]
2022-03-31 00:29:38 +02:00
if __name__ == '__main__':
keyboard.go()