ESPHome 2025.6.3
Loading...
Searching...
No Matches
i2c_sensirion.h
Go to the documentation of this file.
1#pragma once
4
5#include <vector>
6
7namespace esphome {
8namespace sensirion_common {
9
19 public:
20 enum CommandLen : uint8_t { ADDR_8_BIT = 1, ADDR_16_BIT = 2 };
21
28 bool read_data(uint16_t *data, uint8_t len);
29
34 bool read_data(uint16_t &data) { return this->read_data(&data, 1); }
35
44 bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay = 0) {
45 return get_register_(command, ADDR_16_BIT, data, len, delay);
46 }
53 bool get_register(uint16_t i2c_register, uint16_t &data, uint8_t delay = 0) {
54 return this->get_register_(i2c_register, ADDR_16_BIT, &data, 1, delay);
55 }
56
65 bool get_8bit_register(uint8_t i2c_register, uint16_t *data, uint8_t len, uint8_t delay = 0) {
66 return get_register_(i2c_register, ADDR_8_BIT, data, len, delay);
67 }
68
75 bool get_8bit_register(uint8_t i2c_register, uint16_t &data, uint8_t delay = 0) {
76 return this->get_register_(i2c_register, ADDR_8_BIT, &data, 1, delay);
77 }
78
83 template<class T> bool write_command(T i2c_register) { return write_command(i2c_register, nullptr, 0); }
84
90 template<class T> bool write_command(T i2c_register, uint16_t data) { return write_command(i2c_register, &data, 1); }
91
97 template<class T> bool write_command(T i2c_register, const std::vector<uint16_t> &data) {
98 return write_command_(i2c_register, sizeof(T), data.data(), data.size());
99 }
100
107 template<class T> bool write_command(T i2c_register, const uint16_t *data, uint8_t len) {
108 // limit to 8 or 16 bit only
109 static_assert(sizeof(i2c_register) == 1 || sizeof(i2c_register) == 2,
110 "only 8 or 16 bit command types are supported.");
111 return write_command_(i2c_register, CommandLen(sizeof(T)), data, len);
112 }
113
114 protected:
115 uint8_t crc_polynomial_{0x31u}; // default for sensirion
123 bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len);
124
134 bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay);
135
142 uint8_t sht_crc_(uint16_t data);
143
150 uint8_t sht_crc_(uint8_t data1, uint8_t data2) { return sht_crc_(encode_uint16(data1, data2)); }
151
155};
156
157} // namespace sensirion_common
158} // namespace esphome
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking.
bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay)
get data words from i2c register.
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_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 .
uint8_t sht_crc_(uint16_t data)
8-bit CRC checksum that is transmitted after each data word for read and write operation
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.
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
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
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:192
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29