pub struct Bme280<B, D> { /* private fields */ }Expand description
A BME280 on an I2C or SPI bus, measuring on demand in forced mode.
Bme280::i2c or Bme280::spi wraps the bus. init resets the
part, checks its chip id, reads its calibration, and writes the oversampling and
filter settings, in the order the datasheet requires (ctrl_hum takes effect only
after the following ctrl_meas write). measure runs one
forced-mode conversion, waits the datasheet’s maximum measurement time for the
settings in use, confirms the part is idle, and returns the compensated
Measurement. As a [Sensor] its reading is the whole measurement; one channel
is selected with [Sensor::map].
§Examples
A weather station’s reading, taken from a BME280 that is not plugged in yet: the simulated part holds 18.5 °C, 1013.2 hPa, and 55 % relative humidity, and the driver compensates its raw readings with the calibration the part carries.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::bme280::{sim, Bme280, I2C_ADDRESS_PRIMARY};
let part = sim::reporting(I2C_ADDRESS_PRIMARY, 18.5, 1013.2, 55.0);
let mut station = Bme280::i2c(part, I2C_ADDRESS_PRIMARY, DelayLog::new());
let weather = block_on(station.read())?;
assert!((weather.celsius() - 18.5).abs() < 0.01);
assert!((weather.hectopascals() - 1013.2).abs() < 0.1);
assert!((weather.relative_humidity_percent() - 55.0).abs() < 0.1);Implementations§
Source§impl<I2C: I2c, D> Bme280<I2cRegisters<I2C>, D>
impl<I2C: I2c, D> Bme280<I2cRegisters<I2C>, D>
Sourcepub fn i2c(bus: I2C, address: u8, delay: D) -> Self
pub fn i2c(bus: I2C, address: u8, delay: D) -> Self
Wraps an I2C bus and the part’s address.
§Arguments
bus- the I2C bus the part is on.address-I2C_ADDRESS_PRIMARYwith SDO low,I2C_ADDRESS_SECONDARYwith SDO high.delay- the timer that paces the reset and the conversions.
§Returns
The driver, with every measurement at oversampling x1 and the filter off.
Source§impl<SPI: SpiDevice, D> Bme280<SpiRegisters<SPI>, D>
impl<SPI: SpiDevice, D> Bme280<SpiRegisters<SPI>, D>
Source§impl<B, D> Bme280<B, D>
impl<B, D> Bme280<B, D>
Sourcepub fn with_oversampling(
self,
temperature: Oversampling,
pressure: Oversampling,
humidity: Oversampling,
) -> Self
pub fn with_oversampling( self, temperature: Oversampling, pressure: Oversampling, humidity: Oversampling, ) -> Self
Sourcepub fn with_filter(self, filter: Filter) -> Self
pub fn with_filter(self, filter: Filter) -> Self
Sourcepub fn calibration(&self) -> Option<&Calibration>
pub fn calibration(&self) -> Option<&Calibration>
Returns the calibration read at initialization, if it has happened.
Source§impl<B: RegisterBus, D: DelayNs> Bme280<B, D>
impl<B: RegisterBus, D: DelayNs> Bme280<B, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<B::Error>>
pub fn init(&mut self) -> Result<(), DriverError<B::Error>>
Resets the part, checks its identity, reads its calibration, and writes the settings.
The sequence is the datasheet’s: the soft-reset word, the start-up time, a wait
for the calibration image to finish loading, the chip id, the two calibration
blocks, then config, ctrl_hum, and ctrl_meas in that order, leaving the
part asleep.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if the
calibration image never finishes loading, and
DriverError::Sensor with SensorError::Identity if the chip id is not a
BME280’s.
Sourcepub fn measure(&mut self) -> Result<Measurement, DriverError<B::Error>>
pub fn measure(&mut self) -> Result<Measurement, DriverError<B::Error>>
Runs one forced-mode conversion and returns the compensated measurement.
Initializes the part first if init has not run. The wait is
the datasheet’s maximum measurement time for the oversampling in use, after
which the status register is polled until the part reports idle.
§Returns
The compensated measurement.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if the
part is still measuring after the polls, or any error of
init.