pythonify Potentiometer state

This commit is contained in:
BigTuna94 2022-05-04 20:29:28 -04:00 committed by Kyle Brown
parent b97fca7c76
commit a4d6a44a04
2 changed files with 17 additions and 11 deletions

View File

@ -4,6 +4,12 @@ from supervisor import ticks_ms
from kmk.modules import Module
class PotentiometerState:
def __init__(self, direction: int, position: int):
self.direction = direction
self.position = position
class Potentiometer:
def __init__(self, pin, move_callback, is_inverted=False):
self.is_inverted = is_inverted
@ -16,11 +22,11 @@ class Potentiometer:
# callback function on events.
self.on_move_do = lambda state: self.cb(state)
def get_state(self):
return {
'direction': self.is_inverted and -self._direction or self._direction,
'position': self.is_inverted and -self._pos or self._pos,
}
def get_state(self) -> PotentiometerState:
return PotentiometerState(
direction=(self.is_inverted and -self._direction or self._direction),
position=(self.is_inverted and -self._pos or self._pos),
)
def get_pos(self):
'''

View File

@ -70,7 +70,7 @@ level_lut = [int(x) for x in np.linspace(0, level_steps, 64).tolist()]
def set_sys_vol(state):
# convert to 0-100
new_pos = int((state['position'] / 127) * 64)
new_pos = int((state.position / 127) * 64)
level = level_lut[new_pos]
# print(f"new vol level: {level}")
# print(f"last: {keyboard.last_level}")
@ -112,9 +112,9 @@ def set_led_var(state):
return
if rgb.animation_mode == AnimationModes.STATIC:
rgb.hue = hue_lut[state['position']]
rgb.hue = hue_lut[state.position]
else:
rgb.animation_speed = int((state['position'] / 127) * 5)
rgb.animation_speed = int((state.position / 127) * 5)
rgb._do_update()
return
@ -124,7 +124,7 @@ def set_led_brightness(state):
if rgb is None:
return
rgb.val = int((state['position'] / 127) * rgb.val_limit)
rgb.val = int((state.position / 127) * rgb.val_limit)
rgb._do_update()
return
@ -144,7 +144,7 @@ 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)
bend_idx = int((state.position / 127) * 64)
bend = pb_lut[bend_idx]
key = KC.MIDI_PB(bend)
keyboard.tap_key(key)
@ -158,7 +158,7 @@ keyboard.__midi_velocity = 0
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)
keyboard.__midi_velocity = int((state.position / 127) * 127)
else:
set_led_brightness(state)