7 static const char *
const TAG =
"ags10";
10 static const uint8_t REG_TVOC = 0x00;
12 static const uint8_t REG_CALIBRATION = 0x01;
14 static const uint8_t REG_VERSION = 0x11;
16 static const uint8_t REG_RESISTANCE = 0x20;
18 static const uint8_t REG_ADDRESS = 0x21;
21 static const uint16_t ZP_CURRENT = 0x0000;
23 static const uint16_t ZP_DEFAULT = 0xFFFF;
26 ESP_LOGCONFIG(TAG,
"Setting up ags10...");
30 ESP_LOGD(TAG,
"AGS10 Sensor Version: 0x%02X", *
version);
35 ESP_LOGE(TAG,
"AGS10 Sensor Version: unknown");
40 ESP_LOGD(TAG,
"AGS10 Sensor Resistance: 0x%08" PRIX32, *resistance);
45 ESP_LOGE(TAG,
"AGS10 Sensor Resistance: unknown");
48 ESP_LOGD(TAG,
"Sensor initialized");
62 ESP_LOGCONFIG(TAG,
"AGS10:");
64 switch (this->error_code_) {
68 ESP_LOGE(TAG,
"Communication with AGS10 failed!");
71 ESP_LOGE(TAG,
"The crc check failed");
74 ESP_LOGE(TAG,
"AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
77 ESP_LOGE(TAG,
"AGS10 returns TVOC data in unsupported units.");
80 ESP_LOGE(TAG,
"Unknown error: %d", this->error_code_);
83 LOG_UPDATE_INTERVAL(
this);
84 LOG_SENSOR(
" ",
"TVOC Sensor", this->
tvoc_);
85 LOG_SENSOR(
" ",
"Firmware Version Sensor", this->
version_);
86 LOG_SENSOR(
" ",
"Resistance Sensor", this->
resistance_);
93 uint8_t rev_newaddress = ~newaddress;
94 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
99 ESP_LOGE(TAG,
"couldn't write the new I2C address 0x%02X", newaddress);
103 ESP_LOGW(TAG,
"changed I2C address to 0x%02X", newaddress);
104 this->error_code_ =
NONE;
114 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
119 ESP_LOGE(TAG,
"unable to set zero-point calibration with 0x%02X", value);
122 if (value == ZP_CURRENT) {
123 ESP_LOGI(TAG,
"zero-point calibration has been set with current resistance");
124 }
else if (value == ZP_DEFAULT) {
125 ESP_LOGI(TAG,
"zero-point calibration has been reset to the factory defaults");
127 ESP_LOGI(TAG,
"zero-point calibration has been set with 0x%02X", value);
129 this->error_code_ =
NONE;
135 auto data = this->read_and_check_<5>(REG_TVOC);
141 auto status_byte = res[0];
143 int units = status_byte & 0x0e;
144 int status_bit = status_byte & 0x01;
146 if (status_bit != 0) {
148 ESP_LOGW(TAG,
"Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
154 ESP_LOGE(TAG,
"Reading AGS10 data failed: unsupported units (%d)!", units);
162 auto data = this->read_and_check_<5>(REG_VERSION);
171 auto data = this->read_and_check_<5>(REG_RESISTANCE);
180 auto data = this->read_bytes<N>(a_register);
181 if (!data.has_value()) {
183 ESP_LOGE(TAG,
"Reading AGS10 version failed!");
188 auto crc_byte = res[
len];
192 ESP_LOGE(TAG,
"Reading AGS10 version failed: crc error!");
200 uint8_t i, byte1,
crc = 0xFF;
201 for (byte1 = 0; byte1 < num; byte1++) {
203 for (i = 0; i < 8; i++) {
205 crc = (crc << 1) ^ 0x31;
optional< uint32_t > read_resistance_()
Reads and returns the resistance of AGS10.
optional< uint32_t > read_tvoc_()
Reads and returns value of TVOC.
bool new_i2c_address(uint8_t newaddress)
Modifies target address of AGS10.
optional< uint8_t > read_version_()
Reads and returns a firmware version of AGS10.
void status_set_warning(const char *message="unspecified")
bool set_zero_point_with_factory_defaults()
Sets zero-point with factory defaults.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
void dump_config() override
bool set_zero_point_with_current_resistance()
Sets zero-point with current sensor resistance.
const nullopt_t nullopt((nullopt_t::init()))
void status_clear_warning()
uint8_t calc_crc8_(std::array< uint8_t, N > dat, uint8_t num)
Calculates CRC8 value.
void publish_state(float state)
Publish a new state to the front-end.
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
sensor::Sensor * version_
Firmvare version.
sensor::Sensor * tvoc_
TVOC.
sensor::Sensor * resistance_
Resistance.
bool set_zero_point_with(uint16_t value)
Sets zero-point with the value.
Implementation of SPI Controller mode.
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
optional< std::array< uint8_t, N > > read_and_check_(uint8_t a_register)
Read, checks and returns data from the sensor.
enum esphome::ags10::AGS10Component::ErrorCode NONE
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)