I2Cdev class

The I2C bus operator.

Derived classes

template<i2c_port_t port = I2C_NUM_0, bool sdaPullup = false, bool sclPullup = false, uint32_t frequency = 100000UL>
class perif::I2CPerif
template<i2c_port_t port = I2C_NUM_0, bool sdaPullup = false, bool sclPullup = false, uint32_t frequency = 100000UL>
class perif::I2CPerif

Constructors, destructors, conversion operators

I2Cdev(const i2c_port_t port) explicit
~I2Cdev()

Public functions

void SelectRegister(uint8_t dev, uint8_t reg)
auto readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto readByte(uint8_t devAddr, uint8_t regAddr, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto readWord(uint8_t devAddr, uint8_t regAddr, uint16_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) -> bool
auto writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) -> bool
auto writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) -> bool
auto writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) -> bool
auto writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) -> bool

Protected functions

auto initialize(gpio_num_t sda, gpio_num_t scl, bool sdaPullUp = false, bool sclPullUp = false, uint32_t frequency = 100000UL) -> bool
auto readByte_(uint8_t devAddr, uint8_t regAddr, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto readBytes_(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) -> int8_t
auto writeByte_(uint8_t devAddr, uint8_t regAddr, uint8_t data) -> bool
auto writeBytes_(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) -> bool

Protected variables

const i2c_port_t port
bool initialized

Function documentation

I2Cdev::I2Cdev(const i2c_port_t port) explicit

Parameters
port the I2C port

Creates an instance to an I2C device on the specified port.

I2Cdev::~I2Cdev()

Performs I2C bus cleanup on this port, if necessary.

void I2Cdev::SelectRegister(uint8_t dev, uint8_t reg)

Parameters
dev the device
reg the register

Selects a device register.

int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT)

Parameters
devAddr the device address
regAddr the register address
bitNum the bit number in the register
data the destination to read into
timeout read timeout
Returns the length of data read

Reads a single bit with a mutex lock for thread-safety.

int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT)

Parameters
devAddr the device address
regAddr the register address
bitStart the register bit to start
length the number of bytes to read
data the destination to read into
timeout read timeout
Returns the length of data read

Reads multiple bits with a mutex lock for thread-safety.

int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT)

Parameters
devAddr the device address
regAddr the register address
data the destination to read into
timeout read timeout
Returns the number of bytes read.

Reads a single byte with a mutex lock for thread-safety.

int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT)

Parameters
devAddr the device address
regAddr the register address
data the destination to read into
timeout read timeout
Returns the number of bytes read.

Reads a single word with a mutex lock for thread-safety.

int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT)

Parameters
devAddr the device address
regAddr the register address
length the number of bytes to read
data the destination to read into
timeout read timeout
Returns the number of bytes read.

Reads multiple words with a mutex lock for thread-safety.

bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data)

Parameters
devAddr the device address
regAddr the register address
bitNum the register bit to write
data the data to write
Returns if write succeeded

Writes a single bit with a mutex lock for thread-safety.

bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data)

Parameters
devAddr the device address
regAddr the register address
bitStart the register bit to start
length the number of bytes to write
data the data to write
Returns if write succeeded

Writes multiple bits with a mutex lock for thread-safety.

bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data)

Parameters
devAddr the device address
regAddr the register address
data the data to write
Returns if write succeeded

Writes a single byte with a mutex lock for thread-safety.

bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data)

Parameters
devAddr the device address
regAddr the register address
length the number of bytes to write
data the data to write
Returns if write succeeded

Writes multiple bytes with a mutex lock for thread-safety.

bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data)

Parameters
devAddr the device address
regAddr the register address
data the destination to read into
Returns if write succeeded.

Writes a single word with a mutex lock for thread-safety.

bool I2Cdev::initialize(gpio_num_t sda, gpio_num_t scl, bool sdaPullUp = false, bool sclPullUp = false, uint32_t frequency = 100000UL) protected

Parameters
sda the SDA pin
scl the SCL pin
sdaPullUp whether SDA is a pullup
sclPullUp whether SCL is a pullup
frequency the bus frequency
Returns whether initialization was successfull

Initializes the I2C port if not already and verifies this device is there.

int8_t I2Cdev::readByte_(uint8_t devAddr, uint8_t regAddr, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) protected

Parameters
devAddr the device address
regAddr the register address
data the destination to read into
timeout read timeout
Returns the number of bytes read.

Reads a single byte.

int8_t I2Cdev::readBytes_(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data, uint16_t timeout = I2CDEV_DEFAULT_READ_TIMEOUT) protected

Parameters
devAddr the device address
regAddr the register address
length the number of bytes to read
data the destination to read into
timeout read timeout
Returns the number of bytes read.

Reads multiple bytes.

bool I2Cdev::writeByte_(uint8_t devAddr, uint8_t regAddr, uint8_t data) protected

Parameters
devAddr the device address
regAddr the register address
data the data to write
Returns if write succeeded

Writes a single byte.

bool I2Cdev::writeBytes_(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) protected

Parameters
devAddr the device address
regAddr the register address
length the number of bytes to write
data the data to write
Returns if write succeeded

Writes multiple bytes.

Variable documentation

const i2c_port_t I2Cdev::port protected

The I2C port for this device.

bool I2Cdev::initialized protected

Whether the device is initialized.