Communicate with devices using SPI directly from Node.js
npm install spi-node!npm version
!npm version
!npm version
!npm version
Communicate with devices using SPI directly from your Node.js app. Data transfers are executed asynchronously in their own thread. Supports mock data transfers on machines without SPI support for easy local development. Uses Node's now-stable N-API for native addon support. Written in Typescript.
To install:
```
npm install --save spi-node
When installing, the native binaries will be rebuilt and added to your project's /build directory. If installing on a machine without SPI support, you'll see this warning from the compiler:
``
warning: "SPI not supported on this machine" [-W#warnings]
Not a problem, you can programatically opt-in to using mock data transfers by overriding the underlying transfer function.
[TODO: Add MCP3008 example]
The spi-node package exports the following:
``
import {
Flags,
Mode,
Order,
SPI,
Settings, // TS type
SpiAddon // TS type
TransferCallback, // TS type
TransferConfig, // TS type
TransferFunction, // TS type
} from 'spi-node';
EnumFlags is an enum of the underlying flags used to set an SPI channel's mode. You (probably) won't need to use Flags directly as they're built into the values of Mode.
``
enum Flags {
CPHA = 0x01,
CPOL = 0x02
}
- Flags.CPOL: If set, sets clock polarity such that the leading edge of the clock cycle is a falling edge.Flags.CPHA
- : If set, data is read on the leading edge of the clock cycle.
EnumMode is an enum of the four possible SPI modes, which are just permutations of the Flags.
``
enum Mode {
M0 = 0 | 0,
M1 = 0 | Flags.CPHA,
M2 = Flags.CPOL | 0,
M3 = Flags.CPOL | Flags.CPHA,
}
> The default mode of an SPI instance is Mode.M0.
EnumOrder is an enum whose values set the bit order of data transferred over an SPI connection.
``
enum Order {
MSB_FIRST = 0,
LSB_FIRST = 1,
}
> The default order of an SPI instance is Order.MSB_FIRST.
Class[TODO: Add descriptions]
##### Property SPI.spiSupported: boolean
...
##### Constructor SPI.fromDevicePath(path: string)
...
##### Constructor SPI.fromFileDescriptor(fd: number)
...
---
##### mode: Mode
...
##### order: Order
...
##### speed: number
...
##### transferOverride: TransferFunction | null
...
---
##### write(dataIn: Buffer): Promise
...
##### read(readcount: number): Promise
...
##### transfer(dataIn: Buffer, readcount: number): Promise
...
##### close(): void
...
---
All of the properties of an SPI instance can be set using chainable setter methods, allowing you to instantiate and configure an SPI connection in one go:
```
const spi = SPI.fromDevicePath('/dev/spidev0.0')
.setMode(Mode.M0)
.setOrder(Order.MSB_FIRST)
.setSpeed(1e7)
.setTransferOverride(dataIn => Promise.resolve(dataIn)); // jumper MISO/MOSI