ESPHome 2025.8.2
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 {
10namespace i2c {
11
12#define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
13
14class I2CDevice; // forward declaration
15
34 public:
38 I2CRegister &operator=(uint8_t value);
39
43 I2CRegister &operator&=(uint8_t value);
44
48 I2CRegister &operator|=(uint8_t value);
49
52 explicit operator uint8_t() const { return get(); }
53
56 uint8_t get() const;
57
58 protected:
59 friend class I2CDevice;
60
65 I2CRegister(I2CDevice *parent, uint8_t a_register) : parent_(parent), register_(a_register) {}
66
68 uint8_t register_;
69};
70
89 public:
93 I2CRegister16 &operator=(uint8_t value);
94
98 I2CRegister16 &operator&=(uint8_t value);
99
103 I2CRegister16 &operator|=(uint8_t value);
104
107 explicit operator uint8_t() const { return get(); }
108
111 uint8_t get() const;
112
113 protected:
114 friend class I2CDevice;
115
120 I2CRegister16(I2CDevice *parent, uint16_t a_register) : parent_(parent), register_(a_register) {}
121
123 uint16_t register_;
124};
125
126// like ntohs/htons but without including networking headers.
127// ("i2c" byte order is big-endian)
128inline uint16_t i2ctohs(uint16_t i2cshort) { return convert_big_endian(i2cshort); }
129inline uint16_t htoi2cs(uint16_t hostshort) { return convert_big_endian(hostshort); }
130
134 public:
136 I2CDevice() = default;
137
141
144 uint8_t get_i2c_address() const { return this->address_; }
145
148 void set_i2c_bus(I2CBus *bus) { bus_ = bus; }
149
153 I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
154
158 I2CRegister16 reg16(uint16_t a_register) { return {this, a_register}; }
159
164 ErrorCode read(uint8_t *data, size_t len) const { return bus_->write_readv(this->address_, nullptr, 0, data, len); }
165
171 ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len);
172
178 ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len);
179
184 ErrorCode write(const uint8_t *data, size_t len) const {
185 return bus_->write_readv(this->address_, data, len, nullptr, 0);
186 }
187
194 ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const {
195 return bus_->write_readv(this->address_, write_data, write_len, read_data, read_len);
196 }
197
203 ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const;
204
210 ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const;
211
217
218 bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len) {
219 return read_register(a_register, data, len) == ERROR_OK;
220 }
221
222 bool read_bytes_raw(uint8_t *data, uint8_t len) const { return read(data, len) == ERROR_OK; }
223
224 template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
225 std::array<uint8_t, N> res;
226 if (!this->read_bytes(a_register, res.data(), N)) {
227 return {};
228 }
229 return res;
230 }
232 std::array<uint8_t, N> res;
233 if (!this->read_bytes_raw(res.data(), N)) {
234 return {};
235 }
236 return res;
237 }
238
239 bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len);
240
241 bool read_byte(uint8_t a_register, uint8_t *data) { return read_register(a_register, data, 1) == ERROR_OK; }
242
243 optional<uint8_t> read_byte(uint8_t a_register) {
244 uint8_t data;
245 if (!this->read_byte(a_register, &data))
246 return {};
247 return data;
248 }
249
250 bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); }
251
252 bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const {
253 return write_register(a_register, data, len) == ERROR_OK;
254 }
255
256 bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) const {
257 return write_bytes(a_register, data.data(), data.size());
258 }
259
260 template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
261 return write_bytes(a_register, data.data(), data.size());
262 }
263
264 bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const;
265
266 bool write_byte(uint8_t a_register, uint8_t data) const { return write_bytes(a_register, &data, 1); }
267
268 bool write_byte_16(uint8_t a_register, uint16_t data) const { return write_bytes_16(a_register, &data, 1); }
269
270 // Deprecated functions
271
272 ESPDEPRECATED("The stop argument is no longer used. This will be removed from ESPHome 2026.3.0", "2025.9.0")
273 ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop) {
274 return this->read_register(a_register, data, len);
275 }
276
277 ESPDEPRECATED("The stop argument is no longer used. This will be removed from ESPHome 2026.3.0", "2025.9.0")
278 ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len, bool stop) {
279 return this->read_register16(a_register, data, len);
280 }
281
282 ESPDEPRECATED("The stop argument is no longer used; use write_read() for consecutive write and read. This will be "
283 "removed from ESPHome 2026.3.0",
284 "2025.9.0")
285 ErrorCode write(const uint8_t *data, size_t len, bool stop) const { return this->write(data, len); }
286
287 ESPDEPRECATED("The stop argument is no longer used; use write_read() for consecutive write and read. This will be "
288 "removed from ESPHome 2026.3.0",
289 "2025.9.0")
290 ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop) const {
291 return this->write_register(a_register, data, len);
292 }
293
294 ESPDEPRECATED("The stop argument is no longer used; use write_read() for consecutive write and read. This will be "
295 "removed from ESPHome 2026.3.0",
296 "2025.9.0")
297 ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len, bool stop) const {
298 return this->write_register16(a_register, data, len);
299 }
300
301 protected:
302 uint8_t address_{0x00};
303 I2CBus *bus_{nullptr};
304};
305
306} // namespace i2c
307} // namespace esphome
uint8_t address
Definition bl0906.h:4
This Class provides the methods to read and write bytes from an I2CBus.
Definition i2c_bus.h:43
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:133
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:41
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:194
ESPDEPRECATED("The stop argument is no longer used. This will be removed from ESPHome 2026.3.0", "2025.9.0") ErrorCode read_register16(uint16_t a_register
bool read_bytes_raw(uint8_t *data, uint8_t len) const
Definition i2c.h:222
ESPDEPRECATED("The stop argument is no longer used. This will be removed from ESPHome 2026.3.0", "2025.9.0") ErrorCode read_register(uint8_t a_register
bool write_bytes(uint8_t a_register, const std::array< uint8_t, N > &data)
Definition i2c.h:260
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:48
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const
Definition i2c.cpp:64
void set_i2c_bus(I2CBus *bus)
we store the pointer to the I2CBus to use
Definition i2c.h:148
ESPDEPRECATED("The stop argument is no longer used; use write_read() for consecutive write and read. This will be " "removed from ESPHome 2026.3.0", "2025.9.0") ErrorCode write(const uint8_t *data
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
uint8_t get_i2c_address() const
Returns the I2C address of the object.
Definition i2c.h:144
size_t bool stop const
Definition i2c.h:285
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:231
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:303
optional< uint8_t > read_byte(uint8_t a_register)
Definition i2c.h:243
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
uint8_t address_
store the address of the device on the bus
Definition i2c.h:302
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:241
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
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:36
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition i2c.h:224
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:140
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data) const
Definition i2c.h:256
ESPDEPRECATED("The stop argument is no longer used; use write_read() for consecutive write and read. This will be " "removed from ESPHome 2026.3.0", "2025.9.0") ErrorCode write_register(uint8_t a_register
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:32
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:56
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:252
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:268
I2CRegister16 reg16(uint16_t a_register)
calls the I2CRegister16 constructor
Definition i2c.h:158
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:218
uint8_t size_t bool stop
Definition i2c.h:273
uint8_t size_t len
Definition i2c.h:273
ESPDEPRECATED("The stop argument is no longer used; use write_read() for consecutive write and read. This will be " "removed from ESPHome 2026.3.0", "2025.9.0") ErrorCode write_register16(uint16_t a_register
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:88
uint16_t register_
the internal 16 bits address of the register
Definition i2c.h:123
I2CRegister16(I2CDevice *parent, uint16_t a_register)
protected constructor that store the owning object and the register address.
Definition i2c.h:120
I2CRegister16 & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:97
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:102
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:122
uint8_t get() const
returns the register value
Definition i2c.cpp:108
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:93
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
Definition i2c.h:33
I2CRegister(I2CDevice *parent, uint8_t a_register)
protected constructor that stores the owning object and the register address.
Definition i2c.h:65
I2CRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:81
uint8_t get() const
returns the register value
Definition i2c.cpp:87
I2CRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:76
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:67
uint8_t register_
the internal address of the register
Definition i2c.h:68
I2CRegister & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:72
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:14
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:16
uint16_t htoi2cs(uint16_t hostshort)
Definition i2c.h:129
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:229