M5Unit-ENV 1.2.0 git rev:d576302
Loading...
Searching...
No Matches
unit_QMP6988.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_QMP6988_HPP
11#define M5_UNIT_ENV_UNIT_QMP6988_HPP
12#include <M5UnitComponent.hpp>
13#include <m5_utility/stl/extension.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16
17namespace m5 {
18namespace unit {
19
20namespace qmp6988 {
21
26enum class PowerMode : uint8_t {
27 Sleep,
28 Forced,
29 Normal = 0x03,
30};
31
37enum class Oversampling : uint8_t {
38 Skipped,
39 X1,
40 X2,
41 X4,
42 X8,
43 X16,
44 X32,
45 X64,
46};
47
52enum class OversamplingSetting : uint8_t {
53 HighSpeed,
54 LowPower,
55 Standard,
58};
59
64enum class Filter : uint8_t {
65 Off,
66 Coeff2,
67 Coeff4,
68 Coeff8,
69 Coeff16,
70 Coeff32,
71};
72
77enum class Standby : uint8_t {
78 Time1ms,
79 Time5ms,
80 Time50ms,
81 Time250ms,
82 Time500ms,
83 Time1sec,
84 Time2sec,
85 Time4sec,
86};
87
92enum class UseCase : uint8_t {
93 Weather,
94 Drop,
95 Elevator,
96 Stair,
97 Indoor,
98};
99
101struct Calibration {
102 int32_t b00{}, bt1{}, bp1{};
103 int64_t bt2{};
104 int32_t b11{}, bp2{}, b12{}, b21{}, bp3{}, a0{}, a1{}, a2{};
105};
107
112struct Data {
113 std::array<uint8_t, 6> raw{};
115 inline float temperature() const
116 {
117 return celsius();
118 }
119 float celsius() const;
120 float fahrenheit() const;
121 float pressure() const;
122 const Calibration* calib{};
123};
124
125}; // namespace qmp6988
126
131class UnitQMP6988 : public Component, public PeriodicMeasurementAdapter<UnitQMP6988, qmp6988::Data> {
132 M5_UNIT_COMPONENT_HPP_BUILDER(UnitQMP6988, 0x70);
133
134public:
139 struct config_t {
141 bool start_periodic{true};
143 qmp6988::Oversampling osrs_pressure{qmp6988::Oversampling::X8};
145 qmp6988::Oversampling osrs_temperature{qmp6988::Oversampling::X1};
147 qmp6988::Filter filter{qmp6988::Filter::Coeff4};
149 qmp6988::Standby standby{qmp6988::Standby::Time1sec};
150 };
151
152 explicit UnitQMP6988(const uint8_t addr = DEFAULT_ADDRESS)
153 : Component(addr), _data{new m5::container::CircularBuffer<qmp6988::Data>(1)}
154 {
155 auto ccfg = component_config();
156 ccfg.clock = 400 * 1000U;
157 component_config(ccfg);
158 }
159 virtual ~UnitQMP6988()
160 {
161 }
162
163 virtual bool begin() override;
164 virtual void update(const bool force = false) override;
165
168
170 {
171 return _cfg;
172 }
174 inline void config(const config_t& cfg)
175 {
176 _cfg = cfg;
177 }
179
183 inline float temperature() const
184 {
185 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
186 }
188 inline float celsius() const
189 {
190 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
191 }
193 inline float fahrenheit() const
194 {
195 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
196 }
198 inline float pressure() const
199 {
200 return !empty() ? oldest().pressure() : std::numeric_limits<float>::quiet_NaN();
201 }
203
206
215 inline bool startPeriodicMeasurement(const qmp6988::Oversampling osrsPressure,
216 const qmp6988::Oversampling osrsTemperature, const qmp6988::Filter f,
217 const qmp6988::Standby st)
218 {
219 return PeriodicMeasurementAdapter<UnitQMP6988, qmp6988::Data>::startPeriodicMeasurement(osrsPressure,
220 osrsTemperature, f, st);
221 }
224 {
225 return PeriodicMeasurementAdapter<UnitQMP6988, qmp6988::Data>::startPeriodicMeasurement();
226 }
232 {
233 return PeriodicMeasurementAdapter<UnitQMP6988, qmp6988::Data>::stopPeriodicMeasurement();
234 }
236
239
250 bool measureSingleshot(qmp6988::Data& d, const qmp6988::Oversampling osrsPressure,
251 const qmp6988::Oversampling osrsTemperature, const qmp6988::Filter f);
255
258
264 bool readOversampling(qmp6988::Oversampling& osrsPressure, qmp6988::Oversampling& osrsTemperature);
272 bool writeOversampling(const qmp6988::Oversampling osrsPressure, const qmp6988::Oversampling osrsTemperature);
279 bool writeOversamplingPressure(const qmp6988::Oversampling osrsPressure);
286 bool writeOversamplingTemperature(const qmp6988::Oversampling osrsTemperature);
306 bool writeFilter(const qmp6988::Filter f);
319 bool writeStandbyTime(const qmp6988::Standby st);
333 bool writePowerMode(const qmp6988::PowerMode mode);
342
347 bool softReset();
348
349protected:
350 bool start_periodic_measurement();
351 bool start_periodic_measurement(const qmp6988::Oversampling ost, const qmp6988::Oversampling osp,
352 const qmp6988::Filter f, const qmp6988::Standby st);
353 bool stop_periodic_measurement();
354
355 bool read_calibration(qmp6988::Calibration& c);
356
357 bool read_measurement(qmp6988::Data& d, const bool only_temperature = false);
358 bool is_data_ready();
359
360 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitQMP6988, qmp6988::Data);
361
362protected:
363 std::unique_ptr<m5::container::CircularBuffer<qmp6988::Data>> _data{};
364 qmp6988::Calibration _calibration{};
365 config_t _cfg{};
366 bool _only_temperature{};
367};
368
370namespace qmp6988 {
371namespace command {
372
373constexpr uint8_t CHIP_ID{0xD1};
374
375constexpr uint8_t READ_PRESSURE{0xF7}; // ~ F9 3bytes
376constexpr uint8_t READ_TEMPERATURE{0xFA}; // ~ FC 3bytes
377
378constexpr uint8_t IO_SETUP{0xF5};
379constexpr uint8_t CONTROL_MEASUREMENT{0xF4};
380constexpr uint8_t GET_STATUS{0xF3};
381constexpr uint8_t IIR_FILTER{0xF1};
382
383constexpr uint8_t SOFT_RESET{0xE0};
384
385constexpr uint8_t READ_COMPENSATION_COEFFICIENT{0xA0}; // ~ 0xB8 25 bytes
386
387} // namespace command
388} // namespace qmp6988
390
391} // namespace unit
392} // namespace m5
393#endif
Barometric pressure sensor to measure atmospheric pressure and altitude estimation.
Definition unit_QMP6988.hpp:131
float celsius() const
Oldest measured temperature (Celsius)
Definition unit_QMP6988.hpp:188
bool writeOversampling(const qmp6988::Oversampling osrsPressure, const qmp6988::Oversampling osrsTemperature)
Write the oversampling conditions.
Definition unit_QMP6988.cpp:378
bool readStandbyTime(qmp6988::Standby &st)
Read the standby time.
Definition unit_QMP6988.cpp:481
bool measureSingleshot(qmp6988::Data &d, const qmp6988::Oversampling osrsPressure, const qmp6988::Oversampling osrsTemperature, const qmp6988::Filter f)
Measurement single shot.
Definition unit_QMP6988.cpp:328
bool readFilter(qmp6988::Filter &f)
Read the IIR filter co-efficient.
Definition unit_QMP6988.cpp:462
bool writePowerMode(const qmp6988::PowerMode mode)
Write the power mode.
Definition unit_QMP6988.cpp:441
bool writeFilter(const qmp6988::Filter f)
Write the IIR filter co-efficient.
Definition unit_QMP6988.cpp:472
float fahrenheit() const
Oldest measured temperature (Fahrenheit)
Definition unit_QMP6988.hpp:193
bool softReset()
Soft reset.
Definition unit_QMP6988.cpp:512
bool readOversampling(qmp6988::Oversampling &osrsPressure, qmp6988::Oversampling &osrsTemperature)
Read the oversampling conditions.
Definition unit_QMP6988.cpp:367
bool writeOversamplingPressure(const qmp6988::Oversampling osrsPressure)
Write the oversampling conditions for pressure.
Definition unit_QMP6988.cpp:395
config_t config()
Gets the configration.
Definition unit_QMP6988.hpp:169
float temperature() const
Oldest measured temperature (Celsius)
Definition unit_QMP6988.hpp:183
bool writeOversamplingTemperature(const qmp6988::Oversampling osrsTemperature)
Write the oversampling conditions for temperature.
Definition unit_QMP6988.cpp:410
bool startPeriodicMeasurement(const qmp6988::Oversampling osrsPressure, const qmp6988::Oversampling osrsTemperature, const qmp6988::Filter f, const qmp6988::Standby st)
Start periodic measurement.
Definition unit_QMP6988.hpp:215
void config(const config_t &cfg)
Set the configration.
Definition unit_QMP6988.hpp:174
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_QMP6988.hpp:231
bool writeStandbyTime(const qmp6988::Standby st)
Write the standby time.
Definition unit_QMP6988.cpp:491
bool startPeriodicMeasurement()
Start periodic measurement using current settings.
Definition unit_QMP6988.hpp:223
bool writeUseCaseSetting(const qmp6988::UseCase uc)
Write the settings based on use cases.
Definition unit_QMP6988.cpp:506
float pressure() const
Oldest measured pressure (Pa)
Definition unit_QMP6988.hpp:198
bool readPowerMode(qmp6988::PowerMode &mode)
Read the power mode.
Definition unit_QMP6988.cpp:431
Top level namespace of M5stack.
Unit-related namespace.
Settings for begin.
Definition unit_QMP6988.hpp:139
qmp6988::Filter filter
Filter if start on begin.
Definition unit_QMP6988.hpp:147
qmp6988::Standby standby
Standby time if start on begin.
Definition unit_QMP6988.hpp:149
qmp6988::Oversampling osrs_pressure
pressure oversampling if start on begin
Definition unit_QMP6988.hpp:143
qmp6988::Oversampling osrs_temperature
temperature oversampling if start on begin
Definition unit_QMP6988.hpp:145
bool start_periodic
Start periodic measurement on begin?
Definition unit_QMP6988.hpp:141
Measurement data group.
Definition unit_QMP6988.hpp:112
float temperature() const
temperature (Celsius)
Definition unit_QMP6988.hpp:115
float fahrenheit() const
temperature (Fahrenheit)
Definition unit_QMP6988.cpp:189
float celsius() const
temperature (Celsius)
Definition unit_QMP6988.cpp:178
float pressure() const
pressure (Pa)
Definition unit_QMP6988.cpp:194
std::array< uint8_t, 6 > raw
Definition unit_QMP6988.hpp:113
UseCase
Preset settings.
Definition unit_QMP6988.hpp:92
@ Stair
Stair detection.
OversamplingSetting
Oversampling Settings.
Definition unit_QMP6988.hpp:52
PowerMode
Operation mode.
Definition unit_QMP6988.hpp:26
Filter
Filtter setting.
Definition unit_QMP6988.hpp:64
@ Coeff32
co-efficient 32
Oversampling
Oversampling value.
Definition unit_QMP6988.hpp:37
Standby
Measurement standby time for power mode Normal.
Definition unit_QMP6988.hpp:77