I2C
An IO class to communicate over an I2C bus, can be synchronous or asynchronous.
Constructor
I2C
Creates a new synchronous I2C
object instance.
I2C.Async
Creates a new asynchronous i2C
object instance.
Parameters
options
An object of properties used to construct the class.
data
- a pin specifier indicating the data (SDA) pin.
clock
- a pin specifier indicating the clock (SCL) pin.
hz
- The communication speed of the bus in hertz.
address
- The 7-bit address of the peripheral to communicate with.
port
(optional) - For devices with more than one I2C bus port, the port specifier for the instance.
Exceptions
If the constructor requires a resource that is already in use — whether by a script or the native host — an Error
exception is thrown.
If synchronous IO is not supported, the I2C
constructor will throw.
If asynchronous IO is not supported, the I2C.Async
constructor will throw.
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 Byte Buffer data from the IO instance.
Parameters
byteLength
The number of bytes to read into the returned Byte Buffer.
buffer
A pre-allocated Byte Buffer for the instance to fill.
stopBit
A boolean value to indicate the end of the I2C transaction. Defaults to true
.
The I2C protocol is transaction-based. At the end of each read operation, a stop bit is sent. If the stop bit is true
, it indicates the end of the transaction; if false
, it indicates that the transaction has additional operations pending.
callbackFn
For asynchronous classes, a function that executes when the data has been read. It will always be last.
error
The first argument to the completion callback is always a result code. A value of null indicates success; an Error object indicates failure.
buffer
The Byte Buffer of data ifbyteLength
is passed toread
byteLength
The number of bytes read into thebuffer
passed toread
Return value
Byte Buffer if byteLength
is defined, otherwise a number representing the amount of bytes read into the buffer
argument.
write
Sends Byte Buffer data to the IO instance.
Parameters
buffer
A Byte Buffer of data to send to the peripheral.
stopBit
A boolean value to indicate the end of the I2C transaction. Defaults to true
.
The I2C protocol is transaction-based. At the end of each write operation, a stop bit is sent. If the stop bit is true
, it indicates the end of the transaction; if false
, it indicates that the transaction has additional operations pending.
callbackFn
For asynchronous classes, a function that executes when the data has been written. It will always be last.
error
The first argument to the completion callback is always a result code. A value of null indicates success; an Error object indicates failure.
Examples
The class can be imported from the embedded
namespace or found on the host device
global object:
A touch of magic
This example instantiates an I2C bus that reads the number of touch points from an FT6206 touch sensor, and then retrieves the X and Y coordinates for the active touch points.