Struct lis3dh::Lis3dh

source ·
pub struct Lis3dh<CORE> { /* private fields */ }
Expand description

LIS3DH driver.

Implementations§

source§

impl<I2C, E> Lis3dh<Lis3dhI2C<I2C>>
where I2C: WriteRead<Error = E> + Write<Error = E>,

source

pub fn new_i2c( i2c: I2C, address: SlaveAddr ) -> Result<Self, Error<E, Infallible>>

Create a new LIS3DH driver from the given I2C peripheral. Default is Hz_400 HighResolution. An example using the nrf52840_hal:

use nrf52840_hal::gpio::{Level, PushPull};
use lis3dh::Lis3dh;
 
let peripherals = nrf52840_hal::pac::Peripherals::take().unwrap();
let pins = p0::Parts::new(peripherals.P0);
 
let twim0_scl = pins.p0_31.into_floating_input().degrade();
let twim0_sda = pins.p0_30.into_floating_input().degrade();
 
let i2c = nrf52840_hal::twim::Twim::new(
    peripherals.TWIM0,
    nrf52840_hal::twim::Pins {
        scl: twim0_scl,
        sda: twim0_sda,
    },
    nrf52840_hal::twim::Frequency::K400,
);
 
let lis3dh = Lis3dh::new_i2c(i2c, lis3dh::SlaveAddr::Default).unwrap();
source

pub fn new_i2c_with_config( i2c: I2C, address: SlaveAddr, config: Configuration ) -> Result<Self, Error<E, Infallible>>

source§

impl<SPI, NSS, ESPI, ENSS> Lis3dh<Lis3dhSPI<SPI, NSS>>
where SPI: Write<u8, Error = ESPI> + Transfer<u8, Error = ESPI>, NSS: OutputPin<Error = ENSS>,

source

pub fn new_spi(spi: SPI, nss: NSS) -> Result<Self, Error<ESPI, ENSS>>

Create a new LIS3DH driver from the given SPI peripheral. An example using the nrf52840_hal:

use nrf52840_hal::gpio::{p0::{Parts, P0_28}, *};
use nrf52840_hal::spim::Spim;
use lis3dh::Lis3dh;
 
let peripherals = nrf52840_hal::pac::Peripherals::take().unwrap();
let port0 = Parts::new(peripherals.P0);
 
// define the chip select pin
let cs: P0_28<Output<PushPull>> = port0.p0_28.into_push_pull_output(Level::High);
 
// spi pins: clock, miso, mosi
let pins = nrf52840_hal::spim::Pins {
    sck: port0.p0_31.into_push_pull_output(Level::Low).degrade(),
    miso: Some(port0.p0_30.into_push_pull_output(Level::Low).degrade()),
    mosi: Some(port0.p0_29.into_floating_input().degrade()),
};
 
// set up the spi peripheral
let spi = Spim::new(
    peripherals.SPIM2,
    pins,
    nrf52840_hal::spim::Frequency::K500,
    nrf52840_hal::spim::MODE_0,
    0,
);

// create and initialize the sensor
let lis3dh = Lis3dh::new_spi(spi, cs).unwrap();
source

pub fn new_spi_with_config( spi: SPI, nss: NSS, config: Configuration ) -> Result<Self, Error<ESPI, ENSS>>

source§

impl<CORE> Lis3dh<CORE>
where CORE: Lis3dhCore,

source

pub fn configure( &mut self, conf: Configuration ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configure the device

source

pub fn get_device_id( &mut self ) -> Result<u8, Error<CORE::BusError, CORE::PinError>>

WHO_AM_I register.

source

pub fn set_mode( &mut self, mode: Mode ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Operating mode selection. CTRL_REG1: LPen bit, CTRL_REG4: HR bit. You need to wait for stabilization after setting. In future this function will be deprecated and instead take a Delay to do this for you.

FromToWait for
HighResolutionLowPower1/datarate
HighResolutionNormal1/datarate
NormalLowPower1/datarate
NormalHighResolution7/datarate
LowPowerNormal1/datarate
LowPowerHighResolution7/datarate
source

pub fn get_mode( &mut self ) -> Result<Mode, Error<CORE::BusError, CORE::PinError>>

Read the current operating mode.

source

pub fn set_datarate( &mut self, datarate: DataRate ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Data rate selection.

source

pub fn get_datarate( &mut self ) -> Result<DataRate, Error<CORE::BusError, CORE::PinError>>

Read the current data selection rate.

source

pub fn set_range( &mut self, range: Range ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Full-scale selection.

source

pub fn get_range( &mut self ) -> Result<Range, Error<CORE::BusError, CORE::PinError>>

Read the current full-scale.

source

pub fn set_ref( &mut self, reference: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Set REFERENCE register.

source

pub fn get_ref(&mut self) -> Result<u8, Error<CORE::BusError, CORE::PinError>>

Read the REFERENCE register.

source

pub fn get_status( &mut self ) -> Result<DataStatus, Error<CORE::BusError, CORE::PinError>>

Accelerometer data-available status.

source

pub fn is_data_ready( &mut self ) -> Result<bool, Error<CORE::BusError, CORE::PinError>>

Convenience function for STATUS_REG to confirm all three X, Y and Z-axis have new data available for reading by accel_raw and associated function calls.

source

pub fn enable_temp( &mut self, enable: bool ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Temperature sensor enable. TEMP_CGF_REG: TEMP_EN, the BDU bit in CTRL_REG4 is also set.

source

pub fn get_temp_out( &mut self ) -> Result<i16, Error<CORE::BusError, CORE::PinError>>

Raw temperature sensor data as i16. The temperature sensor must be enabled via enable_temp prior to reading.

source

pub fn get_temp_outf( &mut self ) -> Result<f32, Error<CORE::BusError, CORE::PinError>>

Temperature sensor data converted to f32. Output is in degree celsius. The temperature sensor must be enabled via enable_temp prior to reading.

source

pub fn set_click_threshold( &mut self, lir_click: bool, threshold: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configure click threshold.

lir_click: If this bit is set, the interrupt is kept high until the CLICK_SRC register is read. Otherwise, the interrupt is high for the duration of the click event.

threshold: The threshold for click detection from 0-127, in increments of (full scale (in G’s) / 128).

source

pub fn set_click_time_limit( &mut self, time_limit: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Set click time limit.

time_limit: The time interval between the start and end of a click event, from 0-255, in increments of (1/ODR) e.g 2.5ms at 400Hz.

source

pub fn enable_xyz_click_detection( &mut self, count: ClickCount ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Enable single- or double-click detection.

source

pub fn click_count( &mut self ) -> Result<usize, Error<CORE::BusError, CORE::PinError>>

Report number of clicks detected.

source

pub fn register_clear_bits( &mut self, reg: Register, bits: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Clear the given bits in the given register. For example:

lis3dh.register_clear_bits(0b0110)

This call clears (sets to 0) the bits at index 1 and 2. Other bits of the register are not touched.

source

pub fn register_set_bits( &mut self, reg: Register, bits: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Set the given bits in the given register. For example:

lis3dh.register_set_bits(0b0110)

This call sets to 1 the bits at index 1 and 2. Other bits of the register are not touched.

source

pub fn configure_interrupt_pin<P: IrqPin>( &mut self, pin: P ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configure one of the interrupt pins

lis3dh.configure_interrupt_pin(IrqPin1Config {
    // Raise if interrupt 1 is raised
    ia1_en: true,
    // Disable for all other interrupts
    ..IrqPin1Config::default()
})?;
source

pub fn configure_irq_src<I: Interrupt>( &mut self, int: I, interrupt_mode: InterruptMode, interrupt_config: InterruptConfig ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configure an IRQ source

Example: configure interrupt 1 to fire when there is movement along any of the axes.

lis3dh.configure_irq_src(
    lis3dh::Interrupt1,
    lis3dh::InterruptMode::Movement,
    lis3dh::InterruptConfig::high_and_low(),
)?;
source

pub fn configure_irq_src_and_control<I: Interrupt>( &mut self, _int: I, interrupt_mode: InterruptMode, interrupt_config: InterruptConfig, latch_interrupt_request: LatchInterruptRequest, detect_4d: Detect4D ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configure an IRQ source.

LIS (latch interrupt request) will latch (keep active) the interrupt until the Lis3dh::get_irq_src is read.

4D detection is a subset of the 6D detection where detection on the Z axis is disabled. This setting only has effect when the interrupt mode is either Movement or Position.

Example: configure interrupt 1 to fire when there is movement along any of the axes.

lis3dh.configure_irq_src(
    lis3dh::Interrupt1,
    lis3dh::InterruptMode::Movement,
    lis3dh::InterruptConfig::high_and_low(),
    lis3dh::LatchInterruptRequest::Enable,
    lis3dh::Detect4D::Enable,
)?;
source

pub fn configure_irq_duration<I: Interrupt>( &mut self, _int: I, duration: Duration ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Set the minimum duration for the Interrupt event to be recognized.

Example: the event has to last at least 25 miliseconds to be recognized.

// let mut lis3dh = ...
let duration = Duration::miliseconds(DataRate::Hz_400, 25.0);
lis3dh.configure_irq_duration(duration);
source

pub fn configure_irq_threshold<I: Interrupt>( &mut self, _int: I, threshold: Threshold ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Set the minimum magnitude for the Interrupt event to be recognized.

Example: the event has to have a magnitude of at least 1.1g to be recognized.

// let mut lis3dh = ...
let threshold = Threshold::g(Range::G2, 1.1);
lis3dh.configure_irq_threshold(threshold);
source

pub fn get_irq_src<I: Interrupt>( &mut self, _int: I ) -> Result<InterruptSource, Error<CORE::BusError, CORE::PinError>>

Get interrupt source. The interrupt_active field is true when an interrupt is active. The other fields specify what measurement caused the interrupt.

source

pub fn configure_switch_to_low_power( &mut self, threshold: Threshold, duration: Duration ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configure ‘Sleep to wake’ and ‘Return to sleep’ threshold and duration.

The LIS3DH can be programmed to automatically switch to low-power mode upon recognition of a determined event.
Once the event condition is over, the device returns back to the preset normal or highresolution mode.

Example: enter low-power mode. When a measurement above 1.1g is registered, then wake up for 25ms to send the data.

// let mut lis3dh = ...

let range = Range::default();
let data_rate = DataRate::Hz_400;
 
let threshold = Threshold::g(range, 1.1);
let duration = Duration::miliseconds(data_rate, 25.0);
 
lis3dh.configure_switch_to_low_power(threshold, duration)?;
 
lis3dh.set_datarate(data_rate)?;
source

pub fn reboot_memory_content( &mut self ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Reboot memory content

source

pub fn enable_fifo( &mut self, mode: FifoMode, threshold: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Configures FIFO and then enables it

source

pub fn disable_fifo( &mut self ) -> Result<(), Error<CORE::BusError, CORE::PinError>>

Disable FIFO. This resets the FIFO state

source

pub fn get_fifo_status( &mut self ) -> Result<FifoStatus, Error<CORE::BusError, CORE::PinError>>

Get the status of the FIFO

Trait Implementations§

source§

impl<CORE> Accelerometer for Lis3dh<CORE>
where CORE: Lis3dhCore, CORE::PinError: Debug, CORE::BusError: Debug,

source§

fn accel_norm(&mut self) -> Result<F32x3, AccelerometerError<Self::Error>>

Get normalized ±g reading from the accelerometer. You should be reading based on data ready interrupt or if reading in a tight loop you should waiting for is_data_ready.

source§

fn sample_rate(&mut self) -> Result<f32, AccelerometerError<Self::Error>>

Get the sample rate of the accelerometer data.

§

type Error = Error<<CORE as Lis3dhCore>::BusError, <CORE as Lis3dhCore>::PinError>

Error type
source§

impl<CORE> Lis3dhCore for Lis3dh<CORE>
where CORE: Lis3dhCore,

§

type BusError = <CORE as Lis3dhCore>::BusError

§

type PinError = <CORE as Lis3dhCore>::PinError

source§

fn write_register( &mut self, register: Register, value: u8 ) -> Result<(), Error<Self::BusError, Self::PinError>>

source§

fn read_register( &mut self, register: Register ) -> Result<u8, Error<Self::BusError, Self::PinError>>

source§

fn read_accel_bytes( &mut self ) -> Result<[u8; 6], Error<Self::BusError, Self::PinError>>

source§

impl<CORE> RawAccelerometer<I16x3> for Lis3dh<CORE>
where CORE: Lis3dhCore, CORE::PinError: Debug, CORE::BusError: Debug,

source§

fn accel_raw(&mut self) -> Result<I16x3, AccelerometerError<Self::Error>>

Get raw acceleration data from the accelerometer. You should be reading based on data ready interrupt or if reading in a tight loop you should waiting for is_data_ready.

§

type Error = Error<<CORE as Lis3dhCore>::BusError, <CORE as Lis3dhCore>::PinError>

Error type

Auto Trait Implementations§

§

impl<CORE> Freeze for Lis3dh<CORE>
where CORE: Freeze,

§

impl<CORE> RefUnwindSafe for Lis3dh<CORE>
where CORE: RefUnwindSafe,

§

impl<CORE> Send for Lis3dh<CORE>
where CORE: Send,

§

impl<CORE> Sync for Lis3dh<CORE>
where CORE: Sync,

§

impl<CORE> Unpin for Lis3dh<CORE>
where CORE: Unpin,

§

impl<CORE> UnwindSafe for Lis3dh<CORE>
where CORE: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.