Skip to content

Commit 8694c4e

Browse files
committed
cleanup baro device classes, extract .cpp, format code
1 parent 91440aa commit 8694c4e

20 files changed

Lines changed: 796 additions & 753 deletions
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "BaroBMP085.hpp"
2+
3+
#define BMP085_DEFAULT_ADDRESS 0x77
4+
#define BMP085_WHOAMI_ID 0x55
5+
6+
#define BMP085_CALIB_REG 0xAA
7+
#define BMP085_WHOAMI_REG 0xD0
8+
#define BMP085_RESET_REG 0xE0
9+
#define BMP085_CONTROL_REG 0xF4
10+
#define BMP085_MEASUREMENT_REG 0xF6
11+
12+
#define BMP085_MEASURE_T 0x2E // temperature measurent
13+
#define BMP085_MEASURE_P0 0x34 // pressure measurement no oversampling
14+
#define BMP085_MEASURE_P1 0x74 // pressure measurement oversampling x2
15+
#define BMP085_MEASURE_P2 0xB4 // pressure measurement oversampling x4
16+
#define BMP085_MEASURE_P3 0xF4 // pressure measurement oversampling x8
17+
18+
namespace Espfc::Device::Baro {
19+
20+
int BaroBMP085::begin(BusDevice* bus)
21+
{
22+
return begin(bus, BMP085_DEFAULT_ADDRESS);
23+
}
24+
25+
int BaroBMP085::begin(BusDevice* bus, uint8_t addr)
26+
{
27+
setBus(bus, addr);
28+
29+
if (!testConnection()) return 0;
30+
31+
_bus->writeByte(_addr, BMP085_RESET_REG, 0xB6); // device reset
32+
delay(10);
33+
34+
_bus->read(_addr, BMP085_CALIB_REG, sizeof(CalibrationData), (uint8_t*)&_cal);
35+
36+
return 1;
37+
}
38+
39+
BaroDeviceType BaroBMP085::getType() const
40+
{
41+
return BARO_BMP085;
42+
}
43+
44+
float BaroBMP085::readTemperature()
45+
{
46+
uint8_t buffer[2];
47+
_bus->read(_addr, BMP085_MEASUREMENT_REG, 2, buffer);
48+
49+
// calbrate temp
50+
int32_t ut = ((uint16_t)buffer[0] << 8) + buffer[1];
51+
if (ut == 0) return NAN;
52+
53+
int32_t x1 = ((ut - (int32_t)_cal.ac6) * (int32_t)_cal.ac5) >> 15;
54+
int32_t x2 = ((int32_t)_cal.mc << 11) / (x1 + _cal.md);
55+
_t_fine = x1 + x2;
56+
//_t_fine = (_t_fine + x1 + x2 + 1) >> 1; // avg of last two samples
57+
return (float)((_t_fine + 8) >> 4) * 0.1f;
58+
}
59+
60+
float BaroBMP085::readPressure()
61+
{
62+
uint8_t buffer[3];
63+
_bus->read(_addr, BMP085_MEASUREMENT_REG, 3, buffer);
64+
65+
uint32_t up = ((uint32_t)buffer[0] << 16) | ((uint32_t)buffer[1] << 8) | buffer[2];
66+
67+
uint8_t oss = (_mode & 0xC0) >> 6;
68+
if (_mode & 0x34) up = up >> (8 - oss);
69+
if (up == 0) return NAN;
70+
71+
int32_t b6 = _t_fine - 4000;
72+
int32_t x1 = ((int32_t)_cal.b2 * ((b6 * b6) >> 12)) >> 11;
73+
int32_t x2 = ((int32_t)_cal.ac2 * b6) >> 11;
74+
int32_t x3 = x1 + x2;
75+
int32_t b3 = ((((int32_t)_cal.ac1 * 4 + x3) << oss) + 2) >> 2;
76+
x1 = ((int32_t)_cal.ac3 * b6) >> 13;
77+
x2 = ((int32_t)_cal.b1 * ((b6 * b6) >> 12)) >> 16;
78+
x3 = ((x1 + x2) + 2) >> 2;
79+
uint32_t b4 = ((uint32_t)_cal.ac4 * (uint32_t)(x3 + 32768)) >> 15;
80+
uint32_t b7 = ((uint32_t)up - b3) * (uint32_t)(50000UL >> oss);
81+
82+
int32_t p = 0;
83+
if (b7 < 0x80000000)
84+
{
85+
p = (b7 << 1) / b4;
86+
}
87+
else
88+
{
89+
p = (b7 / b4) << 1;
90+
}
91+
x1 = (p >> 8) * (p >> 8);
92+
x1 = (x1 * 3038) >> 16;
93+
x2 = (-7357 * p) >> 16;
94+
95+
p = p + ((x1 + x2 + (int32_t)3791) >> 4);
96+
97+
return p;
98+
}
99+
100+
void BaroBMP085::setMode(BaroDeviceMode mode)
101+
{
102+
_mode = mode == BARO_MODE_TEMP ? BMP085_MEASURE_T : BMP085_MEASURE_P3;
103+
_bus->writeByte(_addr, BMP085_CONTROL_REG, _mode);
104+
}
105+
106+
int BaroBMP085::getDelay(BaroDeviceMode mode) const
107+
{
108+
switch (mode)
109+
{
110+
case BARO_MODE_TEMP: return 4550; // temp
111+
default:
112+
// return 4550; // press_0
113+
// return 7550; // press_1
114+
// return 13550; // press_2
115+
return 25550; // press_3
116+
}
117+
}
118+
119+
bool BaroBMP085::testConnection()
120+
{
121+
uint8_t whoami = 0;
122+
_bus->readByte(_addr, BMP085_WHOAMI_REG, &whoami);
123+
return whoami == BMP085_WHOAMI_ID;
124+
}
125+
126+
} // namespace Espfc::Device::Baro
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include "Debug_Espfc.h"
4+
#include "Device/BaroDevice.hpp"
5+
6+
namespace Espfc::Device::Baro {
7+
8+
class BaroBMP085 : public BaroDevice
9+
{
10+
public:
11+
struct CalibrationData
12+
{
13+
int16_t ac1;
14+
int16_t ac2;
15+
int16_t ac3;
16+
uint16_t ac4;
17+
uint16_t ac5;
18+
uint16_t ac6;
19+
int16_t b1;
20+
int16_t b2;
21+
int16_t mb;
22+
int16_t mc;
23+
int16_t md;
24+
} __attribute__((__packed__));
25+
26+
int begin(BusDevice* bus) final;
27+
int begin(BusDevice* bus, uint8_t addr) final;
28+
BaroDeviceType getType() const final;
29+
30+
float readTemperature() final;
31+
float readPressure() final;
32+
33+
void setMode(BaroDeviceMode mode);
34+
int getDelay(BaroDeviceMode mode) const final;
35+
36+
bool testConnection() final;
37+
38+
protected:
39+
int8_t _mode;
40+
int32_t _t_fine;
41+
CalibrationData _cal;
42+
};
43+
44+
} // namespace Espfc::Device::Baro
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "BaroBMP280.hpp"
2+
3+
#define BMP280_ADDRESS_FIRST 0x76
4+
#define BMP280_ADDRESS_SECOND 0x77
5+
#define BMP280_WHOAMI_ID 0x58
6+
7+
#define BMP280_WHOAMI_REG 0xD0
8+
#define BMP280_VERSION_REG 0xD1
9+
#define BMP280_RESET_REG 0xE0
10+
#define BMP280_RESET_VAL 0xB6
11+
12+
#define BMP280_CALIB_REG 0x88
13+
#define BMP280_CAL26_REG 0xE1 // R calibration stored in 0xE1-0xF0
14+
15+
#define BMP280_STATUS_REG 0xF3
16+
#define BMP280_CONTROL_REG 0xF4
17+
#define BMP280_CONFIG_REG 0xF5
18+
#define BMP280_PRESSURE_REG 0xF7
19+
#define BMP280_TEMPERATURE_REG 0xFA
20+
21+
#define BMP280_MODE_NORMAL 0x03
22+
23+
#define BMP280_FILTER_OFF 0x00
24+
#define BMP280_FILTER_X2 0x01
25+
#define BMP280_FILTER_X4 0x02
26+
#define BMP280_FILTER_X8 0x03
27+
#define BMP280_FILTER_X16 0x04
28+
29+
#define BMP280_SAMPLING_X1 1
30+
#define BMP280_SAMPLING_X2 2
31+
#define BMP280_SAMPLING_X4 3
32+
#define BMP280_SAMPLING_X8 4
33+
34+
namespace Espfc::Device::Baro {
35+
36+
int BaroBMP280::begin(BusDevice* bus)
37+
{
38+
return begin(bus, BMP280_ADDRESS_FIRST) ? 1 : begin(bus, BMP280_ADDRESS_SECOND) ? 1 : 0;
39+
}
40+
41+
int BaroBMP280::begin(BusDevice* bus, uint8_t addr)
42+
{
43+
setBus(bus, addr);
44+
45+
if (!testConnection()) return 0;
46+
47+
_bus->read(_addr, BMP280_CALIB_REG, sizeof(CalibrationData), (uint8_t*)&_cal); // read callibration
48+
49+
writeReg(BMP280_RESET_REG, BMP280_RESET_VAL); // device reset
50+
delay(2);
51+
52+
writeReg(BMP280_CONFIG_REG, BMP280_FILTER_X4 << 2); // set minimal standby and IIR filter X2
53+
// writeReg(BMP280_CONFIG_REG, 0); // set minimal standby and IIR filter off
54+
55+
writeReg(BMP280_CONTROL_REG,
56+
BMP280_SAMPLING_X2 << 5 | BMP280_SAMPLING_X8 << 2 | BMP280_MODE_NORMAL); // set sampling mode
57+
58+
delay(20);
59+
60+
return 1;
61+
}
62+
63+
BaroDeviceType BaroBMP280::getType() const
64+
{
65+
return BARO_BMP280;
66+
}
67+
68+
float BaroBMP280::readTemperature()
69+
{
70+
float T = (_t_fine * 5 + 128) >> 8;
71+
return T * 0.01f;
72+
}
73+
74+
float BaroBMP280::readPressure()
75+
{
76+
readMesurment();
77+
78+
int32_t adc_T = _raw_temp;
79+
adc_T >>= 4;
80+
81+
int32_t vart1 = ((((adc_T >> 3) - ((int32_t)_cal.dig_T1 << 1))) * ((int32_t)_cal.dig_T2)) >> 11;
82+
int32_t vart2 = (((((adc_T >> 4) - ((int32_t)_cal.dig_T1)) * ((adc_T >> 4) - ((int32_t)_cal.dig_T1))) >> 12) *
83+
((int32_t)_cal.dig_T3)) >>
84+
14;
85+
_t_fine = vart1 + vart2;
86+
//_t_fine += ((var1 + var2) - _t_fine + 4) >> 3; // smooth t_fine
87+
88+
int32_t adc_P = _raw_pressure;
89+
adc_P >>= 4;
90+
91+
int64_t var1 = ((int64_t)_t_fine) - 128000;
92+
int64_t var2 = var1 * var1 * (int64_t)_cal.dig_P6;
93+
var2 += ((var1 * (int64_t)_cal.dig_P5) << 17);
94+
var2 += (((int64_t)_cal.dig_P4) << 35);
95+
var1 = ((var1 * var1 * (int64_t)_cal.dig_P3) >> 8) + ((var1 * (int64_t)_cal.dig_P2) << 12);
96+
var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)_cal.dig_P1) >> 33;
97+
98+
if (var1 == 0)
99+
{
100+
return 0; // avoid exception caused by division by zero
101+
}
102+
int64_t p = 1048576 - adc_P;
103+
p = (((p << 31) - var2) * 3125) / var1;
104+
var1 = (((int64_t)_cal.dig_P9) * (p >> 13) * (p >> 13)) >> 25;
105+
var2 = (((int64_t)_cal.dig_P8) * p) >> 19;
106+
107+
p = ((p + var1 + var2) >> 8) + (((int64_t)_cal.dig_P7) << 4);
108+
109+
return ((float)p) / 256.f; // Pa
110+
}
111+
112+
void BaroBMP280::setMode(BaroDeviceMode mode)
113+
{
114+
(void)mode;
115+
}
116+
117+
int BaroBMP280::getDelay(BaroDeviceMode mode) const
118+
{
119+
switch (mode)
120+
{
121+
case BARO_MODE_TEMP: return 0;
122+
default:
123+
// return 5500; // if sapling X1
124+
// return 7500; // if sampling X2
125+
// return 11500; // if sampling X4
126+
return 19500; // if sampling X8
127+
// return 37500; // if sampling X16
128+
}
129+
}
130+
131+
bool BaroBMP280::testConnection()
132+
{
133+
uint8_t whoami = 0;
134+
_bus->read(_addr, BMP280_WHOAMI_REG, 1, &whoami);
135+
return whoami == BMP280_WHOAMI_ID;
136+
}
137+
138+
void BaroBMP280::readMesurment()
139+
{
140+
uint8_t buffer[6] = {0, 0, 0, 0, 0, 0};
141+
_bus->readFast(_addr, BMP280_PRESSURE_REG, 6, buffer);
142+
_raw_pressure = buffer[2] | (buffer[1] << 8) | (buffer[0] << 16);
143+
_raw_temp = buffer[5] | (buffer[4] << 8) | (buffer[3] << 16);
144+
}
145+
146+
int8_t BaroBMP280::writeReg(uint8_t reg, uint8_t val)
147+
{
148+
return _bus->write(_addr, reg, 1, &val);
149+
}
150+
151+
} // namespace Espfc::Device::Baro
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#pragma once
2+
3+
#include "Debug_Espfc.h"
4+
#include "Device/BaroDevice.hpp"
5+
6+
namespace Espfc::Device::Baro {
7+
8+
class BaroBMP280 : public BaroDevice
9+
{
10+
public:
11+
struct CalibrationData
12+
{
13+
uint16_t dig_T1;
14+
int16_t dig_T2;
15+
int16_t dig_T3;
16+
uint16_t dig_P1;
17+
int16_t dig_P2;
18+
int16_t dig_P3;
19+
int16_t dig_P4;
20+
int16_t dig_P5;
21+
int16_t dig_P6;
22+
int16_t dig_P7;
23+
int16_t dig_P8;
24+
int16_t dig_P9;
25+
/*uint8_t dig_H1;
26+
int16_t dig_H2;
27+
uint8_t dig_H3;
28+
int16_t dig_H4;
29+
int16_t dig_H5;
30+
int8_t dig_H6;*/
31+
} __attribute__((__packed__));
32+
33+
int begin(BusDevice* bus) final;
34+
int begin(BusDevice* bus, uint8_t addr) final;
35+
36+
BaroDeviceType getType() const final;
37+
38+
float readTemperature() final;
39+
float readPressure() final;
40+
41+
void setMode(BaroDeviceMode mode);
42+
int getDelay(BaroDeviceMode mode) const final;
43+
44+
bool testConnection() final;
45+
46+
protected:
47+
void readMesurment();
48+
49+
int8_t writeReg(uint8_t reg, uint8_t val);
50+
51+
int8_t _mode;
52+
int32_t _t_fine;
53+
int32_t _raw_temp;
54+
int32_t _raw_pressure;
55+
CalibrationData _cal;
56+
};
57+
58+
} // namespace Espfc::Device::Baro

0 commit comments

Comments
 (0)