Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add addition for range registers to simplify extraction
Browse files Browse the repository at this point in the history
sunsided committed Jul 5, 2024

Verified

This commit was signed with the committer’s verified signature.
sunsided Markus Mayer
1 parent 19c9e62 commit de79bbd
Showing 4 changed files with 97 additions and 9 deletions.
50 changes: 50 additions & 0 deletions src/conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use core::ops::Add;
use crate::{OutXHigh, OutXLow, OutYHigh, OutYLow, OutZHigh, OutZLow};

impl Add<OutXHigh> for OutXLow {
type Output = i16;

fn add(self, hi: OutXHigh) -> Self::Output {
(hi.bits() as i16) << 8 | (self.bits() as i16)
}
}

impl Add<OutXLow> for OutXHigh {
type Output = i16;

fn add(self, lo: OutXLow) -> Self::Output {
lo.add(self)
}
}

impl Add<OutYHigh> for OutYLow {
type Output = i16;

fn add(self, hi: OutYHigh) -> Self::Output {
(hi.bits() as i16) << 8 | (self.bits() as i16)
}
}

impl Add<OutYLow> for OutYHigh {
type Output = i16;

fn add(self, lo: OutYLow) -> Self::Output {
lo.add(self)
}
}

impl Add<OutZHigh> for OutZLow {
type Output = i16;

fn add(self, hi: OutZHigh) -> Self::Output {
(hi.bits() as i16) << 8 | (self.bits() as i16)
}
}

impl Add<OutZLow> for OutZHigh {
type Output = i16;

fn add(self, lo: OutZLow) -> Self::Output {
lo.add(self)
}
}
15 changes: 14 additions & 1 deletion src/gyro.rs
Original file line number Diff line number Diff line change
@@ -296,6 +296,7 @@ writable_register!(ReferenceRegister, RegisterAddress::REFERENCE);
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TemperatureRegister {
/// Temperature data (1LSB/deg - 8-bit resolution). The value is expressed as two's complement.
/// Updates at a rate of 1 Hz.
#[bits(8, access = RO)]
pub temp: u8,
}
@@ -359,6 +360,8 @@ pub struct OutXLow {
/// Low byte of the X-axis value.
///
/// Together with [`OutXHigh`] this forms a reading expressed in two's complement.
///
/// Sensitivity in mdps/digit as well as error depend on [`Sensitivity`] and [`Bandwidth`].
#[bits(8, access = RO)]
pub bits: u8,
}
@@ -382,6 +385,8 @@ pub struct OutXHigh {
/// High byte of the X-axis value.
///
/// Together with [`OutXLowA`] this forms a reading expressed in two's complement.
///
/// Sensitivity in mdps/digit as well as error depend on [`Sensitivity`] and [`Bandwidth`].
#[bits(8, access = RO)]
pub bits: u8,
}
@@ -405,6 +410,8 @@ pub struct OutYLow {
/// Low byte of the Y-axis angular rate value.
///
/// Together with [`OutYHigh`] this forms a reading expressed in two's complement.
///
/// Sensitivity in mdps/digit as well as error depend on [`Sensitivity`] and [`Bandwidth`].
#[bits(8, access = RO)]
pub bits: u8,
}
@@ -428,6 +435,8 @@ pub struct OutYHigh {
/// High byte of the Y-axis angular rate value.
///
/// Together with [`OutYLow`] this forms a reading expressed in two's complement.
///
/// Sensitivity in mdps/digit as well as error depend on [`Sensitivity`] and [`Bandwidth`].
#[bits(8, access = RO)]
pub bits: u8,
}
@@ -451,6 +460,8 @@ pub struct OutZLow {
/// Low byte of the Z-axis angular rate value.
///
/// Together with [`OutZHigh`] this forms a reading expressed in two's complement.
///
/// Sensitivity in mdps/digit as well as error depend on [`Sensitivity`] and [`Bandwidth`].
#[bits(8, access = RO)]
pub bits: u8,
}
@@ -473,7 +484,9 @@ readable_register!(OutZLow, RegisterAddress::OUT_Z_L);
pub struct OutZHigh {
/// High byte of the Z-axis angular rate value.
///
/// Together with [`OutZLowA`] this forms a reading expressed in two's complement.
/// Together with [`OutZLow`] this forms a reading expressed in two's complement.
///
/// Sensitivity in mdps/digit as well as error depend on [`Sensitivity`] and [`Bandwidth`].
#[bits(8, access = RO)]
pub bits: u8,
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ macro_rules! writable_register {
};
}

mod conversions;
mod gyro;
mod types;

40 changes: 32 additions & 8 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -37,25 +37,41 @@ impl OutputDataRate {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum Bandwidth {
/// Selects the narrowest filter bandwidth.
///
/// * 12.5 at 95 Hz ([`OutputDataRate::Hz95`])
/// * 12.5 at 190 Hz ([`OutputDataRate::Hz190`])
/// * 20 at 380 Hz ([`OutputDataRate::Hz380`])
/// * 30 at 760 Hz ([`OutputDataRate::Hz760`])
///
/// Rate noise density: 0.03 ips/√Hz
Narrowest = 0b00,
/// Selects a narrow filter bandwidth.
///
/// * 25 at 95 Hz ([`OutputDataRate::Hz95`])
/// * 25 at 190 Hz ([`OutputDataRate::Hz190`])
/// * 25 at 380 Hz ([`OutputDataRate::Hz380`])
/// * 35 at 760 Hz ([`OutputDataRate::Hz760`])
///
/// Rate noise density: 0.03 ips/√Hz
Narrow = 0b01,
/// Selects a medium filter bandwidth.
///
/// * 25 at 95 Hz ([`OutputDataRate::Hz95`])
/// * 50 at 190 Hz ([`OutputDataRate::Hz190`])
/// * 50 at 380 Hz ([`OutputDataRate::Hz380`])
/// * 50 at 760 Hz ([`OutputDataRate::Hz760`])
///
/// Rate noise density: 0.03 ips/√Hz
Medium = 0b10,
/// Selects a wide filter bandwidth.
///
/// * 25 at 95 Hz ([`OutputDataRate::Hz95`])
/// * 70 at 190 Hz ([`OutputDataRate::Hz190`])
/// * 100 at 380 Hz ([`OutputDataRate::Hz380`])
/// * 100 at 760 Hz ([`OutputDataRate::Hz760`])
///
/// Rate noise density: 0.03 ips/√Hz
Wide = 0b11,
}

@@ -114,13 +130,21 @@ impl HighpassFilterMode {
#[repr(u8)]
pub enum Sensitivity {
/// 250 dps
G250 = 0b00,
///
/// 8.75 mdps/digit; ±10 dps at zero-rate level
D250 = 0b00,
/// 500 dps
G500 = 0b01,
///
/// 17.50 mdps/digit; ±15 dps at zero-rate level
D500 = 0b01,
/// 2000 dps
G2000 = 0b10,
///
/// 70 mdps/digit; ±75 dps at zero-rate level
D2000 = 0b10,
/// 2000 dps
G2000_11 = 0b11,
///
/// 70 mdps/digit; ±75 dps at zero-rate level
D2000_11 = 0b11,
}

impl Sensitivity {
@@ -131,10 +155,10 @@ impl Sensitivity {

pub(crate) const fn from_bits(value: u8) -> Self {
match value {
0b00 => Sensitivity::G250,
0b01 => Sensitivity::G500,
0b10 => Sensitivity::G2000,
0b11 => Sensitivity::G2000_11,
0b00 => Sensitivity::D250,
0b01 => Sensitivity::D500,
0b10 => Sensitivity::D2000,
0b11 => Sensitivity::D2000_11,
_ => unreachable!(),
}
}

0 comments on commit de79bbd

Please sign in to comment.