diff --git a/examples/influx_client.rs b/examples/influx_client.rs index 1587bd9..1adfe06 100644 --- a/examples/influx_client.rs +++ b/examples/influx_client.rs @@ -3,34 +3,35 @@ /// continously send them to an influx database. /// Make sure you adapt the influx constants and likely also the i2c device id and I2CAddress. /// - extern crate bme680; extern crate env_logger; extern crate futures; -extern crate linux_embedded_hal; extern crate influent; +extern crate linux_embedded_hal; extern crate tokio; -use bme680::{Bme680, OversamplingSetting, IIRFilterSize, I2CAddress, SettingsBuilder, PowerMode, FieldDataCondition}; +use bme680::{ + Bme680, FieldDataCondition, I2CAddress, IIRFilterSize, OversamplingSetting, PowerMode, + SettingsBuilder, +}; use futures::Future; use linux_embedded_hal::*; use std::time::Duration; +use influent::client::{Client, ClientError, Credentials}; use influent::create_client; -use influent::client::{Client, Credentials, ClientError}; use influent::measurement::{Measurement, Value}; 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_DATABASE: &str = "influxdb"; -fn main() -> Result<(), ()> -{ - +fn main() -> Result<(), ()> { // Init device let i2c = I2cdev::new("/dev/i2c-1").unwrap(); - let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary).map_err(|e| eprintln!("Init failed: {:?}", e))?; + let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary) + .map_err(|e| eprintln!("Init failed: {:?}", e))?; let settings = SettingsBuilder::new() .with_humidity_oversampling(OversamplingSetting::OS2x) @@ -40,20 +41,24 @@ fn main() -> Result<(), ()> .with_gas_measurement(Duration::from_millis(1500), 320, 25) .with_run_gas(true) .build(); - dev.set_sensor_settings(settings).map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?; + dev.set_sensor_settings(settings) + .map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?; // Set up Influx client let credentials = Credentials { username: INFLUX_USER, password: INFLUX_PASSWORD, - database: INFLUX_DATABASE + database: INFLUX_DATABASE, }; let hosts = vec![INFLUX_ADDRESS]; let client = create_client(credentials, hosts); - dev.set_sensor_mode(PowerMode::ForcedMode).map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?; - let (data, state) = dev.get_sensor_data().map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?; + dev.set_sensor_mode(PowerMode::ForcedMode) + .map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?; + let (data, state) = dev + .get_sensor_data() + .map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?; println!("State {:?}", state); println!("Temperature {}°C", data.temperature_celsius()); @@ -61,14 +66,32 @@ fn main() -> Result<(), ()> println!("Humidity {}%", data.humidity_percent()); println!("Gas Resistence {}Ω", data.gas_resistance_ohm()); - if state != FieldDataCondition::NewData - { - let temperature_f = send_value(&client, "temperature" ,Value::Float(data.temperature_celsius() as f64)); - let pressure_f = send_value(&client, "pressure" ,Value::Float(data.pressure_hpa() as f64)); - let humidity_f = send_value(&client, "humidity" ,Value::Float(data.humidity_percent() as f64)); - let gas_f = send_value(&client, "gasresistence" , Value::Float(data.gas_resistance_ohm() as f64)); + if state != FieldDataCondition::NewData { + let temperature_f = send_value( + &client, + "temperature", + Value::Float(data.temperature_celsius() as f64), + ); + let pressure_f = send_value( + &client, + "pressure", + Value::Float(data.pressure_hpa() as f64), + ); + let humidity_f = send_value( + &client, + "humidity", + Value::Float(data.humidity_percent() as f64), + ); + let gas_f = send_value( + &client, + "gasresistence", + Value::Float(data.gas_resistance_ohm() as f64), + ); - let f = temperature_f.join4(pressure_f, humidity_f, gas_f).map(|_| ()).map_err(|e| eprintln!("Error: {:?}", e)); + let f = temperature_f + .join4(pressure_f, humidity_f, gas_f) + .map(|_| ()) + .map_err(|e| eprintln!("Error: {:?}", e)); let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap(); return rt.block_on(f); @@ -77,7 +100,11 @@ fn main() -> Result<(), ()> } /// Sends a measured value to the influx database -fn send_value(client:&Client, type_name: &str, value: Value) -> impl Future { +fn send_value( + client: &Client, + type_name: &str, + value: Value, +) -> impl Future { let mut measurement = Measurement::new("sensor"); measurement.add_field("value", value); measurement.add_tag("id", "MAC"); diff --git a/examples/reading_temperature.rs b/examples/reading_temperature.rs index 930cc9f..9f4447b 100644 --- a/examples/reading_temperature.rs +++ b/examples/reading_temperature.rs @@ -8,11 +8,11 @@ extern crate linux_embedded_hal as hal; extern crate log; use bme680::*; -use embedded_hal::blocking::i2c; -use embedded_hal::blocking::delay::DelayMs; -use hal::*; use core::result; use core::time::Duration; +use embedded_hal::blocking::delay::DelayMs; +use embedded_hal::blocking::i2c; +use hal::*; fn main( ) -> result::Result<(), Error<::Error, ::Error>> diff --git a/src/calc.rs b/src/calc.rs index 8f05fa0..75955be 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -58,7 +58,8 @@ impl Calc { var2 = var2 + (var1 * calib.par_p5 as (i32) << 1); var2 = (var2 >> 2i32) + (calib.par_p4 as (i32) << 16i32); var1 = (((var1 >> 2i32) * (var1 >> 2i32) >> 13i32) * (calib.par_p3 as (i32) << 5i32) - >> 3i32) + (calib.par_p2 as (i32) * var1 >> 1i32); + >> 3i32) + + (calib.par_p2 as (i32) * var1 >> 1i32); var1 = var1 >> 18i32; var1 = (32768i32 + var1) * calib.par_p1 as (i32) >> 15i32; let mut pressure_comp: i32 = 1048576u32.wrapping_sub(pres_adc) as (i32); @@ -71,8 +72,11 @@ impl Calc { var1 = calib.par_p9 as (i32) * ((pressure_comp >> 3i32) * (pressure_comp >> 3i32) >> 13i32) >> 12i32; var2 = (pressure_comp >> 2i32) * calib.par_p8 as (i32) >> 13i32; - let var3: i32 = (pressure_comp >> 8i32) * (pressure_comp >> 8i32) * (pressure_comp >> 8i32) - * calib.par_p10 as (i32) >> 17i32; + let var3: i32 = (pressure_comp >> 8i32) + * (pressure_comp >> 8i32) + * (pressure_comp >> 8i32) + * calib.par_p10 as (i32) + >> 17i32; pressure_comp = pressure_comp + (var1 + var2 + var3 + (calib.par_p7 as (i32) << 7i32) >> 4i32); pressure_comp as (u32) @@ -80,12 +84,14 @@ impl Calc { pub fn calc_humidity(calib: &CalibData, t_fine: i32, hum_adc: u16) -> u32 { let temp_scaled: i32 = t_fine * 5i32 + 128i32 >> 8i32; - let var1: i32 = hum_adc as (i32) - calib.par_h1 as (i32) * 16i32 + let var1: i32 = hum_adc as (i32) + - calib.par_h1 as (i32) * 16i32 - (temp_scaled * calib.par_h3 as (i32) / 100i32 >> 1i32); let var2: i32 = calib.par_h2 as (i32) * (temp_scaled * calib.par_h4 as (i32) / 100i32 + (temp_scaled * (temp_scaled * calib.par_h5 as (i32) / 100i32) >> 6i32) / 100i32 - + (1i32 << 14i32)) >> 10i32; + + (1i32 << 14i32)) + >> 10i32; let var3: i32 = var1 * var2; let var4: i32 = calib.par_h6 as (i32) << 7i32; let var4: i32 = var4 + temp_scaled * calib.par_h7 as (i32) / 100i32 >> 4i32; diff --git a/src/lib.rs b/src/lib.rs index 7fcf26e..1233b38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,8 +49,10 @@ extern crate embedded_hal as hal; #[macro_use] extern crate log; -pub use self::settings::{DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, - SensorSettings, Settings, SettingsBuilder, TphSett}; +pub use self::settings::{ + DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, SensorSettings, Settings, + SettingsBuilder, TphSett, +}; mod calc; mod settings; @@ -736,24 +738,21 @@ where let os_to_meas_cycles: [u8; 6] = [0u8, 1u8, 2u8, 4u8, 8u8, 16u8]; // TODO check if the following unwrap_ors do not change behaviour let mut meas_cycles = os_to_meas_cycles[sensor_settings - .tph_sett - .os_temp - .unwrap_or(OversamplingSetting::OSNone) - as (usize)] - as (u32); + .tph_sett + .os_temp + .unwrap_or(OversamplingSetting::OSNone) + as (usize)] as (u32); meas_cycles = meas_cycles.wrapping_add( os_to_meas_cycles[sensor_settings - .tph_sett - .os_pres - .unwrap_or(OversamplingSetting::OSNone) - as (usize)] as (u32), + .tph_sett + .os_pres + .unwrap_or(OversamplingSetting::OSNone) as (usize)] as (u32), ); meas_cycles = meas_cycles.wrapping_add( os_to_meas_cycles[sensor_settings - .tph_sett - .os_hum - .unwrap_or(OversamplingSetting::OSNone) - as (usize)] as (u32), + .tph_sett + .os_hum + .unwrap_or(OversamplingSetting::OSNone) as (usize)] as (u32), ); let mut tph_dur = meas_cycles.wrapping_mul(1963u32); tph_dur = tph_dur.wrapping_add(477u32.wrapping_mul(4u32)); @@ -832,14 +831,16 @@ where calib.res_heat_range = (I2CUtil::read_byte::(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_RANGE_ADDR)? - & 0x30) / 16; + & 0x30) + / 16; calib.res_heat_val = I2CUtil::read_byte::(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_VAL_ADDR)? as i8; calib.range_sw_err = (I2CUtil::read_byte::(i2c, dev_id.addr(), BME680_ADDR_RANGE_SW_ERR_ADDR)? - & BME680_RSERROR_MSK) / 16; + & BME680_RSERROR_MSK) + / 16; Ok(calib) }