Skip to content

Digital

An IO class that represents a digital input or output

Creates a new Digital object instance.

Digital(options)

options

An object of properties used to construct the class.

pin - a pin specifier indicating the GPIO pin.

mode - A value indicating the mode of the IO. This value may be one of the static properties on the class: Digital.Input, Digital.InputPullUp, Digital.InputPullDown, Digital.InputPullUpDown, Digital.Output, Digital.OutputDrain

edge - A value indicating the conditions for invoking the onReadable callback. This value may be one of the static properties on the class: Digital.Rising, Digital.Falling, Digital.Rising | Digital.Falling

onReadable() (optional): A callback function that is invoked when the input value changes depending on the edge property, which can be retrieved using the read method.

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

The IO class will invoke the onReadable callback based on the edge value and accepts the read method. It will read whatever logic level is connected to it and ‘float’ to random high or low values if nothing is connected

An input peripheral with the default value as “pulled high”, or 1, when nothing else is connected.

An input peripheral with the default value as “pulled low”, or 0, when nothing else is connected.

An input peripheral with the default value as VCC/2, or half of the standard voltage (typically 3.3v).

The IO class accepts the write method to set the configured pin in push-pull mode: low (0) or high (1).

The IO class accepts the write method to set the configured pin in open drain mode: low (0) or floating disconnected (1).

The digital signal is transitioning from 0 to 1.

The digital signal is transitioning from 1 to 0.

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

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

Returns digital data from the IO instance.

read()

0 or 1.

Sends digital data to the IO instance.

write(value)

value

0 or 1.

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

import Digital from "embedded:io/digital";
const Digital = device.io.Digital;

This example instantiates a digital output to control an LED based on the global device pin names and toggle it on/off every 200 milliseconds using System.setInterval.

const led = new device.io.Digital({
pin: device.pin.led,
mode: Digital.Output,
});
led.write(1);
let state = 0;
System.setInterval(() => {
led.write(state);
state = state == 0 ? 1 : 0;
}, 200);

This example instantiates a digital input to read a push button to toggle a digital output LED based on the global device pin names. The button is configured to trigger the onReadable callback when has been pushed (falling) and released (rising).

const led = new device.io.Digital({
pin: device.pin.led,
mode: Digital.Output,
});
// button
new device.io.Digital({
pin: device.pin.button,
mode: Digital.InputPullUp,
edge: Digital.Rising | Digital.Falling,
onReadable() {
led.write(this.read());
}
});

Digital