ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
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::InternalI2CBus esphome::tca9548a::TCA9548AChannel esphome::i2c::ArduinoI2CBus esphome::i2c::IDFI2CBus esphome::i2c::ZephyrI2CBus

Public Member Functions

virtual ~I2CBus ()=default
 
virtual ErrorCode write_readv (uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
 This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBuffer.
 
ErrorCode read (uint8_t address, uint8_t *buffer, size_t len)
 
ErrorCode write (uint8_t address, const uint8_t *buffer, size_t len, bool stop=true)
 

Protected Member Functions

void i2c_scan_ ()
 Scans the I2C bus for devices.
 

Protected Attributes

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

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 29 of file i2c_bus.h.

Constructor & Destructor Documentation

◆ ~I2CBus()

virtual esphome::i2c::I2CBus::~I2CBus ( )
virtualdefault

Member Function Documentation

◆ i2c_scan_()

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

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 12 of file i2c.cpp.

◆ read()

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

Definition at line 48 of file i2c_bus.h.

◆ write()

ErrorCode esphome::i2c::I2CBus::write ( uint8_t address,
const uint8_t * buffer,
size_t len,
bool stop = true )
inline

Definition at line 52 of file i2c_bus.h.

◆ write_readv()

virtual ErrorCode esphome::i2c::I2CBus::write_readv ( uint8_t address,
const uint8_t * write_buffer,
size_t write_count,
uint8_t * read_buffer,
size_t read_count )
pure virtual

This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBuffer.

Parameters
addressaddress of the I²C device on the i2c bus
write_bufferpointer to data
write_countnumber of bytes to write
read_bufferpointer to an array to receive data
read_countnumber of bytes to read 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, esphome::i2c::ZephyrI2CBus, 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 61 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 60 of file i2c_bus.h.


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