ESPHome  2021.11.3
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) {
50  ErrorCode err = this->write(&a_register, 1);
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) { return bus_->write(address_, data, len); }
57  ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) {
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);
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) { return read_register(a_register, data, 1) == ERROR_OK; }
97 
98  optional<uint8_t> read_byte(uint8_t a_register) {
99  uint8_t data;
100  if (!this->read_byte(a_register, &data))
101  return {};
102  return data;
103  }
104 
105  bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); }
106 
107  bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) {
108  return write_register(a_register, data, len) == ERROR_OK;
109  }
110 
111  bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
112  return write_bytes(a_register, data.data(), data.size());
113  }
114 
115  template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
116  return write_bytes(a_register, data.data(), data.size());
117  }
118 
119  bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len);
120 
121  bool write_byte(uint8_t a_register, uint8_t data) { return write_bytes(a_register, &data, 1); }
122 
123  bool write_byte_16(uint8_t a_register, uint16_t data) { return write_bytes_16(a_register, &data, 1); }
124 
125  protected:
126  uint8_t address_{0x00};
127  I2CBus *bus_{nullptr};
128 };
129 
130 } // namespace i2c
131 } // namespace esphome
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
Definition: i2c.h:73
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order...
Definition: helpers.h:338
I2CRegister(I2CDevice *parent, uint8_t a_register)
Definition: i2c.h:28
bool write_byte(uint8_t a_register, uint8_t data)
Definition: i2c.h:121
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:105
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len)
Definition: i2c.h:107
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:36
friend class I2CDevice
Definition: i2c.h:26
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
Definition: i2c.h:49
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len)
Definition: i2c.h:57
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
ErrorCode write(const uint8_t *data, uint8_t len)
Definition: i2c.h:56
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:115
optional< uint8_t > read_byte(uint8_t a_register)
Definition: i2c.h:98
bool read_byte(uint8_t a_register, uint8_t *data)
Definition: i2c.h:96
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data)
Definition: i2c.h:111
const uint8_t * data
Definition: i2c_bus.h:23
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
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:123
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