cleanup and fixes

This commit is contained in:
Zach Richard 2022-04-09 12:23:44 -04:00 committed by Kyle Brown
parent 9b1976ec3e
commit 52b0ff5ca1

View File

@ -5,15 +5,15 @@ 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
self.read_pin = None self.read_pin = None
self._state = None
self._direction = None self._direction = None
self._pos = 0.0 self._pos = 0
self._timestamp = ticks_ms() self._timestamp = ticks_ms()
self._truncate_bits = 6
# 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
@ -21,30 +21,35 @@ class BasePotentiometer:
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,
'velocity': self._velocity,
} }
def update_state(self): def get_pos(self):
new_state = self.read_pin.value
self._direction = 0
if new_state != self._state:
# movement detected!
# Debounce... # Debounce...
# AnalogRead always reports 16 bit values - truncate to 6 to de-noise
# convert to percentage and round to quarter of a percent
# AnalogRead always reports 16 bit values # readings = [(self.read_pin.value >> (16 - self._truncate_bits)) for i in range(3)]
# convert to percentage and round to hundreths # reading = sum(readings) / len(readings)
new_pos = round(new_state / 0xFFFF, 2)
if new_pos != self._pos:
self._pos = new_pos
if new_state > self._state: reading = self.read_pin.value >> 10
dec_val = reading / (pow(2, self._truncate_bits) - 1)
# new_pos = round(dec_val * 4, 1) / 4
new_pos = round(dec_val, 2)
return int(new_pos * 127)
def update_state(self):
self._direction = 0
new_pos = self.get_pos()
if abs(new_pos - self._pos) > 2:
# movement detected!
if new_pos > self._pos:
self._direction = 1 self._direction = 1
else: else:
self._direction = -1 self._direction = -1
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())
@ -52,11 +57,9 @@ 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)
self.read_pin = AnalogIn(pin) self.read_pin = AnalogIn(pin)
self.update_state() self._pos = self.get_pos()
self.cb = move_callback self.cb = move_callback
self.on_move_do = lambda state: self.cb(state)
def on_move_do(self, state):
self.cb(state)
class PotentiometerHandler(Module): class PotentiometerHandler(Module):
def __init__(self): def __init__(self):
@ -71,7 +74,7 @@ class PotentiometerHandler(Module):
def during_bootup(self, keyboard): def during_bootup(self, keyboard):
if self.pins: if self.pins:
for args in enumerate(self.pins): for args in self.pins:
self.potentiometers.append( GPIOPotentiometer(*args) ) self.potentiometers.append( GPIOPotentiometer(*args) )
return return
@ -79,8 +82,8 @@ class PotentiometerHandler(Module):
''' '''
Return value will be injected as an extra matrix update Return value will be injected as an extra matrix update
''' '''
for encoder in self.encoders: for potentiometer in self.potentiometers:
encoder.update_state() potentiometer.update_state()
return keyboard return keyboard