kopia lustrzana https://github.com/robert-hh/BME280
bme280*.py: Use names for the power modes.
rodzic
a7074fd2d5
commit
3bc048f93e
|
@ -92,10 +92,10 @@ Copy `bme280_float.py` onto the board. Then:
|
||||||
# On pycom devuces that is P9 = SDA, P10 = scl
|
# On pycom devuces that is P9 = SDA, P10 = scl
|
||||||
#
|
#
|
||||||
import machine
|
import machine
|
||||||
import bme280_float
|
import bme280_float as bme280
|
||||||
|
|
||||||
i2c = machine.I2C()
|
i2c = machine.I2C()
|
||||||
bme = bme280_float.BME280(i2c=i2c)
|
bme = bme280.BME280(i2c=i2c)
|
||||||
|
|
||||||
print(bme.values)
|
print(bme.values)
|
||||||
```
|
```
|
||||||
|
|
|
@ -58,6 +58,9 @@ BME280_REGISTER_CONTROL_HUM = 0xF2
|
||||||
BME280_REGISTER_STATUS = 0xF3
|
BME280_REGISTER_STATUS = 0xF3
|
||||||
BME280_REGISTER_CONTROL = 0xF4
|
BME280_REGISTER_CONTROL = 0xF4
|
||||||
|
|
||||||
|
MODE_SLEEP = const(0)
|
||||||
|
MODE_FORCED = const(1)
|
||||||
|
MODE_NORMAL = const(3)
|
||||||
|
|
||||||
class BME280:
|
class BME280:
|
||||||
|
|
||||||
|
@ -95,15 +98,16 @@ class BME280:
|
||||||
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
||||||
self.dig_H5 //= 16
|
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
|
# temporary data holders which stay allocated
|
||||||
self._l1_barray = bytearray(1)
|
self._l1_barray = bytearray(1)
|
||||||
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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
|
self._l1_barray)
|
||||||
|
self.t_fine = 0
|
||||||
|
|
||||||
def read_raw_data(self, result):
|
def read_raw_data(self, result):
|
||||||
""" Reads the raw (uncompensated) data from the sensor.
|
""" Reads the raw (uncompensated) data from the sensor.
|
||||||
|
|
||||||
|
@ -117,7 +121,7 @@ class BME280:
|
||||||
self._l1_barray[0] = self._mode
|
self._l1_barray[0] = self._mode
|
||||||
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 | 1
|
self._l1_barray[0] = self._mode << 5 | self._mode << 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)
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,10 @@ BME280_REGISTER_CONTROL_HUM = 0xF2
|
||||||
BME280_REGISTER_STATUS = 0xF3
|
BME280_REGISTER_STATUS = 0xF3
|
||||||
BME280_REGISTER_CONTROL = 0xF4
|
BME280_REGISTER_CONTROL = 0xF4
|
||||||
|
|
||||||
|
MODE_SLEEP = const(0)
|
||||||
|
MODE_FORCED = const(1)
|
||||||
|
MODE_NORMAL = const(3)
|
||||||
|
|
||||||
|
|
||||||
class BME280:
|
class BME280:
|
||||||
|
|
||||||
|
@ -98,8 +102,6 @@ class BME280:
|
||||||
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
self.dig_H4 = (self.dig_H4 * 16) + (self.dig_H5 & 0xF)
|
||||||
self.dig_H5 //= 16
|
self.dig_H5 //= 16
|
||||||
|
|
||||||
self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
|
||||||
bytearray([0x3F]))
|
|
||||||
self.t_fine = 0
|
self.t_fine = 0
|
||||||
|
|
||||||
# temporary data holders which stay allocated
|
# temporary data holders which stay allocated
|
||||||
|
@ -107,6 +109,10 @@ 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.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL,
|
||||||
|
bytearray([0x3c | MODE_SLEEP]))
|
||||||
|
|
||||||
def read_raw_data(self, result):
|
def read_raw_data(self, result):
|
||||||
""" Reads the raw (uncompensated) data from the sensor.
|
""" Reads the raw (uncompensated) data from the sensor.
|
||||||
|
|
||||||
|
@ -120,7 +126,7 @@ class BME280:
|
||||||
self._l1_barray[0] = self._mode
|
self._l1_barray[0] = self._mode
|
||||||
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 | 1
|
self._l1_barray[0] = self._mode << 5 | self._mode << 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)
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,10 @@
|
||||||
#
|
#
|
||||||
from machine import I2C
|
from machine import I2C
|
||||||
from bme280 import *
|
from bme280 import *
|
||||||
|
from utime import sleep
|
||||||
i2c=I2C()
|
i2c=I2C()
|
||||||
bme280 = BME280(i2c=i2c)
|
bme280 = BME280(i2c=i2c)
|
||||||
bme280.values
|
while True:
|
||||||
|
print(bme280.values)
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue