ESPHome  1.15.2
i2c.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Wire.h>
5 #include "esphome/core/helpers.h"
6 
7 namespace esphome {
8 namespace i2c {
9 
10 #define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
11 
26 class I2CComponent : public Component {
27  public:
28  I2CComponent();
29  void set_sda_pin(uint8_t sda_pin) { sda_pin_ = sda_pin; }
30  void set_scl_pin(uint8_t scl_pin) { scl_pin_ = scl_pin; }
31  void set_frequency(uint32_t frequency) { frequency_ = frequency; }
32  void set_scan(bool scan) { scan_ = scan; }
33 
44  bool read_bytes(uint8_t address, uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion = 0);
45  bool read_bytes_raw(uint8_t address, uint8_t *data, uint8_t len);
46 
56  bool read_bytes_16(uint8_t address, uint8_t a_register, uint16_t *data, uint8_t len, uint32_t conversion = 0);
57 
59  bool read_byte(uint8_t address, uint8_t a_register, uint8_t *data, uint32_t conversion = 0);
60 
62  bool read_byte_16(uint8_t address, uint8_t a_register, uint16_t *data, uint32_t conversion = 0);
63 
72  bool write_bytes(uint8_t address, uint8_t a_register, const uint8_t *data, uint8_t len);
73  bool write_bytes_raw(uint8_t address, const uint8_t *data, uint8_t len);
74 
83  bool write_bytes_16(uint8_t address, uint8_t a_register, const uint16_t *data, uint8_t len);
84 
86  bool write_byte(uint8_t address, uint8_t a_register, uint8_t data);
87 
89  bool write_byte_16(uint8_t address, uint8_t a_register, uint16_t data);
90 
91  // ========== INTERNAL METHODS ==========
92  // (In most use cases you won't need these)
94  void raw_begin_transmission(uint8_t address);
95 
97  bool raw_end_transmission(uint8_t address);
98 
105  bool raw_request_from(uint8_t address, uint8_t len);
106 
108  void raw_write(uint8_t address, const uint8_t *data, uint8_t len);
109 
111  void raw_write_16(uint8_t address, const uint16_t *data, uint8_t len);
112 
114  bool raw_receive(uint8_t address, uint8_t *data, uint8_t len);
115 
117  bool raw_receive_16(uint8_t address, uint16_t *data, uint8_t len);
118 
120  void setup() override;
121  void dump_config() override;
123  float get_setup_priority() const override;
124 
125  protected:
126  TwoWire *wire_;
127  uint8_t sda_pin_;
128  uint8_t scl_pin_;
129  uint32_t frequency_;
130  bool scan_;
131 };
132 
133 #ifdef ARDUINO_ARCH_ESP32
134 extern uint8_t next_i2c_bus_num_;
135 #endif
136 
137 class I2CDevice;
138 
139 class I2CRegister {
140  public:
141  I2CRegister(I2CDevice *parent, uint8_t a_register) : parent_(parent), register_(a_register) {}
142 
143  I2CRegister &operator=(uint8_t value);
144  I2CRegister &operator=(const std::vector<uint8_t> &value);
145  I2CRegister &operator&=(uint8_t value);
146  I2CRegister &operator|=(uint8_t value);
147 
148  uint8_t get();
149 
150  protected:
152  uint8_t register_;
153 };
154 
163 class I2CDevice {
164  public:
165  I2CDevice() = default;
166  I2CDevice(I2CComponent *parent, uint8_t address) : address_(address), parent_(parent) {}
167 
169  void set_i2c_address(uint8_t address);
170 
172  void set_i2c_parent(I2CComponent *parent);
173 
174  I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
175 
185  bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion = 0);
186  bool read_bytes_raw(uint8_t *data, uint8_t len) { return this->parent_->read_bytes_raw(this->address_, data, len); }
187 
188  template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
189  std::array<uint8_t, N> res;
190  if (!this->read_bytes(a_register, res.data(), N)) {
191  return {};
192  }
193  return res;
194  }
196  std::array<uint8_t, N> res;
197  if (!this->read_bytes_raw(res.data(), N)) {
198  return {};
199  }
200  return res;
201  }
202 
211  bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len, uint32_t conversion = 0);
212 
214  bool read_byte(uint8_t a_register, uint8_t *data, uint32_t conversion = 0);
215 
216  optional<uint8_t> read_byte(uint8_t a_register) {
217  uint8_t data;
218  if (!this->read_byte(a_register, &data))
219  return {};
220  return data;
221  }
222 
224  bool read_byte_16(uint8_t a_register, uint16_t *data, uint32_t conversion = 0);
225 
233  bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len);
234  bool write_bytes_raw(const uint8_t *data, uint8_t len) {
235  return this->parent_->write_bytes_raw(this->address_, data, len);
236  }
237 
244  bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
245  return this->write_bytes(a_register, data.data(), data.size());
246  }
247  bool write_bytes_raw(const std::vector<uint8_t> &data) { return this->write_bytes_raw(data.data(), data.size()); }
248 
249  template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
250  return this->write_bytes(a_register, data.data(), data.size());
251  }
252  template<size_t N> bool write_bytes_raw(const std::array<uint8_t, N> &data) {
253  return this->write_bytes_raw(data.data(), data.size());
254  }
255 
263  bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len);
264 
266  bool write_byte(uint8_t a_register, uint8_t data);
267 
269  bool write_byte_16(uint8_t a_register, uint16_t data);
270 
271  protected:
272  uint8_t address_{0x00};
273  I2CComponent *parent_{nullptr};
274 };
275 
276 } // namespace i2c
277 } // namespace esphome
void dump_config() override
Definition: i2c.cpp:27
bool write_bytes_raw(const uint8_t *data, uint8_t len)
Definition: i2c.h:234
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition: i2c.h:188
bool write_bytes_raw(uint8_t address, const uint8_t *data, uint8_t len)
Definition: i2c.cpp:162
I2CRegister(I2CDevice *parent, uint8_t a_register)
Definition: i2c.h:141
void set_frequency(uint32_t frequency)
Definition: i2c.h:31
float get_setup_priority() const override
Set a very high setup priority to make sure it&#39;s loaded before all other hardware.
Definition: i2c.cpp:53
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:174
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:195
uint8_t next_i2c_bus_num_
Definition: i2c.cpp:208
bool write_bytes_16(uint8_t address, uint8_t a_register, const uint16_t *data, uint8_t len)
Write len amount of 16-bit words (MSB first) to the specified register for address.
Definition: i2c.cpp:167
void set_scan(bool scan)
Definition: i2c.h:32
bool write_byte(uint8_t address, uint8_t a_register, uint8_t data)
Write a single byte of data into the specified register of address. Return true if successful...
Definition: i2c.cpp:173
bool read_bytes_raw(uint8_t *data, uint8_t len)
Definition: i2c.h:186
bool read_bytes_raw(uint8_t address, uint8_t *data, uint8_t len)
Definition: i2c.cpp:138
void raw_write(uint8_t address, const uint8_t *data, uint8_t len)
Write len amount of bytes from data to address. begin_transmission_ must be called before this...
Definition: i2c.cpp:91
bool write_bytes(uint8_t a_register, const std::array< uint8_t, N > &data)
Definition: i2c.h:249
void raw_begin_transmission(uint8_t address)
Begin a write transmission to an address.
Definition: i2c.cpp:55
bool read_bytes(uint8_t address, uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion=0)
Read len amount of bytes from a register into data.
Definition: i2c.cpp:130
void set_scl_pin(uint8_t scl_pin)
Definition: i2c.h:30
void raw_write_16(uint8_t address, const uint16_t *data, uint8_t len)
Write len amount of 16-bit words from data to address. begin_transmission_ must be called before this...
Definition: i2c.cpp:98
bool raw_receive_16(uint8_t address, uint16_t *data, uint8_t len)
Request len amount of 16-bit words from address and write the result into data. Returns true iff was ...
Definition: i2c.cpp:118
bool read_byte_16(uint8_t address, uint8_t a_register, uint16_t *data, uint32_t conversion=0)
Read a single 16-bit words (MSB first) from a register into the data variable. Return true if success...
Definition: i2c.cpp:153
bool write_byte_16(uint8_t address, uint8_t a_register, uint16_t data)
Write a single 16-bit word of data into the specified register of address. Return true if successful...
Definition: i2c.cpp:176
bool read_byte(uint8_t address, uint8_t a_register, uint8_t *data, uint32_t conversion=0)
Read a single byte from a register into the data variable. Return true if successful.
Definition: i2c.cpp:150
void set_sda_pin(uint8_t sda_pin)
Definition: i2c.h:29
optional< uint8_t > read_byte(uint8_t a_register)
Definition: i2c.h:216
bool write_bytes_raw(const std::array< uint8_t, N > &data)
Definition: i2c.h:252
void setup() override
Setup the i2c. bus.
Definition: i2c.cpp:23
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data)
Write a vector of data to a register.
Definition: i2c.h:244
bool raw_request_from(uint8_t address, uint8_t len)
Request data from an address with a number of (8-bit) bytes.
Definition: i2c.cpp:82
I2CDevice(I2CComponent *parent, uint8_t address)
Definition: i2c.h:166
bool write_bytes(uint8_t address, uint8_t a_register, const uint8_t *data, uint8_t len)
Write len amount of 8-bit bytes to the specified register for address.
Definition: i2c.cpp:156
bool read_bytes_16(uint8_t address, uint8_t a_register, uint16_t *data, uint8_t len, uint32_t conversion=0)
Read len amount of 16-bit words (MSB first) from a register into data.
Definition: i2c.cpp:141
bool raw_receive(uint8_t address, uint8_t *data, uint8_t len)
Request len amount of bytes from address and write the result it into data. Returns true iff was succ...
Definition: i2c.cpp:108
Definition: a4988.cpp:4
I2CDevice * parent_
Definition: i2c.h:151
The I2CComponent is the base of ESPHome&#39;s i2c communication.
Definition: i2c.h:26
All components doing communication on the I2C bus should subclass I2CDevice.
Definition: i2c.h:163
bool raw_end_transmission(uint8_t address)
End a write transmission to an address, return true if successful.
Definition: i2c.cpp:59
bool write_bytes_raw(const std::vector< uint8_t > &data)
Definition: i2c.h:247