-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathunit_SGP30.cpp
More file actions
358 lines (318 loc) · 10.5 KB
/
unit_SGP30.cpp
File metadata and controls
358 lines (318 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*!
@file unit_SGP30.cpp
@brief SGP30 Unit for M5UnitUnified
*/
#include "unit_SGP30.hpp"
#include <M5Utility.hpp>
#include <array>
#include <cmath>
#include <limits>
using namespace m5::utility::mmh3;
using namespace m5::unit::types;
using namespace m5::unit::sgp30;
using namespace m5::unit::sgp30::command;
namespace {
// Supported lower limit version
constexpr uint8_t lower_limit_version{0x20};
// constexpr elapsed_time_t BASELINE_INTERVAL{1000 * 60 * 60}; // 1 hour (ms)
inline bool delayMeasurementDuration(const uint16_t ms)
{
m5::utility::delay(ms);
return true;
}
} // namespace
namespace m5 {
namespace unit {
namespace sgp30 {
uint16_t Data::co2eq() const
{
// M5_LIB_LOGE(">>> %x:%x => %u", raw[0], raw[1], m5::types::big_uint16_t(raw[0], raw[1]).get());
return m5::types::big_uint16_t(raw[0], raw[1]).get();
}
uint16_t Data::tvoc() const
{
return m5::types::big_uint16_t(raw[3], raw[4]).get();
}
} // namespace sgp30
// class UnitSGP30
const char UnitSGP30::name[] = "UnitSGP30";
const types::uid_t UnitSGP30::uid{"UnitSGP30"_mmh3};
const types::attr_t UnitSGP30::attr{attribute::AccessI2C};
bool UnitSGP30::begin()
{
auto ssize = stored_size();
assert(ssize && "stored_size must be greater than zero");
if (ssize != _data->capacity()) {
_data.reset(new m5::container::CircularBuffer<Data>(ssize));
if (!_data) {
M5_LIB_LOGE("Failed to allocate");
return false;
}
}
m5::utility::delay(1);
Feature f{};
if (!readFeatureSet(f)) {
M5_LIB_LOGE("Failed to read feature");
return false;
}
if (f.productType() != 0) {
// May be SGPC3 gas sensor if value is 1
M5_LIB_LOGE("This unit is NOT SGP30");
return false;
}
_version = f.productVersion();
if (_version < lower_limit_version) {
M5_LIB_LOGE("Not enough the product version %x", _version);
return false;
}
return _cfg.start_periodic
? startPeriodicMeasurement(_cfg.baseline_co2eq, _cfg.baseline_tvoc, _cfg.humidity, _cfg.interval)
: true;
}
void UnitSGP30::update(const bool force)
{
_updated = false;
if (_periodic) {
elapsed_time_t at{m5::utility::millis()};
if (_waiting) {
_waiting = (at < _can_measure_time);
return;
}
if (force || !_latest || at >= _latest + _interval) {
Data d{};
_updated = read_measurement(d);
if (_updated) {
_latest = at;
_data->push_back(d);
}
}
}
}
bool UnitSGP30::start_periodic_measurement(const uint16_t co2eq, const uint16_t tvoc, const uint16_t humidity,
const uint32_t interval, const uint32_t duration)
{
// Baseline and absolute humidity restoration must take place during
// this 15-second period
return start_periodic_measurement(interval, duration) && write_iaq_baseline(co2eq, tvoc) &&
writeAbsoluteHumidity(humidity);
}
bool UnitSGP30::start_periodic_measurement(const uint32_t interval, const uint32_t duration)
{
if (inPeriodic()) {
return false;
}
if (interval < sgp30::MEASURE_IAQ_DURATION) {
M5_LIB_LOGE("Interval too short %u. Must ne greater equal %u", interval, sgp30::MEASURE_IAQ_DURATION);
return false;
}
if (writeRegister(IAQ_INIT)) {
// For the first 15s after the “sgp30_iaq_init” command the sensor
// is an initialization phase during which a “sgp30_measure_iaq”
// command returns fixed values of 400 ppm CO2eq and 0 ppb TVOC.A
// new “sgp30_iaq_init” command has to be sent after every power-up
// or soft reset.
// Baseline and absolute humidity restoration must take place during
// this 15-second period
_can_measure_time = m5::utility::millis() + 15 * 1000;
_periodic = true;
_latest = 0;
_waiting = true;
_interval = interval;
m5::utility::delay(duration);
}
return _periodic;
}
bool UnitSGP30::stop_periodic_measurement()
{
_periodic = false;
return true;
}
bool UnitSGP30::readRaw(uint16_t& h2, uint16_t& ethanol)
{
std::array<uint8_t, 6> rbuf{};
h2 = ethanol = 0;
if (readRegister(MEASURE_RAW, rbuf.data(), rbuf.size(), MEASURE_RAW_DURATION)) {
m5::utility::CRC8_Checksum crc{};
if (crc.range(rbuf.data(), 2) == rbuf[2] && crc.range(rbuf.data() + 3, 2) == rbuf[5]) {
h2 = m5::types::big_uint16_t(rbuf[0], rbuf[1]).get();
ethanol = m5::types::big_uint16_t(rbuf[3], rbuf[4]).get();
}
return true;
}
return false;
}
bool UnitSGP30::readRaw(float& h2, float& ethanol)
{
uint16_t hh{}, et{};
h2 = ethanol = std::numeric_limits<float>::quiet_NaN();
if (!readRaw(hh, et)) {
return false;
}
h2 = 0.5f * std::exp((13119 - hh) / 512.f);
ethanol = 0.4f * std::exp((18472 - et) / 512.f);
return true;
}
bool UnitSGP30::readIaqBaseline(uint16_t& co2eq, uint16_t& tvoc)
{
std::array<uint8_t, 6> rbuf{};
co2eq = tvoc = 0;
if (readRegister(GET_IAQ_BASELINE, rbuf.data(), rbuf.size(), GET_IAQ_BASELINE_DURATION)) {
m5::utility::CRC8_Checksum crc{};
if (crc.range(rbuf.data(), 2) == rbuf[2] && crc.range(rbuf.data() + 3, 2) == rbuf[5]) {
co2eq = m5::types::big_uint16_t(rbuf[0], rbuf[1]).get();
tvoc = m5::types::big_uint16_t(rbuf[3], rbuf[4]).get();
return true;
}
}
return false;
}
bool UnitSGP30::writeAbsoluteHumidity(const uint16_t raw, const uint32_t duration)
{
m5::utility::CRC8_Checksum crc;
std::array<uint8_t, 3> buf{};
m5::types::big_uint16_t rr(raw);
std::memcpy(buf.data(), rr.data(), 2);
buf[2] = crc.range(rr.data(), 2);
return writeRegister(SET_ABSOLUTE_HUMIDITY, buf.data(), buf.size()) && delayMeasurementDuration(duration);
}
bool UnitSGP30::writeAbsoluteHumidity(const float gm3, const uint32_t duration)
{
int32_t tmp = static_cast<int32_t>(std::round(gm3 * 256.f));
if (tmp > 32767 || tmp < -32768) {
M5_LIB_LOGE("Over/underflow: %f / %d", gm3, tmp);
return false;
}
return writeAbsoluteHumidity(static_cast<uint16_t>(static_cast<int16_t>(tmp)), duration);
}
bool UnitSGP30::measureTest(uint16_t& result)
{
if (inPeriodic()) {
M5_LIB_LOGD("Periodic measurements are running");
return false;
}
std::array<uint8_t, 3> rbuf{};
if (readRegister(MEASURE_TEST, rbuf.data(), rbuf.size(), MEASURE_TEST_DURATION)) {
m5::utility::CRC8_Checksum crc;
if (crc.range(rbuf.data(), 2) == rbuf[2]) {
result = m5::types::big_uint16_t(rbuf[0], rbuf[1]).get();
return true;
}
}
return false;
}
#if 0
bool UnitSGP30::readTvocInceptiveBaseline(uint16_t& inceptive_tvoc) {
if (_version < 0x21) {
M5_LIB_LOGE("Not enough the product version %x", _version);
return false;
}
std::array<uint8_t, 3> rbuf{};
if (readRegister(GET_TVOC_INCEPTIVE_BASELINE, rbuf.data(), rbuf.size(), GET_TVOC_INCEPTIVE_BASELINE_DURATION)) {
m5::utility::CRC8_Checksum crc;
if (crc.range(rbuf.data(), 2) == rbuf[2]) {
inceptive_tvoc = m5::types::big_uint16_t(rbuf[0], rbuf[1]).get();
return true;
}
}
return false;
}
bool UnitSGP30::writeTvocInceptiveBaseline(const uint16_t inceptive_tvoc, const uint32_t duration) {
if (_version < 0x21) {
M5_LIB_LOGE("Not enough the product version %x", _version);
return false;
}
M5_LIB_LOGW(">>>>>> %u", inceptive_tvoc);
m5::utility::CRC8_Checksum crc;
std::array<uint8_t, 3> buf{};
m5::types::big_uint16_t tt(inceptive_tvoc);
std::memcpy(buf.data(), tt.data(), 2);
buf[2] = crc.range(tt.data(), 2);
return writeRegister(SET_TVOC_INCEPTIVE_BASELINE, buf.data(), buf.size()) && delayMeasurementDuration(duration);
}
#endif
bool UnitSGP30::generalReset()
{
uint8_t cmd{0x06};
if (generalCall(&cmd, 1)) {
_periodic = false;
m5::utility::delay(10);
return true;
}
return false;
}
bool UnitSGP30::readFeatureSet(sgp30::Feature& feature)
{
std::array<uint8_t, 3> rbuf{};
if (readRegister(GET_FEATURE_SET, rbuf.data(), rbuf.size(), GET_FEATURE_SET_DURATION)) {
m5::utility::CRC8_Checksum crc;
if (crc.range(rbuf.data(), 2) == rbuf[2]) {
feature.value = m5::types::big_uint16_t(rbuf[0], rbuf[1]).get();
return true;
}
}
return false;
}
bool UnitSGP30::readSerialNumber(uint64_t& number)
{
std::array<uint8_t, 9> rbuf{};
number = 0;
if (readRegister(GET_SERIAL_ID, rbuf.data(), rbuf.size(), GET_SERIAL_ID_DURATION)) {
m5::utility::CRC8_Checksum crc;
for (uint_fast8_t i = 0; i < 3; i++) {
if (crc.range(rbuf.data() + i * 3, 2) != rbuf[i * 3 + 2]) {
return false;
}
}
for (uint_fast8_t i = 0; i < 3; ++i) {
number |= ((uint64_t)(m5::types::big_uint16_t(rbuf[i * 3], rbuf[i * 3 + 1]).get())) << (16U * (2 - i));
}
return true;
}
return false;
}
bool UnitSGP30::readSerialNumber(char* number)
{
if (!number) {
return false;
}
*number = '\0';
uint64_t sno{};
if (readSerialNumber(sno)) {
uint_fast8_t i{12};
while (i--) {
*number++ = m5::utility::uintToHexChar((sno >> (i * 4)) & 0x0F);
}
*number = '\0';
return true;
}
return false;
}
//
bool UnitSGP30::write_iaq_baseline(const uint16_t co2eq, const uint16_t tvoc)
{
m5::utility::CRC8_Checksum crc{};
m5::types::big_uint16_t cc(co2eq);
m5::types::big_uint16_t tt(tvoc);
std::array<uint8_t, (2 + 1) * 2> buf{};
// Note that the order is different for get and set
std::memcpy(buf.data() + 0, tt.data(), 2);
buf[2] = crc.range(tt.data(), 2);
std::memcpy(buf.data() + 3, cc.data(), 2);
buf[5] = crc.range(cc.data(), 2);
return writeRegister(SET_IAQ_BASELINE, buf.data(), buf.size());
}
bool UnitSGP30::read_measurement(Data& d)
{
if (readRegister(MEASURE_IAQ, d.raw.data(), d.raw.size(), MEASURE_IAQ_DURATION)) {
m5::utility::CRC8_Checksum crc{};
return crc.range(d.raw.data(), 2) == d.raw[2] && crc.range(d.raw.data() + 3, 2) == d.raw[5];
}
return false;
}
} // namespace unit
} // namespace m5