]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/rotaryio.py
Merge branch 'main' of https://github.com/adafruit/Adafruit_Blinka
[Adafruit_Blinka-hackapet.git] / src / rotaryio.py
1 # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """
5 `rotaryio` - Support for reading rotation sensors
6 ===========================================================
7 See `CircuitPython:rotaryio` in CircuitPython for more details.
8
9 * Author(s): Melissa LeBlanc-Williams
10 """
11
12 from __future__ import annotations
13 import threading
14 import microcontroller
15 import digitalio
16
17 # Define the state transition table for the quadrature encoder
18 transitions = [
19     0,  # 00 -> 00 no movement
20     -1,  # 00 -> 01 3/4 ccw (11 detent) or 1/4 ccw (00 at detent)
21     +1,  # 00 -> 10 3/4 cw or 1/4 cw
22     0,  # 00 -> 11 non-Gray-code transition
23     +1,  # 01 -> 00 2/4 or 4/4 cw
24     0,  # 01 -> 01 no movement
25     0,  # 01 -> 10 non-Gray-code transition
26     -1,  # 01 -> 11 4/4 or 2/4 ccw
27     -1,  # 10 -> 00 2/4 or 4/4 ccw
28     0,  # 10 -> 01 non-Gray-code transition
29     0,  # 10 -> 10 no movement
30     +1,  # 10 -> 11 4/4 or 2/4 cw
31     0,  # 11 -> 00 non-Gray-code transition
32     +1,  # 11 -> 01 1/4 or 3/4 cw
33     -1,  # 11 -> 10 1/4 or 3/4 ccw
34     0,  # 11 -> 11 no movement
35 ]
36
37
38 class IncrementalEncoder:
39     """
40     IncrementalEncoder determines the relative rotational position based on two series of
41     pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables
42     pull-ups on pin_a and pin_b.
43
44     Create an IncrementalEncoder object associated with the given pins. It tracks the
45     positional state of an incremental rotary encoder (also known as a quadrature encoder.)
46     Position is relative to the position when the object is constructed.
47     """
48
49     def __init__(
50         self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
51     ):
52         """
53         Create an IncrementalEncoder object associated with the given pins. It tracks the
54         positional state of an incremental rotary encoder (also known as a quadrature encoder.)
55         Position is relative to the position when the object is constructed.
56
57         :param microcontroller.Pin pin_a: The first pin connected to the encoder.
58         :param microcontroller.Pin pin_b: The second pin connected to the encoder.
59         :param int divisor: The number of pulses per encoder step. Default is 4.
60         """
61         self._pin_a = digitalio.DigitalInOut(pin_a)
62         self._pin_a.switch_to_input(pull=digitalio.Pull.UP)
63         self._pin_b = digitalio.DigitalInOut(pin_b)
64         self._pin_b.switch_to_input(pull=digitalio.Pull.UP)
65         self._position = 0
66         self._last_state = 0
67         self._divisor = divisor
68         self._sub_count = 0
69         self._poll_thread = threading.Thread(target=self._polling_loop, daemon=True)
70         self._poll_thread.start()
71
72     def deinit(self):
73         """Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
74         self._pin_a.deinit()
75         self._pin_b.deinit()
76         if self._poll_thread.is_alive():
77             self._poll_thread.join()
78
79     def __enter__(self) -> IncrementalEncoder:
80         """No-op used by Context Managers."""
81         return self
82
83     def __exit__(self, _type, _value, _traceback):
84         """
85         Automatically deinitializes when exiting a context. See
86         :ref:`lifetime-and-contextmanagers` for more info.
87         """
88         self.deinit()
89
90     @property
91     def divisor(self) -> int:
92         """The divisor of the quadrature signal. Use 1 for encoders without detents, or encoders
93         with 4 detents per cycle. Use 2 for encoders with 2 detents per cycle. Use 4 for encoders
94         with 1 detent per cycle."""
95         return self._divisor
96
97     @divisor.setter
98     def divisor(self, value: int):
99         self._divisor = value
100
101     @property
102     def position(self) -> int:
103         """The current position in terms of pulses. The number of pulses per rotation is defined
104         by the specific hardware and by the divisor."""
105         return self._position
106
107     @position.setter
108     def position(self, value: int):
109         self._position = value
110
111     def _get_pin_state(self) -> int:
112         """Returns the current state of the pins."""
113         return self._pin_a.value << 1 | self._pin_b.value
114
115     def _polling_loop(self):
116         while True:
117             self._poll_encoder()
118
119     def _poll_encoder(self):
120         # Check the state of the pins
121         # if either pin has changed, update the state
122         new_state = self._get_pin_state()
123         if new_state != self._last_state:
124             self._state_update(new_state)
125             self._last_state = new_state
126
127     def _state_update(self, new_state: int):
128         new_state &= 3
129         index = self._last_state << 2 | new_state
130         sub_increment = transitions[index]
131         self._sub_count += sub_increment
132         if self._sub_count >= self._divisor:
133             self._position += 1
134             self._sub_count = 0
135         elif self._sub_count <= -self._divisor:
136             self._position -= 1
137             self._sub_count = 0