From 400e446ded25659d6300a83cdce027f3cc0fb3b4 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 2 Sep 2019 16:18:58 +0100 Subject: [PATCH 1/5] Port to i2cdevice-next --- library/bme280/__init__.py | 73 ++++++++++++++------------------------ 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/library/bme280/__init__.py b/library/bme280/__init__.py index 1e4d130..7dbddb5 100644 --- a/library/bme280/__init__.py +++ b/library/bme280/__init__.py @@ -75,6 +75,12 @@ def __init__(self): self.temperature_fine = 0 + def set_from_tuple(self, value): + # Iterate through a tuple supplied by i2cdevice + # and copy its values into the class attributes + for key in value._asdict().keys(): + setattr(self, key, value.asdict()[key]) + def compensate_temperature(self, raw_temperature): var1 = (raw_temperature / 16384.0 - self.dig_t1 / 1024.0) * self.dig_t2 var2 = raw_temperature / 131072.0 - self.dig_t1 / 8192.0 @@ -210,63 +216,36 @@ def setup(self, mode='normal', temperature_oversampling=16, pressure_oversamplin except IOError: raise RuntimeError("Unable to find bme280 on 0x{:02x}, IOError".format(self._i2c_addr)) - self._bme280.RESET.set_reset(0xB6) + self._bme280.set('RESET', reset=0xB6) time.sleep(0.1) - self._bme280.CTRL_HUM.set_osrs_h(humidity_oversampling) - - with self._bme280.CTRL_MEAS as CTRL_MEAS: - CTRL_MEAS.set_mode(mode) - CTRL_MEAS.set_osrs_t(temperature_oversampling) - CTRL_MEAS.set_osrs_p(pressure_oversampling) - CTRL_MEAS.write() - - with self._bme280.CONFIG as CONFIG: - CONFIG.set_t_sb(temperature_standby) - CONFIG.set_filter(2) - CONFIG.write() - - with self._bme280.CALIBRATION as CALIBRATION: - self.calibration.dig_t1 = CALIBRATION.get_dig_t1() - self.calibration.dig_t2 = CALIBRATION.get_dig_t2() - self.calibration.dig_t3 = CALIBRATION.get_dig_t3() - - self.calibration.dig_p1 = CALIBRATION.get_dig_p1() - self.calibration.dig_p2 = CALIBRATION.get_dig_p2() - self.calibration.dig_p3 = CALIBRATION.get_dig_p3() - self.calibration.dig_p4 = CALIBRATION.get_dig_p4() - self.calibration.dig_p5 = CALIBRATION.get_dig_p5() - self.calibration.dig_p6 = CALIBRATION.get_dig_p6() - self.calibration.dig_p7 = CALIBRATION.get_dig_p7() - self.calibration.dig_p8 = CALIBRATION.get_dig_p8() - self.calibration.dig_p9 = CALIBRATION.get_dig_p9() - - self.calibration.dig_h1 = CALIBRATION.get_dig_h1() - - with self._bme280.CALIBRATION2 as CALIBRATION: - self.calibration.dig_h2 = CALIBRATION.get_dig_h2() - self.calibration.dig_h3 = CALIBRATION.get_dig_h3() - self.calibration.dig_h4 = CALIBRATION.get_dig_h4() - self.calibration.dig_h5 = CALIBRATION.get_dig_h5() - self.calibration.dig_h6 = CALIBRATION.get_dig_h6() + self._bme280.set('CTRL_HUM', osrs_h=humidity_oversampling) + + self._bme280.set('CTRL_MEAS', + mode=mode, + osrs_t=temperature_oversampling, + osrs_p=pressure_oversampling) + + self._bme280.set('CONFIG', + t_sb=temperature_standby, + filter=2) + + self.calibration.set_from_namedtuple(self._bme280.get('CALIBRATION')) + self.calibration.set_from_namedtuple(self._bme280.get('CALIBRATION2')) def update_sensor(self): self.setup() if self._mode == "forced": - # Trigger a reading in forced mode and wait for result - self._bme280.CTRL_MEAS.set_mode("forced") - while self._bme280.STATUS.get_measuring(): + self._bme280.set('CTRL_MEAS', mode="forced") + while self._bme280.get('STATUS').measuring: time.sleep(0.001) - with self._bme280.DATA as DATA: - raw_temperature = DATA.get_temperature() - raw_pressure = DATA.get_pressure() - raw_humidity = DATA.get_humidity() + raw = self._bme280.get('DATA') - self.temperature = self.calibration.compensate_temperature(raw_temperature) - self.pressure = self.calibration.compensate_pressure(raw_pressure) / 100.0 - self.humidity = self.calibration.compensate_humidity(raw_humidity) + self.temperature = self.calibration.compensate_temperature(raw.temperature) + self.pressure = self.calibration.compensate_pressure(raw.pressure) / 100.0 + self.humidity = self.calibration.compensate_humidity(raw.humidity) def get_temperature(self): self.update_sensor() From 0a5bac48f369e59dcc78ab21102d6998b6a7208e Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 2 Sep 2019 16:23:43 +0100 Subject: [PATCH 2/5] Fixes --- library/bme280/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/bme280/__init__.py b/library/bme280/__init__.py index 7dbddb5..8bd35fb 100644 --- a/library/bme280/__init__.py +++ b/library/bme280/__init__.py @@ -75,11 +75,11 @@ def __init__(self): self.temperature_fine = 0 - def set_from_tuple(self, value): + def set_from_namedtuple(self, value): # Iterate through a tuple supplied by i2cdevice # and copy its values into the class attributes for key in value._asdict().keys(): - setattr(self, key, value.asdict()[key]) + setattr(self, key, value._asdict()[key]) def compensate_temperature(self, raw_temperature): var1 = (raw_temperature / 16384.0 - self.dig_t1 / 1024.0) * self.dig_t2 From 81d94c94da7d11a46e1ce7ebf8beed919f72b063 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 2 Sep 2019 17:04:31 +0100 Subject: [PATCH 3/5] Ported CHIP_ID check --- library/bme280/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/bme280/__init__.py b/library/bme280/__init__.py index 8bd35fb..e1a3fe7 100644 --- a/library/bme280/__init__.py +++ b/library/bme280/__init__.py @@ -210,9 +210,11 @@ def setup(self, mode='normal', temperature_oversampling=16, pressure_oversamplin if mode == "forced": mode = "sleep" + chip = self._bme280.get('CHIP_ID') + try: - if self._bme280.CHIP_ID.get_id() != CHIP_ID: - raise RuntimeError("Unable to find bme280 on 0x{:02x}, CHIP_ID returned {:02x}".format(self._i2c_addr, self._bme280.CHIP_ID.get_id())) + if chip.id != CHIP_ID: + raise RuntimeError("Unable to find bme280 on 0x{:02x}, CHIP_ID returned {:02x}".format(self._i2c_addr, chip.id)) except IOError: raise RuntimeError("Unable to find bme280 on 0x{:02x}, IOError".format(self._i2c_addr)) From 0e447f1480471a9f3b30c0b2015c61db30d22e7e Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 4 Sep 2019 16:35:31 +0100 Subject: [PATCH 4/5] Reversed set_from_namedtuple so it works in circuitpython --- library/bme280/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/bme280/__init__.py b/library/bme280/__init__.py index 7dbddb5..def8a2f 100644 --- a/library/bme280/__init__.py +++ b/library/bme280/__init__.py @@ -75,11 +75,14 @@ def __init__(self): self.temperature_fine = 0 - def set_from_tuple(self, value): + def set_from_namedtuple(self, value): # Iterate through a tuple supplied by i2cdevice # and copy its values into the class attributes - for key in value._asdict().keys(): - setattr(self, key, value.asdict()[key]) + for key in self.__dict__.keys(): + try: + setattr(self, key, getattr(value, key)) + except AttributeError: + pass def compensate_temperature(self, raw_temperature): var1 = (raw_temperature / 16384.0 - self.dig_t1 / 1024.0) * self.dig_t2 From 970052e28aa7166d615fb2c98be5b1b9a3246007 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 4 Sep 2019 16:45:52 +0100 Subject: [PATCH 5/5] Move i2c transaction back into try/catch for detect --- library/bme280/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/bme280/__init__.py b/library/bme280/__init__.py index 3b6b834..8508c9b 100644 --- a/library/bme280/__init__.py +++ b/library/bme280/__init__.py @@ -213,9 +213,8 @@ def setup(self, mode='normal', temperature_oversampling=16, pressure_oversamplin if mode == "forced": mode = "sleep" - chip = self._bme280.get('CHIP_ID') - try: + chip = self._bme280.get('CHIP_ID') if chip.id != CHIP_ID: raise RuntimeError("Unable to find bme280 on 0x{:02x}, CHIP_ID returned {:02x}".format(self._i2c_addr, chip.id)) except IOError: