Skip to content

Instantly share code, notes, and snippets.

@aavogt
Created November 25, 2024 19:40
Show Gist options
  • Save aavogt/cf53291b71f084ec0952e87ce37142eb to your computer and use it in GitHub Desktop.
Save aavogt/cf53291b71f084ec0952e87ce37142eb to your computer and use it in GitHub Desktop.
esp-rs bme280
fn bmp_tptp(
i2c_ref: Arc<Mutex<I2C0>>,
sda: AnyIOPin,
scl: AnyIOPin,
config: Config,
) -> anyhow::Result<[f32; 4]> {
use bme280::i2c::BME280;
let i2c = i2c_ref.lock().unwrap();
let i2c = RefCell::new(I2cDriver::new(i2c, sda, scl, &config)?);
let mut r = [0f32; 4];
{
let mut bm = BME280::new_primary(i2c::RefCellDevice::new(&i2c));
bm.init(&mut esp_idf_hal::delay::Ets)
.map_err(|x| anyhow!("bme280 init error {:?}", x))?;
let meas = bm
.measure(&mut esp_idf_hal::delay::Ets)
.map_err(|x| anyhow!("bme280 measure error {:?}", x))?;
r[0] = meas.temperature;
r[1] = meas.pressure;
}
{
let mut bm = BME280::new_secondary(i2c::RefCellDevice::new(&i2c));
bm.init(&mut esp_idf_hal::delay::Ets)
.map_err(|x| anyhow!("bme280 init error {:?}", x))?;
let meas = bm
.measure(&mut esp_idf_hal::delay::Ets)
.map_err(|x| anyhow!("bme280 measure error {:?}", x))?;
r[2] = meas.temperature;
r[3] = meas.pressure;
}
return Ok(r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment