Skip to content

Commit 0cef532

Browse files
committed
docs/library/machine: Add documentation of Counter and Encoder classes.
Add documentation for the core Counter/Encoder API as currently implemented by the esp32 port. This documentation is a simplification of that in PR #8072.
1 parent 61179d8 commit 0cef532

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

docs/esp32/quickref.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,26 @@ Use the :ref:`esp32.PCNT <esp32.PCNT>` class::
458458
counter.value(0) # reset counter
459459
count = counter.value(0) # read and reset
460460

461+
The PCNT hardware supports monitoring multiple pins in a single unit to
462+
implement quadrature decoding or up/down signal counters.
463+
464+
See the :ref:`machine.Counter <machine.Counter>` and
465+
:ref:`machine.Encoder <machine.Encoder>` classes for simpler abstractions of
466+
common pulse counting applications. These classes are implemented as thin Python
467+
shims around the ``PCNT()`` class::
468+
469+
from machine import Pin, Counter
470+
471+
counter = Counter(0, Pin(2)) # create a counter as above and start it
472+
count = counter.value() # read the count as an arbitrary precision signed integer
473+
474+
encoder = Encoder(0, Pin(12), Pin(14)) # create an encoder and begin counting
475+
count = encoder.value() # read the count as an arbitrary precision signed integer
476+
477+
Note that the id of these ``Counter()`` and ``Encoder()`` objects is an
478+
arbitrary number, each uniquely identified object will be allocated a free PCNT
479+
unit.
480+
461481

462482
Software SPI bus
463483
----------------

docs/library/esp32.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ There are 8 pulse counter units, with id 0..7.
268268
value. Thus the ``IRQ_ZERO`` event will also trigger when either of these
269269
events occurs.
270270

271+
See the :ref:`machine.Counter <machine.Counter>` and
272+
:ref:`machine.Encoder <machine.Encoder>` classes for simpler abstractions of
273+
common pulse counting applications.
274+
271275

272276
.. _esp32.RMT:
273277

docs/library/machine.Counter.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.. currentmodule:: machine
2+
.. _machine.Counter:
3+
4+
class Counter -- pulse counter
5+
==============================
6+
7+
Counter implements pulse counting by monitoring an input signal and counting
8+
rising or falling edges.
9+
10+
Minimal example usage::
11+
12+
from machine import Pin, Counter
13+
14+
counter = Counter(0, Pin(0, Pin.IN)) # create Counter for pin 0 and begin counting
15+
value = counter.value() # retrieve current pulse count
16+
17+
Availability: esp32 port
18+
19+
Constructors
20+
------------
21+
22+
.. class:: Counter(id, ...)
23+
24+
Construct a Counter object with the given id. Values of *id* depend on a
25+
particular port and its hardware. Values 0, 1, etc. are commonly used to
26+
select hardware block #0, #1, etc. Additional arguments are passed to the
27+
``init()`` method described below.
28+
29+
Methods
30+
-------
31+
32+
.. method:: Counter.init(src, *, ...)
33+
34+
Initialise the Counter with the given parameters:
35+
36+
- *src* specifies the input pin as a :ref:`machine.Pin <machine.Pin>` object.
37+
May be omitted on ports that have a predefined pin for a given hardware
38+
block.
39+
40+
Additional keyword-only parameters that may be supported by a port are:
41+
42+
- *edge* specifies the edge to count. Either ``Counter.RISING`` (the default)
43+
or ``Counter.FALLING``.
44+
45+
- *direction* specifies the direction to count. Either ``Counter.UP`` (the
46+
default) or ``Counter.DOWN``.
47+
48+
- *filter_ns* specifies a minimum period of time in nanoseconds that the
49+
source signal needs to be stable for a pulse to be counted. Implementations
50+
should use the longest filter supported by the hardware that is less than
51+
or equal to this value. The default is 0 (no filter).
52+
53+
.. method:: Counter.deinit()
54+
55+
Stops the Counter, disabling any interrupts and releasing hardware resources.
56+
A Soft Reset should deinitialize all Counter objects.
57+
58+
.. method:: Counter.value([value])
59+
60+
Get, and optionally set, the counter value as a signed integer.
61+
Implementations should aim to do the get and set atomically.
62+
63+
Constants
64+
---------
65+
66+
.. data:: Counter.RISING
67+
Counter.FALLING
68+
69+
Select the pulse edge.
70+
71+
.. data:: Counter.UP
72+
Counter.DOWN
73+
74+
Select the counting direction.

docs/library/machine.Encoder.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
.. currentmodule:: machine
2+
.. _machine.Encoder:
3+
4+
class Encoder -- quadrature decoding
5+
====================================
6+
7+
Encoder implements decoding of quadrature signals as commonly output from
8+
rotary encoders, by counting either up or down depending on the order of two
9+
input pulses.
10+
11+
Minimal example usage::
12+
13+
from machine import Pin, Encoder
14+
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
17+
18+
Availability: esp32 port
19+
20+
Constructors
21+
------------
22+
23+
.. class:: Encoder(id, ...)
24+
25+
Construct an Encoder object with the given id. Values of *id* depend on a
26+
particular port and its hardware. Values 0, 1, etc. are commonly used to
27+
select hardware block #0, #1, etc. Additional arguments are passed to the
28+
``init()`` method described below.
29+
30+
Methods
31+
-------
32+
33+
.. method:: Encoder.init(phase_a, phase_b, *, ...)
34+
35+
Initialise the Encoder with the given parameters:
36+
37+
- *phase_a* specifies the first input pin as a
38+
:ref:`machine.Pin <machine.Pin>` object.
39+
40+
- *phase_a* specifies the second input pin as a
41+
:ref:`machine.Pin <machine.Pin>` object.
42+
43+
These pins may be omitted on ports that have predefined pins for a given
44+
hardware block.
45+
46+
Additional keyword-only parameters that may be supported by a port are:
47+
48+
- *filter_ns* specifies a minimum period of time in nanoseconds that the
49+
source signal needs to be stable for a pulse to be counted. Implementations
50+
should use the longest filter supported by the hardware that is less than
51+
or equal to this value. The default is 0 (no filter).
52+
53+
- *phases* specifies the number of signal edges to count and thus the
54+
granularity of the decoding. Ports may support either 1, 2 or 4 phases.
55+
56+
.. method:: Encoder.deinit()
57+
58+
Stops the Encoder, disabling any interrupts and releasing hardware resources.
59+
A Soft Reset should deinitialize all Encoder objects.
60+
61+
.. method:: Encoder.value([value])
62+
63+
Get, and optionally set, the encoder value as a signed integer.
64+
Implementations should aim to do the get and set atomically.

docs/library/machine.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ Classes
262262
machine.I2S.rst
263263
machine.RTC.rst
264264
machine.Timer.rst
265+
machine.Counter.rst
266+
machine.Encoder.rst
265267
machine.WDT.rst
266268
machine.SD.rst
267269
machine.SDCard.rst

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy