fix formatting/sorting

This commit is contained in:
BigTuna94 2022-05-02 19:58:47 -04:00 committed by Kyle Brown
parent d0c60a7694
commit 9898a1ed73
3 changed files with 481 additions and 73 deletions

View File

@ -32,9 +32,74 @@ class KMKKeyboard(_KMKKeyboard):
rgb_num_pixels = 70 rgb_num_pixels = 70
coord_mapping = [ coord_mapping = [
0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15, 0,
24, 17, 25, 18, 26, 19, 27, 20, 28, 21, 29, 22, 23, 31, 8,
40, 33, 41, 34, 42, 35, 43, 36, 44, 37, 45, 38, 46, 39, 47, 1,
56, 49, 57, 50, 58, 51, 59, 52, 60, 53, 61, 54, 55, 63, 9,
72, 65, 73, 74, 75, 76, 69, 77, 78, 71, 79, 2,
10,
3,
11,
4,
12,
5,
13,
6,
14,
7,
15,
24,
17,
25,
18,
26,
19,
27,
20,
28,
21,
29,
22,
23,
31,
40,
33,
41,
34,
42,
35,
43,
36,
44,
37,
45,
38,
46,
39,
47,
56,
49,
57,
50,
58,
51,
59,
52,
60,
53,
61,
54,
55,
63,
72,
65,
73,
74,
75,
76,
69,
77,
78,
71,
79,
] ]

View File

@ -1,9 +1,9 @@
import busio
from analogio import AnalogIn from analogio import AnalogIn
from supervisor import ticks_ms from supervisor import ticks_ms
from kmk.modules import Module from kmk.modules import Module
class BasePotentiometer: class BasePotentiometer:
def __init__(self, is_inverted=False): def __init__(self, is_inverted=False):
self.is_inverted = is_inverted self.is_inverted = is_inverted
@ -11,23 +11,24 @@ class BasePotentiometer:
self._direction = None self._direction = None
self._pos = 0 self._pos = 0
self._timestamp = ticks_ms() self._timestamp = ticks_ms()
# callback function on events. Needs to be defined externally # callback function on events. Needs to be defined externally
self.on_move_do = None self.on_move_do = None
def get_state(self): def get_state(self):
return { return {
'direction': self.is_inverted and -self._direction or self._direction, 'direction': self.is_inverted and -self._direction or self._direction,
'position': self.is_inverted and -self._pos or self._pos, 'position': self.is_inverted and -self._pos or self._pos,
} }
def get_pos(self): def get_pos(self):
"""Read from the analog pin assingned, truncate to 7 bits, '''
Read from the analog pin assingned, truncate to 7 bits,
average over 10 readings, and return a value 0-127 average over 10 readings, and return a value 0-127
""" '''
return int(sum([(self.read_pin.value >> 9) for i in range(10)]) / 10) return int(sum([(self.read_pin.value >> 9) for i in range(10)]) / 10)
def update_state(self): def update_state(self):
self._direction = 0 self._direction = 0
new_pos = self.get_pos() new_pos = self.get_pos()
if abs(new_pos - self._pos) > 2: if abs(new_pos - self._pos) > 2:
@ -39,7 +40,8 @@ class BasePotentiometer:
self._pos = new_pos self._pos = new_pos
if self.on_move_do is not None: if self.on_move_do is not None:
self.on_move_do(self.get_state()) self.on_move_do(self.get_state())
class GPIOPotentiometer(BasePotentiometer): class GPIOPotentiometer(BasePotentiometer):
def __init__(self, pin, move_callback, is_inverted=False): def __init__(self, pin, move_callback, is_inverted=False):
super().__init__(is_inverted) super().__init__(is_inverted)
@ -48,11 +50,12 @@ class GPIOPotentiometer(BasePotentiometer):
self.cb = move_callback self.cb = move_callback
self.on_move_do = lambda state: self.cb(state) self.on_move_do = lambda state: self.cb(state)
class PotentiometerHandler(Module): class PotentiometerHandler(Module):
def __init__(self): def __init__(self):
self.potentiometers = [] self.potentiometers = []
self.pins = None self.pins = None
def on_runtime_enable(self, keyboard): def on_runtime_enable(self, keyboard):
return return
@ -62,9 +65,9 @@ class PotentiometerHandler(Module):
def during_bootup(self, keyboard): def during_bootup(self, keyboard):
if self.pins: if self.pins:
for args in self.pins: for args in self.pins:
self.potentiometers.append( GPIOPotentiometer(*args) ) self.potentiometers.append(GPIOPotentiometer(*args))
return return
def before_matrix_scan(self, keyboard): def before_matrix_scan(self, keyboard):
''' '''
Return value will be injected as an extra matrix update Return value will be injected as an extra matrix update
@ -90,4 +93,4 @@ class PotentiometerHandler(Module):
return return
def on_powersave_disable(self, keyboard): def on_powersave_disable(self, keyboard):
return return

View File

@ -1,28 +1,30 @@
import board import board
import ulab.numpy as np
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.consumer_control import ConsumerControl
import usb_hid import usb_hid
import ulab.numpy as np
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from kb import KMKKeyboard from kb import KMKKeyboard
from kmk.keys import KC
from kmk.extensions.RGB import RGB, AnimationModes from kmk.extensions.RGB import RGB, AnimationModes
from kmk.modules.potentiometer import PotentiometerHandler from kmk.keys import KC
from kmk.modules.layers import Layers from kmk.modules.layers import Layers
from kmk.modules.midi import MidiKeys from kmk.modules.midi import MidiKeys
from kmk.modules.potentiometer import PotentiometerHandler
keyboard = KMKKeyboard() keyboard = KMKKeyboard()
keyboard.modules.append(Layers()) keyboard.modules.append(Layers())
keyboard.modules.append(MidiKeys()) keyboard.modules.append(MidiKeys())
rgb_ext = RGB( rgb_ext = RGB(
val_default=10, val_limit=100, # out of 255 val_default=10,
pixel_pin=keyboard.rgb_pixel_pin, num_pixels=keyboard.rgb_num_pixels, val_limit=100, # out of 255
refresh_rate=30, animation_speed=3, pixel_pin=keyboard.rgb_pixel_pin,
animation_mode=AnimationModes.STATIC) num_pixels=keyboard.rgb_num_pixels,
refresh_rate=30,
animation_speed=3,
animation_mode=AnimationModes.STATIC,
)
keyboard.extensions.append(rgb_ext) keyboard.extensions.append(rgb_ext)
_______ = KC.TRNS _______ = KC.TRNS
@ -42,6 +44,7 @@ MIDI = KC.TG(MIDI_LAYER_IDX)
RGB_TOG = KC.RGB_TOG RGB_TOG = KC.RGB_TOG
RAINBOW = KC.RGB_MODE_RAINBOW RAINBOW = KC.RGB_MODE_RAINBOW
def get_kb_rgb_obj(keyboard): def get_kb_rgb_obj(keyboard):
rgb = None rgb = None
for ext in keyboard.extensions: for ext in keyboard.extensions:
@ -50,6 +53,7 @@ def get_kb_rgb_obj(keyboard):
break break
return rgb return rgb
cc = ConsumerControl(usb_hid.devices) cc = ConsumerControl(usb_hid.devices)
keyboard.last_level = -1 keyboard.last_level = -1
@ -63,6 +67,7 @@ level_inc_step = 1
level_lut = [int(x) for x in np.linspace(0, level_steps, 64).tolist()] level_lut = [int(x) for x in np.linspace(0, level_steps, 64).tolist()]
def set_sys_vol(state): def set_sys_vol(state):
# convert to 0-100 # convert to 0-100
new_pos = int((state['position'] / 127) * 64) new_pos = int((state['position'] / 127) * 64)
@ -87,26 +92,30 @@ def set_sys_vol(state):
cmd = ConsumerControlCode.VOLUME_DECREMENT cmd = ConsumerControlCode.VOLUME_DECREMENT
# print(f"Setting system volume {vol_direction} by {level_diff} to reach {level}") # print(f"Setting system volume {vol_direction} by {level_diff} to reach {level}")
for i in range( int(level_diff / level_inc_step) ): for i in range(int(level_diff / level_inc_step)):
cc.send(cmd) cc.send(cmd)
keyboard.last_level = level keyboard.last_level = level
return return
# LEDs Color or animation speed # LEDs Color or animation speed
hue_lut = [int(x) for x in np.linspace(0, 360, 127).tolist()] hue_lut = [int(x) for x in np.linspace(0, 360, 127).tolist()]
def set_led_var(state): def set_led_var(state):
rgb = get_kb_rgb_obj(keyboard) rgb = get_kb_rgb_obj(keyboard)
if rgb is None: if rgb is None:
return return
if rgb.animation_mode == AnimationModes.STATIC: if rgb.animation_mode == AnimationModes.STATIC:
rgb.hue = hue_lut[ state['position'] ] rgb.hue = hue_lut[state['position']]
else: else:
rgb.animation_speed = int((state['position'] / 127) * 5) rgb.animation_speed = int((state['position'] / 127) * 5)
rgb._do_update() rgb._do_update()
return return
def set_led_brightness(state): def set_led_brightness(state):
rgb = get_kb_rgb_obj(keyboard) rgb = get_kb_rgb_obj(keyboard)
if rgb is None: if rgb is None:
@ -116,14 +125,17 @@ def set_led_brightness(state):
rgb._do_update() rgb._do_update()
return return
def slider_1_handler(state): def slider_1_handler(state):
set_sys_vol(state) set_sys_vol(state)
# reserve middle four values - software detent # reserve middle four values - software detent
pb_lut = [] pb_lut = []
pb_lut[00:29] = [int(x) for x in np.linspace(0, 8192, 30).tolist()] pb_lut[00:29] = [int(x) for x in np.linspace(0, 8192, 30).tolist()]
pb_lut[30:33] = [8192 for x in range(4)] # midpoint - no bend 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)] pb_lut[34:63] = [int(x) for x in np.linspace(8192, 8192 * 2, 30)]
def slider_2_handler(state): def slider_2_handler(state):
if keyboard.active_layers[0] == MIDI_LAYER_IDX: if keyboard.active_layers[0] == MIDI_LAYER_IDX:
@ -133,17 +145,21 @@ def slider_2_handler(state):
bend = pb_lut[bend_idx] bend = pb_lut[bend_idx]
key = KC.MIDI_PB(bend) key = KC.MIDI_PB(bend)
keyboard.tap_key(key) keyboard.tap_key(key)
else: else:
set_led_var(state) set_led_var(state)
keyboard.__midi_velocity = 0 keyboard.__midi_velocity = 0
def slider_3_handler(state): def slider_3_handler(state):
if keyboard.active_layers[0] == MIDI_LAYER_IDX: if keyboard.active_layers[0] == MIDI_LAYER_IDX:
# use as MIDI note velocity # use as MIDI note velocity
keyboard.__midi_velocity = int((state['position'] / 127) * 127) keyboard.__midi_velocity = int((state['position'] / 127) * 127)
else: else:
set_led_brightness(state) set_led_brightness(state)
faders = PotentiometerHandler() faders = PotentiometerHandler()
faders.pins = ( faders.pins = (
(board.RV1, slider_1_handler, False), (board.RV1, slider_1_handler, False),
@ -152,54 +168,378 @@ faders.pins = (
) )
keyboard.modules.append(faders) keyboard.modules.append(faders)
def set_midi_vel(key, keyboard, *args): def set_midi_vel(key, keyboard, *args):
key.meta.velocity = keyboard.__midi_velocity key.meta.velocity = keyboard.__midi_velocity
return True return True
def MN(note : str):
def MN(note: str):
midi_keypress = KC.MIDI_NOTE(note, 1) midi_keypress = KC.MIDI_NOTE(note, 1)
midi_keypress.before_press_handler(set_midi_vel) midi_keypress.before_press_handler(set_midi_vel)
return midi_keypress return midi_keypress
keyboard.keymap = [ keyboard.keymap = [
[ # Base Layer [ # 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.ESC,
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.GRAVE,
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.N1,
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, KC.N2,
FN1, KC.LGUI, KC.LALT, KC.SPC, KC.ENTER, KC.RGUI, KC.RALT, FN2, KC.LEFT, KC.DOWN, KC.RIGHT, 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,
], ],
[ # FN1 Layer
[ # FN1 Layer _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, RGB_TOG, RAINBOW, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
XXXXXXX, _______, _______, _______, _______, _______, _______, FN3, _______, _______, _______, _______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
RGB_TOG,
RAINBOW,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
XXXXXXX,
_______,
_______,
_______,
_______,
_______,
_______,
FN3,
_______,
_______,
_______,
], ],
[ # FN2 Layer
[ # 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 , _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC.F1,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC.F2,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC.F3,
FN3, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______, KC.F4,
KC.F5,
KC.F6,
KC.F7,
KC.F8,
KC.F9,
KC.F10,
KC.F11,
KC.F12,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
FN3,
_______,
_______,
_______,
_______,
_______,
_______,
XXXXXXX,
_______,
_______,
_______,
], ],
[ # FN3 Layer
[ # FN3 Layer _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, MIDI, _______, _______, _______, _______, _______, _______, _______,
XXXXXXX, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______, _______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
_______,
MIDI,
_______,
_______,
_______,
_______,
_______,
_______,
XXXXXXX,
_______,
_______,
_______,
_______,
_______,
_______,
XXXXXXX,
_______,
_______,
_______,
], ],
[ # MIDI Layer
[ # MIDI Layer MIDI,
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('E3'),
_______, MN('F3'), MN('G3'), MN('A3'), MN('B3'), MN('C4'), MN('D4'), MN('E4'), MN('F4'), MN('G4'), MN('A4'), MN('B4'), _______, _______, MN('F#3'),
_______, 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('G#3'),
_______, MN('B1'), MN('C2'), MN('D2'), MN('E2'), MN('F2'), MN('G2'), MN('A2'), MN('B2'), MN('C3'), MN('D3'), MN('E3'), _______, _______, MN('A#3'),
XXXXXXX, _______, _______, _______, _______, _______, _______, XXXXXXX, _______, _______, _______, 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,
_______,
_______,
_______,
], ],
] ]