2022-02-25 20:03:36 +00:00
|
|
|
"""Module containing an implementation for reading the rotary encoder directions through
|
|
|
|
the i2c multiplexer + interrupt"""
|
2024-03-22 07:19:13 +00:00
|
|
|
|
2022-02-25 20:03:36 +00:00
|
|
|
from ctrlboard_hw.hardware_button import HardwareButton
|
|
|
|
|
|
|
|
|
|
|
|
class Encoder:
|
|
|
|
"""Class implementing a detection mechanism to detect the rotary encoder directions
|
|
|
|
through the i2c multiplexer + interrupt"""
|
2022-11-30 05:19:17 +00:00
|
|
|
|
2022-02-25 20:03:36 +00:00
|
|
|
def __init__(self, enc_a: HardwareButton, enc_b: HardwareButton):
|
|
|
|
self.enc_a = enc_a
|
|
|
|
self.enc_b = enc_b
|
|
|
|
self.pos = 0
|
2022-03-02 02:27:14 +00:00
|
|
|
self.state = 0b00000000
|
2022-02-25 20:03:36 +00:00
|
|
|
self.direction = 0
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Updates the internal attributes wrt. to the encoder position and direction."""
|
|
|
|
value_enc_a = self.enc_a.state_interrupt
|
|
|
|
value_enc_b = self.enc_b.state_interrupt
|
|
|
|
|
|
|
|
self.direction = 0
|
2022-03-02 02:27:14 +00:00
|
|
|
state = self.state & 0b00111111
|
2022-02-25 20:03:36 +00:00
|
|
|
|
|
|
|
if value_enc_a:
|
2022-03-02 02:27:14 +00:00
|
|
|
state |= 0b01000000
|
2022-02-25 20:03:36 +00:00
|
|
|
if value_enc_b:
|
2022-03-02 02:27:14 +00:00
|
|
|
state |= 0b10000000
|
2022-02-25 20:03:36 +00:00
|
|
|
|
2022-03-02 02:27:14 +00:00
|
|
|
# clockwise pattern detection
|
2022-11-30 05:19:17 +00:00
|
|
|
if (
|
|
|
|
state == 0b11010010
|
|
|
|
or state == 0b11001000
|
|
|
|
or state == 0b11011000
|
|
|
|
or state == 0b11010001
|
|
|
|
or state == 0b11011011
|
|
|
|
or state == 0b11100000
|
|
|
|
or state == 0b11001011
|
|
|
|
):
|
2022-02-25 20:03:36 +00:00
|
|
|
self.pos += 1
|
|
|
|
self.direction = 1
|
2022-03-02 02:27:14 +00:00
|
|
|
self.state = 0b00000000
|
2022-02-25 20:03:36 +00:00
|
|
|
return
|
2022-03-02 02:27:14 +00:00
|
|
|
# counter-clockwise pattern detection
|
2022-11-30 05:19:17 +00:00
|
|
|
elif (
|
|
|
|
state == 0b11000100
|
|
|
|
or state == 0b11100100
|
|
|
|
or state == 0b11100001
|
|
|
|
or state == 0b11000111
|
|
|
|
or state == 0b11100111
|
|
|
|
):
|
2022-02-25 20:03:36 +00:00
|
|
|
self.pos -= 1
|
|
|
|
self.direction = -1
|
2022-03-02 02:27:14 +00:00
|
|
|
self.state = 0b00000000
|
|
|
|
return
|
2022-02-25 20:03:36 +00:00
|
|
|
|
2022-03-02 02:27:14 +00:00
|
|
|
self.state = state >> 2
|