-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBMP280.cpp
207 lines (168 loc) · 6.4 KB
/
BMP280.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
#include "BMP280.h"
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <linux/i2c-dev.h>
#include <thread>
#include <unistd.h>
#include <sys/ioctl.h>
#undef DBG
BMP280::BMP280(std::string i2c_dev_name, uint8_t ccs811_addr)
: i2c_dev_name(std::move(i2c_dev_name)),
ccs811_addr(ccs811_addr) {
open_device();
init();
}
BMP280::~BMP280() {
close_device();
}
void BMP280::close_device() {
if (i2c_fd >= 0) close(i2c_fd);
}
void BMP280::init() {
std::cout << "Resetting BMP280..." << std::endl;
reset();
std::this_thread::sleep_for(std::chrono::seconds(3));
auto id = read_id();
if (id != 0x58) {
throw "Invalid device id!";
}
std::cout << "Reading calibration data" << std::endl;
read_calibration_data();
std::cout << "Setting the measurement control register" << std::endl;
uint8_t temp_oversampling = 1; // x1 oversampling
uint8_t pres_oversampling = 1; // x1 oversampling
uint8_t power_mode = 3; // Normal mode
auto ctrl_meas_reg = static_cast<uint8_t>(((temp_oversampling & 7) << 5) | ((pres_oversampling & 7) << 2) |
(power_mode & 3));
uint8_t cmd[] = {0xf4, ctrl_meas_reg};
write_data(cmd, 2);
std::cout << "Setting the configuration register" << std::endl;
uint8_t t_standby = 1 << 2; // 500ms
uint8_t iir_filter = 0; // disabled
uint8_t spi_interface = 0; // 3-wire SPI interface is disabled
auto config_reg = static_cast<uint8_t>(((t_standby & 7) << 5) | ((iir_filter & 7) << 2) | (spi_interface & 1));
cmd[0] = 0xf5;
cmd[1] = config_reg;
write_data(cmd, 2);
}
void BMP280::open_device() {
i2c_fd = open(i2c_dev_name.c_str(), O_RDWR);
if (i2c_fd < 0) {
std::cerr << "Unable to open" << i2c_dev_name << ". " << strerror(errno) << std::endl;
throw 1;
}
if (ioctl(i2c_fd, I2C_SLAVE, ccs811_addr) < 0) {
std::cerr << "Failed to communicate with the device. " << strerror(errno) << std::endl;
throw 1;
}
}
std::unique_ptr<std::vector<uint8_t>> BMP280::read_registers(uint8_t start, size_t count) {
uint8_t data[] = {start};
write_data(data, 1);
auto *read_buffer = new uint8_t[count];
auto bytes_read = read(i2c_fd, read_buffer, count);
if (bytes_read < 0) {
// return an empty vector if we can't read anything.
return std::make_unique<std::vector<uint8_t>>();
}
auto result = std::make_unique<std::vector<uint8_t>>(read_buffer, read_buffer + bytes_read);
delete[] read_buffer;
#ifdef DBG
std::cerr << "\tRegisters: ";
for (auto e : *result) {
std::cerr << std::hex << (int) e << " ";
}
std::cerr << std::endl;
#endif
return result;
}
void BMP280::write_data(uint8_t *buffer, size_t buffer_len) {
#ifdef DBG
std::cerr << "\tWrite: ";
for (size_t i = 0; i < buffer_len; i++) {
std::cerr << "0x" << hex << (int) buffer[i] << " ";
}
#endif
auto write_c = write(i2c_fd, buffer, buffer_len);
if (write_c < 0) {
std::cerr << "Unable to send command." << std::endl;
// TODO - Have better exceptions.
throw 1;
}
#ifdef DBG
std::cerr << " ... wrote " << write_c << " bytes." << std::endl;
#endif
}
void BMP280::read_calibration_data() {
auto reg_data = read_registers(0x88, 24);
dig_T1 = (reg_data->at(1) << 8) | reg_data->at(0);
dig_T2 = (reg_data->at(3) << 8) | reg_data->at(2);
dig_T3 = (reg_data->at(5) << 8) | reg_data->at(4);
dig_P1 = (reg_data->at(7) << 8) | reg_data->at(6);
dig_P2 = (reg_data->at(9) << 8) | reg_data->at(8);
dig_P3 = (reg_data->at(11) << 8) | reg_data->at(10);
dig_P4 = (reg_data->at(13) << 8) | reg_data->at(12);
dig_P5 = (reg_data->at(15) << 8) | reg_data->at(14);
dig_P6 = (reg_data->at(17) << 8) | reg_data->at(16);
dig_P7 = (reg_data->at(19) << 8) | reg_data->at(18);
dig_P8 = (reg_data->at(21) << 8) | reg_data->at(20);
dig_P9 = (reg_data->at(23) << 8) | reg_data->at(22);
}
void BMP280::reset() {
uint8_t cmd[] = {0xe0, 0xb6};
write_data(cmd, 2);
}
void BMP280::set_ctrl_meas(uint8_t val) {
uint8_t cmd[] = {0xf4, val};
write_data(cmd, 2);
}
uint8_t BMP280::read_id() {
auto id = read_registers(0xd0, 1);
return id->front();
}
void BMP280::measure() {
auto pressure_data = read_registers(0xf7, 3);
uint8_t pressure_msb = pressure_data->at(0);
uint8_t pressure_lsb = pressure_data->at(1);
uint8_t pressure_xlsb = pressure_data->at(2);
uint32_t pressure_val = (pressure_msb << 12) | (pressure_lsb << 4) | (pressure_xlsb >> 4);
auto temp_data = read_registers(0xfa, 3);
uint8_t temp_msb = temp_data->at(0);
uint8_t temp_lsb = temp_data->at(1);
uint8_t temp_xlsb = temp_data->at(2);
uint32_t temp_val = (temp_msb << 12) | (temp_lsb << 4) | (temp_xlsb >> 4);
pressure = compensate_pressure(pressure_val);
temperature = compensate_temp(temp_val);
last_measurement = time(nullptr);
}
// Compensation formulae are taken from the datasheet.
// Returns temperature in Celsuis, resolution is 0.01 DegC.
double BMP280::compensate_temp(uint32_t adc_t) {
double var1 = (((double) adc_t) / 16384.0 - ((double) dig_T1) / 1024.0) * ((double) dig_T2);
double var2 = ((((double) adc_t) / 131072.0 - ((double) dig_T1) / 8192.0) *
(((double) adc_t) / 131072.0 - ((double) dig_T1) / 8192.0)) * ((double) dig_T3);
t_fine = static_cast<int32_t>(var1 + var2);
return t_fine / 5120.0;
}
// Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
// Output value of 24674867 represents 24674867/256 = 96386.2 Pa = 963.862 hPa
double BMP280::compensate_pressure(int32_t adc_p) {
double var1 = ((double) t_fine / 2.0) - 64000.0;
double var2 = var1 * var1 * ((double) dig_P6) / 32768.0;
var2 = var2 + var1 * ((double) dig_P5) * 2.0;
var2 = (var2 / 4.0) + (((double) dig_P4) * 65536.0);
var1 = (((double) dig_P3) * var1 * var1 / 524288.0 + ((double) dig_P2) * var1) / 524288.0;
var1 = (1.0 + var1 / 32768.0) * ((double) dig_P1);
double p = 1048576.0 - (double) adc_p;
p = (p - (var2 / 4096.0)) * 6250.0 / var1;
var1 = ((double) dig_P9) * p * p / 2147483648.0;
var2 = p * ((double) dig_P8) / 32768.0;
return (p + (var1 + var2 + ((double) dig_P7)) / 16.0) / 100;
}
double BMP280::get_temperature() {
return temperature;
}
double BMP280::get_pressure() {
return pressure;
}