pythonify Potentiometer state
This commit is contained in:
parent
b97fca7c76
commit
a4d6a44a04
@ -4,6 +4,12 @@ from supervisor import ticks_ms
|
|||||||
from kmk.modules import Module
|
from kmk.modules import Module
|
||||||
|
|
||||||
|
|
||||||
|
class PotentiometerState:
|
||||||
|
def __init__(self, direction: int, position: int):
|
||||||
|
self.direction = direction
|
||||||
|
self.position = position
|
||||||
|
|
||||||
|
|
||||||
class Potentiometer:
|
class Potentiometer:
|
||||||
def __init__(self, pin, move_callback, is_inverted=False):
|
def __init__(self, pin, move_callback, is_inverted=False):
|
||||||
self.is_inverted = is_inverted
|
self.is_inverted = is_inverted
|
||||||
@ -16,11 +22,11 @@ class Potentiometer:
|
|||||||
# callback function on events.
|
# callback function on events.
|
||||||
self.on_move_do = lambda state: self.cb(state)
|
self.on_move_do = lambda state: self.cb(state)
|
||||||
|
|
||||||
def get_state(self):
|
def get_state(self) -> PotentiometerState:
|
||||||
return {
|
return PotentiometerState(
|
||||||
'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):
|
||||||
'''
|
'''
|
||||||
|
@ -70,7 +70,7 @@ 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)
|
||||||
level = level_lut[new_pos]
|
level = level_lut[new_pos]
|
||||||
# print(f"new vol level: {level}")
|
# print(f"new vol level: {level}")
|
||||||
# print(f"last: {keyboard.last_level}")
|
# print(f"last: {keyboard.last_level}")
|
||||||
@ -112,9 +112,9 @@ def set_led_var(state):
|
|||||||
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
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ def set_led_brightness(state):
|
|||||||
if rgb is None:
|
if rgb is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
rgb.val = int((state['position'] / 127) * rgb.val_limit)
|
rgb.val = int((state.position / 127) * rgb.val_limit)
|
||||||
rgb._do_update()
|
rgb._do_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -144,7 +144,7 @@ def slider_2_handler(state):
|
|||||||
if keyboard.active_layers[0] == MIDI_LAYER_IDX:
|
if keyboard.active_layers[0] == MIDI_LAYER_IDX:
|
||||||
# use as MIDI Pitch wheel
|
# use as MIDI Pitch wheel
|
||||||
# use 64 values
|
# use 64 values
|
||||||
bend_idx = int((state['position'] / 127) * 64)
|
bend_idx = int((state.position / 127) * 64)
|
||||||
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)
|
||||||
@ -158,7 +158,7 @@ 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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user