### new Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Creates a new driver instance from an I2C peripheral. This is the constructor for the Lsm303dlhc driver. ```APIDOC ## new ### Description Creates a new driver from a I2C peripheral. ### Method Constructor ### Signature pub fn new(i2c: I2C) -> Result ### Parameters - **i2c** (I2C) - Required - The I2C peripheral to use for communication. ### Return Value - **Result** - Returns a new Lsm303dlhc instance on success, or an error of type E on failure. ``` -------------------------------- ### new(i2c: I2C) -> Result Source: https://docs.rs/lsm303dlhc/latest/src/lsm303dlhc/lib.rs.html Creates a new LSM303DLHC driver from an I2C peripheral. It configures the accelerometer to operate at 400 Hz, the magnetometer in continuous mode, and enables the temperature sensor. ```APIDOC ## new(i2c: I2C) -> Result ### Description Creates a new LSM303DLHC driver from an I2C peripheral. It configures the accelerometer to operate at 400 Hz, the magnetometer in continuous mode, and enables the temperature sensor. ### Method Constructor ### Parameters - **i2c** (I2C) - Required - The I2C peripheral to use for communication. ### Returns - **Result** - The new driver instance or an error of type E. ### Example ```rust let lsm303dlhc = Lsm303dlhc::new(i2c)?; ``` ``` -------------------------------- ### lsm303dlhc lib.rs source code Source: https://docs.rs/lsm303dlhc/latest/src/lsm303dlhc/lib.rs.html The complete source code of the lsm303dlhc crate's lib.rs file. It defines the Lsm303dlhc driver struct with methods for accelerometer, magnetometer, and temperature sensor access, using embedded-hal I2C traits. The code includes register configuration for 400 Hz accelerometer operation, continuous magnetometer mode, and temperature sensor enablement. ```rust //! A platform agnostic driver to interface with the LSM303DLHC (accelerometer + compass) //! //! This driver was built using [`embedded-hal`] traits. //! //! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.2 //! //! # Examples //! //! You should find at least one example in the [f3] crate. //! //! [f3]: https://docs.rs/f3/~0.6 #![deny(missing_docs)] #![deny(warnings)] #![no_std] extern crate cast; extern crate embedded_hal as hal; extern crate generic_array; use core::mem; use cast::u16; use generic_array::typenum::consts::*; use generic_array::{ArrayLength, GenericArray}; use hal::blocking::i2c::{Write, WriteRead}; mod accel; mod mag; /// LSM303DLHC driver pub struct Lsm303dlhc { i2c: I2C, } impl Lsm303dlhc where I2C: WriteRead + Write, { /// Creates a new driver from a I2C peripheral pub fn new(i2c: I2C) -> Result { let mut lsm303dlhc = Lsm303dlhc { i2c }; // TODO reset all the registers / the device // configure the accelerometer to operate at 400 Hz lsm303dlhc.write_accel_register(accel::Register::CTRL_REG1_A, 0b0111_0_111)?; // configure the magnetometer to operate in continuous mode lsm303dlhc.write_mag_register(mag::Register::MR_REG_M, 0b00)?; // enable the temperature sensor lsm303dlhc.write_mag_register(mag::Register::CRA_REG_M, 0b0001000 | (1 << 7))?; Ok(lsm303dlhc) } /// Accelerometer measurements pub fn accel(&mut self) -> Result { let buffer: GenericArray = self.read_accel_registers(accel::Register::OUT_X_L_A)?; Ok(I16x3 { x: (u16(buffer[0]) + (u16(buffer[1]) << 8)) as i16, y: (u16(buffer[2]) + (u16(buffer[3]) << 8)) as i16, z: (u16(buffer[4]) + (u16(buffer[5]) << 8)) as i16, }) } /// Sets the accelerometer output data rate pub fn accel_odr(&mut self, odr: AccelOdr) -> Result<(), E> { self.modify_accel_register(accel::Register::CTRL_REG1_A, |r| { r & !(0b1111 << 4) | ((odr as u8) << 4) }) } /// Magnetometer measurements pub fn mag(&mut self) -> Result { let buffer: GenericArray = self.read_mag_registers(mag::Register::OUT_X_H_M)?; Ok(I16x3 { x: (u16(buffer[1]) + (u16(buffer[0]) << 8)) as i16, y: (u16(buffer[5]) + (u16(buffer[4]) << 8)) as i16, z: (u16(buffer[3]) + (u16(buffer[2]) << 8)) as i16, }) } /// Sets the magnetometer output data rate pub fn mag_odr(&mut self, odr: MagOdr) -> Result<(), E> { self.modify_mag_register(mag::Register::CRA_REG_M, |r| { r & !(0b111 << 2) | ((odr as u8) << 2) }) } /// Temperature sensor measurement /// /// - Resolution: 12-bit /// - Range: [-40, +85] pub fn temp(&mut self) -> Result { let temp_out_l = self.read_mag_register(mag::Register::TEMP_OUT_L_M)?; let temp_out_h = self.read_mag_register(mag::Register::TEMP_OUT_H_M)?; Ok(((u16(temp_out_l) + (u16(temp_out_h) << 8)) as i16) >> 4) } /// Changes the `sensitivity` of the accelerometer pub fn set_accel_sensitivity(&mut self, sensitivity: Sensitivity) -> Result<(), E> { self.modify_accel_register(accel::Register::CTRL_REG4_A, |r| { r & !(0b11 << 4) | (sensitivity.value() << 4) }) } fn modify_accel_register(&mut self, reg: accel::Register, f: F) -> Result<(), E> where F: FnOnce(u8) -> u8, { let r = self.read_accel_register(reg)?; self.write_accel_register(reg, f(r))?; Ok(()) } fn modify_mag_register(&mut self, reg: mag::Register, f: F) -> Result<(), E> where F: FnOnce(u8) -> u8, { let r = self.read_mag_register(reg)?; self.write_mag_register(reg, f(r))?; Ok(()) } fn read_accel_registers(&mut self, reg: accel::Register) -> Result, E> where N: ArrayLength, { let mut buffer: GenericArray = unsafe { mem::uninitialized() }; { let buffer: &mut [u8] = &mut buffer; const MULTI: u8 = 1 << 7; self.i2c .write_read(accel::ADDRESS, &[reg.addr() | MULTI], buffer)?; } Ok(buffer) } fn read_accel_register(&mut self, reg: accel::Register) -> Result { ``` -------------------------------- ### Enum Sensitivity Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/enum.Sensitivity.html Defines the acceleration sensitivity settings for the LSM303DLHC accelerometer. Each variant corresponds to a different measurement range and sensitivity. ```APIDOC ## Enum Sensitivity ### Description Represents the acceleration sensitivity settings for the LSM303DLHC accelerometer. ### Variants - **G1** - Range: [-2g, +2g]. Sensitivity ~ 1 g / (1 << 14) LSB - **G2** - Range: [-4g, +4g]. Sensitivity ~ 2 g / (1 << 14) LSB - **G4** - Range: [-8g, +8g]. Sensitivity ~ 4 g / (1 << 14) LSB - **G12** - Range: [-16g, +16g]. Sensitivity ~ 12 g / (1 << 14) LSB ### Usage This enum is used to configure the accelerometer's sensitivity range. ``` -------------------------------- ### temp Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Reads the temperature sensor measurement. The temperature sensor has a 12-bit resolution and a range of -40 to +85 degrees Celsius. ```APIDOC ## temp ### Description Temperature sensor measurement. ### Method Method ### Signature pub fn temp(&mut self) -> Result ### Parameters None ### Return Value - **Result** - Returns the temperature measurement as a 16-bit signed integer on success, or an error of type E on failure. ### Notes - Resolution: 12-bit - Range: [-40, +85] ``` -------------------------------- ### LSM303DLHC Magnetometer Register Definitions Source: https://docs.rs/lsm303dlhc/latest/src/lsm303dlhc/mag.rs.html Defines the I2C address and the Register enum representing the magnetometer's internal registers. The addr method allows for easy retrieval of the raw register address. ```rust 1pub const ADDRESS: u8 = 0b0011110; 2 3#[allow(dead_code)] 4#[allow(non_camel_case_types)] 5#[derive(Copy, Clone)] 6pub enum Register { 7 CRA_REG_M = 0x00, 8 CRB_REG_M = 0x01, 9 MR_REG_M = 0x02, 10 OUT_X_H_M = 0x03, 11 OUT_X_L_M = 0x04, 12 OUT_Z_H_M = 0x05, 13 OUT_Z_L_M = 0x06, 14 OUT_Y_H_M = 0x07, 15 OUT_Y_L_M = 0x08, 16 SR_REG_M = 0x09, 17 IRA_REG_M = 0x0A, 18 IRB_REG_M = 0x0B, 19 IRC_REG_M = 0x0C, 20 TEMP_OUT_H_M = 0x31, 21 TEMP_OUT_L_M = 0x32, 22} 23 24impl Register { 25 pub fn addr(&self) -> u8 { 26 *self as u8 27 } 28} ``` -------------------------------- ### mag_odr Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Sets the magnetometer output data rate. This configures the rate at which magnetometer data is updated. ```APIDOC ## mag_odr ### Description Sets the magnetometer output data rate. ### Method Method ### Signature pub fn mag_odr(&mut self, odr: MagOdr) -> Result<(), E> ### Parameters - **odr** (MagOdr) - Required - The desired output data rate for the magnetometer. ### Return Value - **Result<(), E>** - Returns Ok(()) on success, or an error of type E on failure. ``` -------------------------------- ### MagOdr Enum Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/enum.MagOdr.html The MagOdr enum provides the available output data rates for the magnetometer. Each variant is associated with a numeric value and a frequency in Hz. ```APIDOC ## MagOdr Enum ### Description The `MagOdr` enum defines the output data rate options for the LSM303DLHC magnetometer. Each variant corresponds to a specific frequency in Hz. ### Variants - **Hz0_75** = 0 - 0.75 Hz - **Hz1_5** = 1 - 1.5 Hz - **Hz3** = 2 - 3 Hz - **Hz7_5** = 3 - 7.5 Hz - **Hz15** = 4 - 15 Hz - **Hz30** = 5 - 30 Hz - **Hz75** = 6 - 75 Hz - **Hz220** = 7 - 220 Hz ### Usage This enum is used to configure the magnetometer's output data rate in the LSM303DLHC driver. ``` -------------------------------- ### mag Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Reads magnetometer measurements from the sensor. Returns a 3-axis vector of 16-bit signed integers. ```APIDOC ## mag ### Description Magnetometer measurements. ### Method Method ### Signature pub fn mag(&mut self) -> Result ### Parameters None ### Return Value - **Result** - Returns a 3-axis magnetometer measurement as I16x3 on success, or an error of type E on failure. ``` -------------------------------- ### accel Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Reads accelerometer measurements from the sensor. Returns a 3-axis vector of 16-bit signed integers. ```APIDOC ## accel ### Description Accelerometer measurements. ### Method Method ### Signature pub fn accel(&mut self) -> Result ### Parameters None ### Return Value - **Result** - Returns a 3-axis accelerometer measurement as I16x3 on success, or an error of type E on failure. ``` -------------------------------- ### MagOdr enum definition Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/enum.MagOdr.html Defines the MagOdr enum with variants for output data rates from 0.75 Hz to 220 Hz, each with a numeric value. ```rust pub enum MagOdr { Hz0_75 = 0, Hz1_5 = 1, Hz3 = 2, Hz7_5 = 3, Hz15 = 4, Hz30 = 5, Hz75 = 6, Hz220 = 7, } ``` -------------------------------- ### accel_odr Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Sets the accelerometer output data rate. This configures the rate at which accelerometer data is updated. ```APIDOC ## accel_odr ### Description Sets the accelerometer output data rate. ### Method Method ### Signature pub fn accel_odr(&mut self, odr: AccelOdr) -> Result<(), E> ### Parameters - **odr** (AccelOdr) - Required - The desired output data rate for the accelerometer. ### Return Value - **Result<(), E>** - Returns Ok(()) on success, or an error of type E on failure. ``` -------------------------------- ### AccelOdr Enum Definition Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/enum.AccelOdr.html The AccelOdr enum specifies the supported accelerometer output data rates. ```rust pub enum AccelOdr { Hz1 = 1, Hz10 = 2, Hz25 = 3, Hz50 = 4, Hz100 = 5, Hz200 = 6, Hz400 = 7, } ``` -------------------------------- ### Accelerometer register address constant and enum Source: https://docs.rs/lsm303dlhc/latest/src/lsm303dlhc/accel.rs.html Defines the I2C address (0b0011001) and an enum of register addresses for the LSM303DLHC accelerometer. The enum includes a method to return the address as u8. ```rust pub const ADDRESS: u8 = 0b0011001; #[allow(dead_code)] #[allow(non_camel_case_types)] #[derive(Copy, Clone)] pub enum Register { CTRL_REG1_A = 0x20, CTRL_REG2_A = 0x21, CTRL_REG3_A = 0x22, CTRL_REG4_A = 0x23, CTRL_REG5_A = 0x24, CTRL_REG6_A = 0x25, REFERENCE_A = 0x26, STATUS_REG_A = 0x27, OUT_X_L_A = 0x28, OUT_X_H_A = 0x29, OUT_Y_L_A = 0x2A, OUT_Y_H_A = 0x2B, OUT_Z_L_A = 0x2C, OUT_Z_H_A = 0x2D, FIFO_CTRL_REG_A = 0x2E, FIFO_SRC_REG_A = 0x2F, INT1_CFG_A = 0x30, INT1_SRC_A = 0x31, INT1_THS_A = 0x32, INT1_DURATION_A = 0x33, INT2_CFG_A = 0x34, INT2_SRC_A = 0x35, INT2_THS_A = 0x36, INT2_DURATION_A = 0x37, CLICK_CFG_A = 0x38, CLICK_SRC_A = 0x39, CLICK_THS_A = 0x3A, TIME_LIMIT_A = 0x3B, TIME_LATENCY_A = 0x3C, TIME_WINDOW_A = 0x3D, } impl Register { pub fn addr(&self) -> u8 { *self as u8 } } ``` -------------------------------- ### AccelOdr Enum Definition Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/enum.AccelOdr.html The AccelOdr enum provides the accelerometer output data rate options. Each variant corresponds to a specific frequency in Hz and has a numeric value for register configuration. ```APIDOC ## Enum AccelOdr ### Description The `AccelOdr` enum represents the accelerometer output data rate settings. It defines the available frequencies for the accelerometer's data output. ### Variants - **Hz1** = 1 - 1 Hz - **Hz10** = 2 - 10 Hz - **Hz25** = 3 - 25 Hz - **Hz50** = 4 - 50 Hz - **Hz100** = 5 - 100 Hz - **Hz200** = 6 - 200 Hz - **Hz400** = 7 - 400 Hz ### Usage This enum is used to configure the accelerometer's output data rate. The numeric value associated with each variant is used for register settings. ### Example ```rust let odr = AccelOdr::Hz100; ``` ``` -------------------------------- ### set_accel_sensitivity Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/struct.Lsm303dlhc.html Changes the sensitivity of the accelerometer. This allows adjusting the measurement range of the accelerometer. ```APIDOC ## set_accel_sensitivity ### Description Changes the `sensitivity` of the accelerometer. ### Method Method ### Signature pub fn set_accel_sensitivity(&mut self, sensitivity: Sensitivity) -> Result<(), E> ### Parameters - **sensitivity** (Sensitivity) - Required - The desired sensitivity setting for the accelerometer. ### Return Value - **Result<(), E>** - Returns Ok(()) on success, or an error of type E on failure. ``` -------------------------------- ### Sensitivity enum definition Source: https://docs.rs/lsm303dlhc/latest/lsm303dlhc/enum.Sensitivity.html Defines the `Sensitivity` enum with variants G1, G2, G4, and G12, representing acceleration sensitivity levels. Each variant corresponds to a specific measurement range and sensitivity in LSB units. ```rust pub enum Sensitivity { G1, G2, G4, G12, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.