ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
i2c_sensirion.h
Go to the documentation of this file.
1#pragma once
4
5#include <vector>
6
8
17static const uint8_t CRC_POLYNOMIAL = 0x31; // default for Sensirion
18
20static constexpr uint8_t SENSIRION_OPTION_READ_MAY_NACK = 1u << 0;
21
23 public:
24 enum CommandLen : uint8_t { ADDR_8_BIT = 1, ADDR_16_BIT = 2 };
25
30 static inline const char *sensirion_convert_to_string_in_place(uint16_t *array, size_t length) {
31 for (size_t i = 0; i < length; i++) {
32 array[i] = convert_big_endian(array[i]);
33 }
34 return reinterpret_cast<const char *>(array);
35 }
36
43 bool read_data(uint16_t *data, uint8_t len);
44
49 bool read_data(uint16_t &data) { return this->read_data(&data, 1); }
50
59 bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay = 0) {
60 return get_register_(command, ADDR_16_BIT, data, len, delay);
61 }
68 bool get_register(uint16_t i2c_register, uint16_t &data, uint8_t delay = 0) {
69 return this->get_register_(i2c_register, ADDR_16_BIT, &data, 1, delay);
70 }
71
80 bool get_8bit_register(uint8_t i2c_register, uint16_t *data, uint8_t len, uint8_t delay = 0) {
81 return get_register_(i2c_register, ADDR_8_BIT, data, len, delay);
82 }
83
90 bool get_8bit_register(uint8_t i2c_register, uint16_t &data, uint8_t delay = 0) {
91 return this->get_register_(i2c_register, ADDR_8_BIT, &data, 1, delay);
92 }
93
98 template<class T> bool write_command(T i2c_register) { return write_command(i2c_register, nullptr, 0); }
99
105 template<class T> bool write_command(T i2c_register, uint16_t data) { return write_command(i2c_register, &data, 1); }
106
112 template<class T> bool write_command(T i2c_register, const std::vector<uint16_t> &data) {
113 return write_command_(i2c_register, sizeof(T), data.data(), data.size());
114 }
115
122 template<class T> bool write_command(T i2c_register, const uint16_t *data, uint8_t len) {
123 // limit to 8 or 16 bit only
124 static_assert(sizeof(i2c_register) == 1 || sizeof(i2c_register) == 2, "Only 8 or 16 bit command types supported");
125 return write_command_(i2c_register, CommandLen(sizeof(T)), data, len);
126 }
127
128 protected:
136 bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len);
137
148 bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay,
149 uint8_t sensirion_options = 0);
150
154};
155
156} // namespace esphome::sensirion_common
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
i2c::ErrorCode last_error_
last error code from I2C operation
bool write_command(T i2c_register, const std::vector< uint16_t > &data)
Write a command with arguments as words.
bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay, uint8_t sensirion_options=0)
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.
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from I2C register.
bool get_8bit_register(uint8_t i2c_register, uint16_t &data, uint8_t delay=0)
Read 1 data word from 8 bit I2C register.
bool write_command(T i2c_register, uint16_t data)
Write a command and one data word to the I2C device .
bool get_register(uint16_t i2c_register, uint16_t &data, uint8_t delay=0)
Read 1 data word from 16 bit I2C register.
bool write_command(T i2c_register)
Write a command to the I2C device.
bool read_data(uint16_t *data, uint8_t len)
Read data words from I2C device.
bool read_data(uint16_t &data)
Read 1 data word from I2C device.
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 write_command(T i2c_register, const uint16_t *data, uint8_t len)
Write a command with arguments as words.
static const char * sensirion_convert_to_string_in_place(uint16_t *array, size_t length)
This function performs an in-place conversion of the provided buffer from uint16_t values to big endi...
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
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:1098
const void size_t len
Definition hal.h:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t length
Definition tt21100.cpp:0