ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ags10.cpp
Go to the documentation of this file.
1#include "ags10.h"
3
4#include <cinttypes>
5
6namespace esphome::ags10 {
7ESPHOME_LOG_TAG(TAG, "ags10");
8
9// Data acquisition.
10static const uint8_t REG_TVOC = 0x00;
11// Zero-point calibration.
12static const uint8_t REG_CALIBRATION = 0x01;
13// Read version.
14static const uint8_t REG_VERSION = 0x11;
15// Read current resistance.
16static const uint8_t REG_RESISTANCE = 0x20;
17// Modify target address.
18static const uint8_t REG_ADDRESS = 0x21;
19
20// Zero-point calibration with current resistance.
21static const uint16_t ZP_CURRENT = 0x0000;
22// Zero-point reset.
23static const uint16_t ZP_DEFAULT = 0xFFFF;
24
26 auto version = this->read_version_();
27 if (version) {
28 ESP_LOGD(TAG, "AGS10 Sensor Version: 0x%02X", *version);
29 PUBLISH_STATE(this->version_sensor_, *version);
30 } else {
31 ESP_LOGE(TAG, "AGS10 Sensor Version: unknown");
32 }
33
34 auto resistance = this->read_resistance_();
35 if (resistance) {
36 ESP_LOGD(TAG, "AGS10 Sensor Resistance: 0x%08" PRIX32, *resistance);
37 PUBLISH_STATE(this->resistance_sensor_, *resistance);
38 } else {
39 ESP_LOGE(TAG, "AGS10 Sensor Resistance: unknown");
40 }
41}
42
44 auto tvoc = this->read_tvoc_();
45 if (tvoc) {
46 this->tvoc_sensor_->publish_state(*tvoc);
48 } else {
49 this->status_set_warning();
50 }
51}
52
54 ESP_LOGCONFIG(TAG, "AGS10:");
55 LOG_I2C_DEVICE(this);
56 switch (this->error_code_) {
57 case NONE:
58 break;
60 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
61 break;
63 ESP_LOGE(TAG, "The crc check failed");
64 break;
65 case ILLEGAL_STATUS:
66 ESP_LOGE(TAG, "AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
67 break;
69 ESP_LOGE(TAG, "AGS10 returns TVOC data in unsupported units.");
70 break;
71 default:
72 ESP_LOGE(TAG, "Unknown error: %d", this->error_code_);
73 break;
74 }
75 LOG_UPDATE_INTERVAL(this);
76 LOG_SENSOR(" ", "TVOC Sensor", this->tvoc_sensor_);
77 LOG_SENSOR(" ", "Firmware Version Sensor", this->version_sensor_);
78 LOG_SENSOR(" ", "Resistance Sensor", this->resistance_sensor_);
79}
80
84bool AGS10Component::new_i2c_address(uint8_t newaddress) {
85 uint8_t rev_newaddress = ~newaddress;
86 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
87 data[4] = crc8(data.data(), 4, 0xFF, 0x31, true);
88 if (!this->write_bytes(REG_ADDRESS, data)) {
89 this->error_code_ = COMMUNICATION_FAILED;
90 this->status_set_warning();
91 ESP_LOGE(TAG, "couldn't write the new I2C address 0x%02X", newaddress);
92 return false;
93 }
94 this->set_i2c_address(newaddress);
95 ESP_LOGW(TAG, "changed I2C address to 0x%02X", newaddress);
96 this->error_code_ = NONE;
98 return true;
99}
100
102
104
106 switch (mode) {
107 case FACTORY_DEFAULT:
109 break;
110 case CURRENT_VALUE:
112 break;
113 case CUSTOM_VALUE:
114 this->set_zero_point_with(value);
115 break;
116 }
117}
118
120 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
121 data[4] = crc8(data.data(), 4, 0xFF, 0x31, true);
122 if (!this->write_bytes(REG_CALIBRATION, data)) {
123 this->error_code_ = COMMUNICATION_FAILED;
124 this->status_set_warning();
125 ESP_LOGE(TAG, "unable to set zero-point calibration with 0x%02X", value);
126 return false;
127 }
128 if (value == ZP_CURRENT) {
129 ESP_LOGI(TAG, "zero-point calibration has been set with current resistance");
130 } else if (value == ZP_DEFAULT) {
131 ESP_LOGI(TAG, "zero-point calibration has been reset to the factory defaults");
132 } else {
133 ESP_LOGI(TAG, "zero-point calibration has been set with 0x%02X", value);
134 }
135 this->error_code_ = NONE;
136 this->status_clear_warning();
137 return true;
138}
139
140optional<uint32_t> AGS10Component::read_tvoc_() {
141 auto data = this->read_and_check_<5>(REG_TVOC);
142 if (!data) {
143 return nullopt;
144 }
145
146 auto res = *data;
147 auto status_byte = res[0];
148
149 int units = status_byte & 0x0e;
150 int status_bit = status_byte & 0x01;
151
152 if (status_bit != 0) {
153 this->error_code_ = ILLEGAL_STATUS;
154 ESP_LOGW(TAG, "Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
155 return nullopt;
156 }
157
158 if (units != 0) {
159 this->error_code_ = UNSUPPORTED_UNITS;
160 ESP_LOGE(TAG, "Reading AGS10 data failed: unsupported units (%d)!", units);
161 return nullopt;
162 }
163
164 return encode_uint24(res[1], res[2], res[3]);
165}
166
167optional<uint8_t> AGS10Component::read_version_() {
168 auto data = this->read_and_check_<5>(REG_VERSION);
169 if (data) {
170 auto res = *data;
171 return res[3];
172 }
173 return nullopt;
174}
175
177 auto data = this->read_and_check_<5>(REG_RESISTANCE);
178 if (data) {
179 auto res = *data;
180 return encode_uint32(res[0], res[1], res[2], res[3]);
181 }
182 return nullopt;
183}
184
185template<size_t N> optional<std::array<uint8_t, N>> AGS10Component::read_and_check_(uint8_t a_register) {
186 auto data = this->read_bytes<N>(a_register);
187 if (!data.has_value()) {
188 this->error_code_ = COMMUNICATION_FAILED;
189 ESP_LOGE(TAG, "Reading AGS10 version failed!");
190 return optional<std::array<uint8_t, N>>();
191 }
192 auto len = N - 1;
193 auto res = *data;
194 auto crc_byte = res[len];
195
196 if (crc_byte != crc8(res.data(), len, 0xFF, 0x31, true)) {
197 this->error_code_ = CRC_CHECK_FAILED;
198 ESP_LOGE(TAG, "Reading AGS10 version failed: crc error!");
199 return optional<std::array<uint8_t, N>>();
200 }
201
202 return data;
203}
204} // namespace esphome::ags10
BedjetMode mode
BedJet operating mode.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:309
optional< std::array< uint8_t, N > > read_and_check_(uint8_t a_register)
Read, checks and returns data from the sensor.
Definition ags10.cpp:185
bool set_zero_point_with_factory_defaults()
Sets zero-point with factory defaults.
Definition ags10.cpp:101
optional< uint8_t > read_version_()
Reads and returns a firmware version of AGS10.
Definition ags10.cpp:167
enum esphome::ags10::AGS10Component::ErrorCode NONE
bool set_zero_point_with_current_resistance()
Sets zero-point with current sensor resistance.
Definition ags10.cpp:103
optional< uint32_t > read_resistance_()
Reads and returns the resistance of AGS10.
Definition ags10.cpp:176
optional< uint32_t > read_tvoc_()
Reads and returns value of TVOC.
Definition ags10.cpp:140
void set_zero_point(AGS10SetZeroPointActionMode mode, uint16_t value)
Sets zero-point by mode; the value is only used for CUSTOM_VALUE.
Definition ags10.cpp:105
bool set_zero_point_with(uint16_t value)
Sets zero-point with the value.
Definition ags10.cpp:119
bool new_i2c_address(uint8_t newaddress)
Modifies target address of AGS10.
Definition ags10.cpp:84
void dump_config() override
Definition ags10.cpp:53
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:151
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
ESPHOME_LOG_TAG(TAG, "ags10")
AGS10SetZeroPointActionMode
Definition ags10.h:9
@ CURRENT_VALUE
Definition ags10.h:13
@ FACTORY_DEFAULT
Definition ags10.h:11
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
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.
Definition helpers.h:1047
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.
Definition helpers.h:1051
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
Definition helpers.cpp:68