|
| 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** |
| 18 | + |
| 19 | +Constructors |
| 20 | +------------ |
| 21 | + |
| 22 | +.. class:: Counter(id, ...) |
| 23 | + |
| 24 | + Returns the singleton Counter object for the the given *id*. Values of *id* |
| 25 | + depend on a particular port and its hardware. Values 0, 1, etc. are commonly |
| 26 | + used to select hardware block #0, #1, etc. |
| 27 | + |
| 28 | + Additional arguments are passed to the :meth:`init` method described below, |
| 29 | + and will cause the Counter instance to be re-initialised and reset. |
| 30 | + |
| 31 | + On ESP32, the *id* corresponds to a :ref:`PCNT unit <esp32.PCNT>`. |
| 32 | + |
| 33 | +Methods |
| 34 | +------- |
| 35 | + |
| 36 | +.. method:: Counter.init(src, *, ...) |
| 37 | + |
| 38 | + Initialise and reset the Counter with the given parameters: |
| 39 | + |
| 40 | + - *src* specifies the input pin as a :ref:`machine.Pin <machine.Pin>` object. |
| 41 | + May be omitted on ports that have a predefined pin for a given hardware |
| 42 | + block. |
| 43 | + |
| 44 | + Additional keyword-only parameters that may be supported by a port are: |
| 45 | + |
| 46 | + - *edge* specifies the edge to count. Either ``Counter.RISING`` (the default) |
| 47 | + or ``Counter.FALLING``. *(Supported on ESP32)* |
| 48 | + |
| 49 | + - *direction* specifies the direction to count. Either ``Counter.UP`` (the |
| 50 | + default) or ``Counter.DOWN``. *(Supported on ESP32)* |
| 51 | + |
| 52 | + - *filter_ns* specifies a minimum period of time in nanoseconds that the |
| 53 | + source signal needs to be stable for a pulse to be counted. Implementations |
| 54 | + 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)* |
| 56 | + |
| 57 | +.. method:: Counter.deinit() |
| 58 | + |
| 59 | + Stops the Counter, disabling any interrupts and releasing hardware resources. |
| 60 | + A Soft Reset should deinitialize all Counter objects. |
| 61 | + |
| 62 | +.. method:: Counter.value([value]) |
| 63 | + |
| 64 | + Get, and optionally set, the counter value as a signed integer. |
| 65 | + Implementations must aim to do the get and set atomically (i.e. without |
| 66 | + leading to skipped counts). |
| 67 | + |
| 68 | + This counter value could exceed the range of a :term:`small integer`, which |
| 69 | + means that calling :meth:`Counter.value` could cause a heap allocation, but |
| 70 | + implementations should aim to ensure that internal state only uses small |
| 71 | + integers and therefore will not allocate until the user calls |
| 72 | + :meth:`Counter.value`. |
| 73 | + |
| 74 | + For example, on ESP32, the internal state counts overflows of the hardware |
| 75 | + counter (every 32000 counts), which means that it will not exceed the small |
| 76 | + integer range until ``2**30 * 32000`` counts (slightly over 1 year at 1MHz). |
| 77 | + |
| 78 | + In general, it is recommended that you should use ``Counter.value(0)`` to reset |
| 79 | + the counter (i.e. to measure the counts since the last call), and this will |
| 80 | + avoid this problem. |
| 81 | + |
| 82 | +Constants |
| 83 | +--------- |
| 84 | + |
| 85 | +.. data:: Counter.RISING |
| 86 | + Counter.FALLING |
| 87 | + |
| 88 | + Select the pulse edge. |
| 89 | + |
| 90 | +.. data:: Counter.UP |
| 91 | + Counter.DOWN |
| 92 | + |
| 93 | + Select the counting direction. |
0 commit comments