Driver for the LSM303D digital compass. Permits access to all LSM303D registers by name, including multi-byte register sets, and has convenience methods for floating-point accelerometer and magnetometer reads scaled according to the device settings. Written from scratch for Mbed 6.0

Dependents:   UoY-32C-lab8-exercise UoY-32C-lab5-lsm303d

Driver for the LSM303D digital compass

Permits access to all LSM303D registers by name, including multi-byte register sets. Convenience methods are included for sensor reads, including floating-point accelerometer and magnetometer reads scaled according to the device settings, and for the more common settings (data rates and scales).

I2C connection only. Written from scratch for Mbed 6.0.

The interface is subject to minor changes with each version. Deprecation warnings will be issued where possible.

Defaults

The class constructor checks that an LSM303D is present on the I2C bus, then enables all sensors. The accelerometer and magnetometer update rates are both set to 50Hz, and the scales are left at their device defaults of +-2g for the accelerometer and +-4gauss for the magnetometer.

Basic usage

Some usage examples

#include "lsm303d.h"

LSM303D lsm303d(D14, D15);  // SDA and SCL pins for I2C

lsm303d.setAccelScale(LSM303D::AccelScale::scale_4g); // set +-4g range

lsm303d.INT1_enable_DRDY_A(); // set DRDY_A bit in CTRL3 register (enable INT1 on accel data ready)

short rawX = lsm303d.getAccelX(); // returns 16-bit signed result, unscaled

float x, y, z;
lsm303d.getAccelInto(x, y, z); // overwrites parameters, values in g

float mX = lsm303d.getMagX(); // returns value in gauss
Committer:
ajp109
Date:
Tue Jan 26 22:46:48 2021 +0000
Revision:
5:e96d56fc0884
Parent:
4:3b3370ed7b21
Child:
6:7d624356069e
More documentation experiments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ajp109 0:b07c3d3e3d9c 1 #include "mbed.h"
ajp109 0:b07c3d3e3d9c 2
ajp109 0:b07c3d3e3d9c 3 typedef unsigned char u8;
ajp109 0:b07c3d3e3d9c 4 typedef signed short i16;
ajp109 0:b07c3d3e3d9c 5 typedef unsigned short u16;
ajp109 0:b07c3d3e3d9c 6
ajp109 0:b07c3d3e3d9c 7 #pragma pack(push)
ajp109 0:b07c3d3e3d9c 8 #pragma pack(1)
ajp109 0:b07c3d3e3d9c 9 typedef struct {
ajp109 0:b07c3d3e3d9c 10 u8 x;
ajp109 0:b07c3d3e3d9c 11 u8 y;
ajp109 0:b07c3d3e3d9c 12 u8 z;
ajp109 0:b07c3d3e3d9c 13 } u8_3;
ajp109 0:b07c3d3e3d9c 14 typedef struct {
ajp109 0:b07c3d3e3d9c 15 i16 x;
ajp109 0:b07c3d3e3d9c 16 i16 y;
ajp109 0:b07c3d3e3d9c 17 i16 z;
ajp109 0:b07c3d3e3d9c 18 } i16_3;
ajp109 0:b07c3d3e3d9c 19 #pragma pack(pop)
ajp109 0:b07c3d3e3d9c 20
ajp109 2:27d2a1082572 21 /** LSM303D class.
ajp109 2:27d2a1082572 22 * Communicates with LSM303D electronic compass over I2C. Accelerometer,
ajp109 2:27d2a1082572 23 * magnetometer and temperature sensor supported. Configurable sample rates
ajp109 2:27d2a1082572 24 * and full-scale ranges. All LSM303D config registers available for raw
ajp109 2:27d2a1082572 25 * reads and writes if needed.
ajp109 2:27d2a1082572 26 *
ajp109 2:27d2a1082572 27 * Example:
ajp109 2:27d2a1082572 28 * @code
ajp109 2:27d2a1082572 29 * #include "mbed.h"
ajp109 2:27d2a1082572 30 * #include "lsm303d.h"
ajp109 2:27d2a1082572 31 *
ajp109 2:27d2a1082572 32 * LSM303D lsm303d(SDA, SCL);
ajp109 2:27d2a1082572 33 * lsm303d.setAccelRate(LSM303D::AccelRate::rate_25Hz);
ajp109 2:27d2a1082572 34 * lsm303d.setAccelScale(LSM303D::AccelScale::scale_8g);
ajp109 2:27d2a1082572 35 *
ajp109 2:27d2a1082572 36 * int main() {
ajp109 2:27d2a1082572 37 * short x, y, z;
ajp109 2:27d2a1082572 38 * lsm303d.getRawAccelInto(x, y, z); // 16-bit signed, unscaled
ajp109 2:27d2a1082572 39 * float xf, yf, zf;
ajp109 2:27d2a1082572 40 * lsm303d.getAccelInto(xf, yf, zf); // floats, in g
ajp109 2:27d2a1082572 41 * }
ajp109 2:27d2a1082572 42 * @endcode
ajp109 2:27d2a1082572 43 */
ajp109 0:b07c3d3e3d9c 44 class LSM303D {
ajp109 0:b07c3d3e3d9c 45
ajp109 1:a74f8c175f6c 46 friend class AccelScaleOption;
ajp109 1:a74f8c175f6c 47 friend class MagScaleOption;
ajp109 0:b07c3d3e3d9c 48
ajp109 0:b07c3d3e3d9c 49 unsigned char const addr_8bit_;
ajp109 0:b07c3d3e3d9c 50 unsigned char accel_scale_;
ajp109 0:b07c3d3e3d9c 51 unsigned char mag_scale_;
ajp109 0:b07c3d3e3d9c 52 I2C i2c_;
ajp109 0:b07c3d3e3d9c 53
ajp109 0:b07c3d3e3d9c 54 template <class T>
ajp109 0:b07c3d3e3d9c 55 class Register {
ajp109 1:a74f8c175f6c 56 friend class LSM303D;
ajp109 0:b07c3d3e3d9c 57 protected:
ajp109 0:b07c3d3e3d9c 58 unsigned char addr_;
ajp109 1:a74f8c175f6c 59 LSM303D &parent_;
ajp109 0:b07c3d3e3d9c 60 Register(LSM303D &parent, unsigned char addr) :
ajp109 1:a74f8c175f6c 61 parent_(parent),
ajp109 1:a74f8c175f6c 62 addr_(addr | (sizeof(T)>1 ? 0x80 : 0x00)) {}
ajp109 0:b07c3d3e3d9c 63 virtual T read() const {
ajp109 0:b07c3d3e3d9c 64 T result;
ajp109 1:a74f8c175f6c 65 parent_.i2c_.lock();
ajp109 0:b07c3d3e3d9c 66 parent_.i2c_.write(parent_.addr_8bit_, (char *)&addr_, 1);
ajp109 0:b07c3d3e3d9c 67 parent_.i2c_.read(parent_.addr_8bit_, (char *)&result, sizeof(T));
ajp109 1:a74f8c175f6c 68 parent_.i2c_.unlock();
ajp109 0:b07c3d3e3d9c 69 return result;
ajp109 0:b07c3d3e3d9c 70 }
ajp109 1:a74f8c175f6c 71 inline operator T() const {
ajp109 0:b07c3d3e3d9c 72 return read();
ajp109 0:b07c3d3e3d9c 73 }
ajp109 0:b07c3d3e3d9c 74 };
ajp109 0:b07c3d3e3d9c 75
ajp109 5:e96d56fc0884 76 /** Read-write register class.
ajp109 5:e96d56fc0884 77 * \tparam T The underlying data type of the register.
ajp109 5:e96d56fc0884 78 * Provides an interface onto the writable registers of the LSM303D.
ajp109 5:e96d56fc0884 79 * Operator overloading means that it can be treated as a variable of type
ajp109 5:e96d56fc0884 80 * T for most purposes. Assignment from int is also explicitly supported
ajp109 5:e96d56fc0884 81 * to allow integer literals to be used without qualification or casting.
ajp109 5:e96d56fc0884 82 * The register value is cached on first read and updated on write, so read
ajp109 5:e96d56fc0884 83 * accesses do not use the I2C bus.
ajp109 5:e96d56fc0884 84 */
ajp109 0:b07c3d3e3d9c 85 template<class T>
ajp109 0:b07c3d3e3d9c 86 class RWRegister : public Register<T> {
ajp109 1:a74f8c175f6c 87 friend class LSM303D;
ajp109 0:b07c3d3e3d9c 88 using Register<T>::parent_;
ajp109 0:b07c3d3e3d9c 89 using Register<T>::addr_;
ajp109 1:a74f8c175f6c 90 private:
ajp109 0:b07c3d3e3d9c 91 mutable T cache_;
ajp109 0:b07c3d3e3d9c 92 mutable bool cached_;
ajp109 1:a74f8c175f6c 93 protected:
ajp109 0:b07c3d3e3d9c 94 RWRegister(LSM303D &parent, unsigned char addr) :
ajp109 0:b07c3d3e3d9c 95 Register<T>(parent, addr), cached_(false) {}
ajp109 1:a74f8c175f6c 96 void write(T const &data) {
ajp109 0:b07c3d3e3d9c 97 char packet[sizeof(T)+1];
ajp109 0:b07c3d3e3d9c 98 packet[0] = addr_;
ajp109 0:b07c3d3e3d9c 99 memcpy(&packet[1], &data, sizeof(T));
ajp109 1:a74f8c175f6c 100 cache_ = data;
ajp109 1:a74f8c175f6c 101 cached_ = true;
ajp109 1:a74f8c175f6c 102 parent_.i2c_.lock();
ajp109 0:b07c3d3e3d9c 103 parent_.i2c_.write(parent_.addr_8bit_, packet, sizeof(T)+1);
ajp109 1:a74f8c175f6c 104 parent_.i2c_.unlock();
ajp109 0:b07c3d3e3d9c 105 }
ajp109 0:b07c3d3e3d9c 106 virtual T read() const {
ajp109 0:b07c3d3e3d9c 107 if (!cached_) {
ajp109 0:b07c3d3e3d9c 108 cache_ = Register<T>::read();
ajp109 0:b07c3d3e3d9c 109 cached_ = true;
ajp109 0:b07c3d3e3d9c 110 }
ajp109 0:b07c3d3e3d9c 111 return cache_;
ajp109 0:b07c3d3e3d9c 112 }
ajp109 1:a74f8c175f6c 113 inline RWRegister & operator= (T const &data) {
ajp109 0:b07c3d3e3d9c 114 write(data);
ajp109 0:b07c3d3e3d9c 115 return *this;
ajp109 0:b07c3d3e3d9c 116 }
ajp109 1:a74f8c175f6c 117 inline RWRegister & operator= (int const &data) {
ajp109 1:a74f8c175f6c 118 write(T(data));
ajp109 1:a74f8c175f6c 119 return *this;
ajp109 1:a74f8c175f6c 120 }
ajp109 1:a74f8c175f6c 121 inline RWRegister & operator|= (T const &data) {
ajp109 1:a74f8c175f6c 122 write(cache_ | data);
ajp109 1:a74f8c175f6c 123 return *this;
ajp109 1:a74f8c175f6c 124 }
ajp109 1:a74f8c175f6c 125 inline RWRegister & operator|= (int const &data) {
ajp109 1:a74f8c175f6c 126 write(cache_ | T(data));
ajp109 1:a74f8c175f6c 127 return *this;
ajp109 1:a74f8c175f6c 128 }
ajp109 1:a74f8c175f6c 129 inline RWRegister & operator&= (T const &data) {
ajp109 1:a74f8c175f6c 130 write(cache_ & data);
ajp109 1:a74f8c175f6c 131 return *this;
ajp109 1:a74f8c175f6c 132 }
ajp109 1:a74f8c175f6c 133 inline RWRegister & operator&= (int const &data) {
ajp109 1:a74f8c175f6c 134 write(cache_ & T(data));
ajp109 0:b07c3d3e3d9c 135 return *this;
ajp109 0:b07c3d3e3d9c 136 }
ajp109 0:b07c3d3e3d9c 137 };
ajp109 0:b07c3d3e3d9c 138
ajp109 1:a74f8c175f6c 139 template <class T>
ajp109 1:a74f8c175f6c 140 class Option {
ajp109 1:a74f8c175f6c 141 friend class LSM303D;
ajp109 1:a74f8c175f6c 142 protected:
ajp109 1:a74f8c175f6c 143 T const bits_;
ajp109 1:a74f8c175f6c 144 RWRegister<T> & reg_;
ajp109 1:a74f8c175f6c 145 Option(RWRegister<T> & reg, int bits) :
ajp109 1:a74f8c175f6c 146 reg_(reg), bits_(T(bits)) {}
ajp109 1:a74f8c175f6c 147 public:
ajp109 1:a74f8c175f6c 148 inline void operator() () const { reg_ |= bits_; };
ajp109 1:a74f8c175f6c 149 inline void operator() (bool sr) const
ajp109 1:a74f8c175f6c 150 { if (sr) reg_ |= bits_; else reg_ &= ~bits_; };
ajp109 1:a74f8c175f6c 151 };
ajp109 1:a74f8c175f6c 152
ajp109 1:a74f8c175f6c 153 protected:
ajp109 5:e96d56fc0884 154 const Register <i16_3> OUT_M; ///< Triple magnetometer register
ajp109 5:e96d56fc0884 155 const Register <i16_3> OFFSET_M; ///< Triple magnetometer offset register
ajp109 5:e96d56fc0884 156 const Register <u8_3> REFERENCE; ///< Triple reference register
ajp109 5:e96d56fc0884 157 const Register <i16_3> OUT_A; ///< Triple accelerometer register
ajp109 1:a74f8c175f6c 158
ajp109 0:b07c3d3e3d9c 159 public:
ajp109 3:93343cd8cafe 160 const Register <i16> TEMP_OUT; ///< TEMP_OUT register, read-only
ajp109 3:93343cd8cafe 161 const Register <u8> STATUS_M; ///< STATUS_M register, read-only
ajp109 3:93343cd8cafe 162 const Register <i16> OUT_X_M; ///< OUT_X_M register, read-only
ajp109 3:93343cd8cafe 163 const Register <i16> OUT_Y_M; ///< OUT_Y_M register, read-only
ajp109 3:93343cd8cafe 164 const Register <i16> OUT_Z_M; ///< OUT_Z_M register, read-only
ajp109 3:93343cd8cafe 165 const Register <u8> WHO_AM_I; ///< WHO_AM_I register, read-only
ajp109 3:93343cd8cafe 166 const Register <u8> INT_SRC_M; ///< INT_SRC_M register, read-only
ajp109 3:93343cd8cafe 167 const Register <i16> OFFSET_X_M; ///< OFFSET_X_M register, read-only
ajp109 3:93343cd8cafe 168 const Register <i16> OFFSET_Y_M; ///< OFFSET_Y_M register, read-only
ajp109 3:93343cd8cafe 169 const Register <i16> OFFSET_Z_M; ///< OFFSET_Z_M register, read-only
ajp109 3:93343cd8cafe 170 const Register <u8> STATUS_A; ///< STATUS_A register, read-only
ajp109 3:93343cd8cafe 171 const Register <i16> OUT_X_A; ///< OUT_X_A register, read-only
ajp109 3:93343cd8cafe 172 const Register <i16> OUT_Y_A; ///< OUT_Y_A register, read-only
ajp109 3:93343cd8cafe 173 const Register <i16> OUT_Z_A; ///< OUT_Z_A register, read-only
ajp109 3:93343cd8cafe 174 const Register <u8> FIFO_SRC; ///< FIFO_SRC register, read-only
ajp109 3:93343cd8cafe 175 const Register <u8> IG_SRC1; ///< IG_SRC1 register, read-only
ajp109 3:93343cd8cafe 176 const Register <u8> IG_SRC2; ///< IG_SRC2 register, read-only
ajp109 3:93343cd8cafe 177 const Register <u8> CLICK_SRC; ///< CLICK_SRC register, read-only
ajp109 1:a74f8c175f6c 178
ajp109 3:93343cd8cafe 179 RWRegister <u8> INT_CTRL_M; ///< INT_CTRL_M register, read-write
ajp109 3:93343cd8cafe 180 RWRegister <u16> INT_THS_M; ///< INT_THS_M register, read-write
ajp109 3:93343cd8cafe 181 RWRegister <u8> REFERENCE_X; ///< REFERENCE_X register, read-write
ajp109 3:93343cd8cafe 182 RWRegister <u8> REFERENCE_Y; ///< REFERENCE_Y register, read-write
ajp109 3:93343cd8cafe 183 RWRegister <u8> REFERENCE_Z; ///< REFERENCE_Z register, read-write
ajp109 3:93343cd8cafe 184 RWRegister <u8> CTRL0; ///< CTRL0 register, read-write
ajp109 3:93343cd8cafe 185 RWRegister <u8> CTRL1; ///< CTRL1 register, read-write
ajp109 3:93343cd8cafe 186 RWRegister <u8> CTRL2; ///< CTRL2 register, read-write
ajp109 3:93343cd8cafe 187 RWRegister <u8> CTRL3; ///< CTRL3 register, read-write
ajp109 3:93343cd8cafe 188 RWRegister <u8> CTRL4; ///< CTRL4 register, read-write
ajp109 3:93343cd8cafe 189 RWRegister <u8> CTRL5; ///< CTRL5 register, read-write
ajp109 3:93343cd8cafe 190 RWRegister <u8> CTRL6; ///< CTRL6 register, read-write
ajp109 3:93343cd8cafe 191 RWRegister <u8> CTRL7; ///< CTRL7 register, read-write
ajp109 3:93343cd8cafe 192 RWRegister <u8> FIFO_CTRL; ///< FIFO_CTRL register, read-write
ajp109 3:93343cd8cafe 193 RWRegister <u8> IG_CFG1; ///< IG_CFG1 register, read-write
ajp109 3:93343cd8cafe 194 RWRegister <u8> IG_THS1; ///< IG_THS1 register, read-write
ajp109 3:93343cd8cafe 195 RWRegister <u8> IG_DUR1; ///< IG_DUR1 register, read-write
ajp109 3:93343cd8cafe 196 RWRegister <u8> IG_CFG2; ///< IG_CFG2 register, read-write
ajp109 3:93343cd8cafe 197 RWRegister <u8> IG_THS2; ///< IG_THS2 register, read-write
ajp109 3:93343cd8cafe 198 RWRegister <u8> IG_DUR2; ///< IG_DUR2 register, read-write
ajp109 3:93343cd8cafe 199 RWRegister <u8> CLICK_CFG; ///< CLICK_CFG register, read-write
ajp109 3:93343cd8cafe 200 RWRegister <u8> CLICK_THS; ///< CLICK_THS register, read-write
ajp109 3:93343cd8cafe 201 RWRegister <u8> TIME_LIMIT; ///< TIME_LIMIT register, read-write
ajp109 3:93343cd8cafe 202 RWRegister <u8> TIME_LATENCY;///< TIME_LATENCY register, read-write
ajp109 3:93343cd8cafe 203 RWRegister <u8> TIME_WINDOW; ///< TIME_WINDOW register, read-write
ajp109 3:93343cd8cafe 204 RWRegister <u8> Act_THS; ///< Act_THS register, read-write
ajp109 3:93343cd8cafe 205 RWRegister <u8> Act_DUR; ///< Act_DUR register, read-write
ajp109 1:a74f8c175f6c 206
ajp109 1:a74f8c175f6c 207 // CTRL1
ajp109 3:93343cd8cafe 208 /** AccelRate enum.
ajp109 3:93343cd8cafe 209 * Provides friendly names for the accelerometer update rates
ajp109 3:93343cd8cafe 210 * that are provided by the device.
ajp109 3:93343cd8cafe 211 */
ajp109 1:a74f8c175f6c 212 enum class AccelRate {
ajp109 3:93343cd8cafe 213 rate_3_125Hz = 0x10, ///< 3.125Hz update rate
ajp109 3:93343cd8cafe 214 rate_6_25Hz = 0x20, ///< 6.25Hz update rate
ajp109 3:93343cd8cafe 215 rate_12_5Hz = 0x30, ///< 12.5Hz update rate
ajp109 3:93343cd8cafe 216 rate_25Hz = 0x40, ///< 25Hz update rate
ajp109 3:93343cd8cafe 217 rate_50Hz = 0x50, ///< 50Hz update rate
ajp109 3:93343cd8cafe 218 rate_100Hz = 0x60, ///< 100Hz update rate
ajp109 3:93343cd8cafe 219 rate_200Hz = 0x70, ///< 200Hz update rate
ajp109 3:93343cd8cafe 220 rate_400Hz = 0x80, ///< 400Hz update rate
ajp109 3:93343cd8cafe 221 rate_800Hz = 0x90, ///< 800Hz update rate
ajp109 3:93343cd8cafe 222 rate_1600Hz = 0xA0 ///< 1600Hz update rate
ajp109 1:a74f8c175f6c 223 };
ajp109 3:93343cd8cafe 224 /** Sets the accelerometer update rate.
ajp109 3:93343cd8cafe 225 * Available values are:
ajp109 4:3b3370ed7b21 226 * - AccelRate::rate_3_125Hz (3.125 Hz)
ajp109 4:3b3370ed7b21 227 * - AccelRate::rate_6_25Hz (6.25 Hz)
ajp109 4:3b3370ed7b21 228 * - AccelRate::rate_12_5Hz (12.5 Hz)
ajp109 4:3b3370ed7b21 229 * - AccelRate::rate_25Hz (25 Hz)
ajp109 4:3b3370ed7b21 230 * - AccelRate::rate_50Hz (50 Hz)
ajp109 4:3b3370ed7b21 231 * - AccelRate::rate_100Hz (100 Hz)
ajp109 4:3b3370ed7b21 232 * - AccelRate::rate_200Hz (200 Hz)
ajp109 4:3b3370ed7b21 233 * - AccelRate::rate_400Hz (400 Hz)
ajp109 4:3b3370ed7b21 234 * - AccelRate::rate_800Hz (800 Hz)
ajp109 4:3b3370ed7b21 235 * - AccelRate::rate_1600Hz (1600 Hz)
ajp109 5:e96d56fc0884 236 */
ajp109 1:a74f8c175f6c 237 void setAccelRate(AccelRate const &rate);
ajp109 1:a74f8c175f6c 238
ajp109 5:e96d56fc0884 239
ajp109 1:a74f8c175f6c 240 const Option<u8> accel_enableX;
ajp109 1:a74f8c175f6c 241 const Option<u8> accel_enableY;
ajp109 1:a74f8c175f6c 242 const Option<u8> accel_enableZ;
ajp109 1:a74f8c175f6c 243 const Option<u8> accel_enableAll;
ajp109 1:a74f8c175f6c 244
ajp109 1:a74f8c175f6c 245 // CTRL2
ajp109 1:a74f8c175f6c 246 enum class AccelScale {
ajp109 1:a74f8c175f6c 247 scale_2g = 0x00,
ajp109 1:a74f8c175f6c 248 scale_4g = 0x08,
ajp109 1:a74f8c175f6c 249 scale_6g = 0x10,
ajp109 1:a74f8c175f6c 250 scale_8g = 0x18,
ajp109 1:a74f8c175f6c 251 scale_16g = 0x20
ajp109 1:a74f8c175f6c 252 };
ajp109 1:a74f8c175f6c 253 void setAccelScale(AccelScale const &scale);
ajp109 1:a74f8c175f6c 254
ajp109 1:a74f8c175f6c 255 // CTRL3
ajp109 1:a74f8c175f6c 256 const Option<u8> INT1_enable_BOOT;
ajp109 1:a74f8c175f6c 257 const Option<u8> INT1_enable_CLICK;
ajp109 1:a74f8c175f6c 258 const Option<u8> INT1_enable_IG1;
ajp109 1:a74f8c175f6c 259 const Option<u8> INT1_enable_IG2;
ajp109 1:a74f8c175f6c 260 const Option<u8> INT1_enable_IGM;
ajp109 1:a74f8c175f6c 261 const Option<u8> INT1_enable_DRDY_A;
ajp109 1:a74f8c175f6c 262 const Option<u8> INT1_enable_DRDY_M;
ajp109 1:a74f8c175f6c 263 const Option<u8> INT1_enable_EMPTY;
ajp109 1:a74f8c175f6c 264
ajp109 1:a74f8c175f6c 265 // CTRL4 concerns INT2 which is not broken out on the Pimoroni board
ajp109 1:a74f8c175f6c 266
ajp109 1:a74f8c175f6c 267 // CTRL5
ajp109 1:a74f8c175f6c 268 const Option<u8> temp_enable;
ajp109 1:a74f8c175f6c 269 const Option<u8> LIR1_enable;
ajp109 1:a74f8c175f6c 270
ajp109 1:a74f8c175f6c 271 enum class MagRes {
ajp109 1:a74f8c175f6c 272 high = 0x60,
ajp109 1:a74f8c175f6c 273 low = 0x00,
ajp109 1:a74f8c175f6c 274 };
ajp109 1:a74f8c175f6c 275 void setMagRes(MagRes const &res);
ajp109 1:a74f8c175f6c 276
ajp109 1:a74f8c175f6c 277 enum class MagRate {
ajp109 1:a74f8c175f6c 278 rate_3_125Hz = 0x00,
ajp109 1:a74f8c175f6c 279 rate_6_25Hz = 0x04,
ajp109 1:a74f8c175f6c 280 rate_12_5Hz = 0x08,
ajp109 1:a74f8c175f6c 281 rate_25Hz = 0x0C,
ajp109 1:a74f8c175f6c 282 rate_50Hz = 0x10,
ajp109 1:a74f8c175f6c 283 rate_100Hz = 0x14
ajp109 1:a74f8c175f6c 284 };
ajp109 1:a74f8c175f6c 285 void setMagRate(MagRate const &rate);
ajp109 1:a74f8c175f6c 286
ajp109 1:a74f8c175f6c 287 // CTRL6
ajp109 1:a74f8c175f6c 288 enum class MagScale {
ajp109 2:27d2a1082572 289 scale_2G = 0x00,
ajp109 2:27d2a1082572 290 scale_4G = 0x20,
ajp109 2:27d2a1082572 291 scale_8G = 0x40,
ajp109 2:27d2a1082572 292 scale_12G = 0x60
ajp109 1:a74f8c175f6c 293 };
ajp109 1:a74f8c175f6c 294 void setMagScale(MagScale const &scale);
ajp109 1:a74f8c175f6c 295
ajp109 1:a74f8c175f6c 296 // CTRL7
ajp109 1:a74f8c175f6c 297 enum class MagMode {
ajp109 1:a74f8c175f6c 298 continuous = 0x00,
ajp109 1:a74f8c175f6c 299 single = 0x01,
ajp109 1:a74f8c175f6c 300 off = 0x02
ajp109 1:a74f8c175f6c 301 };
ajp109 1:a74f8c175f6c 302 void setMagMode(MagMode const &mode);
ajp109 0:b07c3d3e3d9c 303
ajp109 0:b07c3d3e3d9c 304 LSM303D(PinName sda, PinName scl, unsigned char i2c_addr = 0x1D);
ajp109 0:b07c3d3e3d9c 305
ajp109 1:a74f8c175f6c 306 inline i16 getRawAccelX() { return OUT_X_A; };
ajp109 1:a74f8c175f6c 307 inline i16 getRawAccelY() { return OUT_Y_A; };
ajp109 1:a74f8c175f6c 308 inline i16 getRawAccelZ() { return OUT_Z_A; };
ajp109 1:a74f8c175f6c 309 inline float getAccelX() { return (float)OUT_X_A / accel_scale_ / 32768; };
ajp109 1:a74f8c175f6c 310 inline float getAccelY() { return (float)OUT_Y_A / accel_scale_ / 32768; };
ajp109 1:a74f8c175f6c 311 inline float getAccelZ() { return (float)OUT_Z_A / accel_scale_ / 32768; };
ajp109 0:b07c3d3e3d9c 312 void getRawAccelInto(short &x, short &y, short &z);
ajp109 0:b07c3d3e3d9c 313 void getAccelInto(float &x, float &y, float &z);
ajp109 0:b07c3d3e3d9c 314
ajp109 1:a74f8c175f6c 315 inline i16 getRawMagX() { return OUT_X_M; };
ajp109 1:a74f8c175f6c 316 inline i16 getRawMagY() { return OUT_Y_M; };
ajp109 1:a74f8c175f6c 317 inline i16 getRawMagZ() { return OUT_Z_M; };
ajp109 1:a74f8c175f6c 318 inline float getMagX() { return (float)OUT_X_M / mag_scale_ / 32768; };
ajp109 1:a74f8c175f6c 319 inline float getMagY() { return (float)OUT_Y_M / mag_scale_ / 32768; };
ajp109 1:a74f8c175f6c 320 inline float getMagZ() { return (float)OUT_Z_M / mag_scale_ / 32768; };
ajp109 0:b07c3d3e3d9c 321 void getRawMagInto(short &x, short &y, short &z);
ajp109 0:b07c3d3e3d9c 322 void getMagInto(float &x, float &y, float &z);
ajp109 0:b07c3d3e3d9c 323
ajp109 1:a74f8c175f6c 324 inline float getTemp() { return TEMP_OUT / 8.0 + 25; };
ajp109 0:b07c3d3e3d9c 325
ajp109 0:b07c3d3e3d9c 326 };
ajp109 0:b07c3d3e3d9c 327