Struct lis3dh_async::Lis3dh

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

LIS3DH driver.

Implementations§

Create a new LIS3DH driver from the given I2C peripheral using the default config. 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();

Create a new driver instance without talking to the LIS3DH. This can be useful when the accelerometer was already on while the microcontroller rebooted and you need continuous operation.

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();

Create a new driver instance without talking to the LIS3DH. This can be useful when the accelerometer was already on while the microcontroller rebooted and you need continuous operation.

Configure the device

WHO_AM_I register.

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

Read the current operating mode.

Data rate selection.

Read the current data selection rate.

Full-scale selection.

Read the current full-scale.

Set REFERENCE register.

Read the REFERENCE register.

Accelerometer data-available status.

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.

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

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

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

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.

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.

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()
})?;

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(),
)?;

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,
)?;

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);

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);

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

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)?;

Reboot memory content

Configures FIFO and then enables it

Disable FIFO. This resets the FIFO state

Get the status of the FIFO

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.

Get the sample rate of the accelerometer data.

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.

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.