-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmp_i2c.cpp
320 lines (281 loc) · 8.96 KB
/
bmp_i2c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "bmp_i2c.h"
TwoWire *_BMP3_i2c;
static int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
static int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
static void delay_msec(uint32_t ms);
///constucteur
BMP3_I2C::BMP3_I2C(uint8_t addr, TwoWire *theWire) : i2c_addr(addr)
{
printf("helllo la terre de bmp_i2c");
_BMP3_i2c = theWire;
}
/// public
bool BMP3_I2C::init()
{
_BMP3_i2c->begin();
the_sensor.dev_id = i2c_addr;
the_sensor.intf = BMP3_I2C_INTF;
the_sensor.read = &i2c_read;
the_sensor.write = &i2c_write;
return initSensor();
}
bool BMP3_I2C::setSensorInSleepMode(void)
{
uint8_t rslt = BMP3_OK;
_forcedModeEnabled = false;
rslt = bmp3_soft_reset(&the_sensor);
return rslt == BMP3_OK;
}
bool BMP3_I2C::getSensorData(bmp_data *sensorData, bool computeAltitude)
{
int8_t rslt;
/* Variable used to select the sensor component */
uint8_t sensor_comp;
/* if forced mode, set power mode of the sensor at each reading*/
if (_forcedModeEnabled)
{
rslt = bmp3_set_op_mode(&the_sensor);
if (rslt != BMP3_OK)
{
printf("error set power mode \n");
if (rslt == BMP3_E_INVALID_ODR_OSR_SETTINGS)
printf("BMP3_E_INVALID_ODR_OSR_SETTINGS \n");
return false;
}
}
/* Variable used to store the compensated data */
struct bmp3_data data;
/* Sensor component selection */
sensor_comp = BMP3_PRESS | BMP3_TEMP;
/* Temperature and Pressure data are read and stored in the bmp3_data instance */
rslt = bmp3_get_sensor_data(sensor_comp, &data, &the_sensor);
if (rslt != BMP3_OK)
return false;
/* Save the temperature and pressure data */
sensorData->temperature = data.temperature;
sensorData->pressure = data.pressure;
if (computeAltitude)
{
sensorData->altitude = calcAltitude(sensorData->pressure, sensorData->seaLevelPressure);
}
return true;
}
bool BMP3_I2C::calcSeaLevelPressure(bmp_data *sensorData, double YourActualAltitude)
{
if (sensorData->pressure != -99999.)
{
sensorData->seaLevelPressure = sensorData->pressure / pow(1. - (YourActualAltitude / 44330.), 5.255);
return true;
}
return false;
}
double BMP3_I2C::calcSeaLevelPressure(double atmosphericPressure, double YourActualAltitude)
{
return atmosphericPressure / pow(1. - (YourActualAltitude / 44330.), 5.255);
}
double BMP3_I2C::calcAltitude(double atmosphericPressure, double seaLevelPressure)
{
return 44330. * (1. - pow(atmosphericPressure / seaLevelPressure, 0.1902949));
}
/// protected
bool BMP3_I2C::initSensor()
{
_forcedModeEnabled = false;
the_sensor.delay_ms = delay_msec;
int8_t rslt = BMP3_OK;
rslt = bmp3_init(&the_sensor);
#ifdef BMP3XX_DEBUG
Serial.print("Result: ");
Serial.println(rslt);
#endif
if (rslt != BMP3_OK)
return false;
#ifdef BMP3XX_DEBUG
Serial.print("T1 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_t1);
Serial.print("T2 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_t2);
Serial.print("T3 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_t3);
Serial.print("P1 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p1);
Serial.print("P2 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p2);
Serial.print("P3 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p3);
Serial.print("P4 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p4);
Serial.print("P5 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p5);
Serial.print("P6 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p6);
Serial.print("P7 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p7);
Serial.print("P8 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p8);
Serial.print("P9 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p9);
Serial.print("P10 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p10);
Serial.print("P11 = ");
Serial.println(the_sensor.calib_data.reg_calib_data.par_p11);
//Serial.print("T lin = "); Serial.println(the_sensor.calib_data.reg_calib_data.t_lin);
#endif
return true;
}
/// private
bool BMP3_I2C::setConfig(uint8_t TemperatureOversampling, uint8_t PressureOversampling, uint8_t IIRFilter, uint8_t PowerMode, uint8_t OutputDataRate, bool DataReadyInterrupt)
{
int8_t rslt = BMP3_OK;
uint16_t settings_sel = 0;
/* Select the pressure and temperature sensor to be enabled */
the_sensor.settings.press_en = BMP3_ENABLE;
the_sensor.settings.temp_en = BMP3_ENABLE;
settings_sel |= BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL;
/* set pressure settings */
if (PressureOversampling > BMP3_OVERSAMPLING_32X)
{
printf("error pressure oversampling \n");
return false;
}
the_sensor.settings.odr_filter.press_os = PressureOversampling;
settings_sel |= BMP3_PRESS_OS_SEL;
/* set temperature settings */
if (TemperatureOversampling > BMP3_OVERSAMPLING_32X)
{
printf("error temp oversampling \n");
return false;
}
the_sensor.settings.odr_filter.temp_os = TemperatureOversampling;
settings_sel |= BMP3_TEMP_OS_SEL;
/* set iirFilter settings */
if (IIRFilter > BMP3_IIR_FILTER_COEFF_127)
{
printf("error iir filter \n");
return false;
}
the_sensor.settings.odr_filter.iir_filter = IIRFilter;
settings_sel |= BMP3_IIR_FILTER_SEL;
/* set output data rate */
if (PowerMode == BMP3_NORMAL_MODE)
{
if (OutputDataRate > BMP3_ODR_0_001_HZ)
{
printf("error data rate \n");
return false;
}
the_sensor.settings.odr_filter.odr = OutputDataRate;
settings_sel |= BMP3_ODR_SEL;
}
/* set interrupt settings */
//TODO check if interrupt settings are ok
the_sensor.settings.int_settings.drdy_en =
(PowerMode == BMP3_NORMAL_MODE && DataReadyInterrupt) ? BMP3_ENABLE : BMP3_DISABLE;
//the_sensor.settings.int_settings.latch = BMP3_INT_PIN_NON_LATCH; //BMP3_INT_PIN_LATCH
//the_sensor.settings.int_settings.level = BMP3_INT_PIN_ACTIVE_HIGH; //BMP3_INT_PIN_ACTIVE_LOW
//the_sensor.settings.int_settings.output_mode = BMP3_INT_PIN_PUSH_PULL; //BMP3_INT_PIN_OPEN_DRAIN
settings_sel |= BMP3_DRDY_EN_SEL; // | BMP3_LEVEL_SEL | BMP3_LATCH_SEL | BMP3_OUTPUT_MODE_SEL;
/* set sensor settings */
rslt = bmp3_set_sensor_settings(settings_sel, &the_sensor);
if (rslt != BMP3_OK)
{
printf("error set sensor settings \n");
return false;
}
#ifdef BMP3XX_DEBUG
bmp3_get_sensor_settings(&the_sensor);
Serial.printf("temp en: %u, temp os: %u, press en: %u, press os: %u, odr: %u", the_sensor.settings.temp_en, the_sensor.settings.odr_filter.temp_os, the_sensor.settings.press_en, the_sensor.settings.odr_filter.press_os, the_sensor.settings.odr_filter.odr);
#endif
/* set power mode */
if (PowerMode == BMP3_NORMAL_MODE || PowerMode == BMP3_FORCED_MODE || PowerMode == BMP3_SLEEP_MODE)
{
_forcedModeEnabled = PowerMode == BMP3_FORCED_MODE;
the_sensor.settings.op_mode = PowerMode;
rslt = bmp3_set_op_mode(&the_sensor);
if (rslt != BMP3_OK)
{
printf("error set power mode \n");
if (rslt == BMP3_E_INVALID_ODR_OSR_SETTINGS)
printf("BMP3_E_INVALID_ODR_OSR_SETTINGS \n");
return false;
}
}
else
return false;
return true;
}
/// static
/**************************************************************************/
/*!
@brief Reads 8 bit values over I2C
*/
/**************************************************************************/
int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
#ifdef BMP3XX_DEBUG
Serial.print("\tI2C $");
Serial.print(reg_addr, HEX);
Serial.print(" => ");
#endif
_BMP3_i2c->beginTransmission((uint8_t)dev_id);
_BMP3_i2c->write((uint8_t)reg_addr);
_BMP3_i2c->endTransmission();
if (len != _BMP3_i2c->requestFrom((uint8_t)dev_id, (byte)len))
{
#ifdef BMP3XX_DEBUG
Serial.print("Failed to read ");
Serial.print(len);
Serial.print(" bytes from ");
Serial.println(dev_id, HEX);
#endif
return 1;
}
while (len--)
{
*reg_data = (uint8_t)_BMP3_i2c->read();
#ifdef BMP3XX_DEBUG
Serial.print("0x");
Serial.print(*reg_data, HEX);
Serial.print(", ");
#endif
reg_data++;
}
#ifdef BMP3XX_DEBUG
Serial.println("");
#endif
return 0;
}
/**************************************************************************/
/*!
@brief Writes 8 bit values over I2C
*/
/**************************************************************************/
int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
#ifdef BMP3XX_DEBUG
Serial.print("\tI2C $");
Serial.print(reg_addr, HEX);
Serial.print(" <= ");
#endif
_BMP3_i2c->beginTransmission((uint8_t)dev_id);
_BMP3_i2c->write((uint8_t)reg_addr);
while (len--)
{
_BMP3_i2c->write(*reg_data);
#ifdef BMP3XX_DEBUG
Serial.print("0x");
Serial.print(*reg_data, HEX);
Serial.print(", ");
#endif
reg_data++;
}
_BMP3_i2c->endTransmission();
#ifdef BMP3XX_DEBUG
Serial.println("");
#endif
return 0;
}
static void delay_msec(uint32_t ms)
{
delay(ms);
}