FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
Bmp280 Class Reference

#include <bmp280.hpp>

Collaboration diagram for Bmp280:

Public Types

enum  Register : uint8_t {
  REG_ID = 0xD0 , REG_RESET = 0xE0 , REG_CTRL_MEAS = 0xF4 , REG_CONFIG = 0xF5 ,
  REG_PRESS_MSB = 0xF7 , REG_TEMP_MSB = 0xFA , REG_CALIB = 0x88
}
 

Public Member Functions

 Bmp280 (I2cDevice &i2c)
 
void ThreadFun ()
 
void Display ()
 
float ReadTemperature ()
 
float ReadPressure ()
 

Static Public Attributes

static constexpr uint8_t DEFAULT_I2C_ADDR = 0x77
 

Private Member Functions

void Configure ()
 
void ReadCalibration ()
 
int32_t ReadRawTemperature ()
 
int32_t ReadRawPressure ()
 
int32_t CompensateTemperature (int32_t adc_T)
 
uint32_t CompensatePressure (int32_t adc_P)
 

Private Attributes

I2cDevicei2c_
 
int32_t t_fine_ = 0
 
uint16_t dig_t1_
 
int16_t dig_t2_
 
int16_t dig_t3_
 
uint16_t dig_p1_
 
int16_t dig_p2_
 
int16_t dig_p3_
 
int16_t dig_p4_
 
int16_t dig_p5_
 
int16_t dig_p6_
 
int16_t dig_p7_
 
int16_t dig_p8_
 
int16_t dig_p9_
 
int32_t adc_t_ = 0
 
std::thread thread_
 

Detailed Description

BMP280 temperature and pressure sensor driver Supports I2C communication and hardware compensation

Definition at line 15 of file bmp280.hpp.

Member Enumeration Documentation

◆ Register

enum Bmp280::Register : uint8_t

Sensor register addresses

Definition at line 21 of file bmp280.hpp.

21 : uint8_t {
22 REG_ID = 0xD0, // Chip ID register
23 REG_RESET = 0xE0, // Reset register
24 REG_CTRL_MEAS = 0xF4, // Measurement control
25 REG_CONFIG = 0xF5, // Configuration
26 REG_PRESS_MSB = 0xF7, // Pressure MSB
27 REG_TEMP_MSB = 0xFA, // Temperature MSB
28 REG_CALIB = 0x88 // Calibration data start
29 };

Constructor & Destructor Documentation

◆ Bmp280()

Bmp280::Bmp280 ( I2cDevice i2c)
inline

Initialize BMP280 with specified I2C device

Parameters
i2cPre-configured I2C device instance

Definition at line 35 of file bmp280.hpp.

35 : i2c_(i2c) {
36 uint8_t id = i2c_.ReadRegister(REG_ID);
37 if (id != 0x58) { // Verify chip ID
38 std::perror("Invalid BMP280 ID");
39 }
40
42 Configure();
43
44 thread_ = std::thread(&Bmp280::ThreadFun, this);
45 }
void Configure()
Definition bmp280.hpp:97
void ReadCalibration()
Definition bmp280.hpp:105
uint8_t ReadRegister(uint8_t reg)
Definition bsp_i2c.hpp:53

Member Function Documentation

◆ CompensatePressure()

uint32_t Bmp280::CompensatePressure ( int32_t  adc_P)
inlineprivate

Pressure compensation algorithm

Definition at line 158 of file bmp280.hpp.

158 {
159 int64_t var1 = static_cast<int64_t>(t_fine_) - 128000;
160 int64_t var2 = var1 * var1 * static_cast<int64_t>(dig_p6_);
161 var2 += (var1 * static_cast<int64_t>(dig_p5_)) << 17;
162 var2 += (static_cast<int64_t>(dig_p4_)) << 35;
163
164 var1 = ((var1 * var1 * static_cast<int64_t>(dig_p3_)) >> 8) +
165 ((var1 * static_cast<int64_t>(dig_p2_)) << 12);
166 var1 = (((static_cast<int64_t>(1) << 47) + var1) *
167 static_cast<int64_t>(dig_p1_)) >>
168 33;
169
170 if (var1 == 0) {
171 return 0; // Prevent division by zero
172 }
173
174 int64_t p = 1048576 - adc_P;
175 p = (((p << 31) - var2) * 3125) / var1;
176 var1 = (static_cast<int64_t>(dig_p9_) * (p >> 13) * (p >> 13)) >> 25;
177 var2 = (static_cast<int64_t>(dig_p8_) * p) >> 19;
178
179 return static_cast<uint32_t>((p + var1 + var2) >> 8) +
180 (static_cast<int64_t>(dig_p7_) << 4);
181 }

◆ CompensateTemperature()

int32_t Bmp280::CompensateTemperature ( int32_t  adc_T)
inlineprivate

Temperature compensation algorithm

Definition at line 142 of file bmp280.hpp.

142 {
143 int32_t var1 = (((adc_T >> 3) - (static_cast<int32_t>(dig_t1_) << 1)) *
144 (static_cast<int32_t>(dig_t2_)) >>
145 11);
146
147 int32_t var2 = (((((adc_T >> 4) - static_cast<int32_t>(dig_t1_)) *
148 ((adc_T >> 4) - static_cast<int32_t>(dig_t1_))) >>
149 12) *
150 (static_cast<int32_t>(dig_t3_)) >>
151 14);
152
153 t_fine_ = var1 + var2;
154 return (t_fine_ * 5 + 128) >> 8;
155 }

◆ Configure()

void Bmp280::Configure ( )
inlineprivate

Configure sensor operating mode

Definition at line 97 of file bmp280.hpp.

97 {
98 // Set oversampling: temp x1, pressure x1, normal mode
99 i2c_.WriteRegister(REG_CTRL_MEAS, 0b00100111);
100 // Set standby time 0.5ms, filter off
101 i2c_.WriteRegister(REG_CONFIG, 0b00000000);
102 }
void WriteRegister(uint8_t reg, uint8_t value)
Definition bsp_i2c.hpp:72

◆ Display()

void Bmp280::Display ( )
inline

Definition at line 55 of file bmp280.hpp.

55 {
56 std::cout << "Temperature: " << ReadTemperature() << " °C\n";
57 std::cout << "Pressure: " << ReadPressure() / 100.0f << " hPa\n";
58 }
float ReadPressure()
Definition bmp280.hpp:73
float ReadTemperature()
Definition bmp280.hpp:64

◆ ReadCalibration()

void Bmp280::ReadCalibration ( )
inlineprivate

Read and store calibration data from sensor

Definition at line 105 of file bmp280.hpp.

105 {
106 uint8_t buf[24];
107 i2c_.ReadRegisters(REG_CALIB, buf, 24);
108
109 // Temperature calibration
110 dig_t1_ = static_cast<uint16_t>((buf[1] << 8) | buf[0]);
111 dig_t2_ = static_cast<int16_t>((buf[3] << 8) | buf[2]);
112 dig_t3_ = static_cast<int16_t>((buf[5] << 8) | buf[4]);
113
114 // Pressure calibration
115 dig_p1_ = (buf[7] << 8) | buf[6];
116 dig_p2_ = static_cast<int16_t>((buf[9] << 8) | buf[8]);
117 dig_p3_ = static_cast<int16_t>((buf[11] << 8) | buf[10]);
118 dig_p4_ = static_cast<int16_t>((buf[13] << 8) | buf[12]);
119 dig_p5_ = static_cast<int16_t>((buf[15] << 8) | buf[14]);
120 dig_p6_ = static_cast<int16_t>((buf[17] << 8) | buf[16]);
121 dig_p7_ = static_cast<int16_t>((buf[19] << 8) | buf[18]);
122 dig_p8_ = static_cast<int16_t>((buf[21] << 8) | buf[20]);
123 dig_p9_ = static_cast<int16_t>((buf[23] << 8) | buf[22]);
124 }
void ReadRegisters(uint8_t reg, uint8_t *buffer, size_t length)
Definition bsp_i2c.hpp:86

◆ ReadPressure()

float Bmp280::ReadPressure ( )
inline

Read compensated pressure in Pascals

Returns
Pressure in Pa with 0.01Pa resolution

Definition at line 73 of file bmp280.hpp.

73 {
74 int32_t adc_p = ReadRawPressure();
75 ReadRawTemperature(); // Required to update t_fine
76 CompensateTemperature(adc_t_); // Ensure t_fine is current
77 return static_cast<float>(CompensatePressure(adc_p)) / 256.0f;
78 }
uint32_t CompensatePressure(int32_t adc_P)
Definition bmp280.hpp:158
int32_t ReadRawPressure()
Definition bmp280.hpp:135
int32_t CompensateTemperature(int32_t adc_T)
Definition bmp280.hpp:142
int32_t ReadRawTemperature()
Definition bmp280.hpp:127

◆ ReadRawPressure()

int32_t Bmp280::ReadRawPressure ( )
inlineprivate

Read raw pressure ADC value

Definition at line 135 of file bmp280.hpp.

135 {
136 uint8_t data[3];
137 i2c_.ReadRegisters(REG_PRESS_MSB, data, 3);
138 return (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
139 }

◆ ReadRawTemperature()

int32_t Bmp280::ReadRawTemperature ( )
inlineprivate

Read raw temperature ADC value

Definition at line 127 of file bmp280.hpp.

127 {
128 uint8_t data[3];
129 i2c_.ReadRegisters(REG_TEMP_MSB, data, 3);
130 adc_t_ = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
131 return adc_t_;
132 }

◆ ReadTemperature()

float Bmp280::ReadTemperature ( )
inline

Read compensated temperature in Celsius

Returns
Temperature in °C with 0.01°C resolution

Definition at line 64 of file bmp280.hpp.

64 {
65 int32_t adc_t = ReadRawTemperature();
66 return static_cast<float>(CompensateTemperature(adc_t)) / 100.0f;
67 }

◆ ThreadFun()

void Bmp280::ThreadFun ( )
inline

Definition at line 47 of file bmp280.hpp.

47 {
48 while (true) {
51 std::this_thread::sleep_for(std::chrono::milliseconds(100));
52 }
53 }

Field Documentation

◆ adc_t_

int32_t Bmp280::adc_t_ = 0
private

Definition at line 92 of file bmp280.hpp.

◆ DEFAULT_I2C_ADDR

constexpr uint8_t Bmp280::DEFAULT_I2C_ADDR = 0x77
staticconstexpr

Default I2C address (0x76 or 0x77)

Definition at line 18 of file bmp280.hpp.

◆ dig_p1_

uint16_t Bmp280::dig_p1_
private

Definition at line 88 of file bmp280.hpp.

◆ dig_p2_

int16_t Bmp280::dig_p2_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p3_

int16_t Bmp280::dig_p3_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p4_

int16_t Bmp280::dig_p4_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p5_

int16_t Bmp280::dig_p5_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p6_

int16_t Bmp280::dig_p6_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p7_

int16_t Bmp280::dig_p7_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p8_

int16_t Bmp280::dig_p8_
private

Definition at line 89 of file bmp280.hpp.

◆ dig_p9_

int16_t Bmp280::dig_p9_
private

Definition at line 90 of file bmp280.hpp.

◆ dig_t1_

uint16_t Bmp280::dig_t1_
private

Definition at line 86 of file bmp280.hpp.

◆ dig_t2_

int16_t Bmp280::dig_t2_
private

Definition at line 87 of file bmp280.hpp.

◆ dig_t3_

int16_t Bmp280::dig_t3_
private

Definition at line 87 of file bmp280.hpp.

◆ i2c_

I2cDevice& Bmp280::i2c_
private

Definition at line 82 of file bmp280.hpp.

◆ t_fine_

int32_t Bmp280::t_fine_ = 0
private

Definition at line 83 of file bmp280.hpp.

◆ thread_

std::thread Bmp280::thread_
private

Definition at line 94 of file bmp280.hpp.


The documentation for this class was generated from the following file: