From 3bc048f93e0d155264d212dea16f589607511ae2 Mon Sep 17 00:00:00 2001 From: robert Date: Thu, 19 Dec 2019 15:49:15 +0100 Subject: [PATCH] bme280*.py: Use names for the power modes. --- README.md | 4 ++-- bme280_float.py | 14 +++++++++----- bme280_int.py | 12 +++++++++--- bmetest.py | 5 ++++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cacd8b3..bf3b9bf 100644 --- a/README.md +++ b/README.md @@ -92,10 +92,10 @@ Copy `bme280_float.py` onto the board. Then: # On pycom devuces that is P9 = SDA, P10 = scl # import machine -import bme280_float +import bme280_float as bme280 i2c = machine.I2C() -bme = bme280_float.BME280(i2c=i2c) +bme = bme280.BME280(i2c=i2c) print(bme.values) ``` diff --git a/bme280_float.py b/bme280_float.py index f96b7d0..58e02e6 100644 --- a/bme280_float.py +++ b/bme280_float.py @@ -58,6 +58,9 @@ BME280_REGISTER_CONTROL_HUM = 0xF2 BME280_REGISTER_STATUS = 0xF3 BME280_REGISTER_CONTROL = 0xF4 +MODE_SLEEP = const(0) +MODE_FORCED = const(1) +MODE_NORMAL = const(3) class BME280: @@ -95,15 +98,16 @@ class BME280: self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF) self.dig_H5 //= 16 - self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, - bytearray([0x3F])) - self.t_fine = 0 - # temporary data holders which stay allocated self._l1_barray = bytearray(1) self._l8_barray = bytearray(8) self._l3_resultarray = array("i", [0, 0, 0]) + self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_SLEEP + self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, + self._l1_barray) + self.t_fine = 0 + def read_raw_data(self, result): """ Reads the raw (uncompensated) data from the sensor. @@ -117,7 +121,7 @@ class BME280: self._l1_barray[0] = self._mode self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM, self._l1_barray) - self._l1_barray[0] = self._mode << 5 | self._mode << 2 | 1 + self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_FORCED self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, self._l1_barray) diff --git a/bme280_int.py b/bme280_int.py index bc7b4e8..b26772c 100644 --- a/bme280_int.py +++ b/bme280_int.py @@ -62,6 +62,10 @@ BME280_REGISTER_CONTROL_HUM = 0xF2 BME280_REGISTER_STATUS = 0xF3 BME280_REGISTER_CONTROL = 0xF4 +MODE_SLEEP = const(0) +MODE_FORCED = const(1) +MODE_NORMAL = const(3) + class BME280: @@ -98,8 +102,6 @@ class BME280: self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF) self.dig_H5 //= 16 - self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, - bytearray([0x3F])) self.t_fine = 0 # temporary data holders which stay allocated @@ -107,6 +109,10 @@ class BME280: self._l8_barray = bytearray(8) self._l3_resultarray = array("i", [0, 0, 0]) + self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_SLEEP + self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, + bytearray([0x3c | MODE_SLEEP])) + def read_raw_data(self, result): """ Reads the raw (uncompensated) data from the sensor. @@ -120,7 +126,7 @@ class BME280: self._l1_barray[0] = self._mode self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM, self._l1_barray) - self._l1_barray[0] = self._mode << 5 | self._mode << 2 | 1 + self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_FORCED self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, self._l1_barray) diff --git a/bmetest.py b/bmetest.py index 1756555..bb74040 100644 --- a/bmetest.py +++ b/bmetest.py @@ -3,7 +3,10 @@ # from machine import I2C from bme280 import * +from utime import sleep i2c=I2C() bme280 = BME280(i2c=i2c) -bme280.values +while True: + print(bme280.values) + sleep(1)