Expand description
A no_std driver for the ST LSM303 accelerometer + magnetometer family
(also sold as the GY-511 breakout board):
- LSM303DLH
- LSM303DLM
- LSM303DLHC
- LSM303D
Built on embedded-hal 1.0. It is async by default; enable the sync
feature for the blocking API.
§Usage
ⓘ
use lsm303_driver::{Lsm303, D, SA0};
// `i2c` is any `embedded_hal_async::i2c::I2c` implementation.
let mut sensor = Lsm303::<_, D>::new(i2c, SA0::High);
sensor.enable_default().await?;
let (accel, mag) = sensor.read().await?;
// accel.x, accel.y, accel.z, mag.x, mag.y, mag.z (raw i16 counts)Don’t know which chip you have? Let the driver detect it:
ⓘ
use lsm303_driver::{AnyLsm303, Lsm303, D};
let found = AnyLsm303::detect(&mut i2c, None, None).await?;
let mut sensor: Lsm303<_, D> = match found.into_driver(i2c) {
Ok(sensor) => sensor,
Err(other) => panic!("expected an LSM303D, found {:?}", other),
};
sensor.enable_default().await?;§Status
Tested only on the LSM303D. The other variants are implemented from their datasheets but not yet tested on hardware.
Structs§
- AnyLsm303
- Result of probing the bus: which chip was found and its SA0 state.
- D
- Marker type for the LSM303D, used as
Lsm303<I2C, D>. - Dlh
- Marker type for the LSM303DLH, used as
Lsm303<I2C, Dlh>. - Dlhc
- Marker type for the LSM303DLHC, used as
Lsm303<I2C, Dlhc>. - Dlm
- Marker type for the LSM303DLM, used as
Lsm303<I2C, Dlm>. - Lsm303
- Driver for one LSM303 chip on an I²C bus.
- Vector
- A 3-axis reading (accelerometer or magnetometer), in raw sensor counts.
Enums§
- DAccOdr
- Accelerometer output data rate for the LSM303D (
AODR[3:0]inCTRL1). - DAcc
Scale - Accelerometer full-scale range for the LSM303D (
AFS[2:0]inCTRL2). - DMagOdr
- Magnetometer output data rate for the LSM303D (
M_ODR[2:0]inCTRL5). - DMag
Scale - Magnetometer full-scale range for the LSM303D (
MFS[1:0]inCTRL6). - Device
- Chip variant, as a runtime value. Returned by
AnyLsm303::device. - DlhDlm
AccOdr - Accelerometer output data rate for the DLH and DLM (
CTRL_REG1_A). - DlhDlm
AccScale - Accelerometer full-scale range for the DLH and DLM (
FS[1:0]inCTRL_REG4_A). - DlhMag
Odr - Magnetometer output data rate for the DLH (
DO[2:0]inCRA_REG_M). - Dlhc
AccOdr - Accelerometer output data rate for the DLHC (
ODR[3:0]inCTRL_REG1_A). - Dlhc
AccScale - Accelerometer full-scale range for the DLHC (
FS[1:0]inCTRL_REG4_A). - DlmDlhc
MagOdr - Magnetometer output data rate for the DLM and DLHC (
DO[2:0]inCRA_REG_M). - DlxMag
Scale - Magnetometer full-scale range for the DLH/DLM/DLHC (
GN[2:0]inCRB_REG_M). - Error
- Errors returned by the driver.
- SA0
- State of the chip’s SA0 pin, which selects its I²C address.
Traits§
- Dlx
- Marker for the DLH/DLM/DLHC family, which share the magnetometer full-scale
(
GN) table and register layout. Gates methods common to those three. - HasTemperature
- Chips with a built-in temperature sensor (currently only the LSM303D).
- Variant
- Per-variant hardware differences: I²C addresses, magnetometer output
layout, and the power-on default configuration. Implemented by the marker
types
Dlh,Dlm,Dlhc, andD.