M5Unit-ENV 1.2.0 git rev:d576302
Loading...
Searching...
No Matches
unit_SHT40.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_SHT40_HPP
11#define M5_UNIT_ENV_UNIT_SHT40_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 sht40 {
25
30enum class Precision : uint8_t {
31 High,
32 Medium,
33 Low
34};
35
40enum class Heater : uint8_t {
41 Long,
42 Short,
43 None
44};
45
50struct Data {
51 std::array<uint8_t, 6> raw{};
52 bool heater{};
53
55 inline float temperature() const
56 {
57 return celsius();
58 }
59 float celsius() const;
60 float fahrenheit() const;
61 float humidity() const;
62};
63
64} // namespace sht40
65
70class UnitSHT40 : public Component, public PeriodicMeasurementAdapter<UnitSHT40, sht40::Data> {
71 M5_UNIT_COMPONENT_HPP_BUILDER(UnitSHT40, 0x44);
72
73public:
78 struct config_t {
80 bool start_periodic{true};
82 sht40::Precision precision{sht40::Precision::High};
84 sht40::Heater heater{sht40::Heater::None};
86 float heater_duty{0.05f};
87 };
88
89 explicit UnitSHT40(const uint8_t addr = DEFAULT_ADDRESS)
90 : Component(addr), _data{new m5::container::CircularBuffer<sht40::Data>(1)}
91 {
92 auto ccfg = component_config();
93 ccfg.clock = 400 * 1000U;
94 component_config(ccfg);
95 }
96 virtual ~UnitSHT40()
97 {
98 }
99
100 virtual bool begin() override;
101 virtual void update(const bool force = false) override;
102
105
107 {
108 return _cfg;
109 }
111 inline void config(const config_t& cfg)
112 {
113 _cfg = cfg;
114 }
116
120 inline float temperature() const
121 {
122 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
123 }
125 inline float celsius() const
126 {
127 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
128 }
130 inline float fahrenheit() const
131 {
132 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
133 }
135 inline float humidity() const
136 {
137 return !empty() ? oldest().humidity() : std::numeric_limits<float>::quiet_NaN();
138 }
140
143
152 inline bool startPeriodicMeasurement(const sht40::Precision precision, const sht40::Heater heater,
153 const float duty = 0.05f)
154 {
155 return PeriodicMeasurementAdapter<UnitSHT40, sht40::Data>::startPeriodicMeasurement(precision, heater, duty);
156 }
162 {
163 return PeriodicMeasurementAdapter<UnitSHT40, sht40::Data>::stopPeriodicMeasurement();
164 }
166
169
180 bool measureSingleshot(sht40::Data& d, const sht40::Precision precision = sht40::Precision::High,
181 const sht40::Heater heater = sht40::Heater::None);
182
184
187
191 bool softReset();
198 bool generalReset();
200
203
210 bool readSerialNumber(uint32_t& serialNumber);
218 bool readSerialNumber(char* serialNumber);
220
221protected:
222 bool start_periodic_measurement(const sht40::Precision precision, const sht40::Heater heater, const float duty);
223 bool stop_periodic_measurement();
224 bool read_measurement(sht40::Data& d);
225 void reset_status();
226 bool soft_reset();
227
228 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitSHT40, sht40::Data);
229
230protected:
231 std::unique_ptr<m5::container::CircularBuffer<sht40::Data>> _data{};
232 config_t _cfg{};
233 uint8_t _cmd{}, _measureCmd{};
234 types::elapsed_time_t _latest_heater{}, _interval_heater{};
235 uint32_t _duration_measure{}, _duration_heater{};
236};
237
239namespace sht40 {
240namespace command {
241constexpr uint8_t MEASURE_HIGH_HEATER_1S{0x39};
242constexpr uint8_t MEASURE_HIGH_HEATER_100MS{0x32};
243constexpr uint8_t MEASURE_HIGH{0xFD};
244
245constexpr uint8_t MEASURE_MEDIUM_HEATER_1S{0x2F};
246constexpr uint8_t MEASURE_MEDIUM_HEATER_100MS{0x24};
247constexpr uint8_t MEASURE_MEDIUM{0xF6};
248
249constexpr uint8_t MEASURE_LOW_HEATER_1S{0x1E};
250constexpr uint8_t MEASURE_LOW_HEATER_100MS{0x15};
251constexpr uint8_t MEASURE_LOW{0xE0};
252
253constexpr uint8_t GET_SERIAL_NUMBER{0x89};
254constexpr uint8_t SOFT_RESET{0x94};
255} // namespace command
256} // namespace sht40
258} // namespace unit
259} // namespace m5
260#endif
Temperature and humidity, sensor unit.
Definition unit_SHT40.hpp:70
bool softReset()
Soft reset.
Definition unit_SHT40.cpp:209
bool readSerialNumber(uint32_t &serialNumber)
Read the serial number value.
Definition unit_SHT40.cpp:242
float humidity() const
Oldest measured humidity (RH)
Definition unit_SHT40.hpp:135
bool generalReset()
General reset.
Definition unit_SHT40.cpp:230
config_t config()
Gets the configration.
Definition unit_SHT40.hpp:106
void config(const config_t &cfg)
Set the configration.
Definition unit_SHT40.hpp:111
bool measureSingleshot(sht40::Data &d, const sht40::Precision precision=sht40::Precision::High, const sht40::Heater heater=sht40::Heater::None)
Measurement single shot.
Definition unit_SHT40.cpp:176
float fahrenheit() const
Oldest measured temperature (Fahrenheit)
Definition unit_SHT40.hpp:130
bool startPeriodicMeasurement(const sht40::Precision precision, const sht40::Heater heater, const float duty=0.05f)
Start periodic measurement.
Definition unit_SHT40.hpp:152
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_SHT40.hpp:161
float celsius() const
Oldest measured temperature (Celsius)
Definition unit_SHT40.hpp:125
float temperature() const
Oldest measured temperature (Celsius)
Definition unit_SHT40.hpp:120
Top level namespace of M5stack.
For SHT40.
Unit-related namespace.
Settings for begin.
Definition unit_SHT40.hpp:78
float heater_duty
Heater duty cycle if start on begin [~ 0.05f].
Definition unit_SHT40.hpp:86
sht40::Precision precision
Precision level if start on begin.
Definition unit_SHT40.hpp:82
bool start_periodic
Start periodic measurement on begin?
Definition unit_SHT40.hpp:80
sht40::Heater heater
Heater behavior if start on begin.
Definition unit_SHT40.hpp:84
Measurement data group.
Definition unit_SHT40.hpp:50
float celsius() const
temperature (Celsius)
Definition unit_SHT40.cpp:55
std::array< uint8_t, 6 > raw
RAW data.
Definition unit_SHT40.hpp:51
float fahrenheit() const
temperature (Fahrenheit)
Definition unit_SHT40.cpp:60
bool heater
Measured data after heater is activated if true.
Definition unit_SHT40.hpp:52
float humidity() const
humidity (RH)
Definition unit_SHT40.cpp:66
float temperature() const
temperature (Celsius)
Definition unit_SHT40.hpp:55
Precision
precision level
Definition unit_SHT40.hpp:30
Heater
Heater behavior.
Definition unit_SHT40.hpp:40
@ Short
Activate heater for 0.1s.
@ Long
Activate heater for 1s.