ESPHome  2024.11.0
Public Member Functions | Protected Member Functions | Protected Attributes
esphome::i2c::I2CRegister Class Reference

This class is used to create I2CRegister objects that act as proxies to read/write internal registers on an I2C device. More...

#include <i2c.h>

Public Member Functions

I2CRegisteroperator= (uint8_t value)
 overloads the = operator. More...
 
I2CRegisteroperator &= (uint8_t value)
 overloads the compound &= operator. More...
 
I2CRegisteroperator|= (uint8_t value)
 overloads the compound |= operator. More...
 
 operator uint8_t () const
 overloads the uint8_t() cast operator to return the I²C register value More...
 
uint8_t get () const
 returns the register value More...
 

Protected Member Functions

 I2CRegister (I2CDevice *parent, uint8_t a_register)
 protected constructor that stores the owning object and the register address. More...
 

Protected Attributes

I2CDeviceparent_
 I2CDevice object pointer. More...
 
uint8_t register_
 the internal address of the register More...
 

Detailed Description

This class is used to create I2CRegister objects that act as proxies to read/write internal registers on an I2C device.


typical usage:

constexpr uint8_t ADDR_REGISTER_1 = 0x12;
i2c::I2CRegister reg_1 = this->reg(ADDR_REGISTER_1); // declare
reg_1 |= 0x01; // set bit
reg_1 &= ~0x01; // reset bit
reg_1 = 10; // Set value
uint val = reg_1.get(); // get value

The I²C protocol specifies how to read/write in sets of 8-bits followed by an Acknowledgement (ACK/NACK) from the device receiving the data. How the device interprets the bits read/written can vary greatly from device to device. However most of the devices follow the same protocol for reading/writing 8 bit registers using as implemented in the I2CRegister: after sending the device address, the controller sends one byte with the internal register address and then read or write the specified register content.

Definition at line 33 of file i2c.h.

Constructor & Destructor Documentation

◆ I2CRegister()

esphome::i2c::I2CRegister::I2CRegister ( I2CDevice parent,
uint8_t  a_register 
)
inlineprotected

protected constructor that stores the owning object and the register address.

Note as only friends can create an I2CRegister

See also
I2CDevice::reg()
Parameters
parentour parent
a_registeraddress of the i2c register

Definition at line 65 of file i2c.h.

Member Function Documentation

◆ get()

uint8_t esphome::i2c::I2CRegister::get ( ) const

returns the register value

Returns
the register value

Definition at line 75 of file i2c.cpp.

◆ operator &=()

I2CRegister& esphome::i2c::I2CRegister::operator&= ( uint8_t  value)

overloads the compound &= operator.

This allows to reset specific bits of an I²C register

Parameters
valueused for the & operation
Returns
pointer to current object

◆ operator uint8_t()

esphome::i2c::I2CRegister::operator uint8_t ( ) const
inlineexplicit

overloads the uint8_t() cast operator to return the I²C register value

Returns
pointer to current object

Definition at line 52 of file i2c.h.

◆ operator=()

I2CRegister & esphome::i2c::I2CRegister::operator= ( uint8_t  value)

overloads the = operator.

This allows to set the value of an i2c register

Parameters
valuevalue to be set in the register
Returns
pointer to current object

Definition at line 60 of file i2c.cpp.

◆ operator|=()

I2CRegister & esphome::i2c::I2CRegister::operator|= ( uint8_t  value)

overloads the compound |= operator.

This allows to set specific bits of an I²C register

Parameters
valueused for the & operation
Returns
pointer to current object

Definition at line 69 of file i2c.cpp.

Field Documentation

◆ parent_

I2CDevice* esphome::i2c::I2CRegister::parent_
protected

I2CDevice object pointer.

Definition at line 67 of file i2c.h.

◆ register_

uint8_t esphome::i2c::I2CRegister::register_
protected

the internal address of the register

Definition at line 68 of file i2c.h.


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