-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
esp32: Add machine.Counter and machine.Encoder. #12346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jimmo
wants to merge
4
commits into
micropython:master
Choose a base branch
from
jimmo:esp32_machine_counter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
98bb577
esp32/modesp32: Add esp32.PCNT class.
jonathanhogg d1ee820
docs/esp32: Add documentation for esp32.PCNT.
jonathanhogg 50d2735
esp32/modules/machine.py: Add Counter and Encoder classes.
jonathanhogg 27a53df
docs/library/machine: Add docs for Counter and Encoder.
jonathanhogg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
.. currentmodule:: machine | ||
.. _machine.Counter: | ||
|
||
class Counter -- pulse counter | ||
============================== | ||
|
||
Counter implements pulse counting by monitoring an input signal and counting | ||
rising or falling edges. | ||
|
||
Minimal example usage:: | ||
|
||
from machine import Pin, Counter | ||
|
||
counter = Counter(0, Pin(0, Pin.IN)) # create Counter for pin 0 and begin counting | ||
value = counter.value() # retrieve current pulse count | ||
|
||
Availability: **ESP32** | ||
|
||
Constructors | ||
------------ | ||
|
||
.. class:: Counter(id, ...) | ||
|
||
Returns the singleton Counter object for the the given *id*. Values of *id* | ||
depend on a particular port and its hardware. Values 0, 1, etc. are commonly | ||
used to select hardware block #0, #1, etc. | ||
|
||
Additional arguments are passed to the :meth:`init` method described below, | ||
and will cause the Counter instance to be re-initialised and reset. | ||
|
||
On ESP32, the *id* corresponds to a :ref:`PCNT unit <esp32.PCNT>`. | ||
|
||
Methods | ||
------- | ||
|
||
.. method:: Counter.init(src, *, ...) | ||
|
||
Initialise and reset the Counter with the given parameters: | ||
|
||
- *src* specifies the input pin as a :ref:`machine.Pin <machine.Pin>` object. | ||
May be omitted on ports that have a predefined pin for a given hardware | ||
block. | ||
|
||
Additional keyword-only parameters that may be supported by a port are: | ||
|
||
- *edge* specifies the edge to count. Either ``Counter.RISING`` (the default) | ||
or ``Counter.FALLING``. *(Supported on ESP32)* | ||
|
||
- *direction* specifies the direction to count. Either ``Counter.UP`` (the | ||
default) or ``Counter.DOWN``. *(Supported on ESP32)* | ||
|
||
- *filter_ns* specifies a minimum period of time in nanoseconds that the | ||
source signal needs to be stable for a pulse to be counted. Implementations | ||
should use the longest filter supported by the hardware that is less than | ||
or equal to this value. The default is 0 (no filter). *(Supported on ESP32)* | ||
|
||
.. method:: Counter.deinit() | ||
|
||
Stops the Counter, disabling any interrupts and releasing hardware resources. | ||
A Soft Reset should deinitialize all Counter objects. | ||
|
||
.. method:: Counter.value([value]) | ||
|
||
Get, and optionally set, the counter value as a signed integer. | ||
Implementations must aim to do the get and set atomically (i.e. without | ||
leading to skipped counts). | ||
|
||
This counter value could exceed the range of a :term:`small integer`, which | ||
means that calling :meth:`Counter.value` could cause a heap allocation, but | ||
implementations should aim to ensure that internal state only uses small | ||
integers and therefore will not allocate until the user calls | ||
:meth:`Counter.value`. | ||
|
||
For example, on ESP32, the internal state counts overflows of the hardware | ||
counter (every 32000 counts), which means that it will not exceed the small | ||
integer range until ``2**30 * 32000`` counts (slightly over 1 year at 1MHz). | ||
|
||
In general, it is recommended that you should use ``Counter.value(0)`` to reset | ||
the counter (i.e. to measure the counts since the last call), and this will | ||
avoid this problem. | ||
|
||
Constants | ||
--------- | ||
|
||
.. data:: Counter.RISING | ||
Counter.FALLING | ||
|
||
Select the pulse edge. | ||
|
||
.. data:: Counter.UP | ||
Counter.DOWN | ||
|
||
Select the counting direction. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
.. currentmodule:: machine | ||
.. _machine.Encoder: | ||
|
||
class Encoder -- quadrature decoding | ||
==================================== | ||
|
||
Encoder implements decoding of quadrature signals as commonly output from | ||
rotary encoders, by counting either up or down depending on the order of two | ||
input pulses. | ||
|
||
Minimal example usage:: | ||
|
||
from machine import Pin, Encoder | ||
|
||
counter = Counter(0, Pin(0, Pin.IN), Pin(1, Pin.IN)) # create Encoder for pins 0, 1 and begin counting | ||
value = counter.value() # retrieve current count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't seem to be the correct example? Only the comment on line 15 is different to the machine.Counter.rst example? |
||
|
||
Availability: **ESP32** | ||
|
||
Constructors | ||
------------ | ||
|
||
.. class:: Encoder(id, ...) | ||
|
||
Returns the singleton Encoder object for the the given *id*. Values of *id* | ||
depend on a particular port and its hardware. Values 0, 1, etc. are commonly | ||
used to select hardware block #0, #1, etc. | ||
|
||
Additional arguments are passed to the :meth:`init` method described below, | ||
and will cause the Encoder instance to be re-initialised and reset. | ||
|
||
On ESP32, the *id* corresponds to a :ref:`PCNT unit <esp32.PCNT>`. | ||
|
||
Methods | ||
------- | ||
|
||
.. method:: Encoder.init(phase_a, phase_b, *, ...) | ||
|
||
Initialise and reset the Encoder with the given parameters: | ||
|
||
- *phase_a* specifies the first input pin as a | ||
:ref:`machine.Pin <machine.Pin>` object. | ||
|
||
- *phase_b* specifies the second input pin as a | ||
:ref:`machine.Pin <machine.Pin>` object. | ||
|
||
These pins may be omitted on ports that have predefined pins for a given | ||
hardware block. | ||
|
||
Additional keyword-only parameters that may be supported by a port are: | ||
|
||
- *filter_ns* specifies a minimum period of time in nanoseconds that the | ||
source signal needs to be stable for a pulse to be counted. Implementations | ||
should use the longest filter supported by the hardware that is less than | ||
or equal to this value. The default is 0 (no filter). *(Supported on ESP32)* | ||
|
||
- *phases* specifies the number of signal edges to count and thus the | ||
granularity of the decoding. e.g. 4 phases corresponds to "4x quadrature | ||
decoding", and will result in four counts per pulse. Ports may support | ||
either 1, 2, or 4 phases and the default is 1 phase. *(Supported on ESP32)* | ||
|
||
.. method:: Encoder.deinit() | ||
|
||
Stops the Encoder, disabling any interrupts and releasing hardware resources. | ||
A Soft Reset should deinitialize all Encoder objects. | ||
|
||
.. method:: Encoder.value([value]) | ||
|
||
Get, and optionally set, the encoder value as a signed integer. | ||
Implementations should aim to do the get and set atomically. | ||
|
||
See :meth:`machine.Counter.value` for details about overflow of this value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.