kopia lustrzana https://github.com/peterhinch/mpy_bme280_esp8266
format the code with yapf
rodzic
12b768d677
commit
dd5b4428b4
23
bme280.py
23
bme280.py
|
@ -50,8 +50,13 @@ BME280_OSAMPLE_16 = 5
|
||||||
BME280_REGISTER_CONTROL_HUM = 0xF2
|
BME280_REGISTER_CONTROL_HUM = 0xF2
|
||||||
BME280_REGISTER_CONTROL = 0xF4
|
BME280_REGISTER_CONTROL = 0xF4
|
||||||
|
|
||||||
|
|
||||||
class BME280:
|
class BME280:
|
||||||
def __init__(self, mode=BME280_OSAMPLE_1, address=BME280_I2CADDR, i2c=None,
|
|
||||||
|
def __init__(self,
|
||||||
|
mode=BME280_OSAMPLE_1,
|
||||||
|
address=BME280_I2CADDR,
|
||||||
|
i2c=None,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
# Check that mode is valid.
|
# Check that mode is valid.
|
||||||
if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
|
if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
|
||||||
|
@ -83,7 +88,6 @@ class BME280:
|
||||||
|
|
||||||
self.dig_H6 = unpack_from("<b", dig_e1_e7, 6)[0]
|
self.dig_H6 = unpack_from("<b", dig_e1_e7, 6)[0]
|
||||||
|
|
||||||
|
|
||||||
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
bytearray([0x3F]))
|
bytearray([0x3F]))
|
||||||
self.t_fine = 0
|
self.t_fine = 0
|
||||||
|
@ -145,10 +149,8 @@ class BME280:
|
||||||
raw_temp, raw_press, raw_hum = self._l3_resultarray
|
raw_temp, raw_press, raw_hum = self._l3_resultarray
|
||||||
#temperature
|
#temperature
|
||||||
var1 = ((raw_temp >> 3) - (self.dig_T1 << 1)) * (self.dig_T2 >> 11)
|
var1 = ((raw_temp >> 3) - (self.dig_T1 << 1)) * (self.dig_T2 >> 11)
|
||||||
var2 = ((
|
var2 = (((((raw_temp >> 4) - self.dig_T1) *
|
||||||
(((raw_temp >> 4) - self.dig_T1) *
|
((raw_temp >> 4) - self.dig_T1)) >> 12) * self.dig_T3) >> 14
|
||||||
((raw_temp >> 4) - self.dig_T1)) >> 12) *
|
|
||||||
self.dig_T3) >> 14
|
|
||||||
self.t_fine = var1 + var2
|
self.t_fine = var1 + var2
|
||||||
temp = (self.t_fine * 5 + 128) >> 8
|
temp = (self.t_fine * 5 + 128) >> 8
|
||||||
|
|
||||||
|
@ -171,10 +173,11 @@ class BME280:
|
||||||
|
|
||||||
#humidity
|
#humidity
|
||||||
h = self.t_fine - 76800
|
h = self.t_fine - 76800
|
||||||
h = (((((raw_hum << 14) - (self.dig_H4 << 20) - (self.dig_H5 * h)) +
|
h = (((((raw_hum << 14) - (self.dig_H4 << 20) -
|
||||||
16384) >> 15) * (((((((h * self.dig_H6) >> 10) * (((h *
|
(self.dig_H5 * h)) + 16384)
|
||||||
self.dig_H3) >> 11) + 32768)) >> 10) + 2097152) *
|
>> 15) * (((((((h * self.dig_H6) >> 10) *
|
||||||
self.dig_H2 + 8192) >> 14))
|
(((h * self.dig_H3) >> 11) + 32768)) >> 10) +
|
||||||
|
2097152) * 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
|
||||||
|
|
Ładowanie…
Reference in New Issue