From a4d6a44a04f63a63a521b2387fbd056c4d98b56c Mon Sep 17 00:00:00 2001 From: BigTuna94 Date: Wed, 4 May 2022 20:29:28 -0400 Subject: [PATCH] pythonify Potentiometer state --- kmk/modules/potentiometer.py | 16 +++++++++++----- user_keymaps/ZFR_KBD/RP2.65-F.py | 12 ++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kmk/modules/potentiometer.py b/kmk/modules/potentiometer.py index 94f8d84..03a3dc7 100644 --- a/kmk/modules/potentiometer.py +++ b/kmk/modules/potentiometer.py @@ -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): ''' diff --git a/user_keymaps/ZFR_KBD/RP2.65-F.py b/user_keymaps/ZFR_KBD/RP2.65-F.py index d0e0d6d..bf22fe8 100644 --- a/user_keymaps/ZFR_KBD/RP2.65-F.py +++ b/user_keymaps/ZFR_KBD/RP2.65-F.py @@ -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)