Skip to content

IO Provider Class Pattern

The IO Provider Class Pattern builds on the Base Class Pattern to provide a foundation to access a collection of IO Classes.

Creates a new IOProvider object instance.

IOProvider(options)

options

An object of properties used to construct the class. The options object defines the hardware connections of the provider. These use the same properties as the IO types corresponding to the hardware connection. The following are defined by this abstract class:

onReady (optional): A callback function that is invoked when the instance is ready for use.

onError (optional): A callback function that is invoked when a non-recoverable error occurs. The function may be passed an Error depending on the IO type.

This class inherits all instance properties from the Base Class Pattern.

Releases all resources associated with the instance before completing. It may be called more than once without erroring. Once this method has been called, calling other methods on the instance throws an exception.

If an instance provides any asynchronous methods, it should provide an asynchronous close method via the callback function.

close()
close(callbackFn)

callbackFn

For asynchronous classes, a function that executes when the close process has completed.

result The first argument to the completion callback is always a result code. A value of 0 indicates success; an Error object indicates failure.

The following code is an example of using an IO Provider to access a Digital pin on a GPIO expander connected via I²C, such as MCP23017:

import Expander from "embedded:io/provider/expander";
const expander = new Expander({
io: device.io.I2C,
data: 5,
clock: 4,
hz: 1_000_000,
address: 0x20,
});
const led = new expander.Digital({
pin: 13,
mode: expander.Digital.Output,
});
led.write(1);

IO Provider Class Pattern