ESPHome  2024.12.0
Public Member Functions | Protected Attributes
esphome::i2c::I2CDevice Class Reference

This Class provides the methods to read/write bytes from/to an i2c device. More...

#include <i2c.h>

Inheritance diagram for esphome::i2c::I2CDevice:
esphome::ade7880::ADE7880 esphome::ade7953_i2c::AdE7953I2c esphome::ads1115::ADS1115Component esphome::ags10::AGS10Component esphome::aht10::AHT10Component esphome::aic3204::AIC3204 esphome::am2315c::AM2315C esphome::am2320::AM2320Component esphome::apds9306::APDS9306 esphome::apds9960::APDS9960 esphome::as3935_i2c::I2CAS3935Component esphome::as5600::AS5600Component esphome::as7341::AS7341Component esphome::at581x::AT581XComponent esphome::axs15231::AXS15231Touchscreen esphome::bh1750::BH1750Sensor esphome::bme280_i2c::BME280I2CComponent esphome::bme680::BME680Component esphome::bme680_bsec::BME680BSECComponent esphome::bme68x_bsec2_i2c::BME68xBSEC2I2CComponent esphome::bmi160::BMI160Component esphome::bmp085::BMP085Component esphome::bmp280_i2c::BMP280I2CComponent esphome::bmp3xx_i2c::BMP3XXI2CComponent esphome::bmp581::BMP581Component esphome::cap1188::CAP1188Component esphome::ccs811::CCS811Component esphome::ch422g::CH422GComponent esphome::cst226::CST226Touchscreen esphome::cst816::CST816Touchscreen esphome::dac7678::DAC7678Output esphome::dht12::DHT12Component esphome::dps310::DPS310Component esphome::ds1307::DS1307Component esphome::ee895::EE895Component esphome::ektf2232::EKTF2232Touchscreen esphome::emc2101::Emc2101Component esphome::ens160_i2c::ENS160I2CComponent esphome::ens210::ENS210Component esphome::es8311::ES8311 esphome::ezo::EZOSensor esphome::ezo_pmp::EzoPMP esphome::fs3000::FS3000Component esphome::ft5x06::FT5x06Touchscreen esphome::ft63x6::FT63X6Touchscreen esphome::gdk101::GDK101Component esphome::gp8403::GP8403 esphome::grove_gas_mc_v2::GroveGasMultichannelV2Component esphome::grove_tb6612fng::GroveMotorDriveTB6612FNG esphome::gt911::GT911Touchscreen esphome::hdc1080::HDC1080Component esphome::hm3301::HM3301Component esphome::hmc5883l::HMC5883LComponent esphome::honeywell_hih_i2c::HoneywellHIComponent esphome::honeywellabp2_i2c::HONEYWELLABP2Sensor esphome::hte501::HTE501Component esphome::htu21d::HTU21DComponent esphome::htu31d::HTU31DComponent esphome::hyt271::HYT271Component esphome::i2c_device::I2CDeviceComponent esphome::iaqcore::IAQCore esphome::ina219::INA219Component esphome::ina226::INA226Component esphome::ina260::INA260Component esphome::ina2xx_i2c::INA2XXI2C esphome::ina3221::INA3221Component esphome::inkplate6::Inkplate6 esphome::kmeteriso::KMeterISOComponent esphome::lcd_pcf8574::PCF8574LCDDisplay esphome::lilygo_t5_47::LilygoT547Touchscreen esphome::ltr390::LTR390Component esphome::ltr501::LTRAlsPs501Component esphome::ltr_als_ps::LTRAlsPsComponent esphome::m5stack_8angle::M5Stack8AngleComponent esphome::max17043::MAX17043Component esphome::max44009::MAX44009Sensor esphome::max6956::MAX6956 esphome::max9611::MAX9611Component esphome::mcp23008::MCP23008 esphome::mcp23016::MCP23016 esphome::mcp23017::MCP23017 esphome::mcp4725::MCP4725 esphome::mcp4728::MCP4728Component esphome::mcp47a1::MCP47A1 esphome::mcp9600::MCP9600Component esphome::mcp9808::MCP9808Sensor esphome::mics_4514::MICS4514Component esphome::mlx90393::MLX90393Cls esphome::mlx90614::MLX90614Component esphome::mmc5603::MMC5603Component esphome::mmc5983::MMC5983Component esphome::mpl3115a2::MPL3115A2Component esphome::mpr121::MPR121Component esphome::mpu6050::MPU6050Component esphome::mpu6886::MPU6886Component esphome::ms5611::MS5611Component esphome::ms8607::MS8607Component esphome::ms8607::MS8607HumidityDevice esphome::nau7802::NAU7802Sensor esphome::npi19::NPI19Component esphome::pca6416a::PCA6416AComponent esphome::pca9554::PCA9554Component esphome::pca9685::PCA9685Output esphome::pcf85063::PCF85063Component esphome::pcf8563::PCF8563Component esphome::pcf8574::PCF8574Component esphome::pmsa003i::PMSA003IComponent esphome::pmwcs3::PMWCS3Component esphome::pn532_i2c::PN532I2C esphome::pn7150_i2c::PN7150I2C esphome::pn7160_i2c::PN7160I2C esphome::qmc5883l::QMC5883LComponent esphome::qmp6988::QMP6988Component esphome::qwiic_pir::QwiicPIRComponent esphome::rc522_i2c::RC522I2C esphome::sen0321_sensor::Sen0321Sensor esphome::sen21231_sensor::Sen21231Sensor esphome::sensirion_common::SensirionI2CDevice esphome::ssd1306_i2c::I2CSSD1306 esphome::ssd1327_i2c::I2CSSD1327 esphome::st7567_i2c::I2CST7567 esphome::sx1509::SX1509Component esphome::tc74::TC74Component esphome::tca9548a::TCA9548AComponent esphome::tca9555::TCA9555Component esphome::tcs34725::TCS34725Component esphome::tee501::TEE501Component esphome::tem3200::TEM3200Component esphome::tlc59208f::TLC59208FOutput esphome::tmp102::TMP102Component esphome::tmp1075::TMP1075Sensor esphome::tmp117::TMP117Component esphome::tof10120::TOF10120Sensor esphome::tsl2561::TSL2561Sensor esphome::tsl2591::TSL2591Component esphome::tt21100::TT21100Touchscreen esphome::ttp229_lsf::TTP229LSFComponent esphome::ufire_ec::UFireECComponent esphome::ufire_ise::UFireISEComponent esphome::veml3235::VEML3235Sensor esphome::veml7700::VEML7700Component esphome::vl53l0x::VL53L0XSensor esphome::weikai_i2c::WeikaiComponentI2C esphome::xgzp68xx::XGZP68XXComponent esphome::xl9535::XL9535Component esphome::zio_ultrasonic::ZioUltrasonicComponent

Public Member Functions

 I2CDevice ()=default
 we use the C++ default constructor More...
 
void set_i2c_address (uint8_t address)
 We store the address of the device on the bus. More...
 
void set_i2c_bus (I2CBus *bus)
 we store the pointer to the I2CBus to use More...
 
I2CRegister reg (uint8_t a_register)
 calls the I2CRegister constructor More...
 
I2CRegister16 reg16 (uint16_t a_register)
 calls the I2CRegister16 constructor More...
 
ErrorCode read (uint8_t *data, size_t len)
 reads an array of bytes from the device using an I2CBus More...
 
ErrorCode read_register (uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
 reads an array of bytes from a specific register in the I²C device More...
 
ErrorCode read_register16 (uint16_t a_register, uint8_t *data, size_t len, bool stop=true)
 reads an array of bytes from a specific register in the I²C device More...
 
ErrorCode write (const uint8_t *data, size_t len, bool stop=true)
 writes an array of bytes to a device using an I2CBus More...
 
ErrorCode write_register (uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
 writes an array of bytes to a specific register in the I²C device More...
 
ErrorCode write_register16 (uint16_t a_register, const uint8_t *data, size_t len, bool stop=true)
 write an array of bytes to a specific register in the I²C device More...
 
bool read_bytes (uint8_t a_register, uint8_t *data, uint8_t len)
 Compat APIs All methods below have been added for compatibility reasons. More...
 
bool read_bytes_raw (uint8_t *data, uint8_t len)
 
template<size_t N>
optional< std::array< uint8_t, N > > read_bytes (uint8_t a_register)
 
template<size_t N>
optional< std::array< uint8_t, N > > read_bytes_raw ()
 
bool read_bytes_16 (uint8_t a_register, uint16_t *data, uint8_t len)
 
bool read_byte (uint8_t a_register, uint8_t *data, bool stop=true)
 
optional< uint8_t > read_byte (uint8_t a_register)
 
bool read_byte_16 (uint8_t a_register, uint16_t *data)
 
bool write_bytes (uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
 
bool write_bytes (uint8_t a_register, const std::vector< uint8_t > &data)
 
template<size_t N>
bool write_bytes (uint8_t a_register, const std::array< uint8_t, N > &data)
 
bool write_bytes_16 (uint8_t a_register, const uint16_t *data, uint8_t len)
 
bool write_byte (uint8_t a_register, uint8_t data, bool stop=true)
 
bool write_byte_16 (uint8_t a_register, uint16_t data)
 

Protected Attributes

uint8_t address_ {0x00}
 store the address of the device on the bus More...
 
I2CBusbus_ {nullptr}
 pointer to I2CBus instance More...
 

Detailed Description

This Class provides the methods to read/write bytes from/to an i2c device.

Objects keep a list of devices found on bus as well as a pointer to the I2CBus in use.

Definition at line 133 of file i2c.h.

Constructor & Destructor Documentation

◆ I2CDevice()

esphome::i2c::I2CDevice::I2CDevice ( )
default

we use the C++ default constructor

Member Function Documentation

◆ read()

ErrorCode esphome::i2c::I2CDevice::read ( uint8_t *  data,
size_t  len 
)
inline

reads an array of bytes from the device using an I2CBus

Parameters
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 160 of file i2c.h.

◆ read_byte() [1/2]

bool esphome::i2c::I2CDevice::read_byte ( uint8_t  a_register,
uint8_t *  data,
bool  stop = true 
)
inline

Definition at line 235 of file i2c.h.

◆ read_byte() [2/2]

optional<uint8_t> esphome::i2c::I2CDevice::read_byte ( uint8_t  a_register)
inline

Definition at line 239 of file i2c.h.

◆ read_byte_16()

bool esphome::i2c::I2CDevice::read_byte_16 ( uint8_t  a_register,
uint16_t *  data 
)
inline

Definition at line 246 of file i2c.h.

◆ read_bytes() [1/2]

bool esphome::i2c::I2CDevice::read_bytes ( uint8_t  a_register,
uint8_t *  data,
uint8_t  len 
)
inline

Compat APIs All methods below have been added for compatibility reasons.

They do not bring any functionality and therefore on new code it is not recommend to use them.

Definition at line 212 of file i2c.h.

◆ read_bytes() [2/2]

template<size_t N>
optional<std::array<uint8_t, N> > esphome::i2c::I2CDevice::read_bytes ( uint8_t  a_register)
inline

Definition at line 218 of file i2c.h.

◆ read_bytes_16()

bool esphome::i2c::I2CDevice::read_bytes_16 ( uint8_t  a_register,
uint16_t *  data,
uint8_t  len 
)

Definition at line 44 of file i2c.cpp.

◆ read_bytes_raw() [1/2]

bool esphome::i2c::I2CDevice::read_bytes_raw ( uint8_t *  data,
uint8_t  len 
)
inline

Definition at line 216 of file i2c.h.

◆ read_bytes_raw() [2/2]

template<size_t N>
optional<std::array<uint8_t, N> > esphome::i2c::I2CDevice::read_bytes_raw ( )
inline

Definition at line 225 of file i2c.h.

◆ read_register()

ErrorCode esphome::i2c::I2CDevice::read_register ( uint8_t  a_register,
uint8_t *  data,
size_t  len,
bool  stop = true 
)

reads an array of bytes from a specific register in the I²C device

Parameters
a_registeran 8 bits internal address of the I²C register to read from
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
stop(true/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 10 of file i2c.cpp.

◆ read_register16()

ErrorCode esphome::i2c::I2CDevice::read_register16 ( uint16_t  a_register,
uint8_t *  data,
size_t  len,
bool  stop = true 
)

reads an array of bytes from a specific register in the I²C device

Parameters
a_registerthe 16 bits internal address of the I²C register to read from
datapointer to an array of bytes to store the information
lenlength of the buffer = number of bytes to read
stop(true/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 17 of file i2c.cpp.

◆ reg()

I2CRegister esphome::i2c::I2CDevice::reg ( uint8_t  a_register)
inline

calls the I2CRegister constructor

Parameters
a_registeraddress of the I²C register
Returns
an I2CRegister proxy object

Definition at line 149 of file i2c.h.

◆ reg16()

I2CRegister16 esphome::i2c::I2CDevice::reg16 ( uint16_t  a_register)
inline

calls the I2CRegister16 constructor

Parameters
a_register16 bits address of the I²C register
Returns
an I2CRegister16 proxy object

Definition at line 154 of file i2c.h.

◆ set_i2c_address()

void esphome::i2c::I2CDevice::set_i2c_address ( uint8_t  address)
inline

We store the address of the device on the bus.

Parameters
addressof the device

Definition at line 140 of file i2c.h.

◆ set_i2c_bus()

void esphome::i2c::I2CDevice::set_i2c_bus ( I2CBus bus)
inline

we store the pointer to the I2CBus to use

Parameters
buspointer to the I2CBus object

Definition at line 144 of file i2c.h.

◆ write()

ErrorCode esphome::i2c::I2CDevice::write ( const uint8_t *  data,
size_t  len,
bool  stop = true 
)
inline

writes an array of bytes to a device using an I2CBus

Parameters
datapointer to an array that contains the bytes to send
lenlength of the buffer = number of bytes to write
stop(true/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 186 of file i2c.h.

◆ write_byte()

bool esphome::i2c::I2CDevice::write_byte ( uint8_t  a_register,
uint8_t  data,
bool  stop = true 
)
inline

Definition at line 262 of file i2c.h.

◆ write_byte_16()

bool esphome::i2c::I2CDevice::write_byte_16 ( uint8_t  a_register,
uint16_t  data 
)
inline

Definition at line 266 of file i2c.h.

◆ write_bytes() [1/3]

bool esphome::i2c::I2CDevice::write_bytes ( uint8_t  a_register,
const uint8_t *  data,
uint8_t  len,
bool  stop = true 
)
inline

Definition at line 248 of file i2c.h.

◆ write_bytes() [2/3]

bool esphome::i2c::I2CDevice::write_bytes ( uint8_t  a_register,
const std::vector< uint8_t > &  data 
)
inline

Definition at line 252 of file i2c.h.

◆ write_bytes() [3/3]

template<size_t N>
bool esphome::i2c::I2CDevice::write_bytes ( uint8_t  a_register,
const std::array< uint8_t, N > &  data 
)
inline

Definition at line 256 of file i2c.h.

◆ write_bytes_16()

bool esphome::i2c::I2CDevice::write_bytes_16 ( uint8_t  a_register,
const uint16_t *  data,
uint8_t  len 
)

Definition at line 52 of file i2c.cpp.

◆ write_register()

ErrorCode esphome::i2c::I2CDevice::write_register ( uint8_t  a_register,
const uint8_t *  data,
size_t  len,
bool  stop = true 
)

writes an array of bytes to a specific register in the I²C device

Parameters
a_registerthe internal address of the register to read from
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
stop(true/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 25 of file i2c.cpp.

◆ write_register16()

ErrorCode esphome::i2c::I2CDevice::write_register16 ( uint16_t  a_register,
const uint8_t *  data,
size_t  len,
bool  stop = true 
)

write an array of bytes to a specific register in the I²C device

Parameters
a_registerthe 16 bits internal address of the register to read from
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
stop(true/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 34 of file i2c.cpp.

Field Documentation

◆ address_

uint8_t esphome::i2c::I2CDevice::address_ {0x00}
protected

store the address of the device on the bus

Definition at line 269 of file i2c.h.

◆ bus_

I2CBus* esphome::i2c::I2CDevice::bus_ {nullptr}
protected

pointer to I2CBus instance

Definition at line 270 of file i2c.h.


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