Class Unit

extends Object
A pulse-counter unit.
The unit shares a counter that is changed by its channels.

Class summary


constructor --channels/List --low/int= --high/int= --glitch-filter-ns/int= --start/bool=
Constructs a pulse-counter unit.
constructor pin/any --on-positive-edge/int= --on-negative-edge/int= --control-pin/any= --when-control-low/int= --when-control-high/int= --low/int= --high/int= --glitch-filter-ns/int= --start/bool=
Variant of constructor --channels that creates a unit with a single channel.

Constructors

constructor --channels/List --low/int=null --high/int=null --glitch-filter-ns/int=null --start/bool=true
Constructs a pulse-counter unit.
channels must be a list of Channel instances.
The low and high values are limits. When the counter reaches this limit, the value is reset to 0. Use null to use the maximum range (typically 16 bits).
If a glitch-filter-ns is given, then all pulses shorter than glitch-filter-ns nanoseconds are ignored.
If start is true (the default), then the unit starts counting immediately.
Advanced
The glitch-filter-ns are converted to APB clock cycles. Usually the APB clock runs at 80MHz, which means that the shortest glitch-filter-ns that makes sense is 1/80MHz = 12.5ns (-> 13).
The glitch filter is limited to 10 bits, and the highest value is thus 1023 ticks, or 12_787ns (12.5 * 1023 = 12_787.5).

constructor pin/any --on-positive-edge/int=Channel.EDGE-INCREMENT --on-negative-edge/int=Channel.EDGE-HOLD --control-pin/any=null --when-control-low/int=Channel.CONTROL-KEEP --when-control-high/int=Channel.CONTROL-KEEP --low/int=null --high/int=null --glitch-filter-ns/int=null --start/bool=true
Variant of constructor --channels that creates a unit with a single channel.
The pin and control-pin are GPIO numbers. The unit reserves the pins and releases them again when the unit is closed.
Passing a gpio.Pin is deprecated; provide the integer GPIO number instead. The gpio.Pin form will be removed in a future release.

Methods

clear -> none
Resets the counter to 0.

close -> none
Closes this unit.
Frees all the underlying resources.

Whether this unit is closed.

Whether this unit is started.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

start -> none
Resumes the counter.
It is safe to call this method multiple times.
The unit must not be closed.

stop -> none
Pauses the counter.
It is safe to call this method multiple times.
The value of the unit is unaffected by this method.
The unit must not be closed.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

The value of the counter.
The ESP32 hardware supports up to 16 bits, but the range can be limited by providing high and low values to the constructor.
The unit must not be closed.