Package com.diozero.api
Class I2CDevice
java.lang.Object
com.diozero.api.I2CDevice
- All Implemented Interfaces:
I2CConstants
,java.io.Closeable
,java.lang.AutoCloseable
public class I2CDevice extends java.lang.Object implements java.io.Closeable, I2CConstants
Utility class reading / writing to I2C devices.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
I2CDevice.ProbeMode
-
Field Summary
Fields inherited from interface com.diozero.api.I2CConstants
ADDR_SIZE_10, ADDR_SIZE_7, BUS_0, BUS_1, DEFAULT_CLOCK_FREQUENCY, SUB_ADDRESS_SIZE_1_BYTE, SUB_ADDRESS_SIZE_2_BYTES, UNASSIGNED
-
Constructor Summary
Constructors Constructor Description I2CDevice(int controller, int address)
I2CDevice(int controller, int address, int addressSize, int clockFrequency)
I2CDevice(int controller, int address, int addressSize, int clockFrequency, java.nio.ByteOrder order)
I2CDevice(int controller, int address, java.nio.ByteOrder order)
I2CDevice(I2CDeviceFactoryInterface deviceFactory, int controller, int address, int addressSize, int clockFrequency, java.nio.ByteOrder order)
-
Method Summary
Modifier and Type Method Description void
close()
int
getAddress()
int
getAddressSize()
int
getClockFrequency()
int
getController()
boolean
isOpen()
boolean
probe()
boolean
probe(I2CDevice.ProbeMode mode)
byte[]
read(int count)
java.nio.ByteBuffer
read(int address, int count)
byte[]
read(int address, int subAddressSize, int count)
void
read(int address, int subAddressSize, java.nio.ByteBuffer buffer)
void
read(int address, java.nio.ByteBuffer dst)
void
read(java.nio.ByteBuffer dst)
boolean
readBit(int regAddr, int bitNum)
Read a single bit from an 8-bit device registerbyte
readBits(int regAddr, int bitStart, int length)
Read multiple bits from an 8-bit device registerbyte
readByte()
byte
readByte(int regAddr)
Read a single byte from an 8-bit device registerbyte
readByte(int register, int subAddressSize)
byte[]
readBytes(int regAddr, int length)
Read multiple bytes from an 8-bit device registershort
readShort(int address)
short
readShort(int address, int subAddressSize)
short
readShort(int address, int subAddressSize, java.nio.ByteOrder order)
short
readUByte(int regAddr)
long
readUInt(int address)
long
readUInt(int address, int subAddressSize, int length)
long
readUInt(int address, int subAddressSize, int length, java.nio.ByteOrder order)
long
readUInt(int address, java.nio.ByteOrder order)
int
readUShort(int address)
int
readUShort(int address, int subAddressSize)
int
readUShort(int address, int subAddressSize, java.nio.ByteOrder order)
void
write(byte[] data)
void
write(byte[] data, java.nio.ByteOrder order)
void
write(int register, int subAddressSize, byte value)
Writes a single byte to a registervoid
write(int register, int subAddressSize, byte[] value)
Writes a single byte to a registervoid
write(int registerAddress, int addressSize, java.nio.ByteBuffer buffer, int payloadLength, java.nio.ByteOrder order)
void
write(int registerAddress, java.nio.ByteBuffer buffer, int payloadLength)
void
write(java.nio.ByteBuffer buffer, int payloadLength)
void
write(java.nio.ByteBuffer buffer, int payloadLength, java.nio.ByteOrder order)
void
writeBit(int regAddr, int bitNum, boolean value)
write a single bit in an 8-bit device registervoid
writeBit(int regAddr, int bitNum, int value)
write a single bit in an 8-bit device registervoid
writeBits(int regAddr, int bitStart, int length, int data)
Write multiple bits in an 8-bit device registervoid
writeByte(byte data)
void
writeByte(int regAddr, byte data)
Write a single byte to an 8-bit device registervoid
writeByte(int regAddr, int data)
Write a single byte to an 8-bit device registervoid
writeBytes(int regAddr, int length, byte[] data)
Write multiple bytes to an 8-bit device registervoid
writeBytes(int regAddr, int length, byte[] data, int offset)
void
writeShort(int regAddr, short val)
void
writeWord(int regAddr, int data)
Write a single word to a 16-bit device register
-
Constructor Details
-
I2CDevice
- Parameters:
controller
- I2C busaddress
- I2C device address- Throws:
RuntimeIOException
- If an I/O error occurred
-
I2CDevice
- Parameters:
controller
- I2C busaddress
- I2C device addressorder
- Default byte order for this device- Throws:
RuntimeIOException
- If an I/O error occurred
-
I2CDevice
public I2CDevice(int controller, int address, int addressSize, int clockFrequency) throws RuntimeIOException- Parameters:
controller
- I2C busaddress
- I2C device addressaddressSize
- I2C device address size. Can be 7 or 10clockFrequency
- I2C clock frequency- Throws:
RuntimeIOException
- If an I/O error occurred
-
I2CDevice
public I2CDevice(int controller, int address, int addressSize, int clockFrequency, java.nio.ByteOrder order) throws RuntimeIOException- Parameters:
controller
- I2C busaddress
- I2C device addressaddressSize
- I2C device address size. Can be 7 or 10clockFrequency
- I2C clock frequencyorder
- Default byte order for this device- Throws:
RuntimeIOException
- If an I/O error occurred.
-
I2CDevice
public I2CDevice(I2CDeviceFactoryInterface deviceFactory, int controller, int address, int addressSize, int clockFrequency, java.nio.ByteOrder order) throws RuntimeIOException- Parameters:
deviceFactory
- Device factory to use to provision this devicecontroller
- I2C busaddress
- I2C device addressaddressSize
- I2C device address size. Can be 7 or 10clockFrequency
- I2C clock frequencyorder
- Default byte order for this device- Throws:
RuntimeIOException
- If an I/O error occurred
-
-
Method Details
-
getController
public int getController() -
getAddress
public int getAddress() -
getAddressSize
public int getAddressSize() -
getClockFrequency
public int getClockFrequency() -
close
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
RuntimeIOException
-
probe
public boolean probe() -
probe
-
isOpen
public final boolean isOpen() -
write
Writes a single byte to a register- Parameters:
register
- Register to writesubAddressSize
- sub-address size in bytes (1 or 2)value
- Bytes to be written- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeShort
- Throws:
RuntimeIOException
-
read
public java.nio.ByteBuffer read(int address, int count) -
read
- Throws:
RuntimeIOException
-
read
public void read(int address, int subAddressSize, java.nio.ByteBuffer buffer) throws RuntimeIOException- Throws:
RuntimeIOException
-
read
- Throws:
RuntimeIOException
-
readByte
Read a single byte from an 8-bit device register- Parameters:
regAddr
- Register regAddr to read from- Returns:
- the byte read
- Throws:
RuntimeIOException
- if an I/O error occurs
-
readUByte
- Throws:
RuntimeIOException
-
readByte
- Throws:
RuntimeIOException
-
readShort
- Throws:
RuntimeIOException
-
readShort
- Throws:
RuntimeIOException
-
readShort
public short readShort(int address, int subAddressSize, java.nio.ByteOrder order) throws RuntimeIOException- Throws:
RuntimeIOException
-
readUShort
- Throws:
RuntimeIOException
-
readUShort
- Throws:
RuntimeIOException
-
readUShort
public int readUShort(int address, int subAddressSize, java.nio.ByteOrder order) throws RuntimeIOException- Throws:
RuntimeIOException
-
readUInt
- Throws:
RuntimeIOException
-
readUInt
- Throws:
RuntimeIOException
-
readUInt
- Throws:
RuntimeIOException
-
readUInt
public long readUInt(int address, int subAddressSize, int length, java.nio.ByteOrder order) throws RuntimeIOException- Throws:
RuntimeIOException
-
readBit
Read a single bit from an 8-bit device register- Parameters:
regAddr
- Register regAddr to read frombitNum
- Bit position to read (0-7)- Returns:
- bit on/off value
- Throws:
RuntimeIOException
- if an I/O error occurs
-
readBits
Read multiple bits from an 8-bit device register- Parameters:
regAddr
- Register regAddr to read frombitStart
- First bit position to read (0-7)length
- Number of bits to read (not more than 8)- Returns:
- the byte read
- Throws:
RuntimeIOException
- if an I/O error occurs
-
readBytes
Read multiple bytes from an 8-bit device register- Parameters:
regAddr
- First register regAddr to read fromlength
- Number of bytes to read- Returns:
- the bytes read
- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeBit
write a single bit in an 8-bit device register- Parameters:
regAddr
- Register regAddr to write tobitNum
- Bit position to write (0-7)value
- New bit value to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeBit
write a single bit in an 8-bit device register- Parameters:
regAddr
- Register regAddr to write tobitNum
- Bit position to write (0-7)value
- New bit value to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeBits
Write multiple bits in an 8-bit device register- Parameters:
regAddr
- Register regAddr to write tobitStart
- First bit position to write (0-7)length
- Number of bits to write (not more than 8)data
- Right-aligned value to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
write
Writes a single byte to a register- Parameters:
register
- Register to writesubAddressSize
- sub-address size in bytes (1 or 2)value
- Byte to be written- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeByte
Write a single byte to an 8-bit device register- Parameters:
regAddr
- Register address to write todata
- New byte value to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeByte
Write a single byte to an 8-bit device register- Parameters:
regAddr
- Register address to write todata
- New byte value to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeWord
Write a single word to a 16-bit device register- Parameters:
regAddr
- Register address to write todata
- New word value to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeBytes
Write multiple bytes to an 8-bit device register- Parameters:
regAddr
- First register address to write tolength
- Number of bytes to writedata
- Buffer to copy new data from- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeBytes
- Throws:
RuntimeIOException
-
read
- Throws:
RuntimeIOException
-
readByte
- Throws:
RuntimeIOException
-
read
- Throws:
RuntimeIOException
-
write
- Throws:
RuntimeIOException
-
write
- Throws:
RuntimeIOException
-
write
public void write(java.nio.ByteBuffer buffer, int payloadLength, java.nio.ByteOrder order) throws RuntimeIOException- Throws:
RuntimeIOException
-
write
- Throws:
RuntimeIOException
-
write
public void write(int registerAddress, int addressSize, java.nio.ByteBuffer buffer, int payloadLength, java.nio.ByteOrder order) throws RuntimeIOException- Throws:
RuntimeIOException
-
write
public void write(int registerAddress, java.nio.ByteBuffer buffer, int payloadLength) throws RuntimeIOException- Throws:
RuntimeIOException
-
writeByte
- Throws:
RuntimeIOException
-