M5Unit-ENV 1.2.0 git rev:d576302
Loading...
Searching...
No Matches
unit_SGP30.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_TVOC_UNIT_SGP30_HPP
11#define M5_UNIT_TVOC_UNIT_SGP30_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <array>
16
17namespace m5 {
18namespace unit {
19
24namespace sgp30 {
25
27// Max command duration(ms)
28// Max measurement duration (ms)
29constexpr uint16_t IAQ_INIT_DURATION{10};
30constexpr uint16_t MEASURE_IAQ_DURATION{12};
31constexpr uint16_t GET_IAQ_BASELINE_DURATION{10};
32constexpr uint16_t SET_IAQ_BASELINE_DURATION{10};
33constexpr uint16_t SET_ABSOLUTE_HUMIDITY_DURATION{10};
34constexpr uint16_t MEASURE_TEST_DURATION{220};
35constexpr uint16_t GET_FEATURE_SET_DURATION{10};
36constexpr uint16_t MEASURE_RAW_DURATION{25};
37constexpr uint16_t GET_TVOC_INCEPTIVE_BASELINE_DURATION{10};
38constexpr uint16_t SET_TVOC_INCEPTIVE_BASELINE_DURATION{10};
39constexpr uint16_t GET_SERIAL_ID_DURATION{10};
41
46struct Feature {
48 uint8_t productType() const
49 {
50 return (value >> 12) & 0x0F;
51 }
59 uint8_t productVersion() const
60 {
61 return value & 0xFF;
62 }
63 uint16_t value{};
64};
65
70struct Data {
71 std::array<uint8_t, 6> raw{};
72 uint16_t co2eq() const;
73 uint16_t tvoc() const;
74};
75
76} // namespace sgp30
77
82class UnitSGP30 : public Component, public PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data> {
83 M5_UNIT_COMPONENT_HPP_BUILDER(UnitSGP30, 0x58);
84
85public:
90 struct config_t {
92 bool start_periodic{true};
94 uint16_t baseline_co2eq{};
96 uint16_t baseline_tvoc{};
98 uint16_t humidity{};
104 uint16_t inceptive_tvoc{};
106 int32_t interval{1000};
107 };
108
109 explicit UnitSGP30(const uint8_t addr = DEFAULT_ADDRESS)
110 : Component(addr), _data{new m5::container::CircularBuffer<sgp30::Data>(1)}
111 {
112 auto ccfg = component_config();
113 ccfg.clock = 400 * 1000U;
114 component_config(ccfg);
115 }
116 virtual ~UnitSGP30()
117 {
118 }
119
120 virtual bool begin() override;
121 virtual void update(const bool force = false) override;
122
125
126 inline config_t config() const
127 {
128 return _cfg;
129 }
131 inline void config(const config_t& cfg)
132 {
133 _cfg = cfg;
134 }
136
139
143 inline uint8_t productVersion() const
144 {
145 return _version;
146 }
153 inline bool canMeasurePeriodic() const
154 {
155 return inPeriodic() && !_waiting;
156 }
158
162 inline uint16_t co2eq() const
163 {
164 return !empty() ? oldest().co2eq() : 0xFFFF;
165 }
167 inline uint16_t tvoc() const
168 {
169 return !empty() ? oldest().tvoc() : 0xFFFF;
170 }
172
175
187 inline bool startPeriodicMeasurement(const uint16_t co2eq, const uint16_t tvoc, const uint16_t humidity,
188 const uint32_t interval = 1000U,
189 const uint32_t duration = sgp30::IAQ_INIT_DURATION)
190 {
191 return PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data>::startPeriodicMeasurement(co2eq, tvoc, humidity,
192 interval, duration);
193 }
204 inline bool startPeriodicMeasurement(const uint32_t interval = 1000U,
205 const uint32_t duration = sgp30::IAQ_INIT_DURATION)
206 {
207 return PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data>::startPeriodicMeasurement(interval, duration);
208 }
214 {
215 return PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data>::stopPeriodicMeasurement();
216 }
218
221
227 bool readIaqBaseline(uint16_t& co2eq, uint16_t& tvoc);
235 bool writeAbsoluteHumidity(const uint16_t raw, const uint32_t duration = sgp30::SET_ABSOLUTE_HUMIDITY_DURATION);
242 bool writeAbsoluteHumidity(const float gm3, const uint32_t duration = sgp30::SET_ABSOLUTE_HUMIDITY_DURATION);
243
244#if 0
251 bool readTvocInceptiveBaseline(uint16_t& inceptive_tvoc);
261 bool writeTvocInceptiveBaseline(const uint16_t inceptive_tvoc,
262 const uint32_t duration = sgp30::SET_TVOC_INCEPTIVE_BASELINE_DURATION);
263#endif
265
272 bool readRaw(uint16_t& h2, uint16_t& ethanol);
273
281 bool readRaw(float& h2, float& ethanol);
282
290 bool measureTest(uint16_t& result);
298 bool generalReset();
304 bool readFeatureSet(sgp30::Feature& feature);
310 bool readSerialNumber(uint64_t& number);
317 bool readSerialNumber(char* number);
318
319protected:
320 bool start_periodic_measurement(const uint16_t co2eq, const uint16_t tvoc, const uint16_t humidity,
321 const uint32_t interval, const uint32_t duration = sgp30::IAQ_INIT_DURATION);
322 bool start_periodic_measurement(const uint32_t interval, const uint32_t duration = sgp30::IAQ_INIT_DURATION);
323 bool stop_periodic_measurement();
324
325 bool write_iaq_baseline(const uint16_t co2eq, const uint16_t tvoc);
326 bool read_measurement(sgp30::Data& d);
327
328 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitSGP30, sgp30::Data);
329
330protected:
331 uint8_t _version{}; // Chip version
332
333 bool _waiting{};
334 types::elapsed_time_t _can_measure_time{};
335 std::unique_ptr<m5::container::CircularBuffer<sgp30::Data>> _data{};
336
337 config_t _cfg{};
338};
339
340namespace sgp30 {
341namespace command {
343constexpr uint16_t IAQ_INIT{0x2003};
344constexpr uint16_t MEASURE_IAQ{0x2008};
345constexpr uint16_t GET_IAQ_BASELINE{0x2015};
346constexpr uint16_t SET_IAQ_BASELINE{0x201E};
347constexpr uint16_t SET_ABSOLUTE_HUMIDITY{0x2061};
348constexpr uint16_t MEASURE_TEST{0x2032};
349constexpr uint16_t GET_FEATURE_SET{0x202F};
350constexpr uint16_t MEASURE_RAW{0x2050};
351constexpr uint16_t GET_TVOC_INCEPTIVE_BASELINE{0x20B3};
352constexpr uint16_t SET_TVOC_INCEPTIVE_BASELINE{0x2077};
353constexpr uint16_t GET_SERIAL_ID{0x3682};
355} // namespace command
356} // namespace sgp30
357
358} // namespace unit
359} // namespace m5
360#endif
SGP30 unit.
Definition unit_SGP30.hpp:82
uint8_t productVersion() const
Gets the product version.
Definition unit_SGP30.hpp:143
bool canMeasurePeriodic() const
Can it be measured?
Definition unit_SGP30.hpp:153
bool writeAbsoluteHumidity(const uint16_t raw, const uint32_t duration=sgp30::SET_ABSOLUTE_HUMIDITY_DURATION)
Write the absolute humidity.
Definition unit_SGP30.cpp:197
bool startPeriodicMeasurement(const uint16_t co2eq, const uint16_t tvoc, const uint16_t humidity, const uint32_t interval=1000U, const uint32_t duration=sgp30::IAQ_INIT_DURATION)
Start periodic measurement.
Definition unit_SGP30.hpp:187
bool readIaqBaseline(uint16_t &co2eq, uint16_t &tvoc)
Read the IAQ baseline.
Definition unit_SGP30.cpp:182
uint16_t co2eq() const
Oldest CO2eq (ppm)
Definition unit_SGP30.hpp:162
bool startPeriodicMeasurement(const uint32_t interval=1000U, const uint32_t duration=sgp30::IAQ_INIT_DURATION)
Start periodic measurement.
Definition unit_SGP30.hpp:204
uint16_t tvoc() const
Oldest TVOC (ppb)
Definition unit_SGP30.hpp:167
bool readSerialNumber(uint64_t &number)
Read the serial number.
Definition unit_SGP30.cpp:293
config_t config() const
Gets the configration.
Definition unit_SGP30.hpp:126
bool readFeatureSet(sgp30::Feature &feature)
Read the feature set.
Definition unit_SGP30.cpp:280
void config(const config_t &cfg)
Set the configration.
Definition unit_SGP30.hpp:131
bool readRaw(uint16_t &h2, uint16_t &ethanol)
Read sensor raw signals.
Definition unit_SGP30.cpp:155
bool measureTest(uint16_t &result)
Run the on-chip self-test.
Definition unit_SGP30.cpp:217
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_SGP30.hpp:213
bool generalReset()
General reset.
Definition unit_SGP30.cpp:269
Top level namespace of M5stack.
For SGP30.
Unit-related namespace.
Settings for begin.
Definition unit_SGP30.hpp:90
uint16_t baseline_tvoc
Baseline TVOC initial value if start on begin.
Definition unit_SGP30.hpp:96
uint16_t baseline_co2eq
Baseline CO2eq initial value if start on begin.
Definition unit_SGP30.hpp:94
int32_t interval
Periodic measurement interval if start on begin.
Definition unit_SGP30.hpp:106
bool start_periodic
Start periodic measurement on begin()?
Definition unit_SGP30.hpp:92
uint16_t inceptive_tvoc
Definition unit_SGP30.hpp:104
uint16_t humidity
Absolute humidity initiali value if start on begin.
Definition unit_SGP30.hpp:98
Measurement data group.
Definition unit_SGP30.hpp:70
uint16_t co2eq() const
Co2Eq (ppm)
Definition unit_SGP30.cpp:37
uint16_t tvoc() const
TVOC (pbb)
Definition unit_SGP30.cpp:43
std::array< uint8_t, 6 > raw
RAW data.
Definition unit_SGP30.hpp:71
Structure of the SGP feature set number.
Definition unit_SGP30.hpp:46
uint8_t productVersion() const
product version
Definition unit_SGP30.hpp:59
uint8_t productType() const
product type (SGP30: 0)
Definition unit_SGP30.hpp:48