Skip to content

Commit

Permalink
Merge pull request #20303 from chrysn-pull-requests/rust-updates
Browse files Browse the repository at this point in the history
treewide: Rust updates
  • Loading branch information
chrysn authored Jan 29, 2024
2 parents 1d11b67 + 002cd42 commit 55cbb7b
Show file tree
Hide file tree
Showing 9 changed files with 1,798 additions and 383 deletions.
4 changes: 1 addition & 3 deletions drivers/lsm303agr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ license = "LGPL-2.1-only"
publish = false

[dependencies]
lsm303agr = "^0.2"
lsm303agr = "1.0"
riot-wrappers = "^0.8"
# Whatever lsm uses
nb = "*"
1 change: 1 addition & 0 deletions drivers/lsm303agr/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
USEMODULE += rust_riotmodules
USEMODULE += ztimer_msec

FEATURES_REQUIRED += periph_i2c
15 changes: 8 additions & 7 deletions drivers/lsm303agr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

use lsm303agr::{interface, mode, Lsm303agr, AccelOutputDataRate::Hz50};
use lsm303agr::{interface, mode, Lsm303agr, AccelOutputDataRate::Hz50, AccelMode};

use riot_wrappers::{saul, println, i2c, cstr::cstr, mutex::Mutex};
use saul::{Phydat, registration};
Expand Down Expand Up @@ -59,9 +59,11 @@ fn init() -> Result<(), &'static str> {
for (&i2cdev, (lsm, (reg, reg_mag))) in I2C_DEVICES.iter().zip(lsm.iter_mut().zip(reg.iter_mut().zip(reg_mag.iter_mut()))) {
let mut device = Lsm303agr::new_with_i2c(i2c::I2CDevice::new(i2cdev));

let mut init_clock = riot_wrappers::ztimer::Clock::msec();

device.init()
.map_err(|_| "Device initialization failed")?;
device.set_accel_odr(Hz50)
device.set_accel_mode_and_odr(&mut init_clock, AccelMode::Normal, Hz50)
.map_err(|_| "Device configuration failed")?;

let lsm = lsm.insert(SaulLSM { device: Mutex::new(device) });
Expand Down Expand Up @@ -90,10 +92,10 @@ impl registration::Drivable for &SaulLSM {
let mut device = self.device.try_lock()
.ok_or(registration::Error)?;

let data = device.accel_data()
let data = device.acceleration()
.map_err(|_| registration::Error)?;
// Data is in the +-2g range by default, which doesn't overflow even the i16 SAUL uses
Ok(Phydat::new(&[data.x as _, data.y as _, data.z as _], Some(saul::Unit::GForce), -3))
Ok(Phydat::new(&[data.x_mg() as _, data.y_mg() as _, data.z_mg() as _], Some(saul::Unit::GForce), -3))
}
}

Expand All @@ -115,9 +117,8 @@ impl registration::Drivable for MagAspect {
let mut device = self.0.device.try_lock()
.ok_or(registration::Error)?;

let data = nb::block!(device.mag_data())
let data = device.magnetic_field()
.map_err(|_| registration::Error)?;
// Original data is in nanotesla
return Ok(Phydat::fit(&[data.x, data.y, data.z], Some(saul::Unit::T), -9))
return Ok(Phydat::fit(&[data.x_nt(), data.y_nt(), data.z_nt()], Some(saul::Unit::T), -9))
}
}
Loading

0 comments on commit 55cbb7b

Please sign in to comment.