1
+ /*
2
+ Author: Mike White ([email protected] ; @mikewhite808)
3
+
4
+ Implementation for reading data from BMP180 breakout board from Adafruit.
5
+ https://www.adafruit.com/product/1603
6
+
7
+ Code below heavily used the Adafruit Python implementation of BMP085 found here:
8
+ https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_BMP085/Adafruit_BMP085.py
9
+ */
10
+
11
+ #include < unistd.h>
12
+ #include < math.h>
1
13
#include " bmp180.h"
2
14
#include " i2c.h"
15
+ #ifdef DEBUG
3
16
#include < iostream>
4
- #include < unistd.h>
5
- #include < math.h>
17
+ #endif
6
18
7
19
using namespace std ;
8
20
21
+ /* ***********************************************************
22
+ * Constructor
23
+ *
24
+ * Parameters:
25
+ * mode - the mode to run in (default is BMP085_STANDARD)
26
+ ***********************************************************/
9
27
BMP180::BMP180 (int mode)
10
28
: _i2c(0 ), _cal_AC1(0 ), _cal_AC2(0 ), _cal_AC3(0 ), _cal_B1(0 ), _cal_B2(0 ), _cal_MB(0 ),
11
29
_cal_MC(0 ), _cal_MD(0 ), _mode(mode), _cal_AC4(0 ), _cal_AC5(0 ), _cal_AC6(0 )
@@ -14,12 +32,19 @@ BMP180::BMP180(int mode)
14
32
_init ();
15
33
}
16
34
35
+ /* ***********************************************************
36
+ * Destructor
37
+ ***********************************************************/
17
38
BMP180::~BMP180 ()
18
39
{
19
40
delete _i2c;
20
41
_i2c = 0 ;
21
42
}
22
43
44
+ /* ***********************************************************
45
+ * Reads the calibrated values that will be used for
46
+ * calculations
47
+ ***********************************************************/
23
48
void BMP180::_init ()
24
49
{
25
50
UINT8 data[2 ];
@@ -61,17 +86,41 @@ void BMP180::_init()
61
86
#endif
62
87
}
63
88
64
- int BMP180::_readU16 (UINT8 *data)
89
+ /* ***********************************************************
90
+ * Reads an unsigned 16-bit value
91
+ *
92
+ * Parameters:
93
+ * data - the data to read the values from
94
+ *
95
+ * Returns a unsigned 16-bit value
96
+ ***********************************************************/
97
+ UINT16 BMP180::_readU16 (UINT8 *data)
65
98
{
66
99
return ((data[0 ] << 8 ) | data[1 ]);
67
100
}
68
101
102
+ /* ***********************************************************
103
+ * Reads a signed 16-bit value
104
+ *
105
+ * Parameters:
106
+ * data - the data to read the values from
107
+ *
108
+ * Returns a signed 16-bit value
109
+ ***********************************************************/
69
110
int BMP180::_readS16 (UINT8 *data)
70
111
{
71
112
int hi = _readS8 (&data[0 ]);
72
113
return ((hi << 8 ) | data[1 ]);
73
114
}
74
115
116
+ /* ***********************************************************
117
+ * Reads a signed 8-bit value
118
+ *
119
+ * Parameters:
120
+ * data - the data to read the values from
121
+ *
122
+ * Returns a signed 8-bit value
123
+ ***********************************************************/
75
124
int BMP180::_readS8 (UINT8 *data)
76
125
{
77
126
if (data[0 ] > 127 )
@@ -80,6 +129,11 @@ int BMP180::_readS8(UINT8 *data)
80
129
return (int )data[0 ];
81
130
}
82
131
132
+ /* ***********************************************************
133
+ * Reads the temperature in degrees celsius
134
+ *
135
+ * Returns the temperature in pascal
136
+ ***********************************************************/
83
137
double BMP180::readTemperature ()
84
138
{
85
139
UINT16 rawTemp = _readRawTemperature ();
@@ -93,6 +147,11 @@ double BMP180::readTemperature()
93
147
return temp;
94
148
}
95
149
150
+ /* ***********************************************************
151
+ * Reads the pressure in pascal
152
+ *
153
+ * Returns the pressure in pascal
154
+ ***********************************************************/
96
155
UINT32 BMP180::readPressure ()
97
156
{
98
157
UINT16 UT = _readRawTemperature ();
@@ -145,6 +204,14 @@ UINT32 BMP180::readPressure()
145
204
return p;
146
205
}
147
206
207
+ /* ***********************************************************
208
+ * Reads the altitude in meters
209
+ *
210
+ * Parameters:
211
+ * seaLevelPressure - the pressure at sea level default
212
+ * value is 101325
213
+ * Returns the altitude in meters
214
+ ***********************************************************/
148
215
double BMP180::readAltitude (UINT32 seaLevelPressure)
149
216
{
150
217
double altitude = 0 ;
@@ -161,6 +228,11 @@ double BMP180::readSeaLevelPressure()
161
228
return 0 ;
162
229
}
163
230
231
+ /* ***********************************************************
232
+ * Reads the raw temperature
233
+ *
234
+ * Returns the raw temperature reading
235
+ ***********************************************************/
164
236
UINT16 BMP180::_readRawTemperature ()
165
237
{
166
238
UINT8 data[2 ];
@@ -175,6 +247,11 @@ UINT16 BMP180::_readRawTemperature()
175
247
return retval;
176
248
}
177
249
250
+ /* ***********************************************************
251
+ * Reads the raw pressure
252
+ *
253
+ * Returns the raw pressure reading
254
+ ***********************************************************/
178
255
UINT32 BMP180::_readRawPressure ()
179
256
{
180
257
_i2c->writeCommand (1 , BMP085_READPRESSURECMD);
0 commit comments