RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
BMP180.h
Go to the documentation of this file.
1#pragma once
2#include "../Common.h"
3#include <stdint.h>
4#include <string.h>
5#include <stdbool.h>
6#include <stdlib.h>
7#include <fcntl.h>
8#include <unistd.h>
9#include <errno.h>
10
11extern "C"
12{
13 #include <linux/i2c-dev.h>
14 #include <i2c/smbus.h>
15}
16
17#include <sys/ioctl.h>
18#include <time.h>
19#include <math.h>
20#include "BaseComponent.h"
21
22namespace RumPi
23{
24 //Pressure oversampling modes
25 constexpr int BMP180_PRE_OSS0 = 0; // ultra low power
26 constexpr int BMP180_PRE_OSS1 = 1; // standard
27 constexpr int BMP180_PRE_OSS2 = 2; // high resolution
28 constexpr int BMP180_PRE_OSS3 = 3; // ultra high resolution
29
30 //Pressure read commands
31 constexpr int BMP180_PRE_OSS0_CMD = 0x34;
32 constexpr int BMP180_PRE_OSS1_CMD = 0x74;
33 constexpr int BMP180_PRE_OSS2_CMD = 0xB4;
34 constexpr int BMP180_PRE_OSS3_CMD = 0xF4;
35
36 //Waiting times in us for reading pressure values
37 constexpr int BMP180_PRE_OSS0_WAIT_US = 5000;
38 constexpr int BMP180_PRE_OSS1_WAIT_US = 8000;
39 constexpr int BMP180_PRE_OSS2_WAIT_US = 14000;
40 constexpr int BMP180_PRE_OSS3_WAIT_US = 26000;
41
42 //AC register
43 constexpr int BMP180_REG_AC1_H = 0xAA;
44 constexpr int BMP180_REG_AC2_H = 0xAC;
45 constexpr int BMP180_REG_AC3_H = 0xAE;
46 constexpr int BMP180_REG_AC4_H = 0xB0;
47 constexpr int BMP180_REG_AC5_H = 0xB2;
48 constexpr int BMP180_REG_AC6_H = 0xB4;
49
50 //B1 register
51 constexpr int BMP180_REG_B1_H = 0xB6;
52
53 //B2 register
54 constexpr int BMP180_REG_B2_H = 0xB8;
55
56 //MB register
57 constexpr int BMP180_REG_MB_H = 0xBA;
58
59 //MC register
60 constexpr int BMP180_REG_MC_H = 0xBC;
61
62 //MD register
63 constexpr int BMP180_REG_MD_H = 0xBE;
64
65 //AC register
66 constexpr int BMP180_CTRL = 0xF4;
67
68 //Temperature register
69 constexpr int BMP180_REG_TMP = 0xF6;
70
71 //Pressure register
72 constexpr int BMP180_REG_PRE = 0xF6;
73
74 //Temperature read command
75 constexpr int BMP180_TMP_READ_CMD = 0x2E;
76
77 //Waiting time in us for reading temperature values
78 constexpr int BMP180_TMP_READ_WAIT_US = 5000;
79
80 //Average sea-level pressure in hPa
81 constexpr double BMP180_SEA_LEVEL = 1013.25;
82
83 //Lookup table for BMP180 register addresses
84 static const int32_t BMP180RegisterTable[11][2] =
85 {
86 { BMP180_REG_AC1_H, 1 },
87 { BMP180_REG_AC2_H, 1 },
88 { BMP180_REG_AC3_H, 1 },
89 { BMP180_REG_AC4_H, 0 },
90 { BMP180_REG_AC5_H, 0 },
91 { BMP180_REG_AC6_H, 0 },
92 { BMP180_REG_B1_H, 1 },
93 { BMP180_REG_B2_H, 1 },
94 { BMP180_REG_MB_H, 1 },
95 { BMP180_REG_MC_H, 1 },
96 { BMP180_REG_MD_H, 1 }
97 };
98
105 class BMP180 : public BaseComponent
106 {
107 private:
110
113
116
119
121 int32_t myAC1;
122 int32_t myAC2;
123 int32_t myAC3;
124 int32_t myAC4;
125 int32_t myAC5;
126 int32_t myAC6;
127 int32_t myB1;
128 int32_t myB2;
129 int32_t myMB;
130 int32_t myMC;
131 int32_t myMD;
132
137
138 //Private functions used by calculations.
154 void ReadSingleEpromFromRegister(int32_t* store, uint8_t reg, int32_t sign);
155
166 void ReadEprom();
167
180 int32_t ReadRawTemperature();
181
196 int32_t ReadRawPressure(uint8_t oversamplingMode);
197
198 public:
212 BMP180(int bmpI2CAddress, const char* i2cDeviceFilePath);
213
225 virtual ~BMP180();
226
238 virtual void TurnOn() override;
239
250 virtual void TurnOff() override;
251
264 virtual std::string ToString() const override;
265
276 virtual void ProcessRawValues() override;
277
290 float CalculateTemperature();
291
304 long CalculatePressure();
305
318 float CalculateAltitude();
319
332 int GetFileDescriptor() const;
333
346 int GetI2CAddress() const;
347
360 int GetOversamplingMode() const;
361
374 char* GetI2CDeviceFilePath() const;
375
388 int32_t GetAC1() const;
389
402 int32_t GetAC2() const;
403
416 int32_t GetAC3() const;
417
430 int32_t GetAC4() const;
431
444 int32_t GetAC5() const;
445
458 int32_t GetAC6() const;
459
472 int32_t GetB1() const;
473
486 int32_t GetB2() const;
487
500 int32_t GetMB() const;
501
514 int32_t GetMC() const;
515
528 int32_t GetMD() const;
529 };
530} // namespace RumPi
BMP180 barometric temperature and pressure sensor (I2C), also deriving altitude from the measured pre...
Definition: BMP180.h:106
void ReadEprom()
Reads all eleven calibration coefficients from the BMP180 EPROM into their member variables.
Definition: BMP180.cpp:265
virtual ~BMP180()
Destroys the sensor, turning it off and freeing the copied device file path.
Definition: BMP180.cpp:71
int32_t myB2
Definition: BMP180.h:128
int32_t GetMD() const
Gets the MD calibration coefficient.
Definition: BMP180.cpp:750
int GetOversamplingMode() const
Gets the sensor's oversampling mode.
Definition: BMP180.cpp:546
int32_t myAC5
Definition: BMP180.h:125
virtual std::string ToString() const override
Serializes the most recently calculated temperature, pressure, and altitude to a string.
Definition: BMP180.cpp:213
float CalculateAltitude()
Approximates the altitude (in meters) from the measured pressure relative to average sea-level pressu...
Definition: BMP180.cpp:486
void ReadSingleEpromFromRegister(int32_t *store, uint8_t reg, int32_t sign)
Reads a single calibration coefficient from the BMP180 EPROM, byte-swapping it from the chip's orderi...
Definition: BMP180.cpp:238
int32_t GetMB() const
Gets the MB calibration coefficient.
Definition: BMP180.cpp:716
int32_t myAC1
EPROM calibration coefficients.
Definition: BMP180.h:121
char * myI2CDeviceFilePath
i2c device file path.
Definition: BMP180.h:118
virtual void TurnOff() override
Closes the i2c device and frees the copied device file path, reporting any close failure through the ...
Definition: BMP180.cpp:156
int myOversamplingMode
BMP180 oversampling mode.
Definition: BMP180.h:115
int32_t GetB2() const
Gets the B2 calibration coefficient.
Definition: BMP180.cpp:699
char * GetI2CDeviceFilePath() const
Gets the i2c device file path.
Definition: BMP180.cpp:563
int32_t myAC3
Definition: BMP180.h:123
int32_t GetMC() const
Gets the MC calibration coefficient.
Definition: BMP180.cpp:733
int32_t GetAC3() const
Gets the AC3 calibration coefficient.
Definition: BMP180.cpp:614
int32_t myAC4
Definition: BMP180.h:124
int32_t myMD
Definition: BMP180.h:131
int GetI2CAddress() const
Gets the sensor's I2C address.
Definition: BMP180.cpp:529
int32_t GetAC4() const
Gets the AC4 calibration coefficient.
Definition: BMP180.cpp:631
int32_t myMC
Definition: BMP180.h:130
int32_t GetAC2() const
Gets the AC2 calibration coefficient.
Definition: BMP180.cpp:597
int32_t ReadRawTemperature()
Reads the raw (uncompensated) temperature value from the sensor, byte-swapped to host ordering.
Definition: BMP180.cpp:309
virtual void TurnOn() override
Opens the i2c device, binds the slave address, and reads the sensor's EPROM calibration coefficients,...
Definition: BMP180.cpp:93
int32_t GetAC1() const
Gets the AC1 calibration coefficient.
Definition: BMP180.cpp:580
int GetFileDescriptor() const
Gets the open i2c device file descriptor.
Definition: BMP180.cpp:512
int myI2CAddress
I2C device address.
Definition: BMP180.h:112
BMP180(int bmpI2CAddress, const char *i2cDeviceFilePath)
Builds a BMP180 bound to the given I2C address, copying the provided i2c device file path.
Definition: BMP180.cpp:19
float CalculateTemperature()
Calculates the compensated temperature (in Celsius) from a raw read using the sensor's calibration co...
Definition: BMP180.cpp:391
int32_t GetAC6() const
Gets the AC6 calibration coefficient.
Definition: BMP180.cpp:665
long CalculatePressure()
Calculates the compensated pressure (in pascal) from raw reads using the sensor's calibration coeffic...
Definition: BMP180.cpp:421
float myRecentlyCalculatedAltitudeInM
Definition: BMP180.h:136
virtual void ProcessRawValues() override
Recomputes and caches the temperature, pressure, and altitude from fresh sensor reads.
Definition: BMP180.cpp:186
int32_t myAC2
Definition: BMP180.h:122
int32_t ReadRawPressure(uint8_t oversamplingMode)
Reads the raw (uncompensated) pressure value from the sensor at the given oversampling mode.
Definition: BMP180.cpp:335
int32_t myAC6
Definition: BMP180.h:126
int32_t GetAC5() const
Gets the AC5 calibration coefficient.
Definition: BMP180.cpp:648
float myRecentlyCalculatedTemperatureInC
Most recently calculated values.
Definition: BMP180.h:134
int32_t myB1
Definition: BMP180.h:127
long myRecentlyCalculatedPressureInHPA
Definition: BMP180.h:135
int myFileDescriptor
File descriptor.
Definition: BMP180.h:109
int32_t myMB
Definition: BMP180.h:129
int32_t GetB1() const
Gets the B1 calibration coefficient.
Definition: BMP180.cpp:682
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
RumPi is the library's top-level facade.
Definition: AlertManager.h:6
constexpr int BMP180_PRE_OSS3_CMD
Definition: BMP180.h:34
constexpr int BMP180_PRE_OSS1_WAIT_US
Definition: BMP180.h:38
constexpr int BMP180_REG_MC_H
Definition: BMP180.h:60
constexpr int BMP180_REG_AC6_H
Definition: BMP180.h:48
constexpr int BMP180_REG_MD_H
Definition: BMP180.h:63
constexpr int BMP180_PRE_OSS1
Definition: BMP180.h:26
constexpr int BMP180_REG_AC2_H
Definition: BMP180.h:44
constexpr int BMP180_REG_AC5_H
Definition: BMP180.h:47
constexpr double BMP180_SEA_LEVEL
Definition: BMP180.h:81
constexpr int BMP180_REG_MB_H
Definition: BMP180.h:57
constexpr int BMP180_PRE_OSS0_WAIT_US
Definition: BMP180.h:37
constexpr int BMP180_REG_B1_H
Definition: BMP180.h:51
constexpr int BMP180_REG_AC3_H
Definition: BMP180.h:45
constexpr int BMP180_PRE_OSS3
Definition: BMP180.h:28
constexpr int BMP180_TMP_READ_WAIT_US
Definition: BMP180.h:78
constexpr int BMP180_PRE_OSS2_WAIT_US
Definition: BMP180.h:39
constexpr int BMP180_REG_TMP
Definition: BMP180.h:69
constexpr int BMP180_REG_B2_H
Definition: BMP180.h:54
constexpr int BMP180_CTRL
Definition: BMP180.h:66
constexpr int BMP180_PRE_OSS1_CMD
Definition: BMP180.h:32
constexpr int BMP180_PRE_OSS0_CMD
Definition: BMP180.h:31
static const int32_t BMP180RegisterTable[11][2]
Definition: BMP180.h:84
constexpr int BMP180_REG_PRE
Definition: BMP180.h:72
constexpr int BMP180_REG_AC4_H
Definition: BMP180.h:46
constexpr int BMP180_PRE_OSS3_WAIT_US
Definition: BMP180.h:40
constexpr int BMP180_REG_AC1_H
Definition: BMP180.h:43
constexpr int BMP180_PRE_OSS2
Definition: BMP180.h:27
constexpr int BMP180_PRE_OSS0
Definition: BMP180.h:25
constexpr int BMP180_PRE_OSS2_CMD
Definition: BMP180.h:33
constexpr int BMP180_TMP_READ_CMD
Definition: BMP180.h:75