Skip to content

Commit 05f8069

Browse files
committed
Merge branch 'ayeks-offset'
2 parents 40b4b63 + 0f0be7f commit 05f8069

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

examples/temp-offset.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
import bme680
3+
4+
print("""Display Temperature, Pressure and Humidity with different offsets.
5+
""")
6+
7+
sensor = bme680.BME680()
8+
9+
# These oversampling settings can be tweaked to
10+
# change the balance between accuracy and noise in
11+
# the data.
12+
13+
sensor.set_humidity_oversample(bme680.OS_2X)
14+
sensor.set_pressure_oversample(bme680.OS_4X)
15+
sensor.set_temperature_oversample(bme680.OS_8X)
16+
sensor.set_filter(bme680.FILTER_SIZE_3)
17+
18+
def display_data(offset=0):
19+
sensor.set_temp_offset(offset)
20+
sensor.get_sensor_data()
21+
output = "{0:.2f} C, {1:.2f} hPa, {2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity)
22+
print(output)
23+
print("")
24+
25+
print("Initial readings")
26+
display_data()
27+
28+
print("SET offset 4 degrees celsius")
29+
display_data(4)
30+
31+
print("SET offset -1.87 degrees celsius")
32+
display_data(-1.87)
33+
34+
print("SET offset -100 degrees celsius")
35+
display_data(-100)
36+
37+
print("SET offset 0 degrees celsius")
38+
display_data(0)
39+

library/LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Pimoroni Ltd
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

library/MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ include CHANGELOG.txt
22
include LICENSE.txt
33
include README.txt
44
include setup.py
5-
include bme680.py
5+
recursive-include bme680 *.py

library/bme680/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, i2c_addr=I2C_ADDR_PRIMARY, i2c_device=None):
3636
self.set_temperature_oversample(OS_8X)
3737
self.set_filter(FILTER_SIZE_3)
3838
self.set_gas_status(ENABLE_GAS_MEAS)
39-
39+
self.set_temp_offset(0)
4040
self.get_sensor_data()
4141

4242
def _get_calibration_data(self):
@@ -56,6 +56,17 @@ def soft_reset(self):
5656
self._set_regs(SOFT_RESET_ADDR, SOFT_RESET_CMD)
5757
time.sleep(RESET_PERIOD / 1000.0)
5858

59+
def set_temp_offset(self, value):
60+
"""Set temperature offset in celsius
61+
62+
If set, the temperature t_fine will be increased by given value in celsius.
63+
:param value: Temperature offset in Celsius, eg. 4, -8, 1.25
64+
"""
65+
if value == 0:
66+
self.offset_temp_in_t_fine = 0
67+
else:
68+
self.offset_temp_in_t_fine = int(math.copysign((((int(abs(value) * 100)) << 8) - 128) / 5, value))
69+
5970
def set_humidity_oversample(self, value):
6071
"""Set humidity oversampling
6172
@@ -293,7 +304,7 @@ def _calc_temperature(self, temperature_adc):
293304
var3 = ((var3) * (self.calibration_data.par_t3 << 4)) >> 14
294305

295306
# Save teperature data for pressure calculations
296-
self.calibration_data.t_fine = (var2 + var3)
307+
self.calibration_data.t_fine = (var2 + var3) + self.offset_temp_in_t_fine
297308
calc_temp = (((self.calibration_data.t_fine * 5) + 128) >> 8)
298309

299310
return calc_temp

0 commit comments

Comments
 (0)