#![no_std]
#[cfg(all(feature = "sync", feature = "async"))]
compile_error!(
"features `sync` and `async` are mutually exclusive - enable exactly one \
(e.g. `default-features = false, features = [\"sync\"]`)"
);
#[cfg(not(any(feature = "sync", feature = "async")))]
compile_error!("enable exactly one of the `sync` or `async` features");
mod lsm303_registers;
#[cfg(test)]
mod tests;
use lsm303_registers::*;
use crate::lsm303_registers::reg_addr::{TEMP_OUT_H, TEMP_OUT_L};
use core::marker::PhantomData;
#[cfg(feature = "sync")]
use embedded_hal::i2c::{Error as _, ErrorKind, I2c};
#[cfg(feature = "async")]
use embedded_hal_async::i2c::{Error as _, ErrorKind, I2c};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlxMagScale {
G1_3,
G1_9,
G2_5,
G4_0,
G4_7,
G5_6,
G8_1,
}
impl DlxMagScale {
const MASK: u8 = 0b1110_0000;
fn bits(self) -> u8 {
match self {
DlxMagScale::G1_3 => 0b001 << 5,
DlxMagScale::G1_9 => 0b010 << 5,
DlxMagScale::G2_5 => 0b011 << 5,
DlxMagScale::G4_0 => 0b100 << 5,
DlxMagScale::G4_7 => 0b101 << 5,
DlxMagScale::G5_6 => 0b110 << 5,
DlxMagScale::G8_1 => 0b111 << 5,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DMagScale {
G2,
G4,
G8,
G12,
}
impl DMagScale {
const MASK: u8 = 0b0110_0000;
fn bits(self) -> u8 {
match self {
DMagScale::G2 => 0b00 << 5,
DMagScale::G4 => 0b01 << 5,
DMagScale::G8 => 0b10 << 5,
DMagScale::G12 => 0b11 << 5,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlhDlmAccScale {
G2,
G4,
G8,
}
impl DlhDlmAccScale {
const MASK: u8 = 0b0011_0000;
fn bits(self) -> u8 {
match self {
DlhDlmAccScale::G2 => 0b00 << 4,
DlhDlmAccScale::G4 => 0b01 << 4,
DlhDlmAccScale::G8 => 0b11 << 4,
}
}
fn from_reg(reg: u8) -> Self {
match reg & Self::MASK {
0x10 => DlhDlmAccScale::G4,
0x30 => DlhDlmAccScale::G8,
_ => DlhDlmAccScale::G2,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlhcAccScale {
G2,
G4,
G8,
G16,
}
impl DlhcAccScale {
const MASK: u8 = 0b0011_0000;
fn bits(self) -> u8 {
match self {
DlhcAccScale::G2 => 0b00 << 4,
DlhcAccScale::G4 => 0b01 << 4,
DlhcAccScale::G8 => 0b10 << 4,
DlhcAccScale::G16 => 0b11 << 4,
}
}
fn from_reg(reg: u8) -> Self {
match reg & Self::MASK {
0x10 => DlhcAccScale::G4,
0x20 => DlhcAccScale::G8,
0x30 => DlhcAccScale::G16,
_ => DlhcAccScale::G2,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DAccScale {
G2,
G4,
G6,
G8,
G16,
}
impl DAccScale {
const MASK: u8 = 0b0011_1000;
fn bits(self) -> u8 {
match self {
DAccScale::G2 => 0b000 << 3,
DAccScale::G4 => 0b001 << 3,
DAccScale::G6 => 0b010 << 3,
DAccScale::G8 => 0b011 << 3,
DAccScale::G16 => 0b100 << 3,
}
}
fn from_reg(reg: u8) -> Self {
match reg & Self::MASK {
0x08 => DAccScale::G4,
0x10 => DAccScale::G6,
0x18 => DAccScale::G8,
0x20 => DAccScale::G16,
_ => DAccScale::G2,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlhDlmAccOdr {
PowerDown,
Hz50,
Hz100,
Hz400,
Hz1000,
}
impl DlhDlmAccOdr {
const MASK: u8 = 0b1111_1000;
fn bits(self) -> u8 {
match self {
DlhDlmAccOdr::PowerDown => 0b000_00 << 3,
DlhDlmAccOdr::Hz50 => 0b001_00 << 3,
DlhDlmAccOdr::Hz100 => 0b001_01 << 3,
DlhDlmAccOdr::Hz400 => 0b001_10 << 3,
DlhDlmAccOdr::Hz1000 => 0b001_11 << 3,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlhcAccOdr {
PowerDown,
Hz1,
Hz10,
Hz25,
Hz50,
Hz100,
Hz200,
Hz400,
}
impl DlhcAccOdr {
const MASK: u8 = 0b1111_0000;
fn bits(self) -> u8 {
match self {
DlhcAccOdr::PowerDown => 0b0000 << 4,
DlhcAccOdr::Hz1 => 0b0001 << 4,
DlhcAccOdr::Hz10 => 0b0010 << 4,
DlhcAccOdr::Hz25 => 0b0011 << 4,
DlhcAccOdr::Hz50 => 0b0100 << 4,
DlhcAccOdr::Hz100 => 0b0101 << 4,
DlhcAccOdr::Hz200 => 0b0110 << 4,
DlhcAccOdr::Hz400 => 0b0111 << 4,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DAccOdr {
PowerDown,
Hz3_125,
Hz6_25,
Hz12_5,
Hz25,
Hz50,
Hz100,
Hz200,
Hz400,
Hz800,
Hz1600,
}
impl DAccOdr {
const MASK: u8 = 0b1111_0000;
fn bits(self) -> u8 {
match self {
DAccOdr::PowerDown => 0b0000 << 4,
DAccOdr::Hz3_125 => 0b0001 << 4,
DAccOdr::Hz6_25 => 0b0010 << 4,
DAccOdr::Hz12_5 => 0b0011 << 4,
DAccOdr::Hz25 => 0b0100 << 4,
DAccOdr::Hz50 => 0b0101 << 4,
DAccOdr::Hz100 => 0b0110 << 4,
DAccOdr::Hz200 => 0b0111 << 4,
DAccOdr::Hz400 => 0b1000 << 4,
DAccOdr::Hz800 => 0b1001 << 4,
DAccOdr::Hz1600 => 0b1010 << 4,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlhMagOdr {
Hz0_75,
Hz1_5,
Hz3,
Hz7_5,
Hz15,
Hz30,
Hz75,
}
impl DlhMagOdr {
const MASK: u8 = 0b0001_1100;
fn bits(self) -> u8 {
match self {
DlhMagOdr::Hz0_75 => 0b000 << 2,
DlhMagOdr::Hz1_5 => 0b001 << 2,
DlhMagOdr::Hz3 => 0b010 << 2,
DlhMagOdr::Hz7_5 => 0b011 << 2,
DlhMagOdr::Hz15 => 0b100 << 2,
DlhMagOdr::Hz30 => 0b101 << 2,
DlhMagOdr::Hz75 => 0b110 << 2,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DlmDlhcMagOdr {
Hz0_75,
Hz1_5,
Hz3,
Hz7_5,
Hz15,
Hz30,
Hz75,
Hz220,
}
impl DlmDlhcMagOdr {
const MASK: u8 = 0b0001_1100;
fn bits(self) -> u8 {
match self {
DlmDlhcMagOdr::Hz0_75 => 0b000 << 2,
DlmDlhcMagOdr::Hz1_5 => 0b001 << 2,
DlmDlhcMagOdr::Hz3 => 0b010 << 2,
DlmDlhcMagOdr::Hz7_5 => 0b011 << 2,
DlmDlhcMagOdr::Hz15 => 0b100 << 2,
DlmDlhcMagOdr::Hz30 => 0b101 << 2,
DlmDlhcMagOdr::Hz75 => 0b110 << 2,
DlmDlhcMagOdr::Hz220 => 0b111 << 2,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DMagOdr {
Hz3_125,
Hz6_25,
Hz12_5,
Hz25,
Hz50,
Hz100,
}
impl DMagOdr {
const MASK: u8 = 0b0001_1100;
fn bits(self) -> u8 {
match self {
DMagOdr::Hz3_125 => 0b000 << 2,
DMagOdr::Hz6_25 => 0b001 << 2,
DMagOdr::Hz12_5 => 0b010 << 2,
DMagOdr::Hz25 => 0b011 << 2,
DMagOdr::Hz50 => 0b100 << 2,
DMagOdr::Hz100 => 0b101 << 2,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Device {
Dlh,
Dlm,
Dlhc,
D,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SA0 {
Low,
High,
}
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error<E> {
I2c(E),
DeviceNotFound,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Vector {
pub x: i16,
pub y: i16,
pub z: i16,
}
mod detect {
pub const D_SA0_HIGH: u8 = 0x1D; pub const D_SA0_LOW: u8 = 0x1E; pub const DLX_ACC_SA0_HIGH: u8 = 0x19; pub const DLX_ACC_SA0_LOW: u8 = 0x18; pub const DLX_MAG: u8 = 0x1E;
pub const WHO_ID_D: u8 = 0x49;
pub const WHO_ID_DLM: u8 = 0x3C;
}
pub struct Dlh;
pub struct Dlm;
pub struct Dlhc;
pub struct D;
pub trait Dlx: Variant {}
impl Dlx for Dlh {}
impl Dlx for Dlm {}
impl Dlx for Dlhc {}
#[allow(async_fn_in_trait)]
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
pub trait Variant: Sized {
const DEVICE: Device;
const MAG_OUT_START: u8;
fn acc_address(sa0: SA0) -> u8;
fn mag_address(sa0: SA0) -> u8;
fn parse_mag(buf: &[u8; 6]) -> Vector;
async fn enable_default<I2C: I2c>(dev: &mut Lsm303<I2C, Self>)
-> Result<(), Error<I2C::Error>>;
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl Variant for D {
const DEVICE: Device = Device::D;
const MAG_OUT_START: u8 = 0x08 | 0x80;
fn acc_address(sa0: SA0) -> u8 {
match sa0 {
SA0::High => detect::D_SA0_HIGH,
SA0::Low => detect::D_SA0_LOW,
}
}
fn mag_address(sa0: SA0) -> u8 {
Self::acc_address(sa0)
}
fn parse_mag(buf: &[u8; 6]) -> Vector {
Vector {
x: i16::from_le_bytes([buf[0], buf[1]]),
y: i16::from_le_bytes([buf[2], buf[3]]),
z: i16::from_le_bytes([buf[4], buf[5]]),
}
}
async fn enable_default<I2C: I2c>(
dev: &mut Lsm303<I2C, Self>,
) -> Result<(), Error<I2C::Error>> {
dev.write_mag_reg(reg_addr::CTRL2, 0x00).await?; dev.write_mag_reg(reg_addr::CTRL1, 0x57).await?; dev.write_mag_reg(reg_addr::CTRL5, 0x64).await?; dev.write_mag_reg(reg_addr::CTRL6, 0x20).await?; dev.write_mag_reg(reg_addr::CTRL7, 0x00).await?; Ok(())
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl Variant for Dlhc {
const DEVICE: Device = Device::Dlhc;
const MAG_OUT_START: u8 = 0x03;
fn acc_address(_sa0: SA0) -> u8 {
detect::DLX_ACC_SA0_HIGH }
fn mag_address(_sa0: SA0) -> u8 {
detect::DLX_MAG
}
fn parse_mag(buf: &[u8; 6]) -> Vector {
Vector {
x: i16::from_le_bytes([buf[1], buf[0]]),
z: i16::from_le_bytes([buf[3], buf[2]]),
y: i16::from_le_bytes([buf[5], buf[4]]),
}
}
async fn enable_default<I2C: I2c>(
dev: &mut Lsm303<I2C, Self>,
) -> Result<(), Error<I2C::Error>> {
dev.write_acc_reg(reg_addr::CTRL_REG4_A, 0x08).await?; dev.write_acc_reg(reg_addr::CTRL_REG1_A, 0x47).await?; dev.write_mag_reg(reg_addr::CRA_REG_M, 0x0C).await?; dev.write_mag_reg(reg_addr::CRB_REG_M, 0x20).await?; dev.write_mag_reg(reg_addr::MR_REG_M, 0x00).await?; Ok(())
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl Variant for Dlm {
const DEVICE: Device = Device::Dlm;
const MAG_OUT_START: u8 = 0x03;
fn acc_address(sa0: SA0) -> u8 {
match sa0 {
SA0::High => detect::DLX_ACC_SA0_HIGH,
SA0::Low => detect::DLX_ACC_SA0_LOW,
}
}
fn mag_address(_sa0: SA0) -> u8 {
detect::DLX_MAG
}
fn parse_mag(buf: &[u8; 6]) -> Vector {
Vector {
x: i16::from_le_bytes([buf[1], buf[0]]),
z: i16::from_le_bytes([buf[3], buf[2]]),
y: i16::from_le_bytes([buf[5], buf[4]]),
}
}
async fn enable_default<I2C: I2c>(
dev: &mut Lsm303<I2C, Self>,
) -> Result<(), Error<I2C::Error>> {
dev.write_acc_reg(reg_addr::CTRL_REG4_A, 0x00).await?; dev.write_acc_reg(reg_addr::CTRL_REG1_A, 0x27).await?; dev.write_mag_reg(reg_addr::CRA_REG_M, 0x0C).await?;
dev.write_mag_reg(reg_addr::CRB_REG_M, 0x20).await?;
dev.write_mag_reg(reg_addr::MR_REG_M, 0x00).await?;
Ok(())
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl Variant for Dlh {
const DEVICE: Device = Device::Dlh;
const MAG_OUT_START: u8 = 0x03;
fn acc_address(sa0: SA0) -> u8 {
match sa0 {
SA0::High => detect::DLX_ACC_SA0_HIGH,
SA0::Low => detect::DLX_ACC_SA0_LOW,
}
}
fn mag_address(_sa0: SA0) -> u8 {
detect::DLX_MAG
}
fn parse_mag(buf: &[u8; 6]) -> Vector {
Vector {
x: i16::from_le_bytes([buf[1], buf[0]]),
y: i16::from_le_bytes([buf[3], buf[2]]),
z: i16::from_le_bytes([buf[5], buf[4]]),
}
}
async fn enable_default<I2C: I2c>(
dev: &mut Lsm303<I2C, Self>,
) -> Result<(), Error<I2C::Error>> {
dev.write_acc_reg(reg_addr::CTRL_REG4_A, 0x00).await?;
dev.write_acc_reg(reg_addr::CTRL_REG1_A, 0x27).await?;
dev.write_mag_reg(reg_addr::CRA_REG_M, 0x0C).await?;
dev.write_mag_reg(reg_addr::CRB_REG_M, 0x20).await?;
dev.write_mag_reg(reg_addr::MR_REG_M, 0x00).await?;
Ok(())
}
}
pub struct AnyLsm303 {
device: Device,
sa0: SA0,
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl AnyLsm303 {
pub async fn detect<I2C: I2c>(
i2c: &mut I2C,
device: Option<Device>,
sa0: Option<SA0>,
) -> Result<Self, Error<I2C::Error>> {
Self::probe(i2c, device, sa0).await
}
async fn probe<I2C: I2c>(
i2c: &mut I2C,
device: Option<Device>,
sa0: Option<SA0>,
) -> Result<Self, Error<I2C::Error>> {
if let (Some(d), Some(s)) = (device, sa0) {
return Ok(AnyLsm303 { device: d, sa0: s });
}
if device.is_none() || device == Some(Device::D) {
if sa0 != Some(SA0::Low)
&& Self::test_reg(i2c, detect::D_SA0_HIGH, reg_addr::WHO_AM_I).await?
== Some(detect::WHO_ID_D)
{
return Ok(AnyLsm303 {
device: Device::D,
sa0: SA0::High,
});
}
if sa0 != Some(SA0::High)
&& Self::test_reg(i2c, detect::D_SA0_LOW, reg_addr::WHO_AM_I).await?
== Some(detect::WHO_ID_D)
{
return Ok(AnyLsm303 {
device: Device::D,
sa0: SA0::Low,
});
}
}
let is_dlx = matches!(
device,
None | Some(Device::Dlhc) | Some(Device::Dlm) | Some(Device::Dlh)
);
if is_dlx {
if sa0 != Some(SA0::Low)
&& Self::test_reg(i2c, detect::DLX_ACC_SA0_HIGH, reg_addr::CTRL_REG1_A)
.await?
.is_some()
{
let dev = match device {
Some(d) => d,
None if Self::mag_is_dlm(i2c).await? => Device::Dlhc,
None => Device::Dlh,
};
return Ok(AnyLsm303 {
device: dev,
sa0: SA0::High,
});
}
if sa0 != Some(SA0::High)
&& Self::test_reg(i2c, detect::DLX_ACC_SA0_LOW, reg_addr::CTRL_REG1_A)
.await?
.is_some()
{
let dev = match device {
Some(d) => d,
None if Self::mag_is_dlm(i2c).await? => Device::Dlm,
None => Device::Dlh,
};
return Ok(AnyLsm303 {
device: dev,
sa0: SA0::Low,
});
}
}
Err(Error::DeviceNotFound)
}
async fn mag_is_dlm<I2C: I2c>(i2c: &mut I2C) -> Result<bool, Error<I2C::Error>> {
Ok(
Self::test_reg(i2c, detect::DLX_MAG, reg_addr::WHO_AM_I_M).await?
== Some(detect::WHO_ID_DLM),
)
}
async fn test_reg<I2C: I2c>(
i2c: &mut I2C,
address: u8,
register: u8,
) -> Result<Option<u8>, Error<I2C::Error>> {
let mut buf = [0u8];
match i2c.write_read(address, &[register], &mut buf).await {
Ok(()) => Ok(Some(buf[0])),
Err(e) => match e.kind() {
ErrorKind::NoAcknowledge(_) => Ok(None),
_ => Err(Error::I2c(e)),
},
}
}
}
impl AnyLsm303 {
pub fn device(&self) -> Device {
self.device
}
pub fn sa0(&self) -> SA0 {
self.sa0
}
pub fn into_driver<I2C: I2c, V: Variant>(self, i2c: I2C) -> Result<Lsm303<I2C, V>, Device> {
if self.device != V::DEVICE {
return Err(self.device);
}
Ok(Lsm303::new_typed(i2c, self.sa0))
}
}
pub struct Lsm303<I2C, V> {
_variant: PhantomData<V>,
i2c: I2C,
sa0: SA0,
}
impl<I2C: I2c, V> Lsm303<I2C, V> {
fn new_typed(i2c: I2C, sa0: SA0) -> Self {
Self {
_variant: PhantomData,
i2c,
sa0,
}
}
pub fn release(self) -> I2C {
self.i2c
}
}
impl<I2C: I2c> Lsm303<I2C, Dlh> {
pub fn new(i2c: I2C, sa0: SA0) -> Self {
Self::new_typed(i2c, sa0)
}
}
impl<I2C: I2c> Lsm303<I2C, Dlm> {
pub fn new(i2c: I2C, sa0: SA0) -> Self {
Self::new_typed(i2c, sa0)
}
}
impl<I2C: I2c> Lsm303<I2C, Dlhc> {
pub fn new(i2c: I2C, sa0: SA0) -> Self {
Self::new_typed(i2c, sa0)
}
}
impl<I2C: I2c> Lsm303<I2C, D> {
pub fn new(i2c: I2C, sa0: SA0) -> Self {
Self::new_typed(i2c, sa0)
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c, V: Variant> Lsm303<I2C, V> {
async fn write_register(
&mut self,
address: u8,
reg: u8,
value: u8,
) -> Result<(), Error<I2C::Error>> {
self.i2c
.write(address, &[reg, value])
.await
.map_err(Error::I2c)
}
async fn read_register(&mut self, address: u8, reg: u8) -> Result<u8, Error<I2C::Error>> {
let mut buf = [0u8; 1];
self.i2c
.write_read(address, &[reg], &mut buf)
.await
.map_err(Error::I2c)?;
Ok(buf[0])
}
pub async fn write_acc_reg(&mut self, reg: u8, value: u8) -> Result<(), Error<I2C::Error>> {
let address = V::acc_address(self.sa0);
self.write_register(address, reg, value).await
}
pub async fn read_acc_reg(&mut self, reg: u8) -> Result<u8, Error<I2C::Error>> {
let address = V::acc_address(self.sa0);
self.read_register(address, reg).await
}
pub async fn write_mag_reg(&mut self, reg: u8, value: u8) -> Result<(), Error<I2C::Error>> {
let address = V::mag_address(self.sa0);
self.write_register(address, reg, value).await
}
pub async fn read_mag_reg(&mut self, reg: u8) -> Result<u8, Error<I2C::Error>> {
let address = V::mag_address(self.sa0);
self.read_register(address, reg).await
}
pub async fn enable_default(&mut self) -> Result<(), Error<I2C::Error>> {
V::enable_default(self).await
}
pub async fn read_accel(&mut self) -> Result<Vector, Error<I2C::Error>> {
let address = V::acc_address(self.sa0);
let mut buf = [0u8; 6];
self.i2c
.write_read(address, &[reg_addr::OUT_X_L_A | 0x80], &mut buf)
.await
.map_err(Error::I2c)?;
Ok(Vector {
x: i16::from_le_bytes([buf[0], buf[1]]),
y: i16::from_le_bytes([buf[2], buf[3]]),
z: i16::from_le_bytes([buf[4], buf[5]]),
})
}
pub async fn read_mag(&mut self) -> Result<Vector, Error<I2C::Error>> {
let address = V::mag_address(self.sa0);
let mut buf = [0u8; 6];
self.i2c
.write_read(address, &[V::MAG_OUT_START], &mut buf)
.await
.map_err(Error::I2c)?;
Ok(V::parse_mag(&buf))
}
pub async fn read(&mut self) -> Result<(Vector, Vector), Error<I2C::Error>> {
Ok((self.read_accel().await?, self.read_mag().await?))
}
async fn modify_acc_reg(
&mut self,
reg: u8,
mask: u8,
bits: u8,
) -> Result<(), Error<I2C::Error>> {
let current = self.read_acc_reg(reg).await?;
self.write_acc_reg(reg, (current & !mask) | (bits & mask))
.await
}
async fn modify_mag_reg(
&mut self,
reg: u8,
mask: u8,
bits: u8,
) -> Result<(), Error<I2C::Error>> {
let current = self.read_mag_reg(reg).await?;
self.write_mag_reg(reg, (current & !mask) | (bits & mask))
.await
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c> Lsm303<I2C, Dlh> {
pub async fn set_accel_scale(
&mut self,
scale: DlhDlmAccScale,
) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL_REG4_A, DlhDlmAccScale::MASK, scale.bits())
.await
}
pub async fn set_accel_odr(&mut self, odr: DlhDlmAccOdr) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL_REG1_A, DlhDlmAccOdr::MASK, odr.bits())
.await
}
pub async fn set_mag_odr(&mut self, odr: DlhMagOdr) -> Result<(), Error<I2C::Error>> {
self.modify_mag_reg(reg_addr::CRA_REG_M, DlhMagOdr::MASK, odr.bits())
.await
}
pub async fn read_accel_scale(&mut self) -> Result<DlhDlmAccScale, Error<I2C::Error>> {
let reg = self.read_acc_reg(reg_addr::CTRL_REG4_A).await?;
Ok(DlhDlmAccScale::from_reg(reg))
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c> Lsm303<I2C, Dlm> {
pub async fn set_accel_scale(
&mut self,
scale: DlhDlmAccScale,
) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL_REG4_A, DlhDlmAccScale::MASK, scale.bits())
.await
}
pub async fn set_accel_odr(&mut self, odr: DlhDlmAccOdr) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL_REG1_A, DlhDlmAccOdr::MASK, odr.bits())
.await
}
pub async fn set_mag_odr(&mut self, odr: DlmDlhcMagOdr) -> Result<(), Error<I2C::Error>> {
self.modify_mag_reg(reg_addr::CRA_REG_M, DlmDlhcMagOdr::MASK, odr.bits())
.await
}
pub async fn read_accel_scale(&mut self) -> Result<DlhDlmAccScale, Error<I2C::Error>> {
let reg = self.read_acc_reg(reg_addr::CTRL_REG4_A).await?;
Ok(DlhDlmAccScale::from_reg(reg))
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c> Lsm303<I2C, Dlhc> {
pub async fn set_accel_scale(&mut self, scale: DlhcAccScale) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL_REG4_A, DlhcAccScale::MASK, scale.bits())
.await
}
pub async fn set_accel_odr(&mut self, odr: DlhcAccOdr) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL_REG1_A, DlhcAccOdr::MASK, odr.bits())
.await
}
pub async fn set_mag_odr(&mut self, odr: DlmDlhcMagOdr) -> Result<(), Error<I2C::Error>> {
self.modify_mag_reg(reg_addr::CRA_REG_M, DlmDlhcMagOdr::MASK, odr.bits())
.await
}
pub async fn read_accel_scale(&mut self) -> Result<DlhcAccScale, Error<I2C::Error>> {
let reg = self.read_acc_reg(reg_addr::CTRL_REG4_A).await?;
Ok(DlhcAccScale::from_reg(reg))
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c> Lsm303<I2C, D> {
pub async fn set_accel_scale(&mut self, scale: DAccScale) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL2, DAccScale::MASK, scale.bits())
.await
}
pub async fn set_accel_odr(&mut self, odr: DAccOdr) -> Result<(), Error<I2C::Error>> {
self.modify_acc_reg(reg_addr::CTRL1, DAccOdr::MASK, odr.bits())
.await
}
pub async fn set_mag_scale(&mut self, scale: DMagScale) -> Result<(), Error<I2C::Error>> {
self.modify_mag_reg(reg_addr::CTRL6, DMagScale::MASK, scale.bits())
.await
}
pub async fn set_mag_odr(&mut self, odr: DMagOdr) -> Result<(), Error<I2C::Error>> {
self.modify_mag_reg(reg_addr::CTRL5, DMagOdr::MASK, odr.bits())
.await
}
pub async fn read_accel_scale(&mut self) -> Result<DAccScale, Error<I2C::Error>> {
let reg = self.read_acc_reg(reg_addr::CTRL2).await?;
Ok(DAccScale::from_reg(reg))
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c, V: Dlx> Lsm303<I2C, V> {
pub async fn set_mag_scale(&mut self, scale: DlxMagScale) -> Result<(), Error<I2C::Error>> {
self.modify_mag_reg(reg_addr::CRB_REG_M, DlxMagScale::MASK, scale.bits())
.await
}
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
#[allow(async_fn_in_trait)]
pub trait HasTemperature<I2C: I2c> {
async fn read_temperature(&mut self) -> Result<i16, Error<I2C::Error>>;
}
#[maybe_async_cfg::maybe(sync(feature = "sync", keep_self), async(feature = "async", keep_self))]
impl<I2C: I2c> HasTemperature<I2C> for Lsm303<I2C, D> {
async fn read_temperature(&mut self) -> Result<i16, Error<I2C::Error>> {
let temp_out_l = self.read_acc_reg(TEMP_OUT_L).await?;
let temp_out_h = self.read_acc_reg(TEMP_OUT_H).await?;
let raw = i16::from_le_bytes([temp_out_l, temp_out_h]);
Ok((raw << 4) >> 4)
}
}