class Encoder – quadrature decoder¶
The Encoder class wraps the i.MX RT QENC hardware block
configured as a quadrature decoder. It tracks a two-phase signal
(phase_a / phase_b) coming from a rotary encoder, increments
or decrements a 32-bit position counter according to the phase
relationship, and can be combined with optional index / reset
inputs for absolute referencing.
Available on the OpenMV Cam RT1062 (mimxrt port) only. On the
STM32-based OpenMV cams, use pyb.Timer configured for
encoder mode (Timer.ENC_AB) instead. Not
exposed on the OpenMV Cam AE3 (alif port).
Example usage:
from machine import Pin, Encoder
enc = Encoder(0, Pin("P0", Pin.IN), Pin("P1", Pin.IN), phases=4)
enc.value(0)
# ... rotate the encoder ...
print("position:", enc.value())
Constructors¶
- class machine.Encoder(id: int, phase_a: Pin | None = None, phase_b: Pin | None = None, *, phases: int = 1, filter_ns: int = 0, max: int | None = None, min: int = 0, index: Pin | None = None, reset: Pin | None = None, match: int | None = None, match_pin: Pin | None = None)¶
Construct (or fetch the singleton for) the QENC encoder block identified by
id. The same arguments are also accepted byinit()to re-configure an existing instance.phase_a/phase_bare the two quadrature input pins.phases(keyword-only) selects the decoding granularity. The QENC supports1(count one edge per pulse pair),2(both edges of phase A) or4(“4x decoding” – every edge of both phases is counted). Default1.filter_ns(keyword-only) – minimum input-stable time in nanoseconds. The driver uses the longest hardware filter that is less than or equal to this value.0(the default) disables filtering.max/min(keyword-only) – modulo range of the position counter. When the counter rolls pastmaxit wraps tominand the cycles counter increments (decrements when moving the other way). Passing both as0disables the range.index(keyword-only) – aPinwhose rising edge reloads the position counter tominand updates the cycles counter according to direction; typical use is a Z-channel mark on a rotary encoder.reset(keyword-only) – aPinwhose rising edge reloads the position counter to the start value (without changing the cycles counter).match(keyword-only) – position value at which anIRQ_MATCHinterrupt fires. PassNoneto disable.match_pin(keyword-only) – aPindriven high while the position counter equalsmatchand low otherwise.Methods¶
- init(phase_a: Pin | None = None, phase_b: Pin | None = None, *, phases: int = 1, filter_ns: int = 0, max: int | None = None, min: int = 0, index: Pin | None = None, reset: Pin | None = None, match: int | None = None, match_pin: Pin | None = None) None¶
Re-initialise the encoder with the given parameters and reset its position and cycles counters. Accepts the same keyword arguments as the constructor.
- deinit() None¶
Stop the encoder, disable any pending interrupts and release the QENC hardware resources. A soft reset deinitialises all
Encoderinstances automatically.
- value() int¶
- value(value: int, /) int
Get or set the signed position counter.
With no argument, return the current position.
With a single
valueargument, atomically set the position counter tovalueand return the previous count. The common idiomenc.value(0)resets the counter at the start of a measurement window.
- cycles() int¶
- cycles(value: int, /) int
Get or set the cycles counter, a signed 16-bit integer that tracks how many times the position counter has rolled past
max/min.With no argument, return the current cycles count.
With a single
valueargument, set the cycles counter tovalue(without touching the position counter) and return the previous count.
- irq(handler: Callable[[Encoder], None] | None = None, trigger: int = 0, hard: bool = False) None¶
Register a callback to be invoked when one of the supported QENC events fires. The handler receives the
Encoderobject as its only argument; the specific event can be identified inside the handler viairq.flags().triggeris a bitmask of one or moreIRQ_*constants:IRQ_RESET– theresetpin asserted.IRQ_INDEX– theindexpin asserted.IRQ_MATCH– the position counter reachedmatch. Match is one-shot and must be re-armed by re-installing the IRQ.IRQ_ROLL_OVER– the position counter wrapped frommaxtomin.IRQ_ROLL_UNDER– the position counter wrapped frommintomax.
hard=Trueregisters a hard interrupt handler (lower latency, but the handler must not allocate). The default is a scheduled callback. Passhandler=Noneto disable the interrupt.
Constants¶