1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Contributions: Jon Trulson <jtrulson@ics.com>
4  * Copyright (c) 2014 Intel Corporation.
5  *
6  * Credits to Adafruit.
7  * Based on Adafruit BMP085 library.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 #pragma once
29 
30 #include <string>
31 #include <mraa/i2c.hpp>
32 #include <math.h>
33 
34 #define ADDR               0x77 // device address
35 
36 // registers address
37 #define BMP085_ULTRALOWPOWER 0
38 #define BMP085_STANDARD      1
39 #define BMP085_HIGHRES       2
40 #define BMP085_ULTRAHIGHRES  3
41 #define BMP085_CAL_AC1           0xAA  // R   Calibration data (16 bits)
42 #define BMP085_CAL_AC2           0xAC  // R   Calibration data (16 bits)
43 #define BMP085_CAL_AC3           0xAE  // R   Calibration data (16 bits)
44 #define BMP085_CAL_AC4           0xB0  // R   Calibration data (16 bits)
45 #define BMP085_CAL_AC5           0xB2  // R   Calibration data (16 bits)
46 #define BMP085_CAL_AC6           0xB4  // R   Calibration data (16 bits)
47 #define BMP085_CAL_B1            0xB6  // R   Calibration data (16 bits)
48 #define BMP085_CAL_B2            0xB8  // R   Calibration data (16 bits)
49 #define BMP085_CAL_MB            0xBA  // R   Calibration data (16 bits)
50 #define BMP085_CAL_MC            0xBC  // R   Calibration data (16 bits)
51 #define BMP085_CAL_MD            0xBE  // R   Calibration data (16 bits)
52 
53 #define BMP085_CONTROL           0xF4
54 #define BMP085_TEMPDATA          0xF6
55 #define BMP085_PRESSUREDATA      0xF6
56 #define BMP085_READTEMPCMD       0x2E
57 #define BMP085_READPRESSURECMD   0x34
58 
59 #define HIGH               1
60 #define LOW                0
61 
62 namespace upm {
63 
64 /**
65  * @brief Bosch BMP & GY65 Atmospheric Pressure Sensor library
66  * @defgroup bmpx8x libupm-bmpx8x
67  * @ingroup seeed adafruit sparkfun i2c pressure
68  */
69 
70 /**
71  * @library bmpx8x
72  * @sensor bmpx8x
73  * @comname BMP Atmospheric Pressure Sensor
74  * @altname GY65 BMP085 BMP180 BMP183
75  * @type pressure
76  * @man seeed adafruit sparkfun
77  * @con i2c
78  *
79  * @brief API for the GY65/BMP085 and BMP180 Atmospheric Pressure Sensors
80  *
81  * Bosch GY65/BMP085 and BMP180 are high-precision, ultra-low
82  * power consumption pressure sensors. They operate in the range of
83  * 30,000-110,000 Pa.
84  *
85  * This module has been tested on the GY65/BMP085 and BMP180 sensors.
86  *
87  * @image html bmp085.jpeg
88  * @snippet bmpx8x.cxx Interesting
89  */
90 
91 class BMPX8X {
92     public:
93         /**
94          * Instantiates a BMPX8X object
95          *
96          * @param bus Number of the used bus
97          * @param devAddr Address of the used I2C device
98          * @param mode BMP085 mode
99          */
100         BMPX8X (int bus, int devAddr=0x77, uint8_t mode=BMP085_ULTRAHIGHRES);
101 
102         /**
103          * BMPX8X object destructor; basically, it closes the I2C connection.
104          * ~BMPX8X ();
105          * LE: there is no need for the destructor, as the I2C connection
106          * will be closed when the m_i2ControlCtx variable will go out of
107          * scope (when all the BMPX8X objects will be destroyed)
108          */
109         /**
110          * Returns the calculated pressure
111          */
112         int32_t getPressure ();
113 
114         /**
115          *
116          * Gets raw pressure data
117          */
118         int32_t getPressureRaw ();
119 
120         /**
121          * Gets raw temperature data from the sensor
122          */
123         int16_t getTemperatureRaw ();
124 
125         /**
126          * Returns the calculated temperature
127          */
128         float getTemperature ();
129 
130         /**
131          * With a given absolute altitude, sea level can be calculated
132          *
133          * @param altitudeMeters Altitude
134          */
135         int32_t getSealevelPressure(float altitudeMeters = 0);
136 
137         /**
138          * With a given sea level, altitude in meters can be calculated
139          *
140          * @param sealevelPressure Sea level
141          */
142         float getAltitude (float sealevelPressure = 101325);
143 
144         /**
145          * Calculates B5 (check the spec for more information)
146          *
147          * @param UT
148          */
149         int32_t computeB5 (int32_t UT);
150 
151         /**
152          * Reads a two-byte register
153          *
154          * @param reg Address of the register
155          */
156         uint16_t i2cReadReg_16 (int reg);
157 
158         /**
159          * Writes to a one-byte register
160          *
161          * @param reg Address of the register
162          * @param value Byte to be written
163          */
164         mraa::Result i2cWriteReg (uint8_t reg, uint8_t value);
165 
166         /**
167          * Reads a one-byte register
168          *
169          * @param reg Address of the register
170          */
171         uint8_t i2cReadReg_8 (int reg);
172 
173     private:
174         std::string m_name;
175 
176         int m_controlAddr;
177         int m_bus;
178         mraa::I2c m_i2ControlCtx;
179 
180         uint8_t oversampling;
181         int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
182         uint16_t ac4, ac5, ac6;
183 };
184 
185 }
186