M5Unit-ENV 1.2.0 git rev:d576302
Loading...
Searching...
No Matches
unit_BMP280.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_BNP280_HPP
11#define M5_UNIT_ENV_UNIT_BNP280_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 bmp280 {
25
30enum class PowerMode : uint8_t {
31 Sleep,
32 Forced,
33 Normal = 0x03,
34};
35
40enum class Oversampling : uint8_t {
41 Skipped,
42 X1,
43 X2,
44 X4,
45 X8,
46 X16,
47};
48
53enum class OversamplingSetting : uint8_t {
54 UltraLowPower,
55 LowPower,
59};
60
65enum class Filter : uint8_t {
66 Off,
67 Coeff2,
68 Coeff4,
69 Coeff8,
70 Coeff16,
71};
72
77enum class Standby : uint8_t {
78 Time0_5ms,
80 Time125ms,
81 Time250ms,
82 Time500ms,
83 Time1sec,
84 Time2sec,
85 Time4sec,
86};
87
92enum class UseCase : uint8_t {
93 LowPower,
94 Dynamic,
95 Weather,
96 Elevator,
97 Drop,
98 Indoor,
99};
100
105union Trimming {
106 uint8_t value[12 * 2]{};
107 struct {
108 //
109 uint16_t dig_T1;
110 int16_t dig_T2;
111 int16_t dig_T3;
112 //
113 uint16_t dig_P1;
114 int16_t dig_P2;
115 int16_t dig_P3;
116 int16_t dig_P4;
117 int16_t dig_P5;
118 int16_t dig_P6;
119 int16_t dig_P7;
120 int16_t dig_P8;
121 int16_t dig_P9;
122 // uint16_t reserved;
123 } __attribute__((packed));
124};
125
130struct Data {
131 std::array<uint8_t, 6> raw{};
133
135 inline float temperature() const
136 {
137 return celsius();
138 }
139 float celsius() const;
140 float fahrenheit() const;
141 float pressure() const;
142};
143
144} // namespace bmp280
145
150class UnitBMP280 : public Component, public PeriodicMeasurementAdapter<UnitBMP280, bmp280::Data> {
151 M5_UNIT_COMPONENT_HPP_BUILDER(UnitBMP280, 0x76);
152
153public:
158 struct config_t {
160 bool start_periodic{true};
162 bmp280::Oversampling osrs_pressure{bmp280::Oversampling::X16};
164 bmp280::Oversampling osrs_temperature{bmp280::Oversampling::X2};
166 bmp280::Filter filter{bmp280::Filter::Coeff16};
168 bmp280::Standby standby{bmp280::Standby::Time1sec};
169 };
170
171 explicit UnitBMP280(const uint8_t addr = DEFAULT_ADDRESS)
172 : Component(addr), _data{new m5::container::CircularBuffer<bmp280::Data>(1)}
173 {
174 auto ccfg = component_config();
175 ccfg.clock = 400 * 1000U;
176 component_config(ccfg);
177 }
178 virtual ~UnitBMP280()
179 {
180 }
181
182 virtual bool begin() override;
183 virtual void update(const bool force = false) override;
184
187
189 {
190 return _cfg;
191 }
193 inline void config(const config_t& cfg)
194 {
195 _cfg = cfg;
196 }
198
202 inline float temperature() const
203 {
204 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
205 }
207 inline float celsius() const
208 {
209 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
210 }
212 inline float fahrenheit() const
213 {
214 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
215 }
217 inline float pressure() const
218 {
219 return !empty() ? oldest().pressure() : std::numeric_limits<float>::quiet_NaN();
220 }
222
225
234 inline bool startPeriodicMeasurement(const bmp280::Oversampling osrsPressure,
235 const bmp280::Oversampling osrsTemperature, const bmp280::Filter filter,
236 const bmp280::Standby st)
237 {
238 return PeriodicMeasurementAdapter<UnitBMP280, bmp280::Data>::startPeriodicMeasurement(
239 osrsPressure, osrsTemperature, filter, st);
240 }
243 {
244 return PeriodicMeasurementAdapter<UnitBMP280, bmp280::Data>::startPeriodicMeasurement();
245 }
246
252 {
253 return PeriodicMeasurementAdapter<UnitBMP280, bmp280::Data>::stopPeriodicMeasurement();
254 }
256
259
270 bool measureSingleshot(bmp280::Data& d, const bmp280::Oversampling osrsPressure,
271 const bmp280::Oversampling osrsTemperature, const bmp280::Filter filter);
274 {
275 return measure_singleshot(d);
276 }
278
281
287 bool readOversampling(bmp280::Oversampling& osrsPressure, bmp280::Oversampling& osrsTemperature);
295 bool writeOversampling(const bmp280::Oversampling osrsPressure, const bmp280::Oversampling osrsTemperature);
302 bool writeOversamplingPressure(const bmp280::Oversampling osrsPressure);
309 bool writeOversamplingTemperature(const bmp280::Oversampling osrsTemperature);
322 bool readFilter(bmp280::Filter& f);
329 bool writeFilter(const bmp280::Filter& f);
342 bool writeStandbyTime(const bmp280::Standby s);
356 bool writePowerMode(const bmp280::PowerMode m);
365
370 bool softReset();
371
372protected:
373 bool start_periodic_measurement(const bmp280::Oversampling osrsPressure, const bmp280::Oversampling osrsTemperature,
374 const bmp280::Filter filter, const bmp280::Standby st);
375 bool start_periodic_measurement();
376 bool stop_periodic_measurement();
377 bool read_measurement(bmp280::Data& d);
378 bool measure_singleshot(bmp280::Data& d);
379
380 bool read_trimming(bmp280::Trimming& t);
381 bool is_data_ready();
382
383 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitBMP280, bmp280::Data);
384
385protected:
386 std::unique_ptr<m5::container::CircularBuffer<bmp280::Data>> _data{};
387 config_t _cfg{};
388 bmp280::Trimming _trimming{};
389};
390
392namespace bmp280 {
393namespace command {
394
395constexpr uint8_t CHIP_ID{0xD0};
396// constexpr uint8_t CHIP_VERSION{0xD1};
397constexpr uint8_t SOFT_RESET{0xE0};
398constexpr uint8_t GET_STATUS{0xF3};
399constexpr uint8_t CONTROL_MEASUREMENT{0xF4};
400constexpr uint8_t CONFIG{0xF5};
401
402constexpr uint8_t GET_MEASUREMENT{0XF7}; // 6bytes
403constexpr uint8_t GET_PRESSURE{0xF7}; // 3byts
404constexpr uint8_t GET_PRESSURE_MSB{0xF7}; // 7:0
405constexpr uint8_t GET_PRESSURE_LSB{0xF8}; // 7:0
406constexpr uint8_t GET_PRESSURE_XLSB{0xF9}; // 7:4
407constexpr uint8_t GET_TEMPERATURE{0XFA}; // 3 bytes
408constexpr uint8_t GET_TEMPERATURE_MSB{0XFA}; // 7:0
409constexpr uint8_t GET_TEMPERATURE_LSB{0XFB}; // 7:0
410constexpr uint8_t GET_TEMPERATURE_XLSB{0XFC}; // 7:4
411
412constexpr uint8_t TRIMMING_DIG{0x88}; // 12 bytes
413constexpr uint8_t TRIMMING_DIG_T1{0x88};
414constexpr uint8_t TRIMMING_DIG_T2{0x8A};
415constexpr uint8_t TRIMMING_DIG_T3{0x8C};
416constexpr uint8_t TRIMMING_DIG_P1{0x8E};
417constexpr uint8_t TRIMMING_DIG_P2{0x90};
418constexpr uint8_t TRIMMING_DIG_P3{0x92};
419constexpr uint8_t TRIMMING_DIG_P4{0x94};
420constexpr uint8_t TRIMMING_DIG_P5{0x96};
421constexpr uint8_t TRIMMING_DIG_P6{0x98};
422constexpr uint8_t TRIMMING_DIG_P7{0x9A};
423constexpr uint8_t TRIMMING_DIG_P8{0x9C};
424constexpr uint8_t TRIMMING_DIG_P9{0x9A};
425constexpr uint8_t TRIMMING_DIG_RESERVED{0xA0};
426
427} // namespace command
428} // namespace bmp280
430
431} // namespace unit
432} // namespace m5
433
434#endif
Pressure and temperature sensor unit.
Definition unit_BMP280.hpp:150
bool measureSingleshot(bmp280::Data &d)
Measurement single shot using current settings.
Definition unit_BMP280.hpp:273
bool writeFilter(const bmp280::Filter &f)
Write the IIR filter co-efficient.
Definition unit_BMP280.cpp:497
float pressure() const
Oldest measured pressure (Pa)
Definition unit_BMP280.hpp:217
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_BMP280.hpp:251
bool writePowerMode(const bmp280::PowerMode m)
Write the power mode.
Definition unit_BMP280.cpp:463
bool writeStandbyTime(const bmp280::Standby s)
Write the standby time.
Definition unit_BMP280.cpp:530
bool readStandbyTime(bmp280::Standby &s)
Read the standby time.
Definition unit_BMP280.cpp:520
float temperature() const
Oldest measured temperature (Celsius)
Definition unit_BMP280.hpp:202
bool writeOversampling(const bmp280::Oversampling osrsPressure, const bmp280::Oversampling osrsTemperature)
Write the oversampling conditions.
Definition unit_BMP280.cpp:401
bool softReset()
Soft reset.
Definition unit_BMP280.cpp:551
float fahrenheit() const
Oldest measured temperature (Fahrenheit)
Definition unit_BMP280.hpp:212
bool writeOversamplingTemperature(const bmp280::Oversampling osrsTemperature)
Write the oversampling conditions for temperature.
Definition unit_BMP280.cpp:432
bool writeOversamplingPressure(const bmp280::Oversampling osrsPressure)
Write the oversampling conditions for pressure.
Definition unit_BMP280.cpp:417
void config(const config_t &cfg)
Set the configration.
Definition unit_BMP280.hpp:193
bool measureSingleshot(bmp280::Data &d, const bmp280::Oversampling osrsPressure, const bmp280::Oversampling osrsTemperature, const bmp280::Filter filter)
Measurement single shot.
Definition unit_BMP280.cpp:347
bool startPeriodicMeasurement(const bmp280::Oversampling osrsPressure, const bmp280::Oversampling osrsTemperature, const bmp280::Filter filter, const bmp280::Standby st)
Start periodic measurement.
Definition unit_BMP280.hpp:234
bool startPeriodicMeasurement()
Start periodic measurement using current settings.
Definition unit_BMP280.hpp:242
bool readPowerMode(bmp280::PowerMode &m)
Read the power mode.
Definition unit_BMP280.cpp:453
config_t config()
Gets the configration.
Definition unit_BMP280.hpp:188
bool writeUseCaseSetting(const bmp280::UseCase uc)
Write the settings based on use cases.
Definition unit_BMP280.cpp:545
float celsius() const
Oldest measured temperature (Celsius)
Definition unit_BMP280.hpp:207
bool readFilter(bmp280::Filter &f)
Read the IIR filter co-efficient.
Definition unit_BMP280.cpp:487
bool readOversampling(bmp280::Oversampling &osrsPressure, bmp280::Oversampling &osrsTemperature)
Read the oversampling conditions.
Definition unit_BMP280.cpp:390
For BMP280.
Top level namespace of M5stack.
Unit-related namespace.
Settings for begin.
Definition unit_BMP280.hpp:158
bmp280::Oversampling osrs_pressure
Pressure oversampling if start on begin.
Definition unit_BMP280.hpp:162
bmp280::Standby standby
Standby time if start on begin.
Definition unit_BMP280.hpp:168
bmp280::Filter filter
Filter if start on begin.
Definition unit_BMP280.hpp:166
bmp280::Oversampling osrs_temperature
Temperature oversampling if start on begin.
Definition unit_BMP280.hpp:164
bool start_periodic
Start periodic measurement on begin?
Definition unit_BMP280.hpp:160
Measurement data group.
Definition unit_BMP280.hpp:130
const Trimming * trimming
For calculate.
Definition unit_BMP280.hpp:132
float temperature() const
temperature (Celsius)
Definition unit_BMP280.hpp:135
float pressure() const
pressure (Pa)
Definition unit_BMP280.cpp:228
float fahrenheit() const
temperature (Fahrenheit)
Definition unit_BMP280.cpp:223
std::array< uint8_t, 6 > raw
RAW data [0,1,2]:pressure [3,4,5]:temperature.
Definition unit_BMP280.hpp:131
float celsius() const
temperature (Celsius)
Definition unit_BMP280.cpp:212
Definition unit_BMP280.hpp:105
Oversampling
Oversampling factor.
Definition unit_BMP280.hpp:40
@ Skipped
Skipped (No measurements are performed)
Filter
Filter setting.
Definition unit_BMP280.hpp:65
@ Coeff4
co-efficient 4
@ Coeff16
co-efficient 16
@ Coeff8
co-efficient 8
@ Coeff2
co-efficient 2
OversamplingSetting
Oversampling Settings.
Definition unit_BMP280.hpp:53
@ StandardResolution
18 bit / 0.66 Pa, 16 bit / 0.0050 C
@ HighResolution
19 bit / 0.33 Pa, 16 bit / 0.0050 C
@ UltraHighResolution
20 bit / 0.16 Pa, 17 bit / 0.0025 C
PowerMode
Operation mode.
Definition unit_BMP280.hpp:30
@ Normal
Periodic measurements are performed.
UseCase
Preset settings.
Definition unit_BMP280.hpp:92
@ Weather
Weather monitoring.
@ Drop
Drop detection.
@ Indoor
Indoor navigation.
@ Elevator
Elevator / floor change detection.
@ Dynamic
Handheld device dynamic.
Standby
Measurement standby time for power mode Normal.
Definition unit_BMP280.hpp:77