make: allows to build in source tree by filtering subdirs
[contrib/upm.git] / src / gy65 / gy65.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Credits to Adafruit.
6  * Based on Adafruit BMP085 library.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28
29 #include <string>
30 #include <mraa/i2c.h>
31 #include <math.h>
32
33 #define ADDR               0x77 // device address
34
35 // registers address
36 #define BMP085_ULTRALOWPOWER 0
37 #define BMP085_STANDARD      1
38 #define BMP085_HIGHRES       2
39 #define BMP085_ULTRAHIGHRES  3
40 #define BMP085_CAL_AC1           0xAA  // R   Calibration data (16 bits)
41 #define BMP085_CAL_AC2           0xAC  // R   Calibration data (16 bits)
42 #define BMP085_CAL_AC3           0xAE  // R   Calibration data (16 bits)
43 #define BMP085_CAL_AC4           0xB0  // R   Calibration data (16 bits)
44 #define BMP085_CAL_AC5           0xB2  // R   Calibration data (16 bits)
45 #define BMP085_CAL_AC6           0xB4  // R   Calibration data (16 bits)
46 #define BMP085_CAL_B1            0xB6  // R   Calibration data (16 bits)
47 #define BMP085_CAL_B2            0xB8  // R   Calibration data (16 bits)
48 #define BMP085_CAL_MB            0xBA  // R   Calibration data (16 bits)
49 #define BMP085_CAL_MC            0xBC  // R   Calibration data (16 bits)
50 #define BMP085_CAL_MD            0xBE  // R   Calibration data (16 bits)
51
52 #define BMP085_CONTROL           0xF4
53 #define BMP085_TEMPDATA          0xF6
54 #define BMP085_PRESSUREDATA      0xF6
55 #define BMP085_READTEMPCMD       0x2E
56 #define BMP085_READPRESSURECMD   0x34
57
58 #define HIGH               1
59 #define LOW                0
60
61 namespace upm {
62
63 /**
64  * @brief C++ API for GY65 chip (Atmospheric Pressure Sensor)
65  *
66  * This file defines the gy65 C++ interface for libgy65
67  *
68  * @snippet gy65.cxx Interesting
69  */
70 class GY65 {
71     public:
72         /**
73          * Instanciates a GY65 object
74          *
75          * @param bus number of used bus
76          * @param devAddr address of used i2c device
77          * @param mode BMP085 mode
78          */
79         GY65 (int bus, int devAddr, uint8_t mode = BMP085_ULTRAHIGHRES);
80
81         /**
82          * GY65 object destructor, basicaly it close i2c connection.
83          */
84         ~GY65 ();
85
86         /**
87          * Return calculated pressure
88          */
89         int32_t getPressure ();
90
91         /**
92          *
93          * Get raw pressure data
94          */
95         int32_t getPressureRaw ();
96
97         /**
98          * Get raw temperature data from chip
99          */
100         int16_t getTemperatureRaw ();
101
102         /**
103          * Return calculated temperature
104          */
105         float getTemperature ();
106
107         /**
108          * With given absolute altitude sea level can be calculated
109          *
110          * @param altitudeMeters altitude
111          */
112         int32_t getSealevelPressure(float altitudeMeters = 0);
113
114         /**
115          * With given sea level altitude in meters can be calculated
116          *
117          * @param sealevelPressure Sea level
118          */
119         float getAltitude (float sealevelPressure = 101325);
120
121         /**
122          * Calculation of B5 (check spec for more information)
123          *
124          * @param UT
125          */
126         int32_t computeB5 (int32_t UT);
127
128         /**
129          * Read two bytes register
130          *
131          * @param reg address of a register
132          */
133         uint16_t i2cReadReg_16 (int reg);
134
135         /**
136          * Write to one byte register
137          *
138          * @param reg address of a register
139          * @param value byte to be written
140          */
141         mraa_result_t i2cWriteReg (uint8_t reg, uint8_t value);
142
143         /**
144          * Read one byte register
145          *
146          * @param reg address of a register
147          */
148         uint8_t i2cReadReg_8 (int reg);
149
150     private:
151         std::string m_name;
152
153         int m_controlAddr;
154         int m_bus;
155         mraa_i2c_context m_i2ControlCtx;
156
157         uint8_t oversampling;
158         int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
159         uint16_t ac4, ac5, ac6;
160 };
161
162 }