Struct bme280_rs::Bme280

source ·
pub struct Bme280<I2c, Delay> { /* private fields */ }
Expand description

Interface to BME280 sensor over I²C

use bme280_rs::{Bme280, Configuration, Oversampling, SensorMode};

let mut bme280 = Bme280::new(i2c, delay);

bme280.init()?;

let configuration = Configuration::default()
    .with_temperature_oversampling(Oversampling::Oversample1)
    .with_pressure_oversampling(Oversampling::Oversample1)
    .with_humidity_oversampling(Oversampling::Oversample1)
    .with_sensor_mode(SensorMode::Normal);
bme280.set_sampling_configuration(configuration)?;

if let Some(temperature) = bme280.read_temperature()? {
    println!("Temperature: {:?}", temperature);
} else {
    println!("Temperature reading was disabled");
}

Implementations§

source§

impl<I2C, D> Bme280<I2C, D>
where I2C: I2c, D: DelayNs,

source

pub fn new(i2c: I2C, delay: D) -> Self

Create a new sensor using an I²C interface and a delay function using the sensor’s default address DEFAULT_ADDRESS)

source

pub fn release(self) -> I2C

Release the I²C interface

source

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

Create a new sensor using an I²C interface and a delay function

source

pub fn init(&mut self) -> Result<(), I2C::Error>

Initialize the sensor

Send a soft-reset signal, obtain the calibration coefficients, and set default sampling configuration.

Note that the default sampling configuration disables measurement of temperature, pressure and humidity.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn chip_id(&mut self) -> Result<u8, I2C::Error>

Obtain the chip id

The chip id is always crate::constants::CHIP_ID, so this function can be used to validate that communication with the chip works fine.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn status(&mut self) -> Result<Status, I2C::Error>

Obtain the chip status

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn set_sampling_configuration( &mut self, configuration: Configuration ) -> Result<(), I2C::Error>

Set the sampling configuration

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn take_forced_measurement(&mut self) -> Result<bool, I2C::Error>

Take a forced measurement

When the chip is set to work in forced mode, it goes back to sleep after every measurement. It must be set again to forced mode in order to force a new measurement.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn read_sample(&mut self) -> Result<Sample, I2C::Error>

Read a sample of temperature, pressure and humidity

Measures that are disabled in the sampling configuration have value None.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn read_temperature(&mut self) -> Result<Option<Temperature>, I2C::Error>

Read a sample of temperature

If temperature is disabled in the sampling configuration, return None.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn read_pressure(&mut self) -> Result<Option<Pressure>, I2C::Error>

Read a sample of pressure

The temperature value, necessary to compute the compensated pressure value, is also read from the sensor.

If pressure is disabled in the sampling configuration, return None.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn read_pressure_with_temperature( &mut self, temperature: f32 ) -> Result<Option<Pressure>, I2C::Error>

Read a sample of pressure with a user-specified temperature value

Unlike in Self::read_pressure(), this function does not take the temperature from the sensor, so it can be used if the temperature is already known.

If pressure is disabled in the sampling configuration, return None.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn read_humidity(&mut self) -> Result<Option<Humidity>, I2C::Error>

Read a sample of humidity

The temperature value, necessary to compute the compensated pressure value, is also read from the sensor.

If humidity is disabled in the sampling configuration, return None.

§Errors

Return an error if it cannot communicate with the sensor.

source

pub fn read_humidity_with_temperature( &mut self, temperature: f32 ) -> Result<Option<Humidity>, I2C::Error>

Read a sample of humidity with a user-specified temperature value

Unlike in Self::read_humidity(), this function does not take the temperature from the sensor, so it can be used if the temperature is already known.

If humidity is disabled in the sampling configuration, return None.

§Errors

Return an error if it cannot communicate with the sensor.

Auto Trait Implementations§

§

impl<I2c, Delay> RefUnwindSafe for Bme280<I2c, Delay>
where Delay: RefUnwindSafe, I2c: RefUnwindSafe,

§

impl<I2c, Delay> Send for Bme280<I2c, Delay>
where Delay: Send, I2c: Send,

§

impl<I2c, Delay> Sync for Bme280<I2c, Delay>
where Delay: Sync, I2c: Sync,

§

impl<I2c, Delay> Unpin for Bme280<I2c, Delay>
where Delay: Unpin, I2c: Unpin,

§

impl<I2c, Delay> UnwindSafe for Bme280<I2c, Delay>
where Delay: UnwindSafe, I2c: 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>,

§

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

§

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.