ESPHome  1.16.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, bool send_stop = true);
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 
177  void raw_begin_transmission() { this->parent_->raw_begin_transmission(this->address_); };
178 
180  bool raw_end_transmission(bool send_stop = true) {
181  return this->parent_->raw_end_transmission(this->address_, send_stop);
182  };
183 
185  void raw_write(const uint8_t *data, uint8_t len) { this->parent_->raw_write(this->address_, data, len); };
186 
196  bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion = 0);
197  bool read_bytes_raw(uint8_t *data, uint8_t len) { return this->parent_->read_bytes_raw(this->address_, data, len); }
198 
199  template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
200  std::array<uint8_t, N> res;
201  if (!this->read_bytes(a_register, res.data(), N)) {
202  return {};
203  }
204  return res;
205  }
207  std::array<uint8_t, N> res;
208  if (!this->read_bytes_raw(res.data(), N)) {
209  return {};
210  }
211  return res;
212  }
213 
222  bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len, uint32_t conversion = 0);
223 
225  bool read_byte(uint8_t a_register, uint8_t *data, uint32_t conversion = 0);
226 
227  optional<uint8_t> read_byte(uint8_t a_register) {
228  uint8_t data;
229  if (!this->read_byte(a_register, &data))
230  return {};
231  return data;
232  }
233 
235  bool read_byte_16(uint8_t a_register, uint16_t *data, uint32_t conversion = 0);
236 
244  bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len);
245  bool write_bytes_raw(const uint8_t *data, uint8_t len) {
246  return this->parent_->write_bytes_raw(this->address_, data, len);
247  }
248 
255  bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
256  return this->write_bytes(a_register, data.data(), data.size());
257  }
258  bool write_bytes_raw(const std::vector<uint8_t> &data) { return this->write_bytes_raw(data.data(), data.size()); }
259 
260  template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
261  return this->write_bytes(a_register, data.data(), data.size());
262  }
263  template<size_t N> bool write_bytes_raw(const std::array<uint8_t, N> &data) {
264  return this->write_bytes_raw(data.data(), data.size());
265  }
266 
274  bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len);
275 
277  bool write_byte(uint8_t a_register, uint8_t data);
278 
280  bool write_byte_16(uint8_t a_register, uint16_t data);
281 
282  protected:
283  uint8_t address_{0x00};
284  I2CComponent *parent_{nullptr};
285 };
286 
287 } // namespace i2c
288 } // namespace esphome
void dump_config() override
Definition: i2c.cpp:27
bool write_bytes_raw(const uint8_t *data, uint8_t len)
Definition: i2c.h:245
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition: i2c.h:199
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 raw_begin_transmission()
Begin a write transmission.
Definition: i2c.h:177
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:206
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:197
void raw_write(const uint8_t *data, uint8_t len)
Write len amount of bytes from data. begin_transmission_ must be called before this.
Definition: i2c.h:185
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:260
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:227
bool write_bytes_raw(const std::array< uint8_t, N > &data)
Definition: i2c.h:263
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:255
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 raw_end_transmission(bool send_stop=true)
End a write transmission, return true if successful.
Definition: i2c.h:180
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
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
bool raw_end_transmission(uint8_t address, bool send_stop=true)
End a write transmission to an address, return true if successful.
Definition: i2c.cpp:59
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 write_bytes_raw(const std::vector< uint8_t > &data)
Definition: i2c.h:258