10#ifndef M5_UNIT_ENV_UNIT_SCD40_HPP
11#define M5_UNIT_ENV_UNIT_SCD40_HPP
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
39 std::array<uint8_t, 9>
raw{};
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};
78class UnitSCD40 :
public Component,
public PeriodicMeasurementAdapter<UnitSCD40, scd4x::Data> {
79 M5_UNIT_COMPONENT_HPP_BUILDER(
UnitSCD40, 0x62);
95 explicit UnitSCD40(
const uint8_t addr = DEFAULT_ADDRESS)
96 : Component(addr), _data{new
m5::container::CircularBuffer<
scd4x::Data>(1)}
98 auto ccfg = component_config();
99 ccfg.clock = 400 * 1000U;
100 component_config(ccfg);
106 virtual bool begin()
override;
107 virtual void update(
const bool force =
false)
override;
126 inline uint16_t
co2()
const
128 return !empty() ? oldest().co2() : 0;
133 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
138 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
143 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
148 return !empty() ? oldest().humidity() : std::numeric_limits<float>::quiet_NaN();
161 return PeriodicMeasurementAdapter<UnitSCD40, scd4x::Data>::startPeriodicMeasurement(mode);
178 return PeriodicMeasurementAdapter<UnitSCD40, scd4x::Data>::stopPeriodicMeasurement(duration);
193 bool writeTemperatureOffset(
const float offset,
const uint32_t duration = scd4x::SET_TEMPERATURE_OFFSET_DURATION);
211 bool writeSensorAltitude(
const uint16_t altitude,
const uint32_t duration = scd4x::SET_SENSOR_ALTITUDE_DURATION);
228 bool writeAmbientPressure(
const float pressure,
const uint32_t duration = scd4x::SET_AMBIENT_PRESSURE_DURATION);
249 const bool enabled =
true,
const uint32_t duration = scd4x::SET_AUTOMATIC_SELF_CALIBRATION_ENABLED_DURATION);
306 bool reInit(
const uint32_t duration = scd4x::REINIT_DURATION);
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);
318 std::unique_ptr<m5::container::CircularBuffer<scd4x::Data>> _data{};
326constexpr uint16_t START_PERIODIC_MEASUREMENT{0x21b1};
327constexpr uint16_t READ_MEASUREMENT{0xec05};
328constexpr uint16_t STOP_PERIODIC_MEASUREMENT{0x3f86};
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};
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};
340constexpr uint16_t START_LOW_POWER_PERIODIC_MEASUREMENT{0x21ac};
341constexpr uint16_t GET_DATA_READY_STATUS{0xe4b8};
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};
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.
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