Compare commits

..

No commits in common. "master" and "v0.8.0" have entirely different histories.

8 changed files with 117 additions and 91 deletions

View file

@ -4,8 +4,6 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples/read-sensor-data/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/examples/read-sensor-data/target" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

View file

@ -5,21 +5,19 @@ documentation = "https://github.com/dnutiu/bme680-rust"
license = "MIT"
name = "bme680"
repository = "https://github.com/dnutiu/bme680-rust"
version = "0.9.0"
version = "0.7.0"
edition = "2021"
[badges]
maintenance = { status = "passively-maintained" }
[dependencies]
bitflags = "2.6"
bitflags = "1.2"
embedded-hal = "=1.0.0"
log = "0.4"
serde = { version = "1.0", optional = true, default-features = false, features = ["derive"] }
anyhow = { version = "1.0" , default-features = false}
linux-embedded-hal = "0.4.0"
anyhow = "1.0.80"
[dev-dependencies]
env_logger = "0.11.5"
[target.'cfg(target_os = "linux")'.dependencies]
linux-embedded-hal = "0.4.0"
env_logger = "0.9"

View file

@ -10,18 +10,13 @@ To use this library, create a new project and add it as a dependency:
```toml
[dependencies]
bme680 = {git = "https://github.com/dnutiu/bme680-rust.git", version = "0.9.0"}
bme680 = {git = "https://github.com/dnutiu/bme680-rust.git", version = "0.8.0"}
```
# Getting started on Raspberry Pi
# Alternative
[drogue-bme680](https://github.com/drogue-iot/drogue-bme680)
Assuming that you have connected the sensor to the Raspberry PI's GPIO ports.
Install required libraries for developing I2C.
```bash
sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev
```
# Example getting started Linux
Determine the I2C device path
@ -45,17 +40,3 @@ pi@raspberrypi:~ $ i2cdetect -y 1
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76
```
The read-sensor-data from the examples folder is configured to use the I2C device `/dev/i2c-1` and the `0x76` address.
To run the example, clone the repository and go to the examples' folder:
```bash
git clone https://github.com/dnutiu/bme680-rust.git && cd bme680-rust/examples/read-sensor-data
```
Then run the example:
```bash
export RUST_LOG=info
cargo run
```

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -1,12 +0,0 @@
[package]
name = "read-sensor-data"
version = "0.1.0"
edition = "2021"
[dependencies]
bme680 = {path = "../../"}
env_logger = "0.11.5"
linux-embedded-hal = "0.4.0"
anyhow = { version = "1.0.80" }
log = "0.4"
embedded-hal = "=1.0.0"

View file

@ -1,3 +1,5 @@
#![no_std]
use bme680::i2c::Address;
use bme680::{Bme680, IIRFilterSize, OversamplingSetting, PowerMode, SettingsBuilder};
use core::time::Duration;
@ -28,7 +30,7 @@ fn main() -> Result<(), anyhow::Error> {
let profile_dur = dev.get_profile_duration(&settings.0)?;
info!("Profile duration {:?}", profile_dur);
info!("Setting sensor settings");
dev.set_sensor_settings(&mut delayer, &settings)?;
dev.set_sensor_settings(&mut delayer, settings)?;
info!("Setting forced power modes");
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;

View file

@ -4,44 +4,6 @@ use core::time::Duration;
/// Calculates values needed or returned by the BME80 Sensor.
pub struct Calculation {}
static GAS_LOOKUP_TABLE_1: [u32; 16] = [
2147483647u32,
2147483647u32,
2147483647u32,
2147483647u32,
2147483647u32,
2126008810u32,
2147483647u32,
2130303777u32,
2147483647u32,
2147483647u32,
2143188679u32,
2136746228u32,
2147483647u32,
2126008810u32,
2147483647u32,
2147483647u32,
];
static GAS_LOOKUP_TABLE_2: [u32; 16] = [
4096000000u32,
2048000000u32,
1024000000u32,
512000000u32,
255744255u32,
127110228u32,
64000000u32,
32258064u32,
16016016u32,
8000000u32,
4000000u32,
2000000u32,
1,
500000u32,
250000u32,
125000u32,
];
impl Calculation {
/// Calculates and returns the sensor's heater resistance.
/// * `calibration_data` - The calibration data of the sensor.
@ -179,8 +141,13 @@ impl Calculation {
let var4: i32 = (var4 + temp_scaled * calibration_data.par_h7 as i32 / 100i32) >> 4i32;
let var5: i32 = ((var3 >> 14i32) * (var3 >> 14i32)) >> 10i32;
let var6: i32 = (var4 * var5) >> 1i32;
let calc_hum: i32 = (((var3 + var6) >> 10i32) * 1000i32) >> 12i32;
calc_hum.clamp(0, 100000) as u32
let mut calc_hum: i32 = (((var3 + var6) >> 10i32) * 1000i32) >> 12i32;
if calc_hum > 100000i32 {
calc_hum = 100000i32;
} else if calc_hum < 0i32 {
calc_hum = 0i32;
}
calc_hum as u32
}
/// Calculates and returns the gas resistance.
@ -192,11 +159,47 @@ impl Calculation {
gas_resistance_adc: u16,
gas_range: u8,
) -> u32 {
let lookup_table1: [u32; 16] = [
2147483647u32,
2147483647u32,
2147483647u32,
2147483647u32,
2147483647u32,
2126008810u32,
2147483647u32,
2130303777u32,
2147483647u32,
2147483647u32,
2143188679u32,
2136746228u32,
2147483647u32,
2126008810u32,
2147483647u32,
2147483647u32,
];
let lookup_table2: [u32; 16] = [
4096000000u32,
2048000000u32,
1024000000u32,
512000000u32,
255744255u32,
127110228u32,
64000000u32,
32258064u32,
16016016u32,
8000000u32,
4000000u32,
2000000u32,
1,
500000u32,
250000u32,
125000u32,
];
let var1: i64 = ((1340 + 5 * calibration_data.range_sw_err as i64)
* GAS_LOOKUP_TABLE_1[gas_range as usize] as i64)
* lookup_table1[gas_range as usize] as i64)
>> 16;
let var2: u64 = (((gas_resistance_adc as i64) << 15) - 16777216 + var1) as u64;
let var3: i64 = (GAS_LOOKUP_TABLE_2[gas_range as usize] as i64 * var1) >> 9;
let var3: i64 = (lookup_table2[gas_range as usize] as i64 * var1) >> 9;
let calc_gas_res: u32 = ((var3 + ((var2 as i64) >> 1i64)) / var2 as i64) as u32;
calc_gas_res
}

View file

@ -3,6 +3,62 @@
//!
//! The library uses the embedded-hal crate to abstract reading and writing via I²C.
//! In the examples you can find a demo how to use the library in Linux using the linux-embedded-hal crate (e.g. on a RPI).
//! ```no_run
//! use bme680::{Bme680, IIRFilterSize, OversamplingSetting, PowerMode, SettingsBuilder};
//! use core::time::Duration;
//! use embedded_hal::delay::DelayNs;
//! use linux_embedded_hal as hal;
//! use linux_embedded_hal::Delay;
//! use log::info;
//! use bme680::i2c::Address;
//!
//! // Please export RUST_LOG=info in order to see logs in the console.
//! fn main() -> Result<(), anyhow::Error>
//! {
//! env_logger::init();
//!
//! let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
//! let mut delayer = Delay {};
//!
//! let mut dev = Bme680::init(i2c, &mut delayer, Address::Primary)?;
//! let mut delay = Delay {};
//!
//! let settings = SettingsBuilder::new()
//! .with_humidity_oversampling(OversamplingSetting::OS2x)
//! .with_pressure_oversampling(OversamplingSetting::OS4x)
//! .with_temperature_oversampling(OversamplingSetting::OS8x)
//! .with_temperature_filter(IIRFilterSize::Size3)
//! .with_gas_measurement(Duration::from_millis(1500), 320, 25)
//! .with_temperature_offset(-2.2)
//! .with_run_gas(true)
//! .build();
//!
//! let profile_dur = dev.get_profile_duration(&settings.0)?;
//! info!("Profile duration {:?}", profile_dur);
//! info!("Setting sensor settings");
//! dev.set_sensor_settings(&mut delayer, settings)?;
//! info!("Setting forced power modes");
//! dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
//!
//! let sensor_settings = dev.get_sensor_settings(settings.1);
//! info!("Sensor settings: {:?}", sensor_settings);
//!
//! loop {
//! let _ = delay.delay_ms(5000u32);
//! let power_mode = dev.get_sensor_mode();
//! info!("Sensor power mode: {:?}", power_mode);
//! info!("Setting forced power modes");
//! dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
//! info!("Retrieving sensor data");
//! let (data, _state) = dev.get_measurement(&mut delayer)?;
//! info!("Sensor Data {:?}", data);
//! info!("Temperature {}°C", data.temperature_celsius());
//! info!("Pressure {}hPa", data.pressure_hpa());
//! info!("Humidity {}%", data.humidity_percent());
//! info!("Gas Resistence {}Ω", data.gas_resistance_ohm());
//! }
//! }
#![no_std]
#![forbid(unsafe_code)]
@ -360,7 +416,7 @@ where
pub fn set_sensor_settings(
&mut self,
delay: &mut D,
settings: &Settings,
settings: Settings,
) -> Result<(), anyhow::Error> {
let (sensor_settings, desired_settings) = settings;
let tph_sett = sensor_settings.temperature_settings;

View file

@ -140,7 +140,7 @@ bitflags! {
/// To set NB conversion setting.
const NBCONV_SEL = 128;
/// To set all gas sensor related settings
const GAS_SENSOR_SEL = Self::GAS_MEAS_SEL.bits() | Self::RUN_GAS_SEL.bits() | Self::NBCONV_SEL.bits();
const GAS_SENSOR_SEL = Self::GAS_MEAS_SEL.bits | Self::RUN_GAS_SEL.bits | Self::NBCONV_SEL.bits;
}
}