ESPHome  2024.12.0
Public Member Functions | Protected Member Functions | Protected Attributes
esphome::i2c::I2CBus Class Referenceabstract

This Class provides the methods to read and write bytes from an I2CBus. More...

#include <i2c_bus.h>

Inheritance diagram for esphome::i2c::I2CBus:
esphome::i2c::ArduinoI2CBus esphome::i2c::IDFI2CBus esphome::tca9548a::TCA9548AChannel

Public Member Functions

virtual ErrorCode read (uint8_t address, uint8_t *buffer, size_t len)
 Creates a ReadBuffer and calls the virtual readv() method to read bytes into this buffer. More...
 
virtual ErrorCode readv (uint8_t address, ReadBuffer *buffers, size_t count)=0
 This virtual method reads bytes from an I2CBus into an array of ReadBuffer. More...
 
virtual ErrorCode write (uint8_t address, const uint8_t *buffer, size_t len)
 
virtual ErrorCode write (uint8_t address, const uint8_t *buffer, size_t len, bool stop)
 Creates a WriteBuffer and calls the writev() method to send the bytes from this buffer. More...
 
virtual ErrorCode writev (uint8_t address, WriteBuffer *buffers, size_t cnt)
 
virtual ErrorCode writev (uint8_t address, WriteBuffer *buffers, size_t count, bool stop)=0
 This virtual method writes bytes to an I2CBus from an array of WriteBuffer. More...
 

Protected Member Functions

void i2c_scan_ ()
 Scans the I2C bus for devices. More...
 

Protected Attributes

std::vector< std::pair< uint8_t, bool > > scan_results_
 array containing scan results More...
 
bool scan_ {false}
 Should we scan ? Can be set in the yaml. More...
 

Detailed Description

This Class provides the methods to read and write bytes from an I2CBus.

Note
The I2CBus virtual class follows a Factory design pattern that provides all the interfaces methods required by clients while deferring the actual implementation of these methods to a subclasses. I2C-bus specification and user manual can be found here https://www.nxp.com/docs/en/user-guide/UM10204.pdf and an interesting I²C Application note https://www.nxp.com/docs/en/application-note/AN10216.pdf

Definition at line 40 of file i2c_bus.h.

Member Function Documentation

◆ i2c_scan_()

void esphome::i2c::I2CBus::i2c_scan_ ( )
inlineprotected

Scans the I2C bus for devices.

Devices presence is kept in an array of std::pair that contains the address and the corresponding bool presence flag.

Definition at line 97 of file i2c_bus.h.

◆ read()

virtual ErrorCode esphome::i2c::I2CBus::read ( uint8_t  address,
uint8_t *  buffer,
size_t  len 
)
inlinevirtual

Creates a ReadBuffer and calls the virtual readv() method to read bytes into this buffer.

Parameters
addressaddress of the I²C component on the i2c bus
bufferpointer to an array of bytes that will be used to store the data received
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 47 of file i2c_bus.h.

◆ readv()

virtual ErrorCode esphome::i2c::I2CBus::readv ( uint8_t  address,
ReadBuffer buffers,
size_t  count 
)
pure virtual

This virtual method reads bytes from an I2CBus into an array of ReadBuffer.

Parameters
addressaddress of the I²C component on the i2c bus
bufferspointer to an array of ReadBuffer
countnumber of ReadBuffer to read
Returns
an i2c::ErrorCode

This is a pure virtual method that must be implemented in a subclass.

Implemented in esphome::i2c::ArduinoI2CBus, esphome::i2c::IDFI2CBus, and esphome::tca9548a::TCA9548AChannel.

◆ write() [1/2]

virtual ErrorCode esphome::i2c::I2CBus::write ( uint8_t  address,
const uint8_t *  buffer,
size_t  len 
)
inlinevirtual

Definition at line 62 of file i2c_bus.h.

◆ write() [2/2]

virtual ErrorCode esphome::i2c::I2CBus::write ( uint8_t  address,
const uint8_t *  buffer,
size_t  len,
bool  stop 
)
inlinevirtual

Creates a WriteBuffer and calls the writev() method to send the bytes from this buffer.

Parameters
addressaddress of the I²C component on the i2c bus
bufferpointer to an array of bytes that contains the data to be sent
lenlength of the buffer = number of bytes to write
stoptrue or false: True will send a stop message, releasing the bus after transmission. False will send a restart, keeping the connection active.
Returns
an i2c::ErrorCode

Definition at line 73 of file i2c_bus.h.

◆ writev() [1/2]

virtual ErrorCode esphome::i2c::I2CBus::writev ( uint8_t  address,
WriteBuffer buffers,
size_t  cnt 
)
inlinevirtual

Definition at line 80 of file i2c_bus.h.

◆ writev() [2/2]

virtual ErrorCode esphome::i2c::I2CBus::writev ( uint8_t  address,
WriteBuffer buffers,
size_t  count,
bool  stop 
)
pure virtual

This virtual method writes bytes to an I2CBus from an array of WriteBuffer.

Parameters
addressaddress of the I²C component on the i2c bus
bufferspointer to an array of WriteBuffer
countnumber of WriteBuffer to write
stoptrue or false: True will send a stop message, releasing the bus after transmission. False will send a restart, keeping the connection active.
Returns
an i2c::ErrorCode

This is a pure virtual method that must be implemented in the subclass.

Implemented in esphome::i2c::ArduinoI2CBus, esphome::i2c::IDFI2CBus, and esphome::tca9548a::TCA9548AChannel.

Field Documentation

◆ scan_

bool esphome::i2c::I2CBus::scan_ {false}
protected

Should we scan ? Can be set in the yaml.

Definition at line 108 of file i2c_bus.h.

◆ scan_results_

std::vector<std::pair<uint8_t, bool> > esphome::i2c::I2CBus::scan_results_
protected

array containing scan results

Definition at line 107 of file i2c_bus.h.


The documentation for this class was generated from the following file: