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