ESPHome  2024.11.3
i2c_sensirion.h
Go to the documentation of this file.
1 #pragma once
3 
4 #include <vector>
5 
6 namespace esphome {
7 namespace sensirion_common {
8 
18  public:
19  enum CommandLen : uint8_t { ADDR_8_BIT = 1, ADDR_16_BIT = 2 };
20 
27  bool read_data(uint16_t *data, uint8_t len);
28 
33  bool read_data(uint16_t &data) { return this->read_data(&data, 1); }
34 
43  bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay = 0) {
44  return get_register_(command, ADDR_16_BIT, data, len, delay);
45  }
52  bool get_register(uint16_t i2c_register, uint16_t &data, uint8_t delay = 0) {
53  return this->get_register_(i2c_register, ADDR_16_BIT, &data, 1, delay);
54  }
55 
64  bool get_8bit_register(uint8_t i2c_register, uint16_t *data, uint8_t len, uint8_t delay = 0) {
65  return get_register_(i2c_register, ADDR_8_BIT, data, len, delay);
66  }
67 
74  bool get_8bit_register(uint8_t i2c_register, uint16_t &data, uint8_t delay = 0) {
75  return this->get_register_(i2c_register, ADDR_8_BIT, &data, 1, delay);
76  }
77 
82  template<class T> bool write_command(T i2c_register) { return write_command(i2c_register, nullptr, 0); }
83 
89  template<class T> bool write_command(T i2c_register, uint16_t data) { return write_command(i2c_register, &data, 1); }
90 
96  template<class T> bool write_command(T i2c_register, const std::vector<uint16_t> &data) {
97  return write_command_(i2c_register, sizeof(T), data.data(), data.size());
98  }
99 
106  template<class T> bool write_command(T i2c_register, const uint16_t *data, uint8_t len) {
107  // limit to 8 or 16 bit only
108  static_assert(sizeof(i2c_register) == 1 || sizeof(i2c_register) == 2,
109  "only 8 or 16 bit command types are supported.");
110  return write_command_(i2c_register, CommandLen(sizeof(T)), data, len);
111  }
112 
113  protected:
114  uint8_t crc_polynomial_{0x31u}; // default for sensirion
122  bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len);
123 
133  bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay);
134 
141  uint8_t sht_crc_(uint16_t data);
142 
149  uint8_t sht_crc_(uint8_t data1, uint8_t data2) { return sht_crc_(encode_uint16(data1, data2)); }
150 
154 };
155 
156 } // namespace sensirion_common
157 } // 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:64
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:74
bool write_command(T i2c_register)
Write a command to the i2c device.
Definition: i2c_sensirion.h:82
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
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:52
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:183
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking...
Definition: i2c_sensirion.h:17
bool write_command(T i2c_register, uint16_t data)
Write a command and one data word to the i2c device .
Definition: i2c_sensirion.h:89
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:293
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:43
bool read_data(uint16_t &data)
Read 1 data word from i2c device.
Definition: i2c_sensirion.h:33
i2c::ErrorCode last_error_
last error code from i2c operation
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
bool write_command(T i2c_register, const std::vector< uint16_t > &data)
Write a command with arguments as words.
Definition: i2c_sensirion.h:96
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26