10#ifndef M5_UNIT_TVOC_UNIT_SGP30_HPP
11#define M5_UNIT_TVOC_UNIT_SGP30_HPP
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
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};
50 return (value >> 12) & 0x0F;
71 std::array<uint8_t, 6>
raw{};
72 uint16_t
co2eq()
const;
73 uint16_t
tvoc()
const;
82class UnitSGP30 :
public Component,
public PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data> {
83 M5_UNIT_COMPONENT_HPP_BUILDER(
UnitSGP30, 0x58);
109 explicit UnitSGP30(
const uint8_t addr = DEFAULT_ADDRESS)
110 : Component(addr), _data{new
m5::container::CircularBuffer<
sgp30::Data>(1)}
112 auto ccfg = component_config();
113 ccfg.clock = 400 * 1000U;
114 component_config(ccfg);
120 virtual bool begin()
override;
121 virtual void update(
const bool force =
false)
override;
155 return inPeriodic() && !_waiting;
164 return !empty() ? oldest().co2eq() : 0xFFFF;
169 return !empty() ? oldest().tvoc() : 0xFFFF;
188 const uint32_t interval = 1000U,
189 const uint32_t duration = sgp30::IAQ_INIT_DURATION)
191 return PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data>::startPeriodicMeasurement(
co2eq,
tvoc, humidity,
205 const uint32_t duration = sgp30::IAQ_INIT_DURATION)
207 return PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data>::startPeriodicMeasurement(interval, duration);
215 return PeriodicMeasurementAdapter<UnitSGP30, sgp30::Data>::stopPeriodicMeasurement();
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);
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);
272 bool readRaw(uint16_t& h2, uint16_t& ethanol);
281 bool readRaw(
float& h2,
float& ethanol);
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();
325 bool write_iaq_baseline(
const uint16_t
co2eq,
const uint16_t
tvoc);
334 types::elapsed_time_t _can_measure_time{};
335 std::unique_ptr<m5::container::CircularBuffer<sgp30::Data>> _data{};
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};
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 ðanol)
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.
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