Skip to content

Digital Bank

An IO class that provides simultaneous access to a group of digital inputs or outputs.

Creates a new DigitalBank object instance.

DigitalBank(options)

options

An object of properties used to construct the class.

pins - a bitmask with pins to include in the configured bank set to 1, e.g. 0b1100 or (1 << 3) | (1 << 2) will select pin 3 and 2.

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

bank (optional) - For devices with more than 1 digital bank of pins, a number or string value specifying the bank. Defaults to 0.

rises - A bitmask indicating the pins which invoke the onReadable callback when the digital signal is rising, or transitioning from 0 to 1. Must be configured if the falls property is not set.

falls - A bitmask indicating the pins which invoke the onReadable callback when the digital signal is falling, or transitioning from 1 to 0. Must be configured if the rises property is not set.

onReadable(triggered) (optional): A callback function that is invoked when the input signals change depending on the rises or falls properties with the triggered argument as a bitmask of the pins invoking the callback.

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.

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

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

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()

A bitmask of the configured pins’ current value as 0 (low) or 1 (high) set in the respective bit field.

Sends digital data to the IO instance.

write(bitmask)

bitmask

A bitmask of the configured pins’ current value with 0 (low) or 1 (high) set in the respective bit field.

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

import DigitalBank from "embedded:io/digitalbank";
const DigitalBank = device.io.DigitalBank;

This example instantiates a bank of digital outputs to control a series of LEDs on pins 12 through 15 and toggle it on/off every 200 milliseconds using System.setInterval.

// could also be 0xF000 or 0b1111000000000000 or 61440
const mask = (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
const leds = new device.io.Digital({
pins: mask,
mode: DigitalBank.Output,
});
led.write(mask);
let state = 0;
System.setInterval(() => {
led.write(state);
state = state == 0 ? mask : 0;
}, 200);

This example instantiates a bank of a single digital input to read a push button to toggle a digital output LED. The button is configured to trigger the onReadable callback when has been pushed (falling) and released (rising) due to the InputPullUp mode.

const led = new device.io.Digital({
pin: device.pin.led,
mode: Digital.Output,
});
// button
new device.io.DigitalBank({
pins: device.pin.button,
mode: Digital.InputPullUp,
rises: device.pin.button,
falls: device.pin.button,
onReadable() {
led.write(this.read() ? 1 : 0);
}
});

This example instantiates a bank of digital inputs to monitor two buttons on pins 1 and 15, triggering when 1 rises or 15 falls.

const pin1 = 1 << 1;
const pin15 = 1 << 15;
new device.io.DigitalBank({
pins: pin1 | pin15,
mode: Digital.Input,
rises: pin1,
falls: pin15,
onReadable(triggered) {
if (triggered == pin1) {
console.log("Pin 1 has risen!");
}
if (triggered == pin15) {
console.log("Pin 15 has fallen!");
}
}
});

Digital bank