Skip to main content

Bme280

Struct Bme280 

Source
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>

Source

pub fn i2c(bus: I2C, address: u8, delay: D) -> Self

Wraps an I2C bus and the part’s address.

§Arguments
§Returns

The driver, with every measurement at oversampling x1 and the filter off.

Source§

impl<SPI: SpiDevice, D> Bme280<SpiRegisters<SPI>, D>

Source

pub fn spi(device: SPI, delay: D) -> Self

Wraps an SPI device, the bus plus the part’s chip-select line.

§Arguments
  • device - the SPI device, in mode 0 or 3.
  • 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<B, D> Bme280<B, D>

Source

pub fn new(bus: B, delay: D) -> Self

Wraps any register bus.

§Arguments
  • bus - the part’s register map, over whichever bus reaches it.
  • delay - the timer that paces the reset and the conversions.
§Returns

The driver, with every measurement at oversampling x1 and the filter off.

Source

pub fn with_oversampling( self, temperature: Oversampling, pressure: Oversampling, humidity: Oversampling, ) -> Self

Sets the oversampling of each measurement; Skipped leaves one out.

The settings are written to the part by init.

§Arguments
  • temperature - temperature oversampling.
  • pressure - pressure oversampling.
  • humidity - humidity oversampling.
§Returns

The driver.

Source

pub fn with_filter(self, filter: Filter) -> Self

Sets the IIR filter that smooths pressure and temperature.

The setting is written to the part by init.

§Arguments
  • filter - the filter coefficient.
§Returns

The driver.

Source

pub fn calibration(&self) -> Option<&Calibration>

Returns the calibration read at initialization, if it has happened.

Source

pub fn release(self) -> (B, D)

Gives back the bus and the delay.

§Returns

The bus and delay the driver was built from.

Source§

impl<B: RegisterBus, D: DelayNs> Bme280<B, D>

Source

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.

Source

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.

Trait Implementations§

Source§

impl<B: Debug, D: Debug> Debug for Bme280<B, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B, D> Sensor for Bme280<B, D>
where B: RegisterBus + Send, B::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Measurement

The value produced by a single read, for example a temperature or a fix.
Source§

async fn read(&mut self) -> Result<Measurement>

Takes a single reading from the sensor. Read more
§

fn map<F, T>(self, select: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Reading) -> T,

Wraps this sensor so every reading passes through select first. Read more

Auto Trait Implementations§

§

impl<B, D> Freeze for Bme280<B, D>
where B: Freeze, D: Freeze,

§

impl<B, D> RefUnwindSafe for Bme280<B, D>

§

impl<B, D> Send for Bme280<B, D>
where B: Send, D: Send,

§

impl<B, D> Sync for Bme280<B, D>
where B: Sync, D: Sync,

§

impl<B, D> Unpin for Bme280<B, D>
where B: Unpin, D: Unpin,

§

impl<B, D> UnsafeUnpin for Bme280<B, D>
where B: UnsafeUnpin, D: UnsafeUnpin,

§

impl<B, D> UnwindSafe for Bme280<B, D>
where B: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.