forked from janw-cz/JWA_BME280
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBme280BoschWrapper.cpp
241 lines (180 loc) · 4.8 KB
/
Bme280BoschWrapper.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
#include <Arduino.h>
#include <limits.h>
#include <SPI.h>
#include <Wire.h>
#include "Bme280BoschWrapper.h"
#define DOUBLE_NOT_CALCULATED -1000.0
int Bme280BoschWrapper::_cs = -1;
Bme280BoschWrapper::Bme280BoschWrapper(bool forced)
{
this->forced = forced;
}
bool Bme280BoschWrapper::beginI2C(uint8_t dev_addr)
{
I2CInit();
bme280.dev_id = dev_addr;
int8_t ret = bme280_init(&bme280);
setSensorSettings();
return (ret == BME280_OK);
}
bool Bme280BoschWrapper::beginSPI(int8_t cspin)
{
Bme280BoschWrapper::_cs = cspin;
SPIInit();
pinMode(_cs, OUTPUT);
int8_t ret = bme280_init(&bme280);
setSensorSettings();
return (ret == BME280_OK);
}
bool Bme280BoschWrapper::measure()
{
int8_t ret = BME280_OK;
if(forced)
{
setSensorSettings();
bme280.delay_ms(255);
ret += bme280_get_sensor_data(BME280_PRESS | BME280_HUM | BME280_TEMP, &comp_data, &bme280);
}
else
{
ret += bme280_get_sensor_data(BME280_PRESS | BME280_HUM | BME280_TEMP, &comp_data, &bme280);
}
if(ret != BME280_OK) {
error = true;
}
return (ret == BME280_OK);
}
int32_t Bme280BoschWrapper::getTemperature()
{
return comp_data.temperature;
}
uint32_t Bme280BoschWrapper::getHumidity()
{
return comp_data.humidity;
}
uint32_t Bme280BoschWrapper::getPressure()
{
return comp_data.pressure;
}
/**
* Wrapper functions for Bosch BME280 driver.
*/
#define SPI_READ 0x80
#define SPI_WRITE 0x7F
SPISettings bme280SpiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0);
void Bme280BoschWrapper::I2CInit()
{
bme280.intf = BME280_I2C_INTF;
bme280.write = Bme280BoschWrapper::I2CWrite;
bme280.read = Bme280BoschWrapper::I2CRead;
bme280.delay_ms = Bme280BoschWrapper::delaymsec;
Wire.begin();
}
void Bme280BoschWrapper::SPIInit()
{
bme280.dev_id = 0;
bme280.intf = BME280_SPI_INTF;
bme280.write = Bme280BoschWrapper::SPIWrite;
bme280.read = Bme280BoschWrapper::SPIRead;
bme280.delay_ms = Bme280BoschWrapper::delaymsec;
SPI.begin();
}
int8_t Bme280BoschWrapper::I2CRead(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t cnt)
{
// Serial.println("I2C_bus_read");
int8_t ret = BME280_OK;
// Serial.println(dev_addr, HEX);
Wire.beginTransmission(dev_addr);
// Serial.println(reg_addr, HEX);
Wire.write(reg_addr);
Wire.endTransmission();
Wire.requestFrom((int)dev_addr, (int)cnt);
uint8_t available = Wire.available();
if(available != cnt)
{
ret = BME280_E_COMM_FAIL;
}
for(uint8_t i = 0; i < available; i++)
{
if(i < cnt)
{
*(reg_data + i) = Wire.read();
// Serial.print(*(reg_data + i), HEX);
// Serial.print(" ");
}
else
Wire.read();
}
// Serial.println();
return ret;
}
int8_t Bme280BoschWrapper::I2CWrite(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t cnt)
{
// Serial.println("I2C_bus_write");
int8_t ret = BME280_OK;
// Serial.println(dev_addr, HEX);
Wire.beginTransmission(dev_addr);
// Serial.println(reg_addr, HEX);
Wire.write(reg_addr);
Wire.write(reg_data, cnt);
Wire.endTransmission();
return ret;
}
int8_t Bme280BoschWrapper::SPIRead(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t cnt)
{
// Serial.println("SPI_bus_read");
int32_t ret = BME280_OK;
SPI.beginTransaction(bme280SpiSettings);
digitalWrite(_cs, LOW);
// Serial.println(reg_addr | SPI_READ, HEX);
SPI.transfer(reg_addr | SPI_READ);
for (uint8_t i = 0; i < cnt; i++) {
*(reg_data + i) = SPI.transfer(0);
// Serial.print(*(reg_data + i), HEX);
// Serial.print(" ");
}
// Serial.println();
digitalWrite(_cs, HIGH);
SPI.endTransaction();
return ret;
}
int8_t Bme280BoschWrapper::SPIWrite(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t cnt)
{
// Serial.println("SPI_bus_write");
int8_t ret = BME280_OK;
SPI.beginTransaction(bme280SpiSettings);
digitalWrite(_cs, LOW);
for (uint8_t i = 0; i < cnt; i++)
{
uint8_t addr = (reg_addr++) & SPI_WRITE;
uint8_t data = *(reg_data + i);
// Serial.print(addr, HEX);
// Serial.print(" ");
// Serial.print(data, HEX);
SPI.transfer(addr);
SPI.transfer(data);
}
// Serial.println();
digitalWrite(_cs, HIGH);
SPI.endTransaction();
return ret;
}
void Bme280BoschWrapper::delaymsec(uint32_t msec)
{
delay(msec);
}
int8_t Bme280BoschWrapper::setSensorSettings()
{
int8_t ret = BME280_OK;
uint8_t settings_sel;
bme280.settings.osr_h = BME280_OVERSAMPLING_16X;
bme280.settings.osr_p = BME280_OVERSAMPLING_16X;
bme280.settings.osr_t = BME280_OVERSAMPLING_16X;
settings_sel = BME280_OSR_PRESS_SEL|BME280_OSR_TEMP_SEL|BME280_OSR_HUM_SEL;
ret += bme280_set_sensor_settings(settings_sel, &bme280);
if(forced)
ret += bme280_set_sensor_mode(BME280_FORCED_MODE, &bme280);
else
ret += bme280_set_sensor_mode(BME280_NORMAL_MODE, &bme280);
return ret;
}