ESPHome 2025.5.0
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1#pragma once
2
3#include "i2c_bus.h"
6#include <array>
7#include <vector>
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) { return bus_->read(address_, data, len); }
165
173 ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop = true);
174
182 ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len, bool stop = true);
183
190 ErrorCode write(const uint8_t *data, size_t len, bool stop = true) { return bus_->write(address_, data, len, stop); }
191
199 ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop = true);
200
208 ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len, bool stop = true);
209
215
216 bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len) {
217 return read_register(a_register, data, len) == ERROR_OK;
218 }
219
220 bool read_bytes_raw(uint8_t *data, uint8_t len) { return read(data, len) == ERROR_OK; }
221
222 template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
223 std::array<uint8_t, N> res;
224 if (!this->read_bytes(a_register, res.data(), N)) {
225 return {};
226 }
227 return res;
228 }
230 std::array<uint8_t, N> res;
231 if (!this->read_bytes_raw(res.data(), N)) {
232 return {};
233 }
234 return res;
235 }
236
237 bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len);
238
239 bool read_byte(uint8_t a_register, uint8_t *data, bool stop = true) {
240 return read_register(a_register, data, 1, stop) == ERROR_OK;
241 }
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, bool stop = true) {
253 return write_register(a_register, data, len, stop) == ERROR_OK;
254 }
255
256 bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
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);
265
266 bool write_byte(uint8_t a_register, uint8_t data, bool stop = true) {
267 return write_bytes(a_register, &data, 1, stop);
268 }
269
270 bool write_byte_16(uint8_t a_register, uint16_t data) { return write_bytes_16(a_register, &data, 1); }
271
272 protected:
273 uint8_t address_{0x00};
274 I2CBus *bus_{nullptr};
275};
276
277} // namespace i2c
278} // 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:40
virtual ErrorCode read(uint8_t address, uint8_t *buffer, size_t len)
Creates a ReadBuffer and calls the virtual readv() method to read bytes into this buffer.
Definition i2c_bus.h:47
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len)
Definition i2c_bus.h:62
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
bool read_bytes_raw(uint8_t *data, uint8_t len)
Definition i2c.h:220
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
Definition i2c.cpp:25
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
bool write_bytes(uint8_t a_register, const std::array< uint8_t, N > &data)
Definition i2c.h:260
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data)
Definition i2c.h:256
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len)
Definition i2c.cpp:52
void set_i2c_bus(I2CBus *bus)
we store the pointer to the I2CBus to use
Definition i2c.h:148
uint8_t get_i2c_address() const
Returns the I2C address of the object.
Definition i2c.h:144
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
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
Definition i2c.cpp:34
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:229
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:274
optional< uint8_t > read_byte(uint8_t a_register)
Definition i2c.h:243
uint8_t address_
store the address of the device on the bus
Definition i2c.h:273
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
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
Definition i2c.cpp:17
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_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
Definition i2c.cpp:10
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition i2c.h:222
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:140
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:44
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
I2CRegister16 reg16(uint16_t a_register)
calls the I2CRegister16 constructor
Definition i2c.h:158
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
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:216
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
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:85
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:90
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:122
uint8_t get() const
returns the register value
Definition i2c.cpp:96
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:81
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:69
uint8_t get() const
returns the register value
Definition i2c.cpp:75
I2CRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:64
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:60
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
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
std::string size_t len
Definition helpers.h:301
constexpr14 T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:248