Migrate to futures-0.3 and tokio-0.2
This commit is contained in:
parent
900688f5cc
commit
ad8d0798ed
5 changed files with 39 additions and 34 deletions
16
Cargo.toml
16
Cargo.toml
|
@ -1,21 +1,23 @@
|
||||||
[package]
|
[package]
|
||||||
name = "bme680"
|
|
||||||
version = "0.4.3"
|
|
||||||
authors = ["marcelbuesing <buesing.marcel@googlemail.com>"]
|
authors = ["marcelbuesing <buesing.marcel@googlemail.com>"]
|
||||||
description = "A pure Rust implementation for the BME680 environmental sensor."
|
description = "A pure Rust implementation for the BME680 environmental sensor."
|
||||||
repository = "https://github.com/marcelbuesing/bme680-hal"
|
|
||||||
documentation = "https://docs.rs/bme680"
|
documentation = "https://docs.rs/bme680"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
name = "bme680"
|
||||||
|
repository = "https://github.com/marcelbuesing/bme680-hal"
|
||||||
|
version = "0.4.3"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "1.0"
|
bitflags = "1.2"
|
||||||
embedded-hal = "0.2"
|
embedded-hal = "0.2"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
env_logger = "0.6"
|
env_logger = "0.7"
|
||||||
futures = "0.1"
|
futures = { version = "0.3", features = ["compat"] }
|
||||||
|
futures-timer = "2.0"
|
||||||
i2cdev = "0.4"
|
i2cdev = "0.4"
|
||||||
influent = "0.5"
|
influent = "0.5"
|
||||||
linux-embedded-hal = "0.2"
|
linux-embedded-hal = "0.2"
|
||||||
tokio = "0.1"
|
tokio = {version = "0.2", features = ["full"] }
|
||||||
|
|
|
@ -10,24 +10,25 @@ extern crate influent;
|
||||||
extern crate linux_embedded_hal;
|
extern crate linux_embedded_hal;
|
||||||
extern crate tokio;
|
extern crate tokio;
|
||||||
|
|
||||||
|
use crate::futures::compat::Future01CompatExt;
|
||||||
use bme680::{
|
use bme680::{
|
||||||
Bme680, FieldDataCondition, I2CAddress, IIRFilterSize, OversamplingSetting, PowerMode,
|
Bme680, FieldDataCondition, I2CAddress, IIRFilterSize, OversamplingSetting, PowerMode,
|
||||||
SettingsBuilder,
|
SettingsBuilder,
|
||||||
};
|
};
|
||||||
use futures::Future;
|
use futures::prelude::*;
|
||||||
use linux_embedded_hal::*;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use influent::client::{Client, ClientError, Credentials};
|
use influent::client::{Client, ClientError, Credentials};
|
||||||
use influent::create_client;
|
use influent::create_client;
|
||||||
use influent::measurement::{Measurement, Value};
|
use influent::measurement::{Measurement, Value};
|
||||||
|
use linux_embedded_hal::*;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
const INFLUX_ADDRESS: &str = "http://127.0.0.1:8086";
|
const INFLUX_ADDRESS: &str = "http://127.0.0.1:8086";
|
||||||
const INFLUX_USER: &str = "user";
|
const INFLUX_USER: &str = "user";
|
||||||
const INFLUX_PASSWORD: &str = "pass";
|
const INFLUX_PASSWORD: &str = "pass";
|
||||||
const INFLUX_DATABASE: &str = "influxdb";
|
const INFLUX_DATABASE: &str = "influxdb";
|
||||||
|
|
||||||
fn main() -> Result<(), ()> {
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), ()> {
|
||||||
// Init device
|
// Init device
|
||||||
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
||||||
let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)
|
let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)
|
||||||
|
@ -88,28 +89,25 @@ fn main() -> Result<(), ()> {
|
||||||
Value::Float(data.gas_resistance_ohm() as f64),
|
Value::Float(data.gas_resistance_ohm() as f64),
|
||||||
);
|
);
|
||||||
|
|
||||||
let f = temperature_f
|
if let Err(e) = future::try_join4(temperature_f, pressure_f, humidity_f, gas_f).await {
|
||||||
.join4(pressure_f, humidity_f, gas_f)
|
eprintln!("Error: {:?}", e);
|
||||||
.map(|_| ())
|
}
|
||||||
.map_err(|e| eprintln!("Error: {:?}", e));
|
|
||||||
|
|
||||||
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
|
|
||||||
return rt.block_on(f);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a measured value to the influx database
|
/// Sends a measured value to the influx database
|
||||||
fn send_value(
|
async fn send_value<'a>(
|
||||||
client: &Client,
|
client: &dyn Client,
|
||||||
type_name: &str,
|
type_name: &str,
|
||||||
value: Value,
|
value: Value<'a>,
|
||||||
) -> impl Future<Item = (), Error = ClientError> {
|
) -> Result<(), ClientError> {
|
||||||
let mut measurement = Measurement::new("sensor");
|
let mut measurement = Measurement::new("sensor");
|
||||||
measurement.add_field("value", value);
|
measurement.add_field("value", value);
|
||||||
measurement.add_tag("id", "MAC");
|
measurement.add_tag("id", "MAC");
|
||||||
measurement.add_tag("name", "bme680");
|
measurement.add_tag("name", "bme680");
|
||||||
measurement.add_tag("type", type_name);
|
measurement.add_tag("type", type_name);
|
||||||
|
|
||||||
client.write_one(measurement, None)
|
client.write_one(measurement, None).compat().await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,12 @@ extern crate linux_embedded_hal as hal;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
|
use crate::hal::*;
|
||||||
use bme680::*;
|
use bme680::*;
|
||||||
use core::result;
|
use core::result;
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use embedded_hal::blocking::delay::DelayMs;
|
use embedded_hal::blocking::delay::DelayMs;
|
||||||
use embedded_hal::blocking::i2c;
|
use embedded_hal::blocking::i2c;
|
||||||
use hal::*;
|
|
||||||
|
|
||||||
fn main(
|
fn main(
|
||||||
) -> result::Result<(), Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>>
|
) -> result::Result<(), Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>>
|
||||||
|
|
14
src/calc.rs
14
src/calc.rs
|
@ -1,5 +1,5 @@
|
||||||
|
use crate::CalibData;
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use CalibData;
|
|
||||||
|
|
||||||
pub struct Calc {}
|
pub struct Calc {}
|
||||||
|
|
||||||
|
@ -46,22 +46,26 @@ impl Calc {
|
||||||
/// * `temp_adc`
|
/// * `temp_adc`
|
||||||
/// * `temp_offset` - If set, the temperature t_fine will be increased by given
|
/// * `temp_offset` - If set, the temperature t_fine will be increased by given
|
||||||
/// value in celsius. Temperature offset in Celsius, e.g. 4, -8, 1.25
|
/// value in celsius. Temperature offset in Celsius, e.g. 4, -8, 1.25
|
||||||
pub fn calc_temperature(calib: &CalibData, temp_adc: u32, temp_offset: Option<f32>) -> (i16, i32) {
|
pub fn calc_temperature(
|
||||||
|
calib: &CalibData,
|
||||||
|
temp_adc: u32,
|
||||||
|
temp_offset: Option<f32>,
|
||||||
|
) -> (i16, i32) {
|
||||||
let var1: i64 = (temp_adc as (i64) >> 3) - ((calib.par_t1 as (i64)) << 1);
|
let var1: i64 = (temp_adc as (i64) >> 3) - ((calib.par_t1 as (i64)) << 1);
|
||||||
let var2: i64 = (var1 * (calib.par_t2 as i64)) >> 11;
|
let var2: i64 = (var1 * (calib.par_t2 as i64)) >> 11;
|
||||||
let var3: i64 = ((var1 >> 1) * (var1 >> 1)) >> 12;
|
let var3: i64 = ((var1 >> 1) * (var1 >> 1)) >> 12;
|
||||||
let var3: i64 = (var3 * ((calib.par_t3 as i64) << 4)) >> 14;
|
let var3: i64 = (var3 * ((calib.par_t3 as i64) << 4)) >> 14;
|
||||||
|
|
||||||
let temp_offset = match temp_offset {
|
let temp_offset = match temp_offset {
|
||||||
None => 0i32,
|
None => 0i32,
|
||||||
Some(offset) if offset == 0.0 => 0i32,
|
Some(offset) if offset == 0.0 => 0i32,
|
||||||
Some(offset) => {
|
Some(offset) => {
|
||||||
let signum: i32 = if offset.gt(&0.0) { 1 } else { -1 };
|
let signum: i32 = if offset.gt(&0.0) { 1 } else { -1 };
|
||||||
signum * (((((offset * 100.0) as i32).abs() << 8) - 128) / 5)
|
signum * (((((offset * 100.0) as i32).abs() << 8) - 128) / 5)
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let t_fine: i32 = (var2 + var3) as i32 + temp_offset;
|
let t_fine: i32 = (var2 + var3) as i32 + temp_offset;
|
||||||
let calc_temp: i16 = (((t_fine * 5) + 128) >> 8) as i16;
|
let calc_temp: i16 = (((t_fine * 5) + 128) >> 8) as i16;
|
||||||
(calc_temp, t_fine)
|
(calc_temp, t_fine)
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,12 +59,12 @@ pub use self::settings::{
|
||||||
mod calc;
|
mod calc;
|
||||||
mod settings;
|
mod settings;
|
||||||
|
|
||||||
use calc::Calc;
|
use crate::calc::Calc;
|
||||||
|
|
||||||
|
use crate::hal::blocking::delay::DelayMs;
|
||||||
|
use crate::hal::blocking::i2c::{Read, Write};
|
||||||
use core::result;
|
use core::result;
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use hal::blocking::delay::DelayMs;
|
|
||||||
use hal::blocking::i2c::{Read, Write};
|
|
||||||
|
|
||||||
/// BME680 General config
|
/// BME680 General config
|
||||||
pub const BME680_POLL_PERIOD_MS: u8 = 10;
|
pub const BME680_POLL_PERIOD_MS: u8 = 10;
|
||||||
|
@ -935,7 +935,8 @@ where
|
||||||
data.status = data.status | buff[14] & BME680_HEAT_STAB_MSK;
|
data.status = data.status | buff[14] & BME680_HEAT_STAB_MSK;
|
||||||
|
|
||||||
if data.status & BME680_NEW_DATA_MSK != 0 {
|
if data.status & BME680_NEW_DATA_MSK != 0 {
|
||||||
let (temp, t_fine) = Calc::calc_temperature(&self.calib, adc_temp, self.tph_sett.temperature_offset);
|
let (temp, t_fine) =
|
||||||
|
Calc::calc_temperature(&self.calib, adc_temp, self.tph_sett.temperature_offset);
|
||||||
debug!(
|
debug!(
|
||||||
"adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}",
|
"adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}",
|
||||||
adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine
|
adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine
|
||||||
|
|
Loading…
Reference in a new issue