SPI
An IO class that implements a SPI controller to communicate with a single peripheral.
Constructor
SPI
Creates a new SPI
object instance.
Parameters
options
An object of properties used to construct the class.
out
- a pin specifier indicating the Serial Data Out (PICO/MOSI) pin. Required to write data.
in
- a pin specifier indicating the Serial Data In (POCI/MISO) pin. Required to read data.
clock
- a pin specifier indicating the Serial Clock (SCK/CLK) pin.
select
(optional) - a pin specifier indicating the Serial Chip Select (CS/SS) pin. May be required to read data.
active
(optional) - binary value (0 or 1) to write to theselect
pin when the SPI instance is active. Defaults to 0.
hz
- a number specifying the speed of data over the SPI bus in hertz.
mode
(optional) - a 2-bit mask to specify the SPI bus mode with the clock polarity at bit 1 and phase at bit 0. Defaults to0b00
.
port
(optional) - For devices with more than one SPI bus port, the port specifier for the instance.
Exceptions
If both in
and out
are undefined, a TypeError
is thrown.
If the constructor requires a resource that is already in use — whether by a script or the native host — an Error
exception is thrown.
Instance Properties
Includes properties of the IO Class Pattern
. Specific to this class:
format
Always returns "buffer"
. The string value set by the constructor options or by the script at any time to change how it reads data.
Instance Methods
read
Returns data from the IO instance.
Parameters
byteLength
Accepted when the format
is a "buffer"
, the number of bytes to read into the returned Byte Buffer.
buffer
Accepted when the format
is a "buffer"
, a pre-allocated Byte Buffer for the instance to fill.
Return value
undefined
if no data is available.
Returns Byte Buffer if byteLength
is defined, otherwise a number representing the amount of bytes read into the buffer
argument.
write
Sends data to the IO instance. Any input data is discarded.
Parameters
buffer
A Byte Buffer of data to send to the peripheral.
Exceptions
If the output buffer cannot accept all the bytes to be written, an exception is thrown.
transfer
Sends data while simultaneously reading from the IO instance.
Parameters
buffer
A Byte Buffer of data to send to the peripheral.
Return value
None (undefined
). The results from reading data are stored in the buffer
argument.
flush
Flushes any buffers from the SPI controller instance.
Parameters
deselect
Boolean value to indicate if the chip select (CS/SS) pin should be set to inactive after the flush operation completes.
Examples
The class can be imported from the embedded
namespace or found on the host device
global object:
I SPI a touch screen
This example instantiates a SPI controller that samples a XPT2046 touch screen controller every 100 milliseconds. A digital input is used to detect when the screen has been touched.
It uses default pins from the device
global.