-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp388.cpp
98 lines (80 loc) · 2.65 KB
/
bmp388.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
#include "bmp388.hpp"
#include <cmath>
#include <cstdio>
#include <cstring>
BMP388::BMP388(i2c_inst_t *i2c_type) {
i2c = i2c_type;
device.intf = BMP3_I2C_INTF;
device.read = &i2c_read;
device.write = &i2c_write;
device.dummy_byte = 0;
device.delay_us = &delay_usec;
device.intf_ptr = &i2c;
}
bool BMP388::begin() {
if (bmp3_init(&device) != BMP3_OK) {
#ifdef VERBOSE
fprintf(stderr, "Error: Could not initialize altimeter\n");
#endif
return false;
}
uint16_t settings_sel = 0;
struct bmp3_settings settings = {0};
settings.int_settings.drdy_en = BMP3_ENABLE;
settings.press_en = BMP3_ENABLE;
settings.temp_en = BMP3_ENABLE;
settings.odr_filter.press_os = BMP3_OVERSAMPLING_8X;
settings.odr_filter.temp_os = BMP3_NO_OVERSAMPLING;
settings.odr_filter.odr = BMP3_ODR_50_HZ;
settings.odr_filter.iir_filter = BMP3_IIR_FILTER_COEFF_3;
settings_sel = BMP3_SEL_PRESS_EN | BMP3_SEL_TEMP_EN | BMP3_SEL_PRESS_OS | BMP3_SEL_TEMP_OS | BMP3_SEL_ODR |
BMP3_SEL_DRDY_EN | BMP3_SEL_IIR_FILTER;
if (bmp3_set_sensor_settings(settings_sel, &settings, &device) != BMP3_OK) {
return false;
}
settings.op_mode = BMP3_MODE_NORMAL;
if (bmp3_set_op_mode(&settings, &device) != BMP3_OK) {
return false;
}
return true;
}
bool BMP388::read_pressure(float *pressure) {
if (bmp3_get_status(&status, &device) != BMP3_OK) {
return false;
}
if (bmp3_get_sensor_data(BMP3_PRESS_TEMP, &data, &device) != BMP3_OK) {
return false;
}
*pressure = (float)data.pressure / 100;
temp = (float)data.temperature;
return true;
}
bool BMP388::read_data(float *altitude, float *temperature, float sea_level_pressure) {
if (read_pressure(&pressure)) {
*altitude = 44330.0 * (1.0 - pow(pressure / sea_level_pressure, 0.1903));
*temperature = temp;
return true;
}
return false;
}
BMP3_INTF_RET_TYPE BMP388::i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) {
if (i2c_write_blocking(i2c0, BMP388_ADDR, ®_addr, 1, true) < 1) {
return 1;
}
if (i2c_read_blocking(i2c0, BMP388_ADDR, reg_data, len, false) < 1) {
return 1;
}
return BMP3_INTF_RET_SUCCESS;
}
BMP3_INTF_RET_TYPE BMP388::i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) {
uint8_t buf[len + 1];
buf[0] = reg_addr;
memcpy(&buf[1], reg_data, len);
if (i2c_write_blocking(i2c0, BMP388_ADDR, buf, len + 1, false) < 1) {
return 1;
}
return BMP3_INTF_RET_SUCCESS;
}
void BMP388::delay_usec(uint32_t us, void *intf_ptr) {
sleep_us(us);
}