SMBus
An IO class extends the I2C
class with additional methods to communicate with devices that implement the SMBus protocol, can be synchronous or asynchronous.
Constructor
SMBus
Creates a new synchronous SMBus
object instance.
SMBus.Async
Creates a new asynchronous SMBus
object instance.
Parameters
options
An object of properties used to construct the class. The same as the I2C
constructor options plus stop
.
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 SMBus port, the port specifier for the instance.
stop
(optional) - A boolean value indicating whether to set the stop bit when writing the SMBus register number. Defaults tofalse
.
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 SMBus
constructor will throw.
If asynchronous IO is not supported, the SMBus.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
Inherited from the underlying I2C
class, 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.
readUint8
Returns unsigned 8-bit integer value from the IO instance.
Parameters
register
The number identifying the register to read from.
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.
value
A positive number with a max value of 255.
Return value
A positive number with a max value of 255.
readUint16
Returns unsigned 16-bit integer value from the IO instance.
Parameters
register
The number identifying the register to read from.
bigEndian
A boolean value to indicate if the value is read in big-endian (BE) byte order (true
) or little-endian (LE) byte order (false
). Defaults to false.
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.
value
A positive number with a max value of 65535.
Return value
A positive number with a max value of 65535.
readBuffer
Returns a stream of bytes starting at the specified register
.
Parameters
register
The number identifying the register to read from.
byteLength
The number of bytes to read into the returned Byte Buffer.
buffer
A pre-allocated Byte Buffer for the instance to fill.
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 toreadBuffer
byteLength
The number of bytes read into thebuffer
passed toreadBuffer
Return value
Byte Buffer if byteLength
is defined, otherwise a number representing the amount of bytes read into the buffer
argument.
readQuick
Send an SMBus Quick command with the Read/Write bit set to 1.
Parameters
callbackFn
For asynchronous classes, a function that executes when the command has been sent. 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.
Return value
None (undefined
)
receiveByte
Returns an 8-bit value from the IO instance.
Parameters
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.
value
A positive number with max value of 255.
Return value
A positive number with max value of 255.
write
Inherited from the underlying I2C
class, 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.
writeUint8
Writes a positive 8-bit value to the IO instance.
Parameters
register
The number identifying the register to read from.
value
A positive number with a max value of 255.
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.
writeUint16
Writes a positive 16-bit value to the IO instance.
Parameters
register
The number identifying the register to read from.
value
A positive number with a max value of 65535.
bigEndian
A boolean value to indicate if the value is written in big-endian (BE) byte order (true
) or little-endian (LE) byte order (false
). Defaults to false.
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.
writeBuffer
Write a stream of bytes starting at the specified register
.
Parameters
register
The number identifying the register to read from.
buffer
A Byte Buffer of data to send to the peripheral.
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.
writeQuick
Send an SMBus Quick command with the Read/Write bit set to 0.
Parameters
callbackFn
For asynchronous classes, a function that executes when the command has been sent. 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.
sendByte
Sends a command
as an 8-bit number to the IO instance.
Parameters
command
A positive number with a max value of 255.
callbackFn
For asynchronous classes, a function that executes when the command
has been sent. 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:
Seeing infrared
This example instantiates a SMBus that interacts with an AMG88 infrared array sensor for high precision temperature detection across an area and thermal imaging.