From 59362c608fb77abd18ad69923759f544e172c94c Mon Sep 17 00:00:00 2001 From: Adam Sasine Date: Fri, 10 Jan 2025 09:57:42 -0500 Subject: [PATCH 1/3] Added arg for which i2c device to use --- Cargo.lock | 193 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 13 ++- examples/linux-sync.rs | 32 +++++++ examples/sync_linux.rs | 25 ------ src/lib.rs | 12 +++ 5 files changed, 246 insertions(+), 29 deletions(-) create mode 100644 examples/linux-sync.rs delete mode 100644 examples/sync_linux.rs diff --git a/Cargo.lock b/Cargo.lock index f6049ef..51e9040 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,55 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "autocfg" version = "1.3.0" @@ -54,6 +103,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "bmp390" version = "0.4.1" dependencies = [ + "clap", "defmt", "embassy-time", "embedded-hal 1.0.0", @@ -89,6 +139,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8eb5e908ef3a6efbe1ed62520fb7287959888c88485abe072543190ecc66783" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b01801b5fc6a0a232407abc821660c9c6d25a1cafc0d4f85f29fb8d9afc121" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.65", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "core-foundation" version = "0.10.0" @@ -264,6 +360,12 @@ dependencies = [ "nix 0.27.1", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "i2cdev" version = "0.6.1" @@ -286,6 +388,12 @@ dependencies = [ "mach2", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "libc" version = "0.2.155" @@ -529,6 +637,12 @@ dependencies = [ "nix 0.26.4", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "1.0.109" @@ -632,6 +746,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "version_check" version = "0.9.4" @@ -665,3 +785,76 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 6360c8f..f5e26e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,15 +19,20 @@ embassy-time = ["dep:embassy-time"] sync = ["dep:embedded-hal"] [dependencies] +defmt = "0.3.8" embedded-hal-async = "1.0.0" -embedded-hal = { version= "1.0.0", optional = true} +embedded-hal = { version = "1.0.0", optional = true } embassy-time = { version = "0.4.0", optional = true } -defmt = "0.3.8" libm = "0.2.8" -uom = { version = "0.36.0", default-features = false, features = ["f32", "si", "autoconvert"] } +uom = { version = "0.36.0", default-features = false, features = [ + "f32", + "si", + "autoconvert", +] } [dev-dependencies] -defmt = { version = "0.3.8", features = [ "unstable-test" ] } +clap = { version = "4.5.26", features = ["derive"] } +defmt = { version = "0.3.8", features = ["unstable-test"] } embedded-hal-mock = { version = "0.11.0", default-features = false, features = [ "eh1", "embedded-hal-async", diff --git a/examples/linux-sync.rs b/examples/linux-sync.rs new file mode 100644 index 0000000..b6e19de --- /dev/null +++ b/examples/linux-sync.rs @@ -0,0 +1,32 @@ +//! This example demonstrates how to use the synchronous BMP390 driver on Linux. +//! +//! # Arguments +//! The program accepts an optional argument that specifies the I2C device to use. If not provided, the default device +//! `/dev/i2c-1` is used. + +use bmp390::{sync::Bmp390, Address, Configuration}; +use clap::Parser; +use linux_embedded_hal::{Delay, I2cdev}; + +#[derive(Parser)] +#[command(version, about, long_about = None)] +struct Args { + /// Which I2C device to use. + #[clap(default_value = "/dev/i2c-1")] + device: String, +} + +fn main() { + let args = Args::parse(); + eprintln!("Using I2C device: {}", args.device); + let i2c = I2cdev::new(args.device) + .map_err(bmp390::Error::I2c) + .expect("Failed to create I2C device"); + + let config = Configuration::default(); + let mut sensor = Bmp390::try_new(i2c, Address::Up, Delay, &config) + .expect("Failed to initialize BMP390 sensor"); + + let measurement = sensor.measure().expect("Failed to measure BMP390 data"); + println!("Measurement: {}", measurement); +} diff --git a/examples/sync_linux.rs b/examples/sync_linux.rs deleted file mode 100644 index 05e5f25..0000000 --- a/examples/sync_linux.rs +++ /dev/null @@ -1,25 +0,0 @@ -use bmp390::{sync::Bmp390, Address, Configuration}; -use linux_embedded_hal::{Delay, I2cdev}; -use uom::si::length::meter; - -fn main() { - let i2c = I2cdev::new("/dev/i2c-1") - .map_err(bmp390::Error::I2c) - .expect("Failed to create I2C device"); - let mut delay = Delay; - - let config = Configuration::default(); - let mut sensor = Bmp390::try_new(i2c, Address::Up, &mut delay, &config) - .expect("Failed to initialize BMP390 sensor"); - - let measurement = sensor.measure().expect("Failed to measure BMP390 data"); - - println!( - "Measurement: Pressure: {} hPa, Temperature: {} °C, Altitude: {} m", - measurement.pressure.get::(), - measurement - .temperature - .get::(), - measurement.altitude.get::() - ); -} diff --git a/src/lib.rs b/src/lib.rs index c69f19e..5f779a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -197,6 +197,18 @@ impl Format for Measurement { } } +impl core::fmt::Display for Measurement { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "Pressure: {} Pa, Temperature: {} °C, Altitude: {} m", + self.pressure.get::(), + self.temperature.get::(), + self.altitude.get::(), + ) + } +} + /// The BMP390 barometer's I2C addresses, either `0x76` or `0x77`. /// /// The BMP390 can be configured to use two different addresses by either pulling the `SDO` pin down to `GND` From 6540dde0d561402c3a6d19c8832f27e18ba890d7 Mon Sep 17 00:00:00 2001 From: Adam Sasine Date: Fri, 10 Jan 2025 10:57:33 -0500 Subject: [PATCH 2/3] Added other args to linux-sync --- examples/linux-sync.rs | 60 +++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/examples/linux-sync.rs b/examples/linux-sync.rs index b6e19de..fb4541a 100644 --- a/examples/linux-sync.rs +++ b/examples/linux-sync.rs @@ -1,25 +1,46 @@ -//! This example demonstrates how to use the synchronous BMP390 driver on Linux. -//! -//! # Arguments -//! The program accepts an optional argument that specifies the I2C device to use. If not provided, the default device -//! `/dev/i2c-1` is used. - use bmp390::{sync::Bmp390, Address, Configuration}; use clap::Parser; +use embedded_hal::delay::DelayNs; use linux_embedded_hal::{Delay, I2cdev}; +/// This example demonstrates how to use the synchronous BMP390 driver on Linux. +/// +/// By default, this will print one measurement from the `/dev/i2c-1` device. The device can be +/// changed with the first positional argument. The program can also print multiple times with the +/// `--count` argument, or made to repeat `--forever`. To speed up the program, use +/// `--frequency `. The BMP390 is configured to 50 Hz by default; any value above this will +/// yield repeated measurements. #[derive(Parser)] -#[command(version, about, long_about = None)] +#[command(version)] +#[command(group = clap::ArgGroup::new("repetition").multiple(false))] struct Args { /// Which I2C device to use. #[clap(default_value = "/dev/i2c-1")] device: String, + + /// How many measurements to take before exiting. Exclusive with `forever`. + #[clap(short, long, default_value_t = 1, group = "repetition")] + count: usize, + + /// Whether to perform measurements continuously. Exclusive with `count`. + #[clap(long, default_value_t = false, group = "repetition")] + forever: bool, + + /// How many measurements to take per second. + #[clap(short, long, default_value_t = 1.0)] + frequency: f32, +} + +impl Args { + fn delay_ms(&self) -> u32 { + (1000.0 / self.frequency) as u32 + } } fn main() { let args = Args::parse(); eprintln!("Using I2C device: {}", args.device); - let i2c = I2cdev::new(args.device) + let i2c = I2cdev::new(&args.device) .map_err(bmp390::Error::I2c) .expect("Failed to create I2C device"); @@ -27,6 +48,25 @@ fn main() { let mut sensor = Bmp390::try_new(i2c, Address::Up, Delay, &config) .expect("Failed to initialize BMP390 sensor"); - let measurement = sensor.measure().expect("Failed to measure BMP390 data"); - println!("Measurement: {}", measurement); + let mut delay = Delay; + let delay_ms = args.delay_ms(); + + if args.forever { + eprintln!("Measuring forever..."); + for i in 1usize.. { + let measurement = sensor.measure().expect("Failed to measure BMP390 data"); + eprintln!("{i}: {measurement}"); + delay.delay_ms(delay_ms); + } + } else { + let count = args.count; + for i in 1..=count { + let measurement = sensor.measure().expect("Failed to measure BMP390 data"); + eprintln!("{i}/{count}: {measurement}"); + if i != count { + delay.delay_ms(delay_ms); + } + } + } } + From 20b6390d61a09d24700a4bd2535a3a92ed68bc32 Mon Sep 17 00:00:00 2001 From: Adam Sasine Date: Fri, 10 Jan 2025 11:06:23 -0500 Subject: [PATCH 3/3] Fixed formatting error --- examples/linux-sync.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/linux-sync.rs b/examples/linux-sync.rs index fb4541a..6269f88 100644 --- a/examples/linux-sync.rs +++ b/examples/linux-sync.rs @@ -33,7 +33,7 @@ struct Args { impl Args { fn delay_ms(&self) -> u32 { - (1000.0 / self.frequency) as u32 + (1000.0 / self.frequency).floor() as u32 } } @@ -69,4 +69,3 @@ fn main() { } } } -