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>
impl<I2C, D> Bme280<I2C, D>
sourcepub fn new(i2c: I2C, delay: D) -> Self
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)
sourcepub fn new_with_address(i2c: I2C, address: u8, delay: D) -> Self
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
sourcepub fn init(&mut self) -> Result<(), I2C::Error>
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.
sourcepub fn chip_id(&mut self) -> Result<u8, I2C::Error>
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.
sourcepub fn set_sampling_configuration(
&mut self,
configuration: Configuration
) -> Result<(), I2C::Error>
pub fn set_sampling_configuration( &mut self, configuration: Configuration ) -> Result<(), I2C::Error>
sourcepub fn take_forced_measurement(&mut self) -> Result<bool, I2C::Error>
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.
sourcepub fn read_sample(&mut self) -> Result<Sample, I2C::Error>
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.
sourcepub fn read_temperature(&mut self) -> Result<Option<Temperature>, I2C::Error>
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.
sourcepub fn read_pressure(&mut self) -> Result<Option<Pressure>, I2C::Error>
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.
sourcepub fn read_pressure_with_temperature(
&mut self,
temperature: f32
) -> Result<Option<Pressure>, I2C::Error>
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.
sourcepub fn read_humidity(&mut self) -> Result<Option<Humidity>, I2C::Error>
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.
sourcepub fn read_humidity_with_temperature(
&mut self,
temperature: f32
) -> Result<Option<Humidity>, I2C::Error>
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.