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.
Constructor
Section titled “Constructor”IOProvider()
Section titled “IOProvider()”Creates a new IOProvider object instance.
IOProvider(options)Parameters
Section titled “Parameters”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 anErrordepending on the IO type.
Instance Properties
Section titled “Instance Properties”This class inherits all instance properties from the Base Class Pattern.
Instance Methods
Section titled “Instance Methods”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)Parameters
Section titled “Parameters”callbackFn
For asynchronous classes, a function that executes when the close process has completed.
resultThe first argument to the completion callback is always a result code. A value of 0 indicates success; an Error object indicates failure.
Examples
Section titled “Examples”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);