ESPHome  2021.9.0
i2c.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Wire.h>
4 #include "esphome/core/defines.h"
6 #include "esphome/core/helpers.h"
7 
8 namespace esphome {
9 namespace i2c {
10 
11 #define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
12 
27 class I2CComponent : public Component {
28  public:
29  I2CComponent();
30  void set_sda_pin(uint8_t sda_pin) { sda_pin_ = sda_pin; }
31  void set_scl_pin(uint8_t scl_pin) { scl_pin_ = scl_pin; }
32  void set_frequency(uint32_t frequency) { frequency_ = frequency; }
33  void set_scan(bool scan) { scan_ = scan; }
34 
45  bool read_bytes(uint8_t address, uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion = 0);
46  bool read_bytes_raw(uint8_t address, uint8_t *data, uint8_t len);
47 
57  bool read_bytes_16(uint8_t address, uint8_t a_register, uint16_t *data, uint8_t len, uint32_t conversion = 0);
58 
60  bool read_byte(uint8_t address, uint8_t a_register, uint8_t *data, uint32_t conversion = 0);
61 
63  bool read_byte_16(uint8_t address, uint8_t a_register, uint16_t *data, uint32_t conversion = 0);
64 
73  bool write_bytes(uint8_t address, uint8_t a_register, const uint8_t *data, uint8_t len);
74  bool write_bytes_raw(uint8_t address, const uint8_t *data, uint8_t len);
75 
84  bool write_bytes_16(uint8_t address, uint8_t a_register, const uint16_t *data, uint8_t len);
85 
87  bool write_byte(uint8_t address, uint8_t a_register, uint8_t data);
88 
90  bool write_byte_16(uint8_t address, uint8_t a_register, uint16_t data);
91 
92  // ========== INTERNAL METHODS ==========
93  // (In most use cases you won't need these)
95  void raw_begin_transmission(uint8_t address);
96 
98  bool raw_end_transmission(uint8_t address, bool send_stop = true);
99 
106  bool raw_request_from(uint8_t address, uint8_t len);
107 
109  void raw_write(uint8_t address, const uint8_t *data, uint8_t len);
110 
112  void raw_write_16(uint8_t address, const uint16_t *data, uint8_t len);
113 
115  bool raw_receive(uint8_t address, uint8_t *data, uint8_t len);
116 
118  bool raw_receive_16(uint8_t address, uint16_t *data, uint8_t len);
119 
121  void setup() override;
122  void dump_config() override;
124  float get_setup_priority() const override;
125 
126  protected:
127  TwoWire *wire_;
128  uint8_t sda_pin_;
129  uint8_t scl_pin_;
130  uint32_t frequency_;
131  bool scan_;
132 };
133 
134 #ifdef ARDUINO_ARCH_ESP32
135 extern uint8_t next_i2c_bus_num_;
136 #endif
137 
138 class I2CDevice;
139 class I2CMultiplexer;
140 class I2CRegister {
141  public:
142  I2CRegister(I2CDevice *parent, uint8_t a_register) : parent_(parent), register_(a_register) {}
143 
144  I2CRegister &operator=(uint8_t value);
145  I2CRegister &operator=(const std::vector<uint8_t> &value);
146  I2CRegister &operator&=(uint8_t value);
147  I2CRegister &operator|=(uint8_t value);
148 
149  uint8_t get();
150 
151  protected:
153  uint8_t register_;
154 };
155 
164 class I2CDevice {
165  public:
166  I2CDevice() = default;
167  I2CDevice(I2CComponent *parent, uint8_t address) : address_(address), parent_(parent) {}
168 
170  void set_i2c_address(uint8_t address);
171 #ifdef USE_I2C_MULTIPLEXER
172  void set_i2c_multiplexer(I2CMultiplexer *multiplexer, uint8_t channel);
174 #endif
175  void set_i2c_parent(I2CComponent *parent);
177 
178  I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
179 
181  void raw_begin_transmission();
182 
184  bool raw_end_transmission(bool send_stop = true);
185 
187  void raw_write(const uint8_t *data, uint8_t len);
188 
198  bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion = 0);
199  bool read_bytes_raw(uint8_t *data, uint8_t len);
200 
201  template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
202  std::array<uint8_t, N> res;
203  if (!this->read_bytes(a_register, res.data(), N)) {
204  return {};
205  }
206  return res;
207  }
209  std::array<uint8_t, N> res;
210  if (!this->read_bytes_raw(res.data(), N)) {
211  return {};
212  }
213  return res;
214  }
215 
224  bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len, uint32_t conversion = 0);
225 
227  bool read_byte(uint8_t a_register, uint8_t *data, uint32_t conversion = 0);
228 
229  optional<uint8_t> read_byte(uint8_t a_register) {
230  uint8_t data;
231  if (!this->read_byte(a_register, &data))
232  return {};
233  return data;
234  }
235 
237  bool read_byte_16(uint8_t a_register, uint16_t *data, uint32_t conversion = 0);
238 
246  bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len);
247  bool write_bytes_raw(const uint8_t *data, uint8_t len);
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  // Checks for multiplexer set and set channel
284  void check_multiplexer_();
285  uint8_t address_{0x00};
286  I2CComponent *parent_{nullptr};
287 #ifdef USE_I2C_MULTIPLEXER
288  I2CMultiplexer *multiplexer_{nullptr};
289  uint8_t channel_;
290 #endif
291 };
292 class I2CMultiplexer : public I2CDevice {
293  public:
294  I2CMultiplexer() = default;
295  virtual void set_channel(uint8_t channelno);
296 };
297 } // namespace i2c
298 } // namespace esphome
void dump_config() override
Definition: i2c.cpp:27
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition: i2c.h:201
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:142
void set_frequency(uint32_t frequency)
Definition: i2c.h:32
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:178
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:208
uint8_t next_i2c_bus_num_
Definition: i2c.cpp:277
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:33
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 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:31
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:30
optional< uint8_t > read_byte(uint8_t a_register)
Definition: i2c.h:229
uint8_t channel_
Definition: i2c.h:289
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:167
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:152
The I2CComponent is the base of ESPHome&#39;s i2c communication.
Definition: i2c.h:27
All components doing communication on the I2C bus should subclass I2CDevice.
Definition: i2c.h:164
bool write_bytes_raw(const std::vector< uint8_t > &data)
Definition: i2c.h:258