kmk_firmware/kmk/modules/potentiometer.py

106 lines
3.0 KiB
Python
Raw Normal View History

2022-03-31 02:49:12 +02:00
import busio
from analogio import AnalogIn
from supervisor import ticks_ms
from kmk.modules import Module
class BasePotentiometer:
def __init__(self, is_inverted=False):
self.is_inverted = is_inverted
self.read_pin = None
self._direction = None
2022-04-09 18:23:44 +02:00
self._pos = 0
2022-03-31 02:49:12 +02:00
self._timestamp = ticks_ms()
2022-04-09 18:23:44 +02:00
self._truncate_bits = 6
2022-03-31 02:49:12 +02:00
# callback function on events. Needs to be defined externally
self.on_move_do = None
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,
}
2022-04-09 18:23:44 +02:00
def get_pos(self):
# Debounce...
# AnalogRead always reports 16 bit values - truncate to 6 to de-noise
# convert to percentage and round to quarter of a percent
2022-03-31 02:49:12 +02:00
readings = [(self.read_pin.value >> (16 - self._truncate_bits)) for i in range(3)]
reading = sum(readings) / len(readings)
2022-04-09 18:23:44 +02:00
2022-03-31 02:49:12 +02:00
# reading = self.read_pin.value >> 10
2022-04-09 18:23:44 +02:00
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:
2022-03-31 02:49:12 +02:00
# movement detected!
2022-04-09 18:23:44 +02:00
if new_pos > self._pos:
self._direction = 1
else:
self._direction = -1
self._pos = new_pos
if self.on_move_do is not None:
self.on_move_do(self.get_state())
2022-03-31 02:49:12 +02:00
class GPIOPotentiometer(BasePotentiometer):
2022-04-09 18:23:44 +02:00
def __init__(self, pin, move_callback, is_inverted=False):
2022-03-31 02:49:12 +02:00
super().__init__(is_inverted)
self.read_pin = AnalogIn(pin)
2022-04-09 18:23:44 +02:00
self._pos = self.get_pos()
2022-03-31 02:49:12 +02:00
self.cb = move_callback
2022-04-09 18:23:44 +02:00
self.on_move_do = lambda state: self.cb(state)
2022-03-31 02:49:12 +02:00
class PotentiometerHandler(Module):
def __init__(self):
self.potentiometers = []
self.pins = None
def on_runtime_enable(self, keyboard):
return
def on_runtime_disable(self, keyboard):
return
def during_bootup(self, keyboard):
if self.pins:
2022-04-09 18:23:44 +02:00
for args in self.pins:
2022-03-31 02:49:12 +02:00
self.potentiometers.append( GPIOPotentiometer(*args) )
return
def before_matrix_scan(self, keyboard):
'''
Return value will be injected as an extra matrix update
'''
2022-04-09 18:23:44 +02:00
for potentiometer in self.potentiometers:
potentiometer.update_state()
2022-03-31 02:49:12 +02:00
return keyboard
def after_matrix_scan(self, keyboard):
'''
Return value will be replace matrix update if supplied
'''
return
def before_hid_send(self, keyboard):
return
def after_hid_send(self, keyboard):
return
def on_powersave_enable(self, keyboard):
return
def on_powersave_disable(self, keyboard):
return