|
| 1 | +use bmp390::{sync::Bmp390, Address, Configuration}; |
| 2 | +use clap::Parser; |
| 3 | +use embedded_hal::delay::DelayNs; |
| 4 | +use linux_embedded_hal::{Delay, I2cdev}; |
| 5 | + |
| 6 | +/// This example demonstrates how to use the synchronous BMP390 driver on Linux. |
| 7 | +/// |
| 8 | +/// By default, this will print one measurement from the `/dev/i2c-1` device. The device can be |
| 9 | +/// changed with the first positional argument. The program can also print multiple times with the |
| 10 | +/// `--count` argument, or made to repeat `--forever`. To speed up the program, use |
| 11 | +/// `--frequency <FREQ>`. The BMP390 is configured to 50 Hz by default; any value above this will |
| 12 | +/// yield repeated measurements. |
| 13 | +#[derive(Parser)] |
| 14 | +#[command(version)] |
| 15 | +#[command(group = clap::ArgGroup::new("repetition").multiple(false))] |
| 16 | +struct Args { |
| 17 | + /// Which I2C device to use. |
| 18 | + #[clap(default_value = "/dev/i2c-1")] |
| 19 | + device: String, |
| 20 | + |
| 21 | + /// How many measurements to take before exiting. Exclusive with `forever`. |
| 22 | + #[clap(short, long, default_value_t = 1, group = "repetition")] |
| 23 | + count: usize, |
| 24 | + |
| 25 | + /// Whether to perform measurements continuously. Exclusive with `count`. |
| 26 | + #[clap(long, default_value_t = false, group = "repetition")] |
| 27 | + forever: bool, |
| 28 | + |
| 29 | + /// How many measurements to take per second. |
| 30 | + #[clap(short, long, default_value_t = 1.0)] |
| 31 | + frequency: f32, |
| 32 | +} |
| 33 | + |
| 34 | +impl Args { |
| 35 | + fn delay_ms(&self) -> u32 { |
| 36 | + (1000.0 / self.frequency).floor() as u32 |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn main() { |
| 41 | + let args = Args::parse(); |
| 42 | + eprintln!("Using I2C device: {}", args.device); |
| 43 | + let i2c = I2cdev::new(&args.device) |
| 44 | + .map_err(bmp390::Error::I2c) |
| 45 | + .expect("Failed to create I2C device"); |
| 46 | + |
| 47 | + let config = Configuration::default(); |
| 48 | + let mut sensor = Bmp390::try_new(i2c, Address::Up, Delay, &config) |
| 49 | + .expect("Failed to initialize BMP390 sensor"); |
| 50 | + |
| 51 | + let mut delay = Delay; |
| 52 | + let delay_ms = args.delay_ms(); |
| 53 | + |
| 54 | + if args.forever { |
| 55 | + eprintln!("Measuring forever..."); |
| 56 | + for i in 1usize.. { |
| 57 | + let measurement = sensor.measure().expect("Failed to measure BMP390 data"); |
| 58 | + eprintln!("{i}: {measurement}"); |
| 59 | + delay.delay_ms(delay_ms); |
| 60 | + } |
| 61 | + } else { |
| 62 | + let count = args.count; |
| 63 | + for i in 1..=count { |
| 64 | + let measurement = sensor.measure().expect("Failed to measure BMP390 data"); |
| 65 | + eprintln!("{i}/{count}: {measurement}"); |
| 66 | + if i != count { |
| 67 | + delay.delay_ms(delay_ms); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments