pub struct Lis3dh<CORE> { /* private fields */ }
Expand description
LIS3DH
driver.
Implementations§
source§impl<I2C, E> Lis3dh<Lis3dhI2C<I2C>>
impl<I2C, E> Lis3dh<Lis3dhI2C<I2C>>
sourcepub fn new_i2c(
i2c: I2C,
address: SlaveAddr
) -> Result<Self, Error<E, Infallible>>
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();
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>>
impl<SPI, NSS, ESPI, ENSS> Lis3dh<Lis3dhSPI<SPI, NSS>>
sourcepub fn new_spi(spi: SPI, nss: NSS) -> Result<Self, Error<ESPI, ENSS>>
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();
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,
impl<CORE> Lis3dh<CORE>where
CORE: Lis3dhCore,
sourcepub fn configure(
&mut self,
conf: Configuration
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn configure( &mut self, conf: Configuration ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Configure the device
sourcepub fn get_device_id(
&mut self
) -> Result<u8, Error<CORE::BusError, CORE::PinError>>
pub fn get_device_id( &mut self ) -> Result<u8, Error<CORE::BusError, CORE::PinError>>
WHO_AM_I
register.
sourcepub fn set_mode(
&mut self,
mode: Mode
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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.
From | To | Wait for |
---|---|---|
HighResolution | LowPower | 1/datarate |
HighResolution | Normal | 1/datarate |
Normal | LowPower | 1/datarate |
Normal | HighResolution | 7/datarate |
LowPower | Normal | 1/datarate |
LowPower | HighResolution | 7/datarate |
sourcepub fn get_mode(
&mut self
) -> Result<Mode, Error<CORE::BusError, CORE::PinError>>
pub fn get_mode( &mut self ) -> Result<Mode, Error<CORE::BusError, CORE::PinError>>
Read the current operating mode.
sourcepub fn set_datarate(
&mut self,
datarate: DataRate
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn set_datarate( &mut self, datarate: DataRate ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Data rate selection.
sourcepub fn get_datarate(
&mut self
) -> Result<DataRate, Error<CORE::BusError, CORE::PinError>>
pub fn get_datarate( &mut self ) -> Result<DataRate, Error<CORE::BusError, CORE::PinError>>
Read the current data selection rate.
sourcepub fn set_range(
&mut self,
range: Range
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn set_range( &mut self, range: Range ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Full-scale selection.
sourcepub fn get_range(
&mut self
) -> Result<Range, Error<CORE::BusError, CORE::PinError>>
pub fn get_range( &mut self ) -> Result<Range, Error<CORE::BusError, CORE::PinError>>
Read the current full-scale.
sourcepub fn set_ref(
&mut self,
reference: u8
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn set_ref( &mut self, reference: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Set REFERENCE
register.
sourcepub fn get_ref(&mut self) -> Result<u8, Error<CORE::BusError, CORE::PinError>>
pub fn get_ref(&mut self) -> Result<u8, Error<CORE::BusError, CORE::PinError>>
Read the REFERENCE
register.
sourcepub fn get_status(
&mut self
) -> Result<DataStatus, Error<CORE::BusError, CORE::PinError>>
pub fn get_status( &mut self ) -> Result<DataStatus, Error<CORE::BusError, CORE::PinError>>
Accelerometer data-available status.
sourcepub fn is_data_ready(
&mut self
) -> Result<bool, Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn enable_temp(
&mut self,
enable: bool
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn get_temp_out(
&mut self
) -> Result<i16, Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn get_temp_outf(
&mut self
) -> Result<f32, Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn set_click_threshold(
&mut self,
lir_click: bool,
threshold: u8
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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).
sourcepub fn set_click_time_limit(
&mut self,
time_limit: u8
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn enable_xyz_click_detection(
&mut self,
count: ClickCount
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn enable_xyz_click_detection( &mut self, count: ClickCount ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Enable single- or double-click detection.
sourcepub fn click_count(
&mut self
) -> Result<usize, Error<CORE::BusError, CORE::PinError>>
pub fn click_count( &mut self ) -> Result<usize, Error<CORE::BusError, CORE::PinError>>
Report number of clicks detected.
sourcepub fn register_clear_bits(
&mut self,
reg: Register,
bits: u8
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn register_set_bits(
&mut self,
reg: Register,
bits: u8
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn configure_interrupt_pin<P: IrqPin>(
&mut self,
pin: P
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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()
})?;
sourcepub fn configure_irq_src<I: Interrupt>(
&mut self,
int: I,
interrupt_mode: InterruptMode,
interrupt_config: InterruptConfig
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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(),
)?;
sourcepub 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>>
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,
)?;
sourcepub fn configure_irq_duration<I: Interrupt>(
&mut self,
_int: I,
duration: Duration
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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);
sourcepub fn configure_irq_threshold<I: Interrupt>(
&mut self,
_int: I,
threshold: Threshold
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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);
sourcepub fn get_irq_src<I: Interrupt>(
&mut self,
_int: I
) -> Result<InterruptSource, Error<CORE::BusError, CORE::PinError>>
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.
sourcepub fn configure_switch_to_low_power(
&mut self,
threshold: Threshold,
duration: Duration
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
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)?;
sourcepub fn reboot_memory_content(
&mut self
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn reboot_memory_content( &mut self ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Reboot memory content
sourcepub fn enable_fifo(
&mut self,
mode: FifoMode,
threshold: u8
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn enable_fifo( &mut self, mode: FifoMode, threshold: u8 ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Configures FIFO and then enables it
sourcepub fn disable_fifo(
&mut self
) -> Result<(), Error<CORE::BusError, CORE::PinError>>
pub fn disable_fifo( &mut self ) -> Result<(), Error<CORE::BusError, CORE::PinError>>
Disable FIFO. This resets the FIFO state
sourcepub fn get_fifo_status(
&mut self
) -> Result<FifoStatus, Error<CORE::BusError, CORE::PinError>>
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>
impl<CORE> Accelerometer for Lis3dh<CORE>
source§fn accel_norm(&mut self) -> Result<F32x3, AccelerometerError<Self::Error>>
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>>
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>
type Error = Error<<CORE as Lis3dhCore>::BusError, <CORE as Lis3dhCore>::PinError>
source§impl<CORE> Lis3dhCore for Lis3dh<CORE>where
CORE: Lis3dhCore,
impl<CORE> Lis3dhCore for Lis3dh<CORE>where
CORE: Lis3dhCore,
type BusError = <CORE as Lis3dhCore>::BusError
type PinError = <CORE as Lis3dhCore>::PinError
fn write_register( &mut self, register: Register, value: u8 ) -> Result<(), Error<Self::BusError, Self::PinError>>
fn read_register( &mut self, register: Register ) -> Result<u8, Error<Self::BusError, Self::PinError>>
fn read_accel_bytes( &mut self ) -> Result<[u8; 6], Error<Self::BusError, Self::PinError>>
source§impl<CORE> RawAccelerometer<I16x3> for Lis3dh<CORE>
impl<CORE> RawAccelerometer<I16x3> for Lis3dh<CORE>
source§fn accel_raw(&mut self) -> Result<I16x3, AccelerometerError<Self::Error>>
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
.