### LIS3DH Orientation Tracking Example Source: https://context7.com/benbergman/lis3dh-rs/llms.txt A comprehensive example demonstrating how to initialize the LIS3DH sensor via I2C, configure its range, and use an orientation tracker to determine the device's orientation from raw acceleration data. This example requires the `accelerometer` and `lis3dh` crates, and platform-specific I2C and delay implementations. ```rust #![no_std] #![no_main] use accelerometer::{RawAccelerometer, Tracker}; use lis3dh::{Lis3dh, SlaveAddr, Range}; fn main() -> ! { // Initialize I2C peripheral (platform-specific) let i2c = /* ... */; // Create LIS3DH driver let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).unwrap(); lis3dh.set_range(Range::G8).unwrap(); // Create orientation tracker let mut tracker = Tracker::new(3700.0); loop { // Wait for new data while !lis3dh.is_data_ready().unwrap() {} // Read acceleration and update orientation let accel = lis3dh.accel_raw().unwrap(); let orientation = tracker.update(accel); // Use orientation for application logic println!("{:?}", orientation); // Delay before next reading delay_ms(100); } } ``` -------------------------------- ### Configure Motion Detection Interrupt Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Configures the LIS3DH to trigger an interrupt on the physical INT1 pin when acceleration exceeds a defined threshold for a specific duration. This example includes setting the data rate, threshold, duration, and routing the interrupt signal. ```rust use lis3dh::{ DataRate, Duration, Interrupt1, InterruptConfig, InterruptMode, IrqPin1Config, LatchInterruptRequest, Detect4D, Range, Threshold, }; let data_rate = DataRate::Hz_200; // Configure threshold: trigger on acceleration > 500mg let threshold = Threshold::mg(Range::G2, 500.0); // Configure duration: motion must persist for 20ms let duration = Duration::seconds(data_rate, 0.020); // Set data rate and interrupt parameters lis3dh.set_datarate(data_rate).unwrap(); lis3dh.configure_irq_threshold(Interrupt1, threshold).unwrap(); lis3dh.configure_irq_duration(Interrupt1, duration).unwrap(); // Configure interrupt source: OR combination, high events, latched lis3dh.configure_irq_src_and_control( Interrupt1, InterruptMode::OrCombination, InterruptConfig::high(), LatchInterruptRequest::Enable, Detect4D::Enable, ).unwrap(); // Route interrupt to physical INT1 pin lis3dh.configure_interrupt_pin(IrqPin1Config { ia1_en: true, ..Default::default() }).unwrap(); // Clear any stale interrupt let _ = lis3dh.get_irq_src(Interrupt1); // Poll for interrupt in main loop loop { let irq_src = lis3dh.get_irq_src(Interrupt1).unwrap(); if irq_src.interrupt_active { println!("Motion detected! X:{} Y:{} Z:{}", irq_src.x_axis_high, irq_src.y_axis_high, irq_src.z_axis_high); } } ``` -------------------------------- ### Read LIS3DH Device ID Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Reads the WHO_AM_I register to verify communication with the LIS3DH sensor. The expected value for a LIS3DH sensor is 0x33. This function is crucial for initial setup and ensuring the sensor is correctly connected and recognized. ```rust // Read device ID (should return 0x33 for LIS3DH) let device_id = lis3dh.get_device_id().unwrap(); assert_eq!(device_id, 0x33); ``` -------------------------------- ### Initialize LIS3DH Driver with I2C or SPI Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Create a new driver instance using either I2C (with selectable slave addresses) or SPI (with a chip select pin). These methods return a Result containing the driver instance or an error. ```rust use lis3dh::{Lis3dh, SlaveAddr}; // Create driver with default I2C address (0x18) let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Default).unwrap(); // Or with alternate address (0x19) let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).unwrap(); ``` ```rust use lis3dh::Lis3dh; // Create driver with SPI and chip select pin let mut lis3dh = Lis3dh::new_spi(spi, chip_select_pin).unwrap(); ``` -------------------------------- ### Configure LIS3DH Driver Settings Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Initialize the driver with a custom configuration struct or update settings like mode, data rate, and measurement range dynamically after initialization. ```rust use lis3dh::{Lis3dh, SlaveAddr, Configuration, Mode, DataRate}; let config = Configuration { mode: Mode::HighResolution, datarate: DataRate::Hz_400, enable_x_axis: true, enable_y_axis: true, enable_z_axis: true, block_data_update: true, enable_temperature: true, }; let mut lis3dh = Lis3dh::new_i2c_with_config(i2c, SlaveAddr::Default, config).unwrap(); ``` -------------------------------- ### Configure FIFO Buffer Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Enables and manages the FIFO buffer for batch data collection, allowing the host to read multiple samples at once. Includes setting the mode, watermark, and checking status flags. ```rust use lis3dh::FifoMode; // Enable FIFO in Stream mode with watermark at 16 samples lis3dh.enable_fifo(FifoMode::Stream, 16).unwrap(); // Check FIFO status let fifo_status = lis3dh.get_fifo_status().unwrap(); println!("FIFO samples: {}", fifo_status.stack_size); println!("Watermark exceeded: {}", fifo_status.watermark); println!("FIFO overrun: {}", fifo_status.overrun); println!("FIFO empty: {}", fifo_status.empty); // Disable FIFO (resets buffer) lis3dh.disable_fifo().unwrap(); ``` -------------------------------- ### Manage Operating Parameters Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Adjust device performance characteristics including operating mode (resolution), output data rate (ODR), and full-scale measurement range. ```rust use lis3dh::Mode; // Set high resolution mode (12-bit output) lis3dh.set_mode(Mode::HighResolution).unwrap(); // Read current mode let current_mode = lis3dh.get_mode().unwrap(); ``` ```rust use lis3dh::DataRate; // Set data rate to 400Hz lis3dh.set_datarate(DataRate::Hz_400).unwrap(); // Read current data rate let rate = lis3dh.get_datarate().unwrap(); ``` ```rust use lis3dh::Range; // Set range to ±8g lis3dh.set_range(Range::G8).unwrap(); // Read current range let range = lis3dh.get_range().unwrap(); ``` -------------------------------- ### Read Temperature Data Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Shows the process of enabling the internal temperature sensor and retrieving the output in both raw and Celsius formats. The sensor must be explicitly enabled before data can be read. ```rust // Enable temperature sensor lis3dh.enable_temp(true).unwrap(); // Read raw temperature value let temp_raw = lis3dh.get_temp_out().unwrap(); // Read temperature in Celsius let temp_celsius = lis3dh.get_temp_outf().unwrap(); println!("Temperature: {:.1}°C", temp_celsius); ``` -------------------------------- ### Configure Click Detection Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Sets up the LIS3DH to detect single or double-click events on the X, Y, or Z axes. It demonstrates configuring thresholds, time limits, and enabling the detection logic. ```rust use lis3dh::ClickCount; // Set click threshold (0-127, in increments of full_scale/128) // lir_click=true keeps interrupt active until CLICK_SRC is read lis3dh.set_click_threshold(true, 40).unwrap(); // Set click time limit (max duration of click event) lis3dh.set_click_time_limit(20).unwrap(); // Enable single-click detection on all axes lis3dh.enable_xyz_click_detection(ClickCount::Single).unwrap(); // Or enable double-click detection lis3dh.enable_xyz_click_detection(ClickCount::Double).unwrap(); // Read click count let clicks = lis3dh.click_count().unwrap(); println!("Clicks detected: {}", clicks); ``` -------------------------------- ### Check Data Status in LIS3DH Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Demonstrates how to query the status register to determine if new acceleration data is available or if an overrun has occurred. It shows both a simple readiness check and a detailed inspection of per-axis status flags. ```rust // Simple check if XYZ data is ready if lis3dh.is_data_ready().unwrap() { let accel = lis3dh.accel_raw().unwrap(); } // Detailed status information let status = lis3dh.get_status().unwrap(); println!("Data ready: {}", status.zyxda); println!("Data overrun: {}", status.zyxor); println!("Per-axis ready: X={}, Y={}, Z={}", status.xyzda.0, status.xyzda.1, status.xyzda.2); ``` -------------------------------- ### Read Acceleration Data Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Retrieve acceleration data in either raw 16-bit format or normalized g-units. The raw method is useful for low-level processing, while the normalized method provides human-readable gravity units. ```rust use accelerometer::RawAccelerometer; // Wait for data to be ready while !lis3dh.is_data_ready().unwrap() {} // Read raw acceleration (left-justified two's complement) let accel = lis3dh.accel_raw().unwrap(); println!("X: {}, Y: {}, Z: {}", accel.x, accel.y, accel.z); ``` ```rust use accelerometer::Accelerometer; // Read normalized acceleration in g's let accel = lis3dh.accel_norm().unwrap(); println!("X: {:.3}g, Y: {:.3}g, Z: {:.3}g", accel.x, accel.y, accel.z); ``` -------------------------------- ### Configure Sleep-to-Wake Mode Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Configures the LIS3DH to automatically transition between low-power and normal operating modes based on motion detection. This is ideal for battery-operated devices. ```rust use lis3dh::{Range, DataRate, Threshold, Duration}; let range = Range::G2; let data_rate = DataRate::Hz_400; // Configure threshold: wake on acceleration > 1.1g let threshold = Threshold::g(range, 1.1); // Configure duration: stay awake for 25ms after motion let duration = Duration::miliseconds(data_rate, 25.0); // Enable sleep-to-wake functionality lis3dh.configure_switch_to_low_power(threshold, duration).unwrap(); lis3dh.set_datarate(data_rate).unwrap(); ``` -------------------------------- ### Configure High-Pass Filter Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Configures the device's internal high-pass filter to remove DC components like gravity from the acceleration data. This is useful for isolating dynamic motion from static orientation. ```rust use lis3dh::{HighPassFilterConfig, HighPassFilterMode, HighPassFilterCutoff}; // Enable high-pass filter for interrupt 1 only lis3dh.configure_high_pass_filter(HighPassFilterConfig { mode: HighPassFilterMode::Normal, cutoff: HighPassFilterCutoff::Lowest, enable_for_interrupt1: true, enable_for_interrupt2: false, enable_for_click: false, enable_for_data: false, // Keep raw data unfiltered }).unwrap(); // Read current high-pass filter configuration let hpf_config = lis3dh.get_high_pass_filter_config().unwrap(); ``` -------------------------------- ### Direct Register Access for LIS3DH Source: https://context7.com/benbergman/lis3dh-rs/llms.txt Allows reading from and writing to individual registers of the LIS3DH sensor for advanced configuration. This includes reading the current value of a register, setting specific bits, and clearing specific bits. Requires the `Register` enum from the `lis3dh` crate. ```rust use lis3dh::Register; // Read a register value let ctrl1 = lis3dh.read_register(Register::CTRL1).unwrap(); // Set specific bits in a register lis3dh.register_set_bits(Register::CTRL1, 0b0000_0111).unwrap(); // Clear specific bits in a register lis3dh.register_clear_bits(Register::CTRL1, 0b0000_0111).unwrap(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.