bme280: Allow using a tuple for the mode parameter.

master
robert-hh 2022-09-06 14:30:02 +02:00
rodzic aaf2a4f271
commit 37a52e149e
4 zmienionych plików z 46 dodań i 28 usunięć

Wyświetl plik

@ -19,10 +19,11 @@ for details.
## Class
bme = BME280(i2c=i2c, mode=BME280_OSAMPLE_8, address=BME280_I2CADDR)
bme = BME280(mode=BME280_OSAMPLE_8, address=BME280_I2CADDR, i2c=i2c)
mode is the setting for oversampling of the humidity value, address the i2c
address used.
`mode` is the setting for oversampling of the humidity value. It must be either a single
int or a tuple of 3 ints, specifying (mode_hum, mode_temp, mode_pressure). `address` is the i2c
address used, and i2c must be a I2C object.
## Properties
@ -88,13 +89,14 @@ Copy `bme280_float.py` onto the board. Then:
``` python
#
# this script assumes the default connection of the I2C bus
# On pycom devuces that is P9 = SDA, P10 = scl
# this script for the rp2040 port assumes the I2C connections at
# GPIO8 and 9. At the RPi Pico, these are the board pins 11 and 12
# Please check that pull-up resistors are in place at sda and scl.
#
import machine
import machine, Pin
import bme280_float as bme280
i2c = machine.I2C()
i2c = machine.I2C(0, sda=machine.Pin(8), scl=machine.Pin(9))
bme = bme280.BME280(i2c=i2c)
print(bme.values)

Wyświetl plik

@ -72,13 +72,21 @@ class BME280:
i2c=None,
**kwargs):
# Check that mode is valid.
if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
raise ValueError(
'Unexpected mode value {0}. Set mode to one of '
'BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,'
'BME280_OSAMPLE_8, BME280_OSAMPLE_16'.format(mode))
self._mode = mode
if type(mode) is tuple and len(mode) == 3:
self._mode_hum, self._mode_temp, self._mode_press = mode
elif type(mode) == int:
self._mode_hum, self._mode_temp, self._mode_press = mode, mode, mode
else:
raise ValueError("Wrong type for the mode parameter, must be int or a 3 element tuple")
for mode in (self._mode_hum, self._mode_temp, self._mode_press):
if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
raise ValueError(
'Unexpected mode value {0}. Set mode to one of '
'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or '
'BME280_ULTRAHIGHRES'.format(mode))
self.address = address
if i2c is None:
raise ValueError('An I2C object is required.')
@ -105,7 +113,7 @@ 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._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_SLEEP
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
self._l1_barray)
self.t_fine = 0
@ -120,10 +128,10 @@ class BME280:
None
"""
self._l1_barray[0] = self._mode
self._l1_barray[0] = self._mode_hum
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM,
self._l1_barray)
self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_FORCED
self._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_FORCED
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
self._l1_barray)

Wyświetl plik

@ -73,13 +73,21 @@ class BME280:
i2c=None,
**kwargs):
# Check that mode is valid.
if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
raise ValueError(
'Unexpected mode value {0}. Set mode to one of '
'BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,'
'BME280_OSAMPLE_8, BME280_OSAMPLE_16'.format(mode))
self._mode = mode
if type(mode) is tuple and len(mode) == 3:
self._mode_hum, self._mode_temp, self._mode_press = mode
elif type(mode) == int:
self._mode_hum, self._mode_temp, self._mode_press = mode, mode, mode
else:
raise ValueError("Wrong type for the mode parameter, must be int or a 3 element tuple")
for mode in (self._mode_hum, self._mode_temp, self._mode_press):
if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
raise ValueError(
'Unexpected mode value {0}. Set mode to one of '
'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or '
'BME280_ULTRAHIGHRES'.format(mode))
self.address = address
if i2c is None:
raise ValueError('An I2C object is required.')
@ -107,7 +115,7 @@ 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._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_SLEEP
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
bytearray([0x3c | MODE_SLEEP]))
@ -121,10 +129,10 @@ class BME280:
None
"""
self._l1_barray[0] = self._mode
self._l1_barray[0] = self._mode_hum
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM,
self._l1_barray)
self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_FORCED
self._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_FORCED
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
self._l1_barray)

Wyświetl plik

@ -4,7 +4,7 @@
from machine import I2C
from bme280 import *
from utime import sleep
i2c=I2C()
i2c=I2C(0)
bme280 = BME280(i2c=i2c)
while True:
print(bme280.values)