Skip to content

Pulse Count

The IO class implements a bi-directional counter typically used with a rotary encoder.

Constructor

PulseCount

Creates a new PulseCount object instance.

PulseCount(options)

Parameters

options

An object of properties used to construct the class.

signal - a pin specifier indicating the signal input pin.

control - a pin specifier indicating the control input pin.

onReadable() (optional): A callback function that is invoked when the input value changes, which can be retrieved using the read method. Multiple changes to the counter may be combined into a single callback.

onError (optional): A callback function that is invoked when a non-recoverable error occurs, such as underflow or overflow of the counter.

Exceptions

If the constructor requires a resource that is already in use — whether by a script or the native host — an Error exception is thrown.

Instance Properties

Includes properties of the IO Class Pattern. Specific to this class:

format

Always returns "number". The string value set by the constructor options or by the script at any time to change how it reads data.

Instance Methods

read

Returns the current count from the IO instance.

read()

Return value

A number describing the current count.

write

Sets the current count on the IO instance.

write(count)

Parameters

count

A number describing the current count.

Examples

The class can be imported from the embedded namespace or found on the host device global object:

import PulseCount from "embedded:io/pulsecount";
const PulseCount = device.io.PulseCount;

Rotary encoder

This example instantiates a pulse counter to monitor a rotary encoder and log the current count when it changes.

new device.io.PulseCount({
signal: 6,
control: 8,
onReadable() {
const count = this.read();
console.log(`count: ${count}`);
}
});

Specifications

Pulse count