9 unstable releases (4 breaking)

0.5.0+1.4.9.2 Feb 12, 2023
0.4.1 Feb 12, 2023
0.3.1 Nov 9, 2021
0.3.0 May 15, 2021
0.1.2 May 4, 2021

#1490 in Embedded development

MIT/Apache

64KB
1K SLoC

bsec crate

Rust API to the Bosch BSEC library.

This readme will use bsec to refer to this crate, while Bosch BSEC is used to refer to the original BSEC library provided by Bosch.

Important license information

The Bosch BSEC library is proprietary. Thus, the Bosch BSEC library and its documentation cannot be included in the bsec Rust crate and need to be obtained separately.

While the bsec documentation covers the Rust crate itself, you will likely have to refer to the Bosch BSEC documentation at some points to get a full understanding.

You are responsible for adhering to the Bosch BSEC lincese terms in your products, despite the Rust API in this crate being published under a permissive license.

Features

  • Safe Rust API bindings to the Bosch BSEC library.
  • Implementation to use it with the BME680 sensor.
  • Extensible to other sensors.
  • Rudimentary fake sensor implementation to use in unit tests.

Documentation

The crate documentation can be found on docs.rs.

Getting started

See the crate documentation.

Usage example

use bsec::{Bsec, Input, InputKind, OutputKind, clock::Clock, SampleRate, SubscriptionRequest};
use nb::block;
use std::time::Duration;

// Acquire handle to the BSEC library.
// Only one such handle can be acquired at any time.
let mut bsec: Bsec<_, TimePassed, _> = Bsec::init(sensor, &clock)?;

// Configure the outputs you want to subscribe to.
bsec.update_subscription(&[
    SubscriptionRequest {
        sample_rate: SampleRate::Lp,
        sensor: OutputKind::Iaq,
    },
])?;

// We need to feed BSEC regularly with new measurements.
loop {
    // Wait for when the next measurement is due.
    sleep_for(Duration::from_nanos((bsec.next_measurement() - clock.timestamp_ns()) as u64));

    // Start the measurement.
    let wait_duration = block!(bsec.start_next_measurement())?;
    sleep_for(wait_duration);
    # clock.advance_by(wait_duration);

    // Process the measurement when ready and print the BSEC outputs.
    let outputs = block!(bsec.process_last_measurement())?;
    for output in &outputs {
        println!("{:?}: {}", output.sensor, output.signal);
    }
}

Dependencies

~0.1–2MB
~41K SLoC