ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ags10.cpp
Go to the documentation of this file.
1#include "ags10.h"
3
4#include <cinttypes>
5
6namespace esphome {
7namespace ags10 {
8static const char *const TAG = "ags10";
9
10// Data acquisition.
11static const uint8_t REG_TVOC = 0x00;
12// Zero-point calibration.
13static const uint8_t REG_CALIBRATION = 0x01;
14// Read version.
15static const uint8_t REG_VERSION = 0x11;
16// Read current resistance.
17static const uint8_t REG_RESISTANCE = 0x20;
18// Modify target address.
19static const uint8_t REG_ADDRESS = 0x21;
20
21// Zero-point calibration with current resistance.
22static const uint16_t ZP_CURRENT = 0x0000;
23// Zero-point reset.
24static const uint16_t ZP_DEFAULT = 0xFFFF;
25
27 auto version = this->read_version_();
28 if (version) {
29 ESP_LOGD(TAG, "AGS10 Sensor Version: 0x%02X", *version);
30 if (this->version_ != nullptr) {
31 this->version_->publish_state(*version);
32 }
33 } else {
34 ESP_LOGE(TAG, "AGS10 Sensor Version: unknown");
35 }
36
37 auto resistance = this->read_resistance_();
38 if (resistance) {
39 ESP_LOGD(TAG, "AGS10 Sensor Resistance: 0x%08" PRIX32, *resistance);
40 if (this->resistance_ != nullptr) {
41 this->resistance_->publish_state(*resistance);
42 }
43 } else {
44 ESP_LOGE(TAG, "AGS10 Sensor Resistance: unknown");
45 }
46}
47
49 auto tvoc = this->read_tvoc_();
50 if (tvoc) {
51 this->tvoc_->publish_state(*tvoc);
53 } else {
54 this->status_set_warning();
55 }
56}
57
59 ESP_LOGCONFIG(TAG, "AGS10:");
60 LOG_I2C_DEVICE(this);
61 switch (this->error_code_) {
62 case NONE:
63 break;
65 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
66 break;
68 ESP_LOGE(TAG, "The crc check failed");
69 break;
70 case ILLEGAL_STATUS:
71 ESP_LOGE(TAG, "AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
72 break;
74 ESP_LOGE(TAG, "AGS10 returns TVOC data in unsupported units.");
75 break;
76 default:
77 ESP_LOGE(TAG, "Unknown error: %d", this->error_code_);
78 break;
79 }
80 LOG_UPDATE_INTERVAL(this);
81 LOG_SENSOR(" ", "TVOC Sensor", this->tvoc_);
82 LOG_SENSOR(" ", "Firmware Version Sensor", this->version_);
83 LOG_SENSOR(" ", "Resistance Sensor", this->resistance_);
84}
85
89bool AGS10Component::new_i2c_address(uint8_t newaddress) {
90 uint8_t rev_newaddress = ~newaddress;
91 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
92 data[4] = calc_crc8_(data, 4);
93 if (!this->write_bytes(REG_ADDRESS, data)) {
94 this->error_code_ = COMMUNICATION_FAILED;
95 this->status_set_warning();
96 ESP_LOGE(TAG, "couldn't write the new I2C address 0x%02X", newaddress);
97 return false;
98 }
99 this->set_i2c_address(newaddress);
100 ESP_LOGW(TAG, "changed I2C address to 0x%02X", newaddress);
101 this->error_code_ = NONE;
102 this->status_clear_warning();
103 return true;
104}
105
107
109
111 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
112 data[4] = calc_crc8_(data, 4);
113 if (!this->write_bytes(REG_CALIBRATION, data)) {
114 this->error_code_ = COMMUNICATION_FAILED;
115 this->status_set_warning();
116 ESP_LOGE(TAG, "unable to set zero-point calibration with 0x%02X", value);
117 return false;
118 }
119 if (value == ZP_CURRENT) {
120 ESP_LOGI(TAG, "zero-point calibration has been set with current resistance");
121 } else if (value == ZP_DEFAULT) {
122 ESP_LOGI(TAG, "zero-point calibration has been reset to the factory defaults");
123 } else {
124 ESP_LOGI(TAG, "zero-point calibration has been set with 0x%02X", value);
125 }
126 this->error_code_ = NONE;
127 this->status_clear_warning();
128 return true;
129}
130
132 auto data = this->read_and_check_<5>(REG_TVOC);
133 if (!data) {
134 return nullopt;
135 }
136
137 auto res = *data;
138 auto status_byte = res[0];
139
140 int units = status_byte & 0x0e;
141 int status_bit = status_byte & 0x01;
142
143 if (status_bit != 0) {
144 this->error_code_ = ILLEGAL_STATUS;
145 ESP_LOGW(TAG, "Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
146 return nullopt;
147 }
148
149 if (units != 0) {
150 this->error_code_ = UNSUPPORTED_UNITS;
151 ESP_LOGE(TAG, "Reading AGS10 data failed: unsupported units (%d)!", units);
152 return nullopt;
153 }
154
155 return encode_uint24(res[1], res[2], res[3]);
156}
157
159 auto data = this->read_and_check_<5>(REG_VERSION);
160 if (data) {
161 auto res = *data;
162 return res[3];
163 }
164 return nullopt;
165}
166
168 auto data = this->read_and_check_<5>(REG_RESISTANCE);
169 if (data) {
170 auto res = *data;
171 return encode_uint32(res[0], res[1], res[2], res[3]);
172 }
173 return nullopt;
174}
175
177 auto data = this->read_bytes<N>(a_register);
178 if (!data.has_value()) {
179 this->error_code_ = COMMUNICATION_FAILED;
180 ESP_LOGE(TAG, "Reading AGS10 version failed!");
182 }
183 auto len = N - 1;
184 auto res = *data;
185 auto crc_byte = res[len];
186
187 if (crc_byte != calc_crc8_(res, len)) {
188 this->error_code_ = CRC_CHECK_FAILED;
189 ESP_LOGE(TAG, "Reading AGS10 version failed: crc error!");
191 }
192
193 return data;
194}
195
196template<size_t N> uint8_t AGS10Component::calc_crc8_(std::array<uint8_t, N> dat, uint8_t num) {
197 uint8_t i, byte1, crc = 0xFF;
198 for (byte1 = 0; byte1 < num; byte1++) {
199 crc ^= (dat[byte1]);
200 for (i = 0; i < 8; i++) {
201 if (crc & 0x80) {
202 crc = (crc << 1) ^ 0x31;
203 } else {
204 crc = (crc << 1);
205 }
206 }
207 }
208 return crc;
209}
210} // namespace ags10
211} // namespace esphome
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
optional< std::array< uint8_t, N > > read_and_check_(uint8_t a_register)
Read, checks and returns data from the sensor.
Definition ags10.cpp:176
bool set_zero_point_with_factory_defaults()
Sets zero-point with factory defaults.
Definition ags10.cpp:106
optional< uint8_t > read_version_()
Reads and returns a firmware version of AGS10.
Definition ags10.cpp:158
enum esphome::ags10::AGS10Component::ErrorCode NONE
sensor::Sensor * tvoc_
TVOC.
Definition ags10.h:60
bool set_zero_point_with_current_resistance()
Sets zero-point with current sensor resistance.
Definition ags10.cpp:108
sensor::Sensor * resistance_
Resistance.
Definition ags10.h:70
uint8_t calc_crc8_(std::array< uint8_t, N > dat, uint8_t num)
Calculates CRC8 value.
Definition ags10.cpp:196
optional< uint32_t > read_resistance_()
Reads and returns the resistance of AGS10.
Definition ags10.cpp:167
optional< uint32_t > read_tvoc_()
Reads and returns value of TVOC.
Definition ags10.cpp:131
sensor::Sensor * version_
Firmvare version.
Definition ags10.h:65
bool set_zero_point_with(uint16_t value)
Sets zero-point with the value.
Definition ags10.cpp:110
bool new_i2c_address(uint8_t newaddress)
Modifies target address of AGS10.
Definition ags10.cpp:89
void dump_config() override
Definition ags10.cpp:58
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:140
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:216
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
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:177
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:181
const nullopt_t nullopt((nullopt_t::init()))