Skip to main content

Bmp280

Struct Bmp280 

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

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 both measurements at oversampling x1 and the filter off.

Source§

impl<SPI: SpiDevice, D> Bmp280<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 both measurements at oversampling x1 and the filter off.

Source§

impl<B, D> Bmp280<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 both measurements at oversampling x1 and the filter off.

Source

pub fn with_oversampling( self, temperature: Oversampling, pressure: 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.
§Returns

The driver.

Source

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-bit filter[2:0] code, 0 for 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.

Source

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

Returns the trimming coefficients 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> Bmp280<B, D>

Source

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.

Source

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.

Trait Implementations§

Source§

impl<B: Debug, D: Debug> Debug for Bmp280<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 Bmp280<B, D>
where B: RegisterBus + Send, B::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Reading

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

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

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 Bmp280<B, D>
where B: Freeze, D: Freeze,

§

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

§

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

§

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

§

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

§

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

§

impl<B, D> UnwindSafe for Bmp280<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.