kopia lustrzana https://github.com/robert-hh/BME280
bme280: Allow using a tuple for the mode parameter.
rodzic
aaf2a4f271
commit
37a52e149e
16
README.md
16
README.md
|
@ -19,10 +19,11 @@ for details.
|
||||||
|
|
||||||
## Class
|
## 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
|
`mode` is the setting for oversampling of the humidity value. It must be either a single
|
||||||
address used.
|
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
|
## Properties
|
||||||
|
|
||||||
|
@ -88,13 +89,14 @@ Copy `bme280_float.py` onto the board. Then:
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
#
|
#
|
||||||
# this script assumes the default connection of the I2C bus
|
# this script for the rp2040 port assumes the I2C connections at
|
||||||
# On pycom devuces that is P9 = SDA, P10 = scl
|
# 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
|
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)
|
bme = bme280.BME280(i2c=i2c)
|
||||||
|
|
||||||
print(bme.values)
|
print(bme.values)
|
||||||
|
|
|
@ -72,13 +72,21 @@ class BME280:
|
||||||
i2c=None,
|
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 type(mode) is tuple and len(mode) == 3:
|
||||||
BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
|
self._mode_hum, self._mode_temp, self._mode_press = mode
|
||||||
raise ValueError(
|
elif type(mode) == int:
|
||||||
'Unexpected mode value {0}. Set mode to one of '
|
self._mode_hum, self._mode_temp, self._mode_press = mode, mode, mode
|
||||||
'BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,'
|
else:
|
||||||
'BME280_OSAMPLE_8, BME280_OSAMPLE_16'.format(mode))
|
raise ValueError("Wrong type for the mode parameter, must be int or a 3 element tuple")
|
||||||
self._mode = mode
|
|
||||||
|
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
|
self.address = address
|
||||||
if i2c is None:
|
if i2c is None:
|
||||||
raise ValueError('An I2C object is required.')
|
raise ValueError('An I2C object is required.')
|
||||||
|
@ -105,7 +113,7 @@ class BME280:
|
||||||
self._l8_barray = bytearray(8)
|
self._l8_barray = bytearray(8)
|
||||||
self._l3_resultarray = array("i", [0, 0, 0])
|
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
self._l1_barray)
|
self._l1_barray)
|
||||||
self.t_fine = 0
|
self.t_fine = 0
|
||||||
|
@ -120,10 +128,10 @@ class BME280:
|
||||||
None
|
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM,
|
||||||
self._l1_barray)
|
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
self._l1_barray)
|
self._l1_barray)
|
||||||
|
|
||||||
|
|
|
@ -73,13 +73,21 @@ class BME280:
|
||||||
i2c=None,
|
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 type(mode) is tuple and len(mode) == 3:
|
||||||
BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
|
self._mode_hum, self._mode_temp, self._mode_press = mode
|
||||||
raise ValueError(
|
elif type(mode) == int:
|
||||||
'Unexpected mode value {0}. Set mode to one of '
|
self._mode_hum, self._mode_temp, self._mode_press = mode, mode, mode
|
||||||
'BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,'
|
else:
|
||||||
'BME280_OSAMPLE_8, BME280_OSAMPLE_16'.format(mode))
|
raise ValueError("Wrong type for the mode parameter, must be int or a 3 element tuple")
|
||||||
self._mode = mode
|
|
||||||
|
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
|
self.address = address
|
||||||
if i2c is None:
|
if i2c is None:
|
||||||
raise ValueError('An I2C object is required.')
|
raise ValueError('An I2C object is required.')
|
||||||
|
@ -107,7 +115,7 @@ class BME280:
|
||||||
self._l8_barray = bytearray(8)
|
self._l8_barray = bytearray(8)
|
||||||
self._l3_resultarray = array("i", [0, 0, 0])
|
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
bytearray([0x3c | MODE_SLEEP]))
|
bytearray([0x3c | MODE_SLEEP]))
|
||||||
|
|
||||||
|
@ -121,10 +129,10 @@ class BME280:
|
||||||
None
|
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM,
|
||||||
self._l1_barray)
|
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
self._l1_barray)
|
self._l1_barray)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
from machine import I2C
|
from machine import I2C
|
||||||
from bme280 import *
|
from bme280 import *
|
||||||
from utime import sleep
|
from utime import sleep
|
||||||
i2c=I2C()
|
i2c=I2C(0)
|
||||||
bme280 = BME280(i2c=i2c)
|
bme280 = BME280(i2c=i2c)
|
||||||
while True:
|
while True:
|
||||||
print(bme280.values)
|
print(bme280.values)
|
||||||
|
|
Ładowanie…
Reference in New Issue