ESPHome  2022.11.5
i2c_sensirion.h
Go to the documentation of this file.
1 #pragma once
3 
4 namespace esphome {
5 namespace sensirion_common {
6 
16  public:
17  enum CommandLen : uint8_t { ADDR_8_BIT = 1, ADDR_16_BIT = 2 };
18 
25  bool read_data(uint16_t *data, uint8_t len);
26 
31  bool read_data(uint16_t &data) { return this->read_data(&data, 1); }
32 
41  bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay = 0) {
42  return get_register_(command, ADDR_16_BIT, data, len, delay);
43  }
50  bool get_register(uint16_t i2c_register, uint16_t &data, uint8_t delay = 0) {
51  return this->get_register_(i2c_register, ADDR_16_BIT, &data, 1, delay);
52  }
53 
62  bool get_8bit_register(uint8_t i2c_register, uint16_t *data, uint8_t len, uint8_t delay = 0) {
63  return get_register_(i2c_register, ADDR_8_BIT, data, len, delay);
64  }
65 
72  bool get_8bit_register(uint8_t i2c_register, uint16_t &data, uint8_t delay = 0) {
73  return this->get_register_(i2c_register, ADDR_8_BIT, &data, 1, delay);
74  }
75 
80  template<class T> bool write_command(T i2c_register) { return write_command(i2c_register, nullptr, 0); }
81 
87  template<class T> bool write_command(T i2c_register, uint16_t data) { return write_command(i2c_register, &data, 1); }
88 
94  template<class T> bool write_command(T i2c_register, const std::vector<uint16_t> &data) {
95  return write_command_(i2c_register, sizeof(T), data.data(), data.size());
96  }
97 
104  template<class T> bool write_command(T i2c_register, const uint16_t *data, uint8_t len) {
105  // limit to 8 or 16 bit only
106  static_assert(sizeof(i2c_register) == 1 || sizeof(i2c_register) == 2,
107  "only 8 or 16 bit command types are supported.");
108  return write_command_(i2c_register, CommandLen(sizeof(T)), data, len);
109  }
110 
111  protected:
112  uint8_t crc_polynomial_{0x31u}; // default for sensirion
120  bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len);
121 
131  bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay);
132 
139  uint8_t sht_crc_(uint16_t data);
140 
147  uint8_t sht_crc_(uint8_t data1, uint8_t data2) { return sht_crc_(encode_uint16(data1, data2)); }
148 
152 };
153 
154 } // namespace sensirion_common
155 } // namespace esphome
bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay)
get data words from i2c register.
bool get_8bit_register(uint8_t i2c_register, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from i2c register.
Definition: i2c_sensirion.h:62
bool get_8bit_register(uint8_t i2c_register, uint16_t &data, uint8_t delay=0)
Read 1 data word from 8 bit i2c register.
Definition: i2c_sensirion.h:72
bool write_command(T i2c_register)
Write a command to the i2c device.
Definition: i2c_sensirion.h:80
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
bool get_register(uint16_t i2c_register, uint16_t &data, uint8_t delay=0)
Read 1 data word from 16 bit i2c register.
Definition: i2c_sensirion.h:50
bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len)
Write a command with arguments as words.
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
uint8_t sht_crc_(uint16_t data)
8-bit CRC checksum that is transmitted after each data word for read and write operation ...
uint8_t sht_crc_(uint8_t data1, uint8_t data2)
8-bit CRC checksum that is transmitted after each data word for read and write operation ...
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition: helpers.h:171
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking...
Definition: i2c_sensirion.h:15
bool write_command(T i2c_register, uint16_t data)
Write a command and one data word to the i2c device .
Definition: i2c_sensirion.h:87
bool write_command(T i2c_register, const uint16_t *data, uint8_t len)
Write a command with arguments as words.
std::string size_t len
Definition: helpers.h:281
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from i2c register.
Definition: i2c_sensirion.h:41
bool read_data(uint16_t &data)
Read 1 data word from i2c device.
Definition: i2c_sensirion.h:31
i2c::ErrorCode last_error_
last error code from i2c operation
Definition: a4988.cpp:4
bool write_command(T i2c_register, const std::vector< uint16_t > &data)
Write a command with arguments as words.
Definition: i2c_sensirion.h:94
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27