Rustfmt code

This commit is contained in:
marcelbuesing 2018-10-17 22:10:55 +02:00
parent f7e81b4573
commit 5c1de2a1d3
No known key found for this signature in database
GPG key ID: 03F06F8179D5549D
4 changed files with 80 additions and 46 deletions

View file

@ -3,34 +3,35 @@
/// continously send them to an influx database. /// continously send them to an influx database.
/// Make sure you adapt the influx constants and likely also the i2c device id and I2CAddress. /// Make sure you adapt the influx constants and likely also the i2c device id and I2CAddress.
/// ///
extern crate bme680; extern crate bme680;
extern crate env_logger; extern crate env_logger;
extern crate futures; extern crate futures;
extern crate linux_embedded_hal;
extern crate influent; extern crate influent;
extern crate linux_embedded_hal;
extern crate tokio; 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 futures::Future;
use linux_embedded_hal::*; use linux_embedded_hal::*;
use std::time::Duration; use std::time::Duration;
use influent::client::{Client, ClientError, Credentials};
use influent::create_client; use influent::create_client;
use influent::client::{Client, Credentials, ClientError};
use influent::measurement::{Measurement, Value}; use influent::measurement::{Measurement, Value};
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<(), ()> 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).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() let settings = SettingsBuilder::new()
.with_humidity_oversampling(OversamplingSetting::OS2x) .with_humidity_oversampling(OversamplingSetting::OS2x)
@ -40,20 +41,24 @@ fn main() -> Result<(), ()>
.with_gas_measurement(Duration::from_millis(1500), 320, 25) .with_gas_measurement(Duration::from_millis(1500), 320, 25)
.with_run_gas(true) .with_run_gas(true)
.build(); .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 // Set up Influx client
let credentials = Credentials { let credentials = Credentials {
username: INFLUX_USER, username: INFLUX_USER,
password: INFLUX_PASSWORD, password: INFLUX_PASSWORD,
database: INFLUX_DATABASE database: INFLUX_DATABASE,
}; };
let hosts = vec![INFLUX_ADDRESS]; let hosts = vec![INFLUX_ADDRESS];
let client = create_client(credentials, hosts); let client = create_client(credentials, hosts);
dev.set_sensor_mode(PowerMode::ForcedMode).map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?; dev.set_sensor_mode(PowerMode::ForcedMode)
let (data, state) = dev.get_sensor_data().map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?; .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!("State {:?}", state);
println!("Temperature {}°C", data.temperature_celsius()); println!("Temperature {}°C", data.temperature_celsius());
@ -61,14 +66,32 @@ fn main() -> Result<(), ()>
println!("Humidity {}%", data.humidity_percent()); println!("Humidity {}%", data.humidity_percent());
println!("Gas Resistence {}Ω", data.gas_resistance_ohm()); println!("Gas Resistence {}Ω", data.gas_resistance_ohm());
if state != FieldDataCondition::NewData if state != FieldDataCondition::NewData {
{ let temperature_f = send_value(
let temperature_f = send_value(&client, "temperature" ,Value::Float(data.temperature_celsius() as f64)); &client,
let pressure_f = send_value(&client, "pressure" ,Value::Float(data.pressure_hpa() as f64)); "temperature",
let humidity_f = send_value(&client, "humidity" ,Value::Float(data.humidity_percent() as f64)); Value::Float(data.temperature_celsius() as f64),
let gas_f = send_value(&client, "gasresistence" , Value::Float(data.gas_resistance_ohm() 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(); let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
return rt.block_on(f); return rt.block_on(f);
@ -77,7 +100,11 @@ fn main() -> Result<(), ()>
} }
/// Sends a measured value to the influx database /// Sends a measured value to the influx database
fn send_value(client:&Client, type_name: &str, value: Value) -> impl Future<Item=(), Error=ClientError> { fn send_value(
client: &Client,
type_name: &str,
value: Value,
) -> impl Future<Item = (), Error = 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");

View file

@ -8,11 +8,11 @@ extern crate linux_embedded_hal as hal;
extern crate log; extern crate log;
use bme680::*; use bme680::*;
use embedded_hal::blocking::i2c;
use embedded_hal::blocking::delay::DelayMs;
use hal::*;
use core::result; use core::result;
use core::time::Duration; use core::time::Duration;
use embedded_hal::blocking::delay::DelayMs;
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>>

View file

@ -58,7 +58,8 @@ impl Calc {
var2 = var2 + (var1 * calib.par_p5 as (i32) << 1); var2 = var2 + (var1 * calib.par_p5 as (i32) << 1);
var2 = (var2 >> 2i32) + (calib.par_p4 as (i32) << 16i32); var2 = (var2 >> 2i32) + (calib.par_p4 as (i32) << 16i32);
var1 = (((var1 >> 2i32) * (var1 >> 2i32) >> 13i32) * (calib.par_p3 as (i32) << 5i32) 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 = var1 >> 18i32;
var1 = (32768i32 + var1) * calib.par_p1 as (i32) >> 15i32; var1 = (32768i32 + var1) * calib.par_p1 as (i32) >> 15i32;
let mut pressure_comp: i32 = 1048576u32.wrapping_sub(pres_adc) as (i32); 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) var1 = calib.par_p9 as (i32) * ((pressure_comp >> 3i32) * (pressure_comp >> 3i32) >> 13i32)
>> 12i32; >> 12i32;
var2 = (pressure_comp >> 2i32) * calib.par_p8 as (i32) >> 13i32; var2 = (pressure_comp >> 2i32) * calib.par_p8 as (i32) >> 13i32;
let var3: i32 = (pressure_comp >> 8i32) * (pressure_comp >> 8i32) * (pressure_comp >> 8i32) let var3: i32 = (pressure_comp >> 8i32)
* calib.par_p10 as (i32) >> 17i32; * (pressure_comp >> 8i32)
* (pressure_comp >> 8i32)
* calib.par_p10 as (i32)
>> 17i32;
pressure_comp = pressure_comp =
pressure_comp + (var1 + var2 + var3 + (calib.par_p7 as (i32) << 7i32) >> 4i32); pressure_comp + (var1 + var2 + var3 + (calib.par_p7 as (i32) << 7i32) >> 4i32);
pressure_comp as (u32) pressure_comp as (u32)
@ -80,12 +84,14 @@ impl Calc {
pub fn calc_humidity(calib: &CalibData, t_fine: i32, hum_adc: u16) -> u32 { pub fn calc_humidity(calib: &CalibData, t_fine: i32, hum_adc: u16) -> u32 {
let temp_scaled: i32 = t_fine * 5i32 + 128i32 >> 8i32; 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); - (temp_scaled * calib.par_h3 as (i32) / 100i32 >> 1i32);
let var2: i32 = calib.par_h2 as (i32) let var2: i32 = calib.par_h2 as (i32)
* (temp_scaled * calib.par_h4 as (i32) / 100i32 * (temp_scaled * calib.par_h4 as (i32) / 100i32
+ (temp_scaled * (temp_scaled * calib.par_h5 as (i32) / 100i32) >> 6i32) / 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 var3: i32 = var1 * var2;
let var4: i32 = calib.par_h6 as (i32) << 7i32; let var4: i32 = calib.par_h6 as (i32) << 7i32;
let var4: i32 = var4 + temp_scaled * calib.par_h7 as (i32) / 100i32 >> 4i32; let var4: i32 = var4 + temp_scaled * calib.par_h7 as (i32) / 100i32 >> 4i32;

View file

@ -49,8 +49,10 @@ extern crate embedded_hal as hal;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
pub use self::settings::{DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, pub use self::settings::{
SensorSettings, Settings, SettingsBuilder, TphSett}; DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, SensorSettings, Settings,
SettingsBuilder, TphSett,
};
mod calc; mod calc;
mod settings; mod settings;
@ -736,24 +738,21 @@ where
let os_to_meas_cycles: [u8; 6] = [0u8, 1u8, 2u8, 4u8, 8u8, 16u8]; let os_to_meas_cycles: [u8; 6] = [0u8, 1u8, 2u8, 4u8, 8u8, 16u8];
// TODO check if the following unwrap_ors do not change behaviour // TODO check if the following unwrap_ors do not change behaviour
let mut meas_cycles = os_to_meas_cycles[sensor_settings let mut meas_cycles = os_to_meas_cycles[sensor_settings
.tph_sett .tph_sett
.os_temp .os_temp
.unwrap_or(OversamplingSetting::OSNone) .unwrap_or(OversamplingSetting::OSNone)
as (usize)] as (usize)] as (u32);
as (u32);
meas_cycles = meas_cycles.wrapping_add( meas_cycles = meas_cycles.wrapping_add(
os_to_meas_cycles[sensor_settings os_to_meas_cycles[sensor_settings
.tph_sett .tph_sett
.os_pres .os_pres
.unwrap_or(OversamplingSetting::OSNone) .unwrap_or(OversamplingSetting::OSNone) as (usize)] as (u32),
as (usize)] as (u32),
); );
meas_cycles = meas_cycles.wrapping_add( meas_cycles = meas_cycles.wrapping_add(
os_to_meas_cycles[sensor_settings os_to_meas_cycles[sensor_settings
.tph_sett .tph_sett
.os_hum .os_hum
.unwrap_or(OversamplingSetting::OSNone) .unwrap_or(OversamplingSetting::OSNone) as (usize)] as (u32),
as (usize)] as (u32),
); );
let mut tph_dur = meas_cycles.wrapping_mul(1963u32); let mut tph_dur = meas_cycles.wrapping_mul(1963u32);
tph_dur = tph_dur.wrapping_add(477u32.wrapping_mul(4u32)); tph_dur = tph_dur.wrapping_add(477u32.wrapping_mul(4u32));
@ -832,14 +831,16 @@ where
calib.res_heat_range = calib.res_heat_range =
(I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_RANGE_ADDR)? (I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_RANGE_ADDR)?
& 0x30) / 16; & 0x30)
/ 16;
calib.res_heat_val = calib.res_heat_val =
I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_VAL_ADDR)? as i8; I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_VAL_ADDR)? as i8;
calib.range_sw_err = calib.range_sw_err =
(I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RANGE_SW_ERR_ADDR)? (I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RANGE_SW_ERR_ADDR)?
& BME680_RSERROR_MSK) / 16; & BME680_RSERROR_MSK)
/ 16;
Ok(calib) Ok(calib)
} }