1 # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
5 `rotaryio` - Support for reading rotation sensors
6 ===========================================================
7 See `CircuitPython:rotaryio` in CircuitPython for more details.
9 * Author(s): Melissa LeBlanc-Williams
12 from __future__ import annotations
14 import microcontroller
17 # Define the state transition table for the quadrature encoder
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
38 class IncrementalEncoder:
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.
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.
50 self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
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.
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.
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)
67 self._divisor = divisor
69 self._poll_thread = threading.Thread(target=self._polling_loop, daemon=True)
70 self._poll_thread.start()
73 """Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
76 if self._poll_thread.is_alive():
77 self._poll_thread.join()
79 def __enter__(self) -> IncrementalEncoder:
80 """No-op used by Context Managers."""
83 def __exit__(self, _type, _value, _traceback):
85 Automatically deinitializes when exiting a context. See
86 :ref:`lifetime-and-contextmanagers` for more info.
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."""
98 def divisor(self, value: int):
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
108 def position(self, value: int):
109 self._position = value
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
115 def _polling_loop(self):
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
127 def _state_update(self, new_state: int):
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:
135 elif self._sub_count <= -self._divisor: