Peripheral Class Pattern
The Peripheral Class Pattern builds on the Base Class Pattern to provide a foundation for implementing access to different kinds of peripheral devices.
Constructor
Peripheral()
Creates a new Peripheral
object instance.
Parameters
options
An object of properties used to construct the class. The options object defines the hardware connections of the peripheral. These use the same properties as the IO types corresponding to the peripheral. The following are defined by this abstract class:
io
: the constructor for the IO Class used to communicate with the peripheral
The rest of the options should include the properties accepted by the io
constructor. See the example for more details.
If the peripheral has multiple hardware connections, the options object will accept seperate top-level properties to nest the options for each io
type, see examples for more detauls.
Instance Properties
Classes that conform to this class pattern may choose to provide accessors, e.g. setters and getters, for configuration properties.
A setter should behave in the same way as the configure
method invoked with a single property.
Instance Methods
close
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.
configure
Modifies how the peripheral operates. A given call to this method should only modify the features specified in the options object. It may be called more than once.
Parameters
options
Follows the same rules as the options
used in the constructor.
Examples
This is an abstract class that is not instantiated directly. The examples that follow are demonstrations of potential implementations.
The following code is an example of using an I2C peripheral:
The following code is an example of a peripheral that has multiple hardware connections: