Skip to content

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(options)

I2C.Async

Creates a new asynchronous i2C object instance.

I2C.Async(options)

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.

read(byteLength)
read(buffer)
read(byteLength, callbackFn)
read(buffer, callbackFn)
read(byteLength, stopBit)
read(buffer, stopBit)
read(byteLength, stopBit, callbackFn)
read(buffer, stopBit, callbackFn)

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

callbackFn(error, buffer)
callbackFn(error, byteLength)

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 if byteLength is passed to read

byteLength The number of bytes read into the buffer passed to read

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.

write(buffer)
write(buffer, callbackFn)
write(buffer, stopBit)
write(buffer, stopBit, callbackFn)

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

callbackFn(error)

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:

import I2C from "embedded:io/i2c";
const I2C = device.io.I2C;

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.

const touch = new device.io.I2C({
data: 4,
clock: 5,
hz: 600000,
address: 0x38
});
touch.write(Uint8Array.of(2));
let count = touch.read(1);
count = (new Uint8Array(count))[0];
console.log(`Touch points ${count}.\n`);
if (count)
touch.write(Uint8Array.of(3), false);
const data = new Uint8Array(touch.read(6 * count));
}

Specifications

I2C - synchronous IO

I2C - asynchronous IO