-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
docs\machine: Add Counter and Encoder classes. #8072
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
IhorNehrutsa
wants to merge
1
commit into
micropython:master
Choose a base branch
from
IhorNehrutsa:doc_Counter_Encoder_classes
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
.. currentmodule:: machine | ||
.. _machine.Counter: | ||
|
||
class Counter-- Pulse Counter | ||
============================= | ||
|
||
This class provides access to hardware-supported pulse counting. | ||
|
||
It is currently provided for ports: | ||
|
||
* :ref:`ESP32 <esp32_machin 8000 e.Counter>` | ||
* :ref:`MIMXRT <mimxrt_machine.Counter>` | ||
|
||
Minimal example usage:: | ||
|
||
from machine import Pin, Counter | ||
|
||
counter = Counter(0, src=Pin(0, mode=Pin.IN)) # create Counter object and start to count input pulses | ||
counter.init(filter_ns=1000) # switch source filtering on | ||
value = counter.value(0) # get current Counter value, set counter to 0 | ||
counter.deinit() # turn off the Counter | ||
|
||
print(counter) # show the Counter object properties | ||
|
||
Constructor | ||
----------- | ||
|
||
.. class:: Counter(id, src=None, \*, direction=Counter.UP, filter_ns=0) | ||
|
||
- *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. | ||
|
||
- *src*. The Counter pulses input pin, which is usually a | ||
:ref:`machine.Pin <machine.Pin>` object, but a port may allow other values, | ||
like integers or strings, which designate a Pin in the *machine.Pin* class. | ||
It may be omitted on ports that have a predefined pin for *id*-specified hardware block. | ||
|
||
- *direction* specifies the direction to count. Values for this include the constants | ||
``Counter.UP`` (default value) and ``Counter.DOWN``. Ports may support additional values or | ||
objects, such as a :ref:`machine.Pin <machine.Pin>` object to control the direction externally. | ||
|
||
- *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. | ||
|
||
Methods | ||
------- | ||
|
||
.. method:: Counter.init(*, src, ...) | ||
|
||
Modify the settings of the Counter object. See the **Constructor** for details about the parameters. | ||
|
||
.. method:: Counter.deinit() | ||
|
||
Stops the Counter, disables interrupts and releases hardware resources used by the counter. | ||
A Soft Reset involve deinitializing all Encoder objects. | ||
|
||
.. method:: Counter.value([value]) | ||
|
||
Get, and optionally set, the counter value as a signed integer. Implementations should aim to do the get and set atomically. | ||
|
||
Constants | ||
--------- | ||
|
||
.. data:: Counter.UP | ||
Counter.DOWN | ||
|
||
Select the counter 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,60 @@ | ||
.. currentmodule:: machine | ||
.. _machine.Encoder: | ||
|
||
class Encoder -- Quadrature Incremental Encoder | ||
=============================================== | ||
|
||
This class provides a hardware-supported Quadrature Incremental Encoder service. | ||
|
||
If your port does not support hardware encoder use `Quadrature incremental encoder based on machine.Pin interrupts <https://github.com/IhorNehrutsa/MicroPython-quadrature-incremental-encoder>`_. | ||
See also Pin-interrupt-based encoders (and problems) from Peter Hinch `Incremental encoders <https://github.com/peterhinch/micropython-samples/blob/master/encoders/ENCODERS.md>`_. | ||
There is also `Dave Hylands an STM specific hardware-Timer-based solution <https://github.com/dhylands/upy-examples/blob/master/encoder.py>`_. | ||
|
||
It is currently provided for ports: | ||
|
||
* :ref:`ESP32 <esp32_machine.Encoder>` | ||
* :ref:`MIMXRT <mimxrt_machine.Encoder>` | ||
|
||
IhorNehrutsa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Minimal example usage:: | ||
|
||
from machine import Pin, Encoder | ||
|
||
enc = Encoder(id, phase_a=Pin(0), phase_b=Pin(1)) # create Quadrature Encoder object and start to encode input pulses | ||
enc.init(filter_ns=1000) # switch source filtering on | ||
value = enc.value(0) # get current Encoder value, set Encoder to 0 | ||
enc.deinit() # turn off the Encoder | ||
|
||
print(enc) # show the Encoder object properties | ||
|
||
Constructor | ||
----------- | ||
|
||
.. class:: Encoder(id, phase_a=None, phase_b=None, \*, filter_ns=0) | ||
|
||
- *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. | ||
|
||
- *phase_a* and *phase_b* are the Quadrature encoder inputs, which are usually | ||
:ref:`machine.Pin <machine.Pin>` objects, but a port may allow other values, | ||
like integers or strings, which designate a Pin in the *machine.Pin* class. | ||
They may be omitted on ports that have predefined pins for *id*-specified hardware block. | ||
|
||
- *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. | ||
|
||
IhorNehrutsa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Methods | ||
------- | ||
|
||
.. method:: Encoder.init(*, phase_a, ...) | ||
|
||
Modify the settings of the Encoder object. See the **Constructor** for details about the parameters. | ||
|
||
.. method:: Encoder.deinit() | ||
|
||
Stops the Encoder, disables interrupts and releases hardware resources used by the encoder. | ||
A Soft Reset involve deinitializing all Encoder objects. | ||
|
||
.. method:: Encoder.value([value]) | ||
|
||
Get, and optionally set, the counter value as a signed integer. Implementations should aim to do the get and set atomically. |
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.