pub struct Bmp280<B, D> { /* private fields */ }Expand description
A BMP280 on an I2C or SPI bus, measuring on demand in forced mode.
Bmp280::i2c or Bmp280::spi wraps the bus. init resets the
part, checks its chip id, reads its trimming coefficients, and writes the
oversampling and filter settings while the part is asleep, the one state in which
the datasheet promises a config write is not ignored. 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
Reading. As a [Sensor] its reading is the whole measurement; one channel is
selected with [Sensor::map].
§Examples
A drone’s barometer, read from a BMP280 that is not plugged in yet: the simulated part holds 25 °C and 1006.5 hPa, and the driver compensates its raw readings with the trimming the part carries.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::bmp280::{sim, Bmp280, I2C_ADDRESS_PRIMARY};
let part = sim::reporting(I2C_ADDRESS_PRIMARY, 25.0, 1006.5);
let mut barometer = Bmp280::i2c(part, I2C_ADDRESS_PRIMARY, DelayLog::new());
let reading = block_on(barometer.read())?;
assert!((reading.celsius() - 25.0).abs() < 0.01);
assert!((reading.hectopascals() - 1006.5).abs() < 0.1);Implementations§
Source§impl<I2C: I2c, D> Bmp280<I2cRegisters<I2C>, D>
impl<I2C: I2c, D> Bmp280<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 to ground,I2C_ADDRESS_SECONDARYwith SDO to VDDIO.delay- the timer that paces the reset and the conversions.
§Returns
The driver, with both measurements at oversampling x1 and the filter off.
Source§impl<SPI: SpiDevice, D> Bmp280<SpiRegisters<SPI>, D>
impl<SPI: SpiDevice, D> Bmp280<SpiRegisters<SPI>, D>
Source§impl<B, D> Bmp280<B, D>
impl<B, D> Bmp280<B, D>
Sourcepub fn with_oversampling(
self,
temperature: Oversampling,
pressure: Oversampling,
) -> Self
pub fn with_oversampling( self, temperature: Oversampling, pressure: Oversampling, ) -> Self
Sourcepub fn with_filter(self, code: u8) -> Self
pub fn with_filter(self, code: u8) -> Self
Sets the IIR filter that smooths pressure and temperature.
The setting is written to the part by init.
§Arguments
code- the raw three-bitfilter[2:0]code,0for the filter off. The datasheet lists the coefficients without publishing which code selects which, so the code is written as given; only its low three bits are used.
§Returns
The driver.
Sourcepub fn calibration(&self) -> Option<&Calibration>
pub fn calibration(&self) -> Option<&Calibration>
Returns the trimming coefficients read at initialization, if it has happened.
Source§impl<B: RegisterBus, D: DelayNs> Bmp280<B, D>
impl<B: RegisterBus, D: DelayNs> Bmp280<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 trimming coefficients, and writes the settings.
The sequence is the datasheet’s: the soft-reset word of section 4.3.2, which
runs the complete power-on-reset procedure, the start-up time of Table 2, a
wait for the im_update flag of Table 19 to clear, the chip id of section
4.3.1, the trimming block of Table 17, then config and ctrl_meas (sections
4.3.5 and 4.3.4) written while the part is asleep, leaving it asleep.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if the
trimming image never finishes loading, and DriverError::Sensor with
SensorError::Identity if the chip id is not the 0x58 section 4.3.1 fixes
for a BMP280; the datasheet names no other value.
Sourcepub fn measure(&mut self) -> Result<Reading, DriverError<B::Error>>
pub fn measure(&mut self) -> Result<Reading, DriverError<B::Error>>
Runs one forced-mode conversion and returns the compensated reading.
Initializes the part first if init has not run. The sequence
is the datasheet’s forced mode (section 3.6.2): ctrl_meas with the forced
code of Table 10, a wait of the maximum measurement time of section 3.8.1,
which is how section 3.9 asks forced-mode readout to be timed, the measuring
flag of Table 19 polled until it clears, then the single burst read of 0xF7 to
0xFC that section 3.9 prescribes.
§Returns
The compensated reading.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if the
part is still measuring after the polls, or any error of
init.