Table of Contents

Class GpioPulseCounter

Namespace
System.Device.Gpio
Assembly
nanoFramework.Hardware.Esp32.dll

Counts changes of a specified polarity on a general-purpose I/O (GPIO) pin.

public sealed class GpioPulseCounter : IDisposable
Inheritance
GpioPulseCounter
Implements
Inherited Members
Extension Methods

Remarks

When the pin is an input, interrupts are used to detect pin changes unless the MCU supports a counter in hardware. Changes of the pin are enabled for the specified polarity, and the count is incremented when a change occurs.

When the pin is an output, the count will increment whenever the specified transition occurs on the pin. For example, if the pin is configured as an output and counting is enabled for rising edges, writing a 0 and then a 1 will cause the count to be incremented.

Constructors

GpioPulseCounter(int, int)

Initializes a new instance of the GpioPulseCounter class associated with the specified pin. Only a single GpioPulseCounter may be associated with a pin at any given time.

public GpioPulseCounter(int pinNumberA, int pinNumberB = -1)

Parameters

pinNumberA int

The first pin on which to count changes.

pinNumberB int

The second pin which can be used to control how the count are done on the first pin. If no use, leave at at -1.

Exceptions

ArgumentException

TThe pin is already associated with a change counter.That change counter must be disposed before the pin can be associated with a new change counter.

Properties

FilterPulses

Gets or sets the signal filter value in microseconds. The filter value may only be changed when pin counting is not started. Valid values from 0 to 1023. The clock used is 80 MHz, filter is a multiple of the period of the clock.

public ushort FilterPulses { get; set; }

Property Value

ushort

Exceptions

ArgumentException

Value must be between 0 and 1023.

IsStarted

Gets whether pin change counting is currently active.

public bool IsStarted { get; }

Property Value

bool

TRUE if this pin change counting is active and FALSE otherwise.

Polarity

Gets or sets the polarity of transitions that will be counted. The polarity may only be changed when pin counting is not started.

public GpioPulsePolarity Polarity { get; set; }

Property Value

GpioPulsePolarity

Remarks

The default polarity value is Falling. See GpioPulsePolarity for more information on polarity values. Counting a single edge can be considerably more efficient than counting both edges.

Exceptions

InvalidOperationException

Change counting is currently active. Polarity can only be set before calling Start() or after calling Stop().

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Finalize()

protected override void Finalize()

Read()

Reads the current count of polarity changes. Before counting has been started, this will return 0.

public GpioPulseCount Read()

Returns

GpioPulseCount

A GpioPulseCount structure containing a count and an associated timestamp.

Exceptions

ObjectDisposedException

The change counter or the associated pin has been disposed.

Reset()

Resets the count to 0 and returns the previous count.

public GpioPulseCount Reset()

Returns

GpioPulseCount

A GpioPulseCount structure containing a count and an associated timestamp.

Exceptions

ObjectDisposedException

The change counter or the associated pin has been disposed.

Start()

Starts counting changes in pin polarity. This method may only be called when change counting is not already active.

public void Start()

Remarks

Calling Start() may enable or reconfigure interrupts for the pin.

The following exceptions can be thrown by this method:

Exceptions

ObjectDisposedException

The change counter or the associated pin has been disposed.

InvalidOperationException

Change counting has already been started.

Stop()

Stop counting changes in pin polarity. This method may only be called when change counting is currently active.

public void Stop()

Remarks

Calling Stop() may enable or reconfigure interrupts for the pin.

The following exceptions can be thrown by this method:

Exceptions

ObjectDisposedException

The change counter or the associated pin has been disposed.

InvalidOperationException

Change counting has not been started.