M5Unit-ENV 1.2.0 git rev:d576302
Loading...
Searching...
No Matches
unit_SCD40.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_ENV_UNIT_SCD40_HPP
11#define M5_UNIT_ENV_UNIT_SCD40_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16
17namespace m5 {
18namespace unit {
19
24namespace scd4x {
29enum class Mode : uint8_t {
30 Normal,
31 LowPower,
32};
33
38struct Data {
39 std::array<uint8_t, 9> raw{};
40 uint16_t co2() const;
42 inline float temperature() const
43 {
44 return celsius();
45 }
46 float celsius() const;
47 float fahrenheit() const;
48 float humidity() const;
49};
50
52// Max command duration(ms)
53// For SCD40/41
54constexpr uint16_t READ_MEASUREMENT_DURATION{1};
55constexpr uint16_t STOP_PERIODIC_MEASUREMENT_DURATION{500};
56constexpr uint16_t SET_TEMPERATURE_OFFSET_DURATION{1};
57constexpr uint16_t GET_TEMPERATURE_OFFSET_DURATION{1};
58constexpr uint16_t SET_SENSOR_ALTITUDE_DURATION{1};
59constexpr uint16_t GET_SENSOR_ALTITUDE_DURATION{1};
60constexpr uint16_t SET_AMBIENT_PRESSURE_DURATION{1};
61constexpr uint16_t PERFORM_FORCED_CALIBRATION_DURATION{400};
62constexpr uint16_t SET_AUTOMATIC_SELF_CALIBRATION_ENABLED_DURATION{1};
63constexpr uint16_t GET_AUTOMATIC_SELF_CALIBRATION_ENABLED_DURATION{1};
64constexpr uint16_t GET_DATA_READY_STATUS_DURATION{1};
65constexpr uint16_t PERSIST_SETTINGS_DURATION{800};
66constexpr uint16_t GET_SERIAL_NUMBER_DURATION{1};
67constexpr uint16_t PERFORM_SELF_TEST_DURATION{10000};
68constexpr uint16_t PERFORM_FACTORY_RESET_DURATION{1200};
69constexpr uint16_t REINIT_DURATION{20};
71
72} // namespace scd4x
73
78class UnitSCD40 : public Component, public PeriodicMeasurementAdapter<UnitSCD40, scd4x::Data> {
79 M5_UNIT_COMPONENT_HPP_BUILDER(UnitSCD40, 0x62);
80
81public:
86 struct config_t {
88 bool start_periodic{true};
90 scd4x::Mode mode{scd4x::Mode::Normal};
92 bool calibration{true};
93 };
94
95 explicit UnitSCD40(const uint8_t addr = DEFAULT_ADDRESS)
96 : Component(addr), _data{new m5::container::CircularBuffer<scd4x::Data>(1)}
97 {
98 auto ccfg = component_config();
99 ccfg.clock = 400 * 1000U;
100 component_config(ccfg);
101 }
102 virtual ~UnitSCD40()
103 {
104 }
105
106 virtual bool begin() override;
107 virtual void update(const bool force = false) override;
108
111
112 inline config_t config() const
113 {
114 return _cfg;
115 }
117 inline void config(const config_t &cfg)
118 {
119 _cfg = cfg;
120 }
122
126 inline uint16_t co2() const
127 {
128 return !empty() ? oldest().co2() : 0;
129 }
131 inline float temperature() const
132 {
133 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
134 }
136 inline float celsius() const
137 {
138 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
139 }
141 inline float fahrenheit() const
142 {
143 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
144 }
146 inline float humidity() const
147 {
148 return !empty() ? oldest().humidity() : std::numeric_limits<float>::quiet_NaN();
149 }
151
154
159 inline bool startPeriodicMeasurement(const scd4x::Mode mode = scd4x::Mode::Normal)
160 {
161 return PeriodicMeasurementAdapter<UnitSCD40, scd4x::Data>::startPeriodicMeasurement(mode);
162 }
168 {
169 return startPeriodicMeasurement(scd4x::Mode::LowPower);
170 }
176 inline bool stopPeriodicMeasurement(const uint32_t duration = scd4x::STOP_PERIODIC_MEASUREMENT_DURATION)
177 {
178 return PeriodicMeasurementAdapter<UnitSCD40, scd4x::Data>::stopPeriodicMeasurement(duration);
179 }
181
184
193 bool writeTemperatureOffset(const float offset, const uint32_t duration = scd4x::SET_TEMPERATURE_OFFSET_DURATION);
201 bool readTemperatureOffset(float &offset);
211 bool writeSensorAltitude(const uint16_t altitude, const uint32_t duration = scd4x::SET_SENSOR_ALTITUDE_DURATION);
219 bool readSensorAltitude(uint16_t &altitude);
228 bool writeAmbientPressure(const float pressure, const uint32_t duration = scd4x::SET_AMBIENT_PRESSURE_DURATION);
230
233
241 bool performForcedRecalibration(const uint16_t concentration, int16_t &correction);
249 const bool enabled = true, const uint32_t duration = scd4x::SET_AUTOMATIC_SELF_CALIBRATION_ENABLED_DURATION);
257 bool readAutomaticSelfCalibrationEnabled(bool &enabled);
259
262
267 bool writePersistSettings(const uint32_t duration = scd4x::PERSIST_SETTINGS_DURATION);
275 bool readSerialNumber(char *serialNumber);
283 bool readSerialNumber(uint64_t &serialNumber);
291 bool performSelfTest(bool &malfunction);
299 bool performFactoryReset(const uint32_t duration = scd4x::PERFORM_FACTORY_RESET_DURATION);
306 bool reInit(const uint32_t duration = scd4x::REINIT_DURATION);
308
309protected:
310 bool start_periodic_measurement(const scd4x::Mode mode = scd4x::Mode::Normal);
311 bool stop_periodic_measurement(const uint32_t duration = scd4x::STOP_PERIODIC_MEASUREMENT_DURATION);
312 bool read_data_ready_status();
313 bool read_measurement(scd4x::Data &d, const bool all = true);
314
315 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitSCD40, scd4x::Data);
316
317protected:
318 std::unique_ptr<m5::container::CircularBuffer<scd4x::Data>> _data{};
319 config_t _cfg{};
320};
321
323namespace scd4x {
324namespace command {
325// Basic Commands
326constexpr uint16_t START_PERIODIC_MEASUREMENT{0x21b1};
327constexpr uint16_t READ_MEASUREMENT{0xec05};
328constexpr uint16_t STOP_PERIODIC_MEASUREMENT{0x3f86};
329// On-chip output signal compensation
330constexpr uint16_t SET_TEMPERATURE_OFFSET{0x241d};
331constexpr uint16_t GET_TEMPERATURE_OFFSET{0x2318};
332constexpr uint16_t SET_SENSOR_ALTITUDE{0x2427};
333constexpr uint16_t GET_SENSOR_ALTITUDE{0x2322};
334constexpr uint16_t SET_AMBIENT_PRESSURE{0xe000};
335// Field calibration
336constexpr uint16_t PERFORM_FORCED_CALIBRATION{0x362f};
337constexpr uint16_t SET_AUTOMATIC_SELF_CALIBRATION_ENABLED{0x2416};
338constexpr uint16_t GET_AUTOMATIC_SELF_CALIBRATION_ENABLED{0x2313};
339// Low power
340constexpr uint16_t START_LOW_POWER_PERIODIC_MEASUREMENT{0x21ac};
341constexpr uint16_t GET_DATA_READY_STATUS{0xe4b8};
342// Advanced features
343constexpr uint16_t PERSIST_SETTINGS{0x3615};
344constexpr uint16_t GET_SERIAL_NUMBER{0x3682};
345constexpr uint16_t PERFORM_SELF_TEST{0x3639};
346constexpr uint16_t PERFORM_FACTORY_RESET{0x3632};
347constexpr uint16_t REINIT{0x3646};
348} // namespace command
349} // namespace scd4x
351
352} // namespace unit
353} // namespace m5
354#endif
SCD40 unit component.
Definition unit_SCD40.hpp:78
float temperature() const
Oldest measured temperature (Celsius)
Definition unit_SCD40.hpp:131
bool performFactoryReset(const uint32_t duration=scd4x::PERFORM_FACTORY_RESET_DURATION)
Peform factory reset.
Definition unit_SCD40.cpp:363
float humidity() const
Oldest measured humidity (RH)
Definition unit_SCD40.hpp:146
bool startPeriodicMeasurement(const scd4x::Mode mode=scd4x::Mode::Normal)
Start periodic measurement.
Definition unit_SCD40.hpp:159
bool performSelfTest(bool &malfunction)
Perform self test.
Definition unit_SCD40.cpp:348
bool writeAmbientPressure(const float pressure, const uint32_t duration=scd4x::SET_AMBIENT_PRESSURE_DURATION)
Write the ambient pressure.
Definition unit_SCD40.cpp:204
bool readSerialNumber(char *serialNumber)
Read the serial number string.
Definition unit_SCD40.cpp:308
bool writeTemperatureOffset(const float offset, const uint32_t duration=scd4x::SET_TEMPERATURE_OFFSET_DURATION)
Write the temperature offset.
Definition unit_SCD40.cpp:142
bool writeSensorAltitude(const uint16_t altitude, const uint32_t duration=scd4x::SET_SENSOR_ALTITUDE_DURATION)
Write the sensor altitude.
Definition unit_SCD40.cpp:177
bool readSensorAltitude(uint16_t &altitude)
Read the sensor altitude.
Definition unit_SCD40.cpp:194
bool writeAutomaticSelfCalibrationEnabled(const bool enabled=true, const uint32_t duration=scd4x::SET_AUTOMATIC_SELF_CALIBRATION_ENABLED_DURATION)
Enable/disable automatic self calibration.
Definition unit_SCD40.cpp:257
float fahrenheit() const
Oldest measured temperature (Fahrenheit)
Definition unit_SCD40.hpp:141
bool reInit(const uint32_t duration=scd4x::REINIT_DURATION)
Re-initialize the sensor, load settings from EEPROM.
Definition unit_SCD40.cpp:376
config_t config() const
Gets the configration.
Definition unit_SCD40.hpp:112
float celsius() const
Oldest measured temperature (Celsius)
Definition unit_SCD40.hpp:136
bool startLowPowerPeriodicMeasurement()
Start low power periodic measurement.
Definition unit_SCD40.hpp:167
void config(const config_t &cfg)
Set the configration.
Definition unit_SCD40.hpp:117
bool stopPeriodicMeasurement(const uint32_t duration=scd4x::STOP_PERIODIC_MEASUREMENT_DURATION)
Stop periodic measurement.
Definition unit_SCD40.hpp:176
uint16_t co2() const
Oldest measured CO2 concentration (ppm)
Definition unit_SCD40.hpp:126
bool readAutomaticSelfCalibrationEnabled(bool &enabled)
Check if automatic self calibration is enabled.
Definition unit_SCD40.cpp:273
bool readTemperatureOffset(float &offset)
Read the temperature offset.
Definition unit_SCD40.cpp:163
bool performForcedRecalibration(const uint16_t concentration, int16_t &correction)
Perform forced recalibration.
Definition unit_SCD40.cpp:220
bool writePersistSettings(const uint32_t duration=scd4x::PERSIST_SETTINGS_DURATION)
Write sensor settings from RAM to EEPROM.
Definition unit_SCD40.cpp:294
Top level namespace of M5stack.
For SCD40/41.
Unit-related namespace.
Settings for begin.
Definition unit_SCD40.hpp:86
bool calibration
Enable calibration on begin?
Definition unit_SCD40.hpp:92
bool start_periodic
Start periodic measurement on begin?
Definition unit_SCD40.hpp:88
scd4x::Mode mode
Mode of periodic measurement if start on begin?
Definition unit_SCD40.hpp:90
Measurement data group.
Definition unit_SCD40.hpp:38
float fahrenheit() const
temperature (Fahrenheit)
Definition unit_SCD40.cpp:59
float celsius() const
temperature (Celsius)
Definition unit_SCD40.cpp:54
float temperature() const
temperature (Celsius)
Definition unit_SCD40.hpp:42
uint16_t co2() const
CO2 concentration (ppm)
Definition unit_SCD40.cpp:49
float humidity() const
humidity (RH)
Definition unit_SCD40.cpp:64
std::array< uint8_t, 9 > raw
RAW data.
Definition unit_SCD40.hpp:39
Mode
Mode of periodic measurement.
Definition unit_SCD40.hpp:29