@@ -8,14 +8,14 @@ Encoder implements decoding of quadrature signals as commonly output from
88rotary encoders, by counting either up or down depending on the order of two
99input pulses.
1010
11- Minimal example usage::
11+ Minimal ESP32 example usage::
1212
1313 from machine import Pin, Encoder
1414
15- counter = Counter (0, Pin(0, Pin.IN), Pin(1, Pin.IN)) # create Encoder for pins 0, 1 and begin counting
16- value = counter .value() # retrieve current count
15+ encoder = Encoder (0, Pin(0, Pin.IN), Pin(1, Pin.IN)) # create Encoder for pins 0, 1 and begin counting
16+ value = encoder .value() # retrieve current count
1717
18- Availability: **ESP32 **
18+ Availability: **ESP32, MIMXRT **
1919
2020Constructors
2121------------
@@ -52,12 +52,46 @@ Methods
5252 - *filter_ns * specifies a minimum period of time in nanoseconds that the
5353 source signal needs to be stable for a pulse to be counted. Implementations
5454 should use the longest filter supported by the hardware that is less than
55- or equal to this value. The default is 0 (no filter). *(Supported on ESP32) *
55+ or equal to this value. The default is 0 (no filter). *(Supported on ESP32 and MIMXRT ) *
5656
5757 - *phases * specifies the number of signal edges to count and thus the
5858 granularity of the decoding. e.g. 4 phases corresponds to "4x quadrature
5959 decoding", and will result in four counts per pulse. Ports may support
60- either 1, 2, or 4 phases and the default is 1 phase. *(Supported on ESP32) *
60+ either 1, 2, or 4 phases and the default is 1 phase. *(Supported on ESP32 and MIMXRT) *
61+
62+ - *max * Specify the upper counting range. The position counter will count up
63+ from a *min * start value up to *max *, then roll over to the init value and
64+ increase the cycles counter by one. When counting down, the cycles counter
65+ decreases at the transition from *min * to *max *. The range is reset by defining
66+ both *max * and *min * to 0. The default value is the hardware's counter range.
67+ *(Supported by MIMXRT and the ESP32 PCNT module) *
68+
69+ - *min *. Specify the lower counting range. The default value is 0.
70+ *(Supported by MIMXRT and the ESP32 PCNT module) *
71+
72+ - *index * A Pin specifier telling to which pin the index pulse is connected.
73+ At a rising slope of the index pulse the encoder counter is set to the min value
74+ and the cycles counter is increased or decreased by one, depending on
75+ the input levels. A *value * of *None * disables the index input.
76+ *(Supported on MIMXRT) *
77+
78+ - *reset * A Pin specifier telling to which pin the reset pulse is connected.
79+ At a rising slope of the reset pulse the position counter is set to the init
80+ value, but the cycles counter is not changed. A *value * of *None * disables the reset input.
81+ *(Supported on MIMXRT) *
82+
83+ - *match * Set the counter value at which the interrupt IRQ_MATCH shall trigger.
84+ The value is not checked for being in the bounds of the counter range. This option
85+ if equivalent to the *threshold * options of the ESP32 PCNT module.
86+ A *value * of *None * resets the match value and disables the IRQ_MATCH interrupt.
87+ *(Supported on MIMXRT) *
88+
89+ - *match_pin * A Pin specifier telling to which pin the match output is connected.
90+ This output will have a high level as long as the position counter matches the
91+ match value. The signal is generated by the encoder logic and requires no
92+ further software support. The pulse width is defined by the input signal frequency
93+ and can be very short, like 20ns, or stay, if the counter stops at the match position.
94+ A *value * of *None * disables the match output. *(Supported on MIMXRT) *
6195
6296.. method :: Encoder.deinit()
6397
@@ -70,3 +104,56 @@ Methods
70104 Implementations should aim to do the get and set atomically.
71105
72106 See :meth: `machine.Counter.value ` for details about overflow of this value.
107+
108+ .. method :: Encoder.cycles([value])
109+
110+ Get or set the current cycles counter of the counter as signed 16 bit integer.
111+ The value represents the overflow or underflow events of the count range.
112+ With no arguments the actual cycles counter value is returned.
113+ With a single *value * argument the cycles counter is set to that value. The
114+ base counter is not changed. The method returns the previous value.
115+ *(Supported on MIMXRT) *
116+
117+ .. method :: Encoder.irq(handler=None, trigger=0, hard=False)
118+
119+ Specifies, that the *handler * is called when the respective *event * happens.
120+
121+ *event * may be:
122+ - Encoder.IRQ_RESET Triggered with a transition at the *reset * input.
123+ - Encoder.IRQ_INDEX Triggered with a transition at the *index * input.
124+ - Encoder.IRQ_MATCH Triggered when the position counter matches the *match * value. For fast signals,
125+ the actual position counter value when retrieved in the callback may be different from the trigger value.
126+ - Encoder.IRQ_ROLL_OVER Triggered when the position counter rolls over from the highest
127+ to the lowest value.
128+ - Encoder.IRQ_ROLL_UNDER Triggered when the position counter rolls under from the lowest
129+ to the highest value.
130+
131+ The callback function *handler * receives a single argument, which is the Encoder object. All
132+ events share the same callback. The event which triggers the callback can be identified
133+ with the irq.flags() method. The argument *hard * specifies, whether the callback is called
134+ as a hard interrupt or as regular scheduled function. Hard interrupts have always a short latency,
135+ but are limited in that they must not allocate memory. Regular scheduled functions are not limited
136+ in what can be used, but depending on the load of the device execution may be delayed.
137+ Under low load, the difference in latency is minor.
138+
139+ The default arguments values are trigger=0, handler=None, hard=False. The callback will be
140+ disabled, when called with handler=None.
141+
142+ The position match event is triggered as long as the position and match value are identical.
143+ Therefore the position match callback is run in a one-shot fashion, and has to be enabled
144+ again when the position has changed. It will be enabled by re-defining the trigger with either
145+ :meth: `Encoder.irq() ` or :meth: `irq().trigger() `. For ESP32, Encoder interrupts are handled
146+ by the :ref: `PCNT unit <esp32.PCNT >`.
147+
148+ *(Supported on MIMXRT) *
149+
150+ Constants
151+ ---------
152+
153+ .. data :: Encoder.IRQ_RESET
154+ Encoder.IRQ_INDEX
155+ Encoder.IRQ_MATCH
156+ Encoder.IRQ_ROLL_OVER
157+ Encoder.IRQ_ROLL_UNDER
158+
159+ Select the IRQ trigger event. *(Supported on MIMXRT) *
0 commit comments