From 170c598216029d921935c37edd71e709f11b935a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemens=20Sch=C3=B6lhorn?= Date: Sun, 31 Mar 2019 15:11:34 +0200 Subject: [PATCH] Read i2c device path from command line --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index ae95acd..84f5486 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,16 @@ use i2cdev_bmp280::*; use si7021::*; use i2csensors::{Barometer, Hygrometer, Thermometer}; +use std::env; + fn main() { - let i2c_device = LinuxI2CDevice::new("/dev/i2c-11", 0b1110110).unwrap(); + let i2c_path = env::args().skip(1).next().expect("i2c bus path (eg. /dev/i2c-1)"); + + i2c_sensors(&i2c_path); +} + +fn i2c_sensors(path: &str) { + let i2c_device = LinuxI2CDevice::new(path, 0b1110110).unwrap(); let settings = BMP280Settings { compensation: BMP280CompensationAlgorithm::Float, @@ -25,7 +33,7 @@ fn main() { println!("pressure: {:6.2} kPa", bmp280.pressure_kpa().unwrap()); println!("temp1: {:6.2} °C", bmp280.temperature_celsius().unwrap()); - let device = LinuxI2CDevice::new("/dev/i2c-11", SI7021_I2C_ADDRESS).unwrap(); + let device = LinuxI2CDevice::new(path, SI7021_I2C_ADDRESS).unwrap(); let mut si7021 = Si7021::new(device); println!("humidity: {:6.2} %", si7021.relative_humidity().unwrap()); println!("temp2: {:6.2} °C", si7021.temperature_celsius().unwrap());