ESPHome  2022.11.4
i2c.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "i2c_bus.h"
4 #include "esphome/core/helpers.h"
6 #include <array>
7 #include <vector>
8 
9 namespace esphome {
10 namespace i2c {
11 
12 #define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
13 
14 class I2CDevice;
15 class I2CRegister {
16  public:
17  I2CRegister &operator=(uint8_t value);
18  I2CRegister &operator&=(uint8_t value);
19  I2CRegister &operator|=(uint8_t value);
20 
21  explicit operator uint8_t() const { return get(); }
22 
23  uint8_t get() const;
24 
25  protected:
26  friend class I2CDevice;
27 
28  I2CRegister(I2CDevice *parent, uint8_t a_register) : parent_(parent), register_(a_register) {}
29 
31  uint8_t register_;
32 };
33 
34 // like ntohs/htons but without including networking headers.
35 // ("i2c" byte order is big-endian)
36 inline uint16_t i2ctohs(uint16_t i2cshort) { return convert_big_endian(i2cshort); }
37 inline uint16_t htoi2cs(uint16_t hostshort) { return convert_big_endian(hostshort); }
38 
39 class I2CDevice {
40  public:
41  I2CDevice() = default;
42 
43  void set_i2c_address(uint8_t address) { address_ = address; }
44  void set_i2c_bus(I2CBus *bus) { bus_ = bus; }
45 
46  I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
47 
48  ErrorCode read(uint8_t *data, size_t len) { return bus_->read(address_, data, len); }
49  ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop = true) {
50  ErrorCode err = this->write(&a_register, 1, stop);
51  if (err != ERROR_OK)
52  return err;
53  return this->read(data, len);
54  }
55 
56  ErrorCode write(const uint8_t *data, uint8_t len, bool stop = true) { return bus_->write(address_, data, len, stop); }
57  ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop = true) {
58  WriteBuffer buffers[2];
59  buffers[0].data = &a_register;
60  buffers[0].len = 1;
61  buffers[1].data = data;
62  buffers[1].len = len;
63  return bus_->writev(address_, buffers, 2, stop);
64  }
65 
66  // Compat APIs
67 
68  bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len) {
69  return read_register(a_register, data, len) == ERROR_OK;
70  }
71  bool read_bytes_raw(uint8_t *data, uint8_t len) { return read(data, len) == ERROR_OK; }
72 
73  template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
74  std::array<uint8_t, N> res;
75  if (!this->read_bytes(a_register, res.data(), N)) {
76  return {};
77  }
78  return res;
79  }
81  std::array<uint8_t, N> res;
82  if (!this->read_bytes_raw(res.data(), N)) {
83  return {};
84  }
85  return res;
86  }
87 
88  bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len) {
89  if (read_register(a_register, reinterpret_cast<uint8_t *>(data), len * 2) != ERROR_OK)
90  return false;
91  for (size_t i = 0; i < len; i++)
92  data[i] = i2ctohs(data[i]);
93  return true;
94  }
95 
96  bool read_byte(uint8_t a_register, uint8_t *data, bool stop = true) {
97  return read_register(a_register, data, 1, stop) == ERROR_OK;
98  }
99 
100  optional<uint8_t> read_byte(uint8_t a_register) {
101  uint8_t data;
102  if (!this->read_byte(a_register, &data))
103  return {};
104  return data;
105  }
106 
107  bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); }
108 
109  bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop = true) {
110  return write_register(a_register, data, len, stop) == ERROR_OK;
111  }
112 
113  bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
114  return write_bytes(a_register, data.data(), data.size());
115  }
116 
117  template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
118  return write_bytes(a_register, data.data(), data.size());
119  }
120 
121  bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len);
122 
123  bool write_byte(uint8_t a_register, uint8_t data, bool stop = true) {
124  return write_bytes(a_register, &data, 1, stop);
125  }
126 
127  bool write_byte_16(uint8_t a_register, uint16_t data) { return write_bytes_16(a_register, &data, 1); }
128 
129  protected:
130  uint8_t address_{0x00};
131  I2CBus *bus_{nullptr};
132 };
133 
134 } // namespace i2c
135 } // namespace esphome
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition: i2c.h:73
I2CRegister(I2CDevice *parent, uint8_t a_register)
Definition: i2c.h:28
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:96
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:107
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:36
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
Definition: i2c.h:49
friend class I2CDevice
Definition: i2c.h:26
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
ErrorCode read(uint8_t *data, size_t len)
Definition: i2c.h:48
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:80
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Definition: i2c.h:68
bool read_bytes_raw(uint8_t *data, uint8_t len)
Definition: i2c.h:71
uint16_t htoi2cs(uint16_t hostshort)
Definition: i2c.h:37
I2CRegister & operator|=(uint8_t value)
Definition: i2c.cpp:27
bool write_bytes(uint8_t a_register, const std::array< uint8_t, N > &data)
Definition: i2c.h:117
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:228
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
optional< uint8_t > read_byte(uint8_t a_register)
Definition: i2c.h:100
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data)
Definition: i2c.h:113
std::string size_t len
Definition: helpers.h:281
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:123
const uint8_t * data
Definition: i2c_bus.h:26
Definition: a4988.cpp:4
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
Definition: i2c.h:57
I2CRegister & operator &=(uint8_t value)
I2CDevice * parent_
Definition: i2c.h:30
I2CRegister & operator=(uint8_t value)
Definition: i2c.cpp:18
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:127
void set_i2c_bus(I2CBus *bus)
Definition: i2c.h:44
void set_i2c_address(uint8_t address)
Definition: i2c.h:43
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition: i2c.h:88
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:109