Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanded linux example #6

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 71 additions & 0 deletions examples/linux-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 <FREQ>`. The BMP390 is configured to 50 Hz by default; any value above this will
/// yield repeated measurements.
#[derive(Parser)]
#[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).floor() as u32
}
}

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 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);
asasine marked this conversation as resolved.
Show resolved Hide resolved
}
} 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);
}
}
}
}
25 changes: 0 additions & 25 deletions examples/sync_linux.rs

This file was deleted.

12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<pascal>(),
self.temperature.get::<degree_celsius>(),
self.altitude.get::<meter>(),
)
}
}

/// 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`
Expand Down
Loading