Digital
An IO class that represents a digital input or output
Constructor
Section titled “Constructor”Digital
Section titled “Digital”Creates a new Digital object instance.
Digital(options)Parameters
Section titled “Parameters”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 theonReadablecallback. 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 theedgeproperty, which can be retrieved using thereadmethod.
Exceptions
Section titled “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.
Static Properties
Section titled “Static Properties”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
InputPullUp
Section titled “InputPullUp”An input peripheral with the default value as “pulled high”, or 1, when nothing else is connected.
InputPullDown
Section titled “InputPullDown”An input peripheral with the default value as “pulled low”, or 0, when nothing else is connected.
InputPullUpDown
Section titled “InputPullUpDown”An input peripheral with the default value as VCC/2, or half of the standard voltage (typically 3.3v).
Output
Section titled “Output”The IO class accepts the write method to set the configured pin in push-pull mode: low (0) or high (1).
OutputDrain
Section titled “OutputDrain”The IO class accepts the write method to set the configured pin in open drain mode: low (0) or floating disconnected (1).
Rising
Section titled “Rising”The digital signal is transitioning from 0 to 1.
Falling
Section titled “Falling”The digital signal is transitioning from 1 to 0.
Instance Properties
Section titled “Instance Properties”Includes properties of the IO Class Pattern. Specific to this class:
format
Section titled “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
Section titled “Instance Methods”Returns digital data from the IO instance.
read()Return value
Section titled “Return value”0 or 1.
Sends digital data to the IO instance.
write(value)Parameters
Section titled “Parameters”value
0 or 1.
Examples
Section titled “Examples”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;Hello Blinky
Section titled “Hello Blinky”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);Button-controlled LED
Section titled “Button-controlled LED”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,});
// buttonnew device.io.Digital({ pin: device.pin.button, mode: Digital.InputPullUp, edge: Digital.Rising | Digital.Falling, onReadable() { led.write(this.read()); }});