Skip to content

encoder

Encoder utility.

Classes:

Name Description
Encoder

Convert encoder value to integer.

Functions:

Name Description
test_encoder

Test for the encoder class.

Encoder

Encoder(track_1: int, track_2: int)

Convert encoder value to integer.

Parameters:

Name Type Description Default
track_1 int

The pin the track_1 of the encoder is connected to.

required
track_2 int

The pin the track_2 of the encoder is connected to.

required

Methods:

Name Description
get_rotate

Get the rotary encoder value.

reset_rotate

Reset rotary encoder value.

rotate_changed

Indicate if the encoder value changed since last call to this method.

Source code in src/aalec/encoder.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __init__(self, track_1: int, track_2: int) -> None:
    self._pin_track_1 = machine.Pin(track_1, machine.Pin.IN, machine.Pin.PULL_UP)
    self._pin_track_2 = machine.Pin(track_2, machine.Pin.IN, machine.Pin.PULL_UP)

    self._pin_track_1.irq(
        self._encoder_isr, trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING
    )
    self._pin_track_2.irq(
        self._encoder_isr, trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING
    )

    self._z: int = 0
    self._n: int = 0
    self._value: int = 0
    self._old_value: int = 0

get_rotate

get_rotate() -> int

Get the rotary encoder value.

Returns:

Name Type Description
int int

Value of the rotary encoder.

Source code in src/aalec/encoder.py
56
57
58
59
60
61
62
def get_rotate(self) -> int:
    """Get the rotary encoder value.

    Returns:
        int: Value of the rotary encoder.
    """
    return self._value

reset_rotate

reset_rotate(value: int) -> None

Reset rotary encoder value.

Parameters:

Name Type Description Default
value int

new value for the rotary encoder.

required
Source code in src/aalec/encoder.py
76
77
78
79
80
81
82
83
def reset_rotate(self, value: int) -> None:
    """Reset rotary encoder value.

    Args:
        value (int): new value for the rotary encoder.
    """
    self._value = value
    self._old_value = value

rotate_changed

rotate_changed() -> bool

Indicate if the encoder value changed since last call to this method.

Returns:

Name Type Description
bool bool

True if the value changed since last call. False otherwise.

Source code in src/aalec/encoder.py
64
65
66
67
68
69
70
71
72
73
74
def rotate_changed(self) -> bool:
    """Indicate if the encoder value changed since last call to this method.

    Returns:
        bool: True if the value changed since last call. False otherwise.
    """
    rc: bool = False
    if self._value != self._old_value:
        rc = True
        self._old_value = self._value
    return rc

test_encoder

test_encoder() -> None

Test for the encoder class.

Source code in src/aalec/encoder.py
86
87
88
89
90
91
92
93
94
95
96
def test_encoder() -> None:
    """Test for the encoder class."""
    from aalec import constants

    e = Encoder(constants.PIN_ENCODER_TRACK_1, constants.PIN_ENCODER_TRACK_2)
    old_val = e.get_rotate()
    while True:
        val = e.get_rotate()
        if old_val != val:
            print(f"Encoder value: {val}", len(f"{old_val}") * " ", end="\r")
            old_val = val