|
| 1 | +//This example reads data from the Bosch BME680 (gas, humidity, temperature, pressure) sensor using I2C. |
| 2 | +#![deny(unsafe_code)] |
| 3 | +#![no_main] |
| 4 | +#![no_std] |
| 5 | + |
| 6 | +use bme680::*; |
| 7 | +use core::time::Duration; |
| 8 | +use embedded_hal::blocking::delay::DelayMs; |
| 9 | +use hal::delay::DelayFromCountDownTimer; |
| 10 | +use hal::i2c::Config; |
| 11 | +use hal::prelude::*; |
| 12 | +use hal::stm32; |
| 13 | +use hal::timer::Timer; |
| 14 | +use stm32g4xx_hal as hal; |
| 15 | + |
| 16 | +use cortex_m_rt::entry; |
| 17 | +use log::info; |
| 18 | + |
| 19 | +#[macro_use] |
| 20 | +mod utils; |
| 21 | + |
| 22 | +#[entry] |
| 23 | +fn main() -> ! { |
| 24 | + utils::logger::init(); |
| 25 | + |
| 26 | + let dp = stm32::Peripherals::take().expect("cannot take peripherals"); |
| 27 | + let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals"); |
| 28 | + |
| 29 | + let mut rcc = dp.RCC.constrain(); |
| 30 | + let gpiob = dp.GPIOB.split(&mut rcc); |
| 31 | + |
| 32 | + let sda = gpiob.pb9.into_alternate_open_drain(); |
| 33 | + let scl = gpiob.pb8.into_alternate_open_drain(); |
| 34 | + |
| 35 | + let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.khz()), &mut rcc); |
| 36 | + |
| 37 | + let mut delayer = cp.SYST.delay(&rcc.clocks); |
| 38 | + let timer2 = Timer::new(dp.TIM2, &rcc.clocks); |
| 39 | + let mut delay = DelayFromCountDownTimer::new(timer2.start_count_down(100.ms())); |
| 40 | + |
| 41 | + let mut dev = |
| 42 | + Bme680::init(i2c, &mut delayer, I2CAddress::Secondary).expect("Init function failed"); |
| 43 | + |
| 44 | + let settings = SettingsBuilder::new() |
| 45 | + .with_humidity_oversampling(OversamplingSetting::OS2x) |
| 46 | + .with_pressure_oversampling(OversamplingSetting::OS4x) |
| 47 | + .with_temperature_oversampling(OversamplingSetting::OS8x) |
| 48 | + .with_temperature_filter(IIRFilterSize::Size3) |
| 49 | + .with_gas_measurement(Duration::from_millis(1500), 320, 25) |
| 50 | + .with_temperature_offset(-2.2) |
| 51 | + .with_run_gas(true) |
| 52 | + .build(); |
| 53 | + |
| 54 | + let profile_dur = dev.get_profile_dur(&settings.0).unwrap(); |
| 55 | + dev.set_sensor_settings(&mut delayer, settings).unwrap(); |
| 56 | + dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode) |
| 57 | + .unwrap(); |
| 58 | + let sensor_settings = dev.get_sensor_settings(settings.1); |
| 59 | + |
| 60 | + loop { |
| 61 | + delay.delay_ms(500u32); |
| 62 | + let power_mode = dev.get_sensor_mode(); |
| 63 | + dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode) |
| 64 | + .unwrap(); |
| 65 | + let (data, _state) = dev.get_sensor_data(&mut delayer).unwrap(); |
| 66 | + info!("Temperature {}°C", data.temperature_celsius()); |
| 67 | + info!("Pressure {}hPa", data.pressure_hpa()); |
| 68 | + info!("Humidity {}%", data.humidity_percent()); |
| 69 | + info!("Gas Resistence {}Ω", data.gas_resistance_ohm()); |
| 70 | + } |
| 71 | +} |
0 commit comments