kopia lustrzana https://github.com/robert-hh/BME280
bme280_xxx.py: Make the code a little bit more PEP8 compliant
rodzic
92773a9c95
commit
0223507e1e
|
@ -1,3 +1,10 @@
|
||||||
|
# Updated 2018
|
||||||
|
# This module is based on the below cited resources, which are all
|
||||||
|
# based on the documentation as provided in the Bosch Data Sheet and
|
||||||
|
# the sample implementation provided therein.
|
||||||
|
#
|
||||||
|
# Final Document: BST-BME280-DS002-15
|
||||||
|
#
|
||||||
# Authors: Paul Cunnane 2016, Peter Dahlebrg 2016
|
# Authors: Paul Cunnane 2016, Peter Dahlebrg 2016
|
||||||
#
|
#
|
||||||
# This module borrows from the Adafruit BME280 Python library. Original
|
# This module borrows from the Adafruit BME280 Python library. Original
|
||||||
|
@ -31,6 +38,7 @@
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
|
#
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from ustruct import unpack, unpack_from
|
from ustruct import unpack, unpack_from
|
||||||
|
@ -82,7 +90,7 @@ class BME280:
|
||||||
_, self.dig_H1 = unpack("<HhhHhhhhhhhhBB", dig_88_a1)
|
_, self.dig_H1 = unpack("<HhhHhhhhhhhhBB", dig_88_a1)
|
||||||
|
|
||||||
self.dig_H2, self.dig_H3, self.dig_H4,\
|
self.dig_H2, self.dig_H3, self.dig_H4,\
|
||||||
self.dig_H5, self.dig_H6 = unpack("<hBbhb", dig_e1_e7)
|
self.dig_H5, self.dig_H6 = unpack("<hBbhb", dig_e1_e7)
|
||||||
# unfold H4, H5, keeping care of a potential sign
|
# unfold H4, H5, keeping care of a potential sign
|
||||||
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
||||||
self.dig_H5 //= 16
|
self.dig_H5 //= 16
|
||||||
|
@ -140,8 +148,8 @@ class BME280:
|
||||||
this to read out the sensor without allocating heap memory
|
this to read out the sensor without allocating heap memory
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
array with temperature, pressure, humidity. Will be the one from
|
array with temperature, pressure, humidity. Will be the one
|
||||||
the result parameter if not None
|
from the result parameter if not None
|
||||||
"""
|
"""
|
||||||
self.read_raw_data(self._l3_resultarray)
|
self.read_raw_data(self._l3_resultarray)
|
||||||
raw_temp, raw_press, raw_hum = self._l3_resultarray
|
raw_temp, raw_press, raw_hum = self._l3_resultarray
|
||||||
|
@ -160,7 +168,7 @@ class BME280:
|
||||||
var1 = (self.dig_P3 * var1 * var1 / 524288.0 + self.dig_P2 * var1) / 524288.0
|
var1 = (self.dig_P3 * var1 * var1 / 524288.0 + self.dig_P2 * var1) / 524288.0
|
||||||
var1 = (1.0 + var1 / 32768.0) * self.dig_P1
|
var1 = (1.0 + var1 / 32768.0) * self.dig_P1
|
||||||
if (var1 == 0.0):
|
if (var1 == 0.0):
|
||||||
pressure = 30000 # avoid exception caused by division by zero
|
pressure = 30000 # avoid exception caused by division by zero
|
||||||
else:
|
else:
|
||||||
p = ((1048576.0 - raw_press) - (var2 / 4096.0)) * 6250.0 / var1
|
p = ((1048576.0 - raw_press) - (var2 / 4096.0)) * 6250.0 / var1
|
||||||
var1 = self.dig_P9 * p * p / 2147483648.0
|
var1 = self.dig_P9 * p * p / 2147483648.0
|
||||||
|
@ -171,8 +179,8 @@ class BME280:
|
||||||
# humidity
|
# humidity
|
||||||
h = (self.t_fine - 76800.0)
|
h = (self.t_fine - 76800.0)
|
||||||
h = ((raw_hum - (self.dig_H4 * 64.0 + self.dig_H5 / 16384.0 * h)) *
|
h = ((raw_hum - (self.dig_H4 * 64.0 + self.dig_H5 / 16384.0 * h)) *
|
||||||
(self.dig_H2 / 65536.0 * (1.0 + self.dig_H6 / 67108864.0 * h *
|
(self.dig_H2 / 65536.0 * (1.0 + self.dig_H6 / 67108864.0 * h *
|
||||||
(1.0 + self.dig_H3 / 67108864.0 * h))))
|
(1.0 + self.dig_H3 / 67108864.0 * h))))
|
||||||
humidity = h * (1.0 - self.dig_H1 * h / 524288.0)
|
humidity = h * (1.0 - self.dig_H1 * h / 524288.0)
|
||||||
# humidity = max(0, min(100, humidity))
|
# humidity = max(0, min(100, humidity))
|
||||||
|
|
||||||
|
@ -190,7 +198,7 @@ class BME280:
|
||||||
|
|
||||||
@sealevel.setter
|
@sealevel.setter
|
||||||
def sealevel(self, value):
|
def sealevel(self, value):
|
||||||
if 30000 < value < 120000: # just ensure some reasonable value
|
if 30000 < value < 120000: # just ensure some reasonable value
|
||||||
self.__sealevel = value
|
self.__sealevel = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -200,8 +208,8 @@ class BME280:
|
||||||
'''
|
'''
|
||||||
from math import pow
|
from math import pow
|
||||||
try:
|
try:
|
||||||
p = 44330 * (1.0 - pow(self.read_compensated_data()[1]
|
p = 44330 * (1.0 - pow(self.read_compensated_data()[1] /
|
||||||
/ self.__sealevel, 0.1903))
|
self.__sealevel, 0.1903))
|
||||||
except:
|
except:
|
||||||
p = 0.0
|
p = 0.0
|
||||||
return p
|
return p
|
||||||
|
@ -225,4 +233,3 @@ class BME280:
|
||||||
|
|
||||||
return ("{:.2f}C".format(t), "{:.2f}hPa".format(p/100),
|
return ("{:.2f}C".format(t), "{:.2f}hPa".format(p/100),
|
||||||
"{:.2f}%".format(h))
|
"{:.2f}%".format(h))
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
|
# Updated 2018
|
||||||
|
# This module is based on the below cited resources, which are all
|
||||||
|
# based on the documentation as provided in the Bosch Data Sheet and
|
||||||
|
# the sample implementation provided therein.
|
||||||
|
#
|
||||||
|
# Final Document: BST-BME280-DS002-15
|
||||||
|
#
|
||||||
# Authors: Paul Cunnane 2016, Peter Dahlebrg 2016
|
# Authors: Paul Cunnane 2016, Peter Dahlebrg 2016
|
||||||
#
|
#
|
||||||
# This module borrows from the Adafruit BME280 Python library. Original
|
# This module borrows from the Adafruit BME280 Python library. Original
|
||||||
|
@ -31,6 +38,11 @@
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
|
#
|
||||||
|
# Based on the documentation as provided in the Bosch Data Sheet and
|
||||||
|
# the sample implementation provided therein.
|
||||||
|
# Document BST-BME280-DS002-15
|
||||||
|
#
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from ustruct import unpack, unpack_from
|
from ustruct import unpack, unpack_from
|
||||||
|
@ -81,7 +93,7 @@ class BME280:
|
||||||
_, self.dig_H1 = unpack("<HhhHhhhhhhhhBB", dig_88_a1)
|
_, self.dig_H1 = unpack("<HhhHhhhhhhhhBB", dig_88_a1)
|
||||||
|
|
||||||
self.dig_H2, self.dig_H3, self.dig_H4,\
|
self.dig_H2, self.dig_H3, self.dig_H4,\
|
||||||
self.dig_H5, self.dig_H6 = unpack("<hBbhb", dig_e1_e7)
|
self.dig_H5, self.dig_H6 = unpack("<hBbhb", dig_e1_e7)
|
||||||
# unfold H4, H5, keeping care of a potential sign
|
# unfold H4, H5, keeping care of a potential sign
|
||||||
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
||||||
self.dig_H5 //= 16
|
self.dig_H5 //= 16
|
||||||
|
@ -139,8 +151,8 @@ class BME280:
|
||||||
this to read out the sensor without allocating heap memory
|
this to read out the sensor without allocating heap memory
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
array with temperature, pressure, humidity. Will be the one from
|
array with temperature, pressure, humidity. Will be the one
|
||||||
the result parameter if not None
|
from the result parameter if not None
|
||||||
"""
|
"""
|
||||||
self.read_raw_data(self._l3_resultarray)
|
self.read_raw_data(self._l3_resultarray)
|
||||||
raw_temp, raw_press, raw_hum = self._l3_resultarray
|
raw_temp, raw_press, raw_hum = self._l3_resultarray
|
||||||
|
@ -170,10 +182,10 @@ class BME280:
|
||||||
# humidity
|
# humidity
|
||||||
h = self.t_fine - 76800
|
h = self.t_fine - 76800
|
||||||
h = (((((raw_hum << 14) - (self.dig_H4 << 20) -
|
h = (((((raw_hum << 14) - (self.dig_H4 << 20) -
|
||||||
(self.dig_H5 * h)) + 16384)
|
(self.dig_H5 * h)) + 16384) >> 15) *
|
||||||
>> 15) * (((((((h * self.dig_H6) >> 10) *
|
(((((((h * self.dig_H6) >> 10) *
|
||||||
(((h * self.dig_H3) >> 11) + 32768)) >> 10) +
|
(((h * self.dig_H3) >> 11) + 32768)) >> 10) + 2097152) *
|
||||||
2097152) * self.dig_H2 + 8192) >> 14))
|
self.dig_H2 + 8192) >> 14))
|
||||||
h = h - (((((h >> 15) * (h >> 15)) >> 7) * self.dig_H1) >> 4)
|
h = h - (((((h >> 15) * (h >> 15)) >> 7) * self.dig_H1) >> 4)
|
||||||
h = 0 if h < 0 else h
|
h = 0 if h < 0 else h
|
||||||
h = 419430400 if h > 419430400 else h
|
h = 419430400 if h > 419430400 else h
|
||||||
|
@ -193,7 +205,7 @@ class BME280:
|
||||||
|
|
||||||
@sealevel.setter
|
@sealevel.setter
|
||||||
def sealevel(self, value):
|
def sealevel(self, value):
|
||||||
if 300 < value < 1200: # just ensure some reasonable value
|
if 300 < value < 1200: # just ensure some reasonable value
|
||||||
self.__sealevel = value
|
self.__sealevel = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -203,8 +215,8 @@ class BME280:
|
||||||
'''
|
'''
|
||||||
from math import pow
|
from math import pow
|
||||||
try:
|
try:
|
||||||
p = 44330 * (1.0 - pow((self.read_compensated_data()[1] / 256)
|
p = 44330 * (1.0 - pow((self.read_compensated_data()[1] / 256) /
|
||||||
/ self.__sealevel, 0.1903))
|
self.__sealevel, 0.1903))
|
||||||
except:
|
except:
|
||||||
p = 0.0
|
p = 0.0
|
||||||
return p
|
return p
|
||||||
|
@ -233,4 +245,3 @@ class BME280:
|
||||||
h = h / 1024
|
h = h / 1024
|
||||||
return ("{}C".format(t / 100), "{:.02f}hPa".format(p/100),
|
return ("{}C".format(t / 100), "{:.02f}hPa".format(p/100),
|
||||||
"{:.02f}%".format(h))
|
"{:.02f}%".format(h))
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue