Black formatting for lint test

This commit is contained in:
elric91 2021-09-19 10:48:35 +02:00 committed by Kyle Brown
parent 7993a4e415
commit 484bcc4d22

View File

@ -20,30 +20,31 @@
import board
import digitalio
from kmk.modules import Module
# NB : not using rotaryio as it requires the pins to be consecutive
class Encoder:
_debug = False
_debug_counter = 0
STATES = {
# old_pos_a, old_pos_b, new_pos_a, new_pos_b
# -1 : Left ; 1 : Right ; 0 : we don't care
((True, True), (True, False)): -1,
((True, True), (False, True)): 1,
((True, False), (False, False)): -1,
((True, False), (True, True)): 0,
((False, True), (False, False)): 1,
((False, True), (True, True)): 0,
((False, False), (True, False)): 0,
((False, False), (False, True)): 0,
((False, False), (True, True)): 0,
((False, True), (True, False)): 0,
((True, False), (False, True)): 0,
((True, True), (False, False)): 0,
}
# old_pos_a, old_pos_b, new_pos_a, new_pos_b
# -1 : Left ; 1 : Right ; 0 : we don't care
((True, True), (True, False)): -1,
((True, True), (False, True)): 1,
((True, False), (False, False)): -1,
((True, False), (True, True)): 0,
((False, True), (False, False)): 1,
((False, True), (True, True)): 0,
((False, False), (True, False)): 0,
((False, False), (False, True)): 0,
((False, False), (True, True)): 0,
((False, True), (True, False)): 0,
((True, False), (False, True)): 0,
((True, True), (False, False)): 0,
}
def __init__(self, pin_a, pin_b, pin_button=None, is_inverted=False):
self.pin_a = EncoderPin(pin_a)
@ -61,29 +62,34 @@ class Encoder:
self.on_move_do = None
self.on_button_do = None
def get_state(self):
return({'direction':self.is_inverted and -self._actual_direction or self._actual_direction,
'position':self.is_inverted and -self._actual_pos or self._actual_pos,
'is_pressed':not self._actual_button_state})
return {
'direction': self.is_inverted
and -self._actual_direction
or self._actual_direction,
'position': self.is_inverted and -self._actual_pos or self._actual_pos,
'is_pressed': not self._actual_button_state,
}
# to be called in a loop
def update_state(self):
# Rotation events
new_state = (self.pin_a.get_value(), self.pin_b.get_value())
if new_state != self._actual_state:
if self._debug : print(" ", new_state)
if self._debug:
print(" ", new_state)
self._movement_counter += 1
new_direction = self.STATES[(self._actual_state, new_state)]
if new_direction != 0:
self._actual_direction = new_direction
# when the encoder settles on a position
if new_state == (True, True) and self._movement_counter > 2 : # if < 2 state changes, it is a misstep
if (
new_state == (True, True) and self._movement_counter > 2
): # if < 2 state changes, it is a misstep
self._movement_counter = 0
self._actual_pos += self._actual_direction
if self._debug :
if self._debug:
self._debug_counter += 1
print(self._debug_counter, self.get_state())
if self.on_move_do is not None:
@ -99,9 +105,7 @@ class Encoder:
self.on_button_do(self.get_state())
class EncoderPin:
def __init__(self, pin, button_type=False):
self.pin = pin
self.button_type = button_type
@ -120,7 +124,6 @@ class EncoderPin:
class EncoderHandler(Module):
def __init__(self):
self.encoders = []
self.pins = None
@ -135,12 +138,12 @@ class EncoderHandler(Module):
def during_bootup(self, keyboard):
if self.pins and self.map:
for idx, pins in enumerate(self.pins):
gpio_pins = pins[:3]
new_encoder = Encoder(*gpio_pins)
# In our case, we need to fix keybord and encoder_id for the callback
new_encoder.on_move_do = lambda x: self.on_move_do(keyboard, idx, x)
new_encoder.on_button_do = lambda x: self.on_button_do(keyboard, idx, x)
self.encoders.append(new_encoder)
gpio_pins = pins[:3]
new_encoder = Encoder(*gpio_pins)
# In our case, we need to fix keybord and encoder_id for the callback
new_encoder.on_move_do = lambda x: self.on_move_do(keyboard, idx, x)
new_encoder.on_button_do = lambda x: self.on_button_do(keyboard, idx, x)
self.encoders.append(new_encoder)
return
def on_move_do(self, keyboard, encoder_id, state):
@ -150,7 +153,7 @@ class EncoderHandler(Module):
if state['direction'] == -1:
key_index = 0
else:
key_index =1
key_index = 1
key = self.map[layer_id][encoder_id][key_index]
keyboard.tap_key(key)
@ -160,7 +163,6 @@ class EncoderHandler(Module):
key = self.map[layer_id][encoder_id][2]
keyboard.tap_key(key)
def before_matrix_scan(self, keyboard):
'''
Return value will be injected as an extra matrix update
@ -186,4 +188,4 @@ class EncoderHandler(Module):
return
def on_powersave_disable(self, keyboard):
return
return