M5Unit-ENV 1.2.0 git rev:d576302
Loading...
Searching...
No Matches
unit_SHT30.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_SHT30_HPP
11#define M5_UNIT_ENV_UNIT_SHT30_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16
17namespace m5 {
18namespace unit {
19
20namespace sht30 {
25enum class Repeatability : uint8_t {
26 High,
27 Medium,
28 Low
29};
30
35enum class MPS : uint8_t {
36 Half,
37 One,
38 Two,
39 Four,
40 Ten,
41};
42
50struct Status {
52 inline bool alertPending() const
53 {
54 return value & (1U << 15);
55 }
57 inline bool heater() const
58 {
59 return value & (1U << 13);
60 }
62 inline bool trackingAlertRH() const
63 {
64 return value & (1U << 11);
65 }
67 inline bool trackingAlert() const
68 {
69 return value & (1U << 10);
70 }
72 inline bool reset() const
73 {
74 return value & (1U << 4);
75 }
77 inline bool command() const
78 {
79 return value & (1U << 1);
80 }
82 inline bool checksum() const
83 {
84 return value & (1U << 0);
85 }
86 uint16_t value{};
87};
88
93struct Data {
94 std::array<uint8_t, 6> raw{};
96 inline float temperature() const
97 {
98 return celsius();
99 }
100 float celsius() const;
101 float fahrenheit() const;
102 float humidity() const;
103};
104
105} // namespace sht30
106
111class UnitSHT30 : public Component, public PeriodicMeasurementAdapter<UnitSHT30, sht30::Data> {
112 M5_UNIT_COMPONENT_HPP_BUILDER(UnitSHT30, 0x44);
113
114public:
119 struct config_t {
121 bool start_periodic{true};
123 sht30::MPS mps{sht30::MPS::One};
125 sht30::Repeatability repeatability{sht30::Repeatability::High};
127 bool start_heater{false};
128 };
129
130 explicit UnitSHT30(const uint8_t addr = DEFAULT_ADDRESS)
131 : Component(addr), _data{new m5::container::CircularBuffer<sht30::Data>(1)}
132 {
133 auto ccfg = component_config();
134 ccfg.clock = 400 * 1000U;
135 component_config(ccfg);
136 }
137 virtual ~UnitSHT30()
138 {
139 }
140
141 virtual bool begin() override;
142 virtual void update(const bool force = false) override;
143
146
148 {
149 return _cfg;
150 }
152 inline void config(const config_t& cfg)
153 {
154 _cfg = cfg;
155 }
157
161 inline float temperature() const
162 {
163 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
164 }
166 inline float celsius() const
167 {
168 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
169 }
171 inline float fahrenheit() const
172 {
173 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
174 }
176 inline float humidity() const
177 {
178 return !empty() ? oldest().humidity() : std::numeric_limits<float>::quiet_NaN();
179 }
181
184
191 {
192 return PeriodicMeasurementAdapter<UnitSHT30, sht30::Data>::startPeriodicMeasurement(mps, rep);
193 }
199 {
200 return PeriodicMeasurementAdapter<UnitSHT30, sht30::Data>::stopPeriodicMeasurement();
201 }
203
206
216 bool measureSingleshot(sht30::Data& d, const sht30::Repeatability rep = sht30::Repeatability::High,
217 const bool stretch = true);
219
227
230
237 bool softReset();
244 bool generalReset();
246
249
253 bool startHeater();
258 bool stopHeater();
260
263
268 bool readStatus(sht30::Status& s);
274 bool clearStatus();
276
279
286 bool readSerialNumber(uint32_t& serialNumber);
294 bool readSerialNumber(char* serialNumber);
296
297protected:
298 bool start_periodic_measurement(const sht30::MPS mps, const sht30::Repeatability rep);
299 bool stop_periodic_measurement();
300 bool read_measurement(sht30::Data& d);
301
302 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitSHT30, sht30::Data);
303
304protected:
305 std::unique_ptr<m5::container::CircularBuffer<sht30::Data>> _data{};
306 config_t _cfg{};
307};
308
310namespace sht30 {
311namespace command {
312// Measurement Commands for Single Shot Data Acquisition Mode
313constexpr uint16_t SINGLE_SHOT_ENABLE_STRETCH_HIGH{0x2C06};
314constexpr uint16_t SINGLE_SHOT_ENABLE_STRETCH_MEDIUM{0x2C0D};
315constexpr uint16_t SINGLE_SHOT_ENABLE_STRETCH_LOW{0x2C10};
316constexpr uint16_t SINGLE_SHOT_DISABLE_STRETCH_HIGH{0x2400};
317constexpr uint16_t SINGLE_SHOT_DISABLE_STRETCH_MEDIUM{0x240B};
318constexpr uint16_t SINGLE_SHOT_DISABLE_STRETCH_LOW{0x2416};
319// Measurement Commands for Periodic Data Acquisition Mode
320constexpr uint16_t START_PERIODIC_MPS_HALF_HIGH{0x2032};
321constexpr uint16_t START_PERIODIC_MPS_HALF_MEDIUM{0x2024};
322constexpr uint16_t START_PERIODIC_MPS_HALF_LOW{0x202f};
323
324constexpr uint16_t START_PERIODIC_MPS_1_HIGH{0x2130};
325constexpr uint16_t START_PERIODIC_MPS_1_MEDIUM{0x2126};
326constexpr uint16_t START_PERIODIC_MPS_1_LOW{0x212D};
327
328constexpr uint16_t START_PERIODIC_MPS_2_HIGH{0x2236};
329constexpr uint16_t START_PERIODIC_MPS_2_MEDIUM{0x2220};
330constexpr uint16_t START_PERIODIC_MPS_2_LOW{0x222B};
331
332constexpr uint16_t START_PERIODIC_MPS_4_HIGH{0x2334};
333constexpr uint16_t START_PERIODIC_MPS_4_MEDIUM{0x2322};
334constexpr uint16_t START_PERIODIC_MPS_4_LOW{0x2329};
335
336constexpr uint16_t START_PERIODIC_MPS_10_HIGH{0x2737};
337constexpr uint16_t START_PERIODIC_MPS_10_MEDIUM{0x2721};
338constexpr uint16_t START_PERIODIC_MPS_10_LOW{0x272A};
339
340constexpr uint16_t STOP_PERIODIC_MEASUREMENT{0x3093};
341constexpr uint16_t ACCELERATED_RESPONSE_TIME{0x2B32};
342constexpr uint16_t READ_MEASUREMENT{0xE000};
343// Reset
344constexpr uint16_t SOFT_RESET{0x30A2};
345// Heater
346constexpr uint16_t START_HEATER{0x306D};
347constexpr uint16_t STOP_HEATER{0x3066};
348// Status
349constexpr uint16_t READ_STATUS{0xF32D};
350constexpr uint16_t CLEAR_STATUS{0x3041};
351// Serial
352constexpr uint16_t GET_SERIAL_NUMBER_ENABLE_STRETCH{0x3780};
353constexpr uint16_t GET_SERIAL_NUMBER_DISABLE_STRETCH{0x3682};
354} // namespace command
355} // namespace sht30
357
358} // namespace unit
359} // namespace m5
360#endif
Temperature and humidity, sensor unit.
Definition unit_SHT30.hpp:111
bool writeModeAccelerateResponseTime()
Write the mode to ART.
Definition unit_SHT30.cpp:206
config_t config()
Gets the configration.
Definition unit_SHT30.hpp:147
bool readSerialNumber(uint32_t &serialNumber)
Read the serial number value.
Definition unit_SHT30.cpp:290
bool softReset()
Soft reset.
Definition unit_SHT30.cpp:236
float celsius() const
Oldest measured temperature (Celsius)
Definition unit_SHT30.hpp:166
bool stopHeater()
Stop heater.
Definition unit_SHT30.cpp:285
void config(const config_t &cfg)
Set the configration.
Definition unit_SHT30.hpp:152
float humidity() const
Oldest measured humidity (RH)
Definition unit_SHT30.hpp:176
bool startPeriodicMeasurement(const sht30::MPS mps, const sht30::Repeatability rep)
Start periodic measurement.
Definition unit_SHT30.hpp:190
float fahrenheit() const
Oldest measured temperature (Fahrenheit)
Definition unit_SHT30.hpp:171
bool readStatus(sht30::Status &s)
Read status.
Definition unit_SHT30.cpp:220
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_SHT30.hpp:198
bool clearStatus()
Clear status.
Definition unit_SHT30.cpp:231
bool startHeater()
Start heater.
Definition unit_SHT30.cpp:280
bool measureSingleshot(sht30::Data &d, const sht30::Repeatability rep=sht30::Repeatability::High, const bool stretch=true)
Measurement single shot.
Definition unit_SHT30.cpp:140
float temperature() const
Oldest measured temperature (Celsius)
Definition unit_SHT30.hpp:161
bool generalReset()
General reset.
Definition unit_SHT30.cpp:253
Top level namespace of M5stack.
Unit-related namespace.
Settings for begin.
Definition unit_SHT30.hpp:119
bool start_heater
start heater on begin?
Definition unit_SHT30.hpp:127
sht30::Repeatability repeatability
Repeatability accuracy level if start on begin.
Definition unit_SHT30.hpp:125
bool start_periodic
Start periodic measurement on begin?
Definition unit_SHT30.hpp:121
sht30::MPS mps
Measuring frequency if start on begin.
Definition unit_SHT30.hpp:123
Measurement data group.
Definition unit_SHT30.hpp:93
float humidity() const
humidity (RH)
Definition unit_SHT30.cpp:82
std::array< uint8_t, 6 > raw
Definition unit_SHT30.hpp:94
float temperature() const
temperature (Celsius)
Definition unit_SHT30.hpp:96
float celsius() const
temperature (Celsius)
Definition unit_SHT30.cpp:72
float fahrenheit() const
temperature (Fahrenheit)
Definition unit_SHT30.cpp:77
Accessor for Status.
Definition unit_SHT30.hpp:50
bool trackingAlertRH() const
RH tracking alert (*)
Definition unit_SHT30.hpp:62
bool trackingAlert() const
Tracking alert (*)
Definition unit_SHT30.hpp:67
bool alertPending() const
Alert pending status (*)
Definition unit_SHT30.hpp:52
bool heater() const
Heater status.
Definition unit_SHT30.hpp:57
bool command() const
Command staus.
Definition unit_SHT30.hpp:77
bool reset() const
System reset detected (*)
Definition unit_SHT30.hpp:72
bool checksum() const
Write data checksum status.
Definition unit_SHT30.hpp:82
MPS
Measuring frequency.
Definition unit_SHT30.hpp:35
@ One
1 measurement per second
@ Four
4 measurement per second
@ Ten
10 measurement per second
@ Two
2 measurement per second
@ Half
0.5 measurement per second
Repeatability
Repeatability accuracy level.
Definition unit_SHT30.hpp:25
@ High
High repeatability.
@ Medium
Medium repeatability.