ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <vector>
7#include "i2c_bus.h"
8
9namespace esphome::i2c {
10
11#define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
12
13class I2CDevice; // forward declaration
14
33 public:
37 I2CRegister &operator=(uint8_t value);
38
42 I2CRegister &operator&=(uint8_t value);
43
47 I2CRegister &operator|=(uint8_t value);
48
51 explicit operator uint8_t() const { return get(); }
52
55 uint8_t get() const;
56
57 protected:
58 friend class I2CDevice;
59
64 I2CRegister(I2CDevice *parent, uint8_t a_register) : parent_(parent), register_(a_register) {}
65
67 uint8_t register_;
68};
69
88 public:
92 I2CRegister16 &operator=(uint8_t value);
93
97 I2CRegister16 &operator&=(uint8_t value);
98
102 I2CRegister16 &operator|=(uint8_t value);
103
106 explicit operator uint8_t() const { return get(); }
107
110 uint8_t get() const;
111
112 protected:
113 friend class I2CDevice;
114
119 I2CRegister16(I2CDevice *parent, uint16_t a_register) : parent_(parent), register_(a_register) {}
120
122 uint16_t register_;
123};
124
125// like ntohs/htons but without including networking headers.
126// ("i2c" byte order is big-endian)
127inline uint16_t i2ctohs(uint16_t i2cshort) { return convert_big_endian(i2cshort); }
128inline uint16_t htoi2cs(uint16_t hostshort) { return convert_big_endian(hostshort); }
129
133 public:
135 I2CDevice() = default;
136
140
143 uint8_t get_i2c_address() const { return this->address_; }
144
147 void set_i2c_bus(I2CBus *bus) { bus_ = bus; }
148
152 I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
153
157 I2CRegister16 reg16(uint16_t a_register) { return {this, a_register}; }
158
163 ErrorCode read(uint8_t *data, size_t len) const { return bus_->write_readv(this->address_, nullptr, 0, data, len); }
164
170 ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len);
171
177 ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len);
178
183 ErrorCode write(const uint8_t *data, size_t len) const {
184 return bus_->write_readv(this->address_, data, len, nullptr, 0);
185 }
186
193 ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const {
194 return bus_->write_readv(this->address_, write_data, write_len, read_data, read_len);
195 }
196
202 ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const;
203
209 ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const;
210
216
217 bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len) {
218 return read_register(a_register, data, len) == ERROR_OK;
219 }
220
221 bool read_bytes_raw(uint8_t *data, uint8_t len) const { return read(data, len) == ERROR_OK; }
222
223 template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
224 std::array<uint8_t, N> res;
225 if (!this->read_bytes(a_register, res.data(), N)) {
226 return {};
227 }
228 return res;
229 }
231 std::array<uint8_t, N> res;
232 if (!this->read_bytes_raw(res.data(), N)) {
233 return {};
234 }
235 return res;
236 }
237
238 bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len);
239
240 bool read_byte(uint8_t a_register, uint8_t *data) { return read_register(a_register, data, 1) == ERROR_OK; }
241
242 optional<uint8_t> read_byte(uint8_t a_register) {
243 uint8_t data;
244 if (!this->read_byte(a_register, &data))
245 return {};
246 return data;
247 }
248
249 bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); }
250
251 bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const {
252 return write_register(a_register, data, len) == ERROR_OK;
253 }
254
255 bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) const {
256 return write_bytes(a_register, data.data(), data.size());
257 }
258
259 template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
260 return write_bytes(a_register, data.data(), data.size());
261 }
262
263 bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const;
264
265 bool write_byte(uint8_t a_register, uint8_t data) const { return write_bytes(a_register, &data, 1); }
266
267 bool write_byte_16(uint8_t a_register, uint16_t data) const { return write_bytes_16(a_register, &data, 1); }
268
269 protected:
270 uint8_t address_{0x00};
271 I2CBus *bus_{nullptr};
272};
273
274} // namespace esphome::i2c
uint8_t address
Definition bl0906.h:4
This Class provides the methods to read and write bytes from an I2CBus.
Definition i2c_bus.h:29
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 ReadBu...
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const
writes an array of bytes to a device, then reads an array, as a single transaction
Definition i2c.h:193
bool read_bytes_raw(uint8_t *data, uint8_t len) const
Definition i2c.h:221
bool write_bytes(uint8_t a_register, const std::array< uint8_t, N > &data)
Definition i2c.h:259
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:43
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const
Definition i2c.cpp:61
void set_i2c_bus(I2CBus *bus)
we store the pointer to the I2CBus to use
Definition i2c.h:147
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
uint8_t get_i2c_address() const
Returns the I2C address of the object.
Definition i2c.h:143
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:230
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:271
optional< uint8_t > read_byte(uint8_t a_register)
Definition i2c.h:242
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:29
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition i2c.h:223
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:139
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data) const
Definition i2c.h:255
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:53
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
I2CRegister16 reg16(uint16_t a_register)
calls the I2CRegister16 constructor
Definition i2c.h:157
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
I2CDevice()=default
we use the C++ default constructor
This class is used to create I2CRegister16 objects that act as proxies to read/write internal registe...
Definition i2c.h:87
uint16_t register_
the internal 16 bits address of the register
Definition i2c.h:122
I2CRegister16(I2CDevice *parent, uint16_t a_register)
protected constructor that store the owning object and the register address.
Definition i2c.h:119
I2CRegister16 & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:94
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:99
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:121
uint8_t get() const
returns the register value
Definition i2c.cpp:105
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:90
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
Definition i2c.h:32
I2CRegister(I2CDevice *parent, uint8_t a_register)
protected constructor that stores the owning object and the register address.
Definition i2c.h:64
I2CRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:78
uint8_t get() const
returns the register value
Definition i2c.cpp:84
I2CRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:73
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:66
uint8_t register_
the internal address of the register
Definition i2c.h:67
I2CRegister & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:69
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:127
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
uint16_t htoi2cs(uint16_t hostshort)
Definition i2c.h:128
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:709
std::string size_t len
Definition helpers.h:817