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:01:49 2021 +0000
Revision:
3:93343cd8cafe
Parent:
2:27d2a1082572
Child:
4:3b3370ed7b21
Doc updates

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 0:b07c3d3e3d9c 76 template<class T>
ajp109 0:b07c3d3e3d9c 77 class RWRegister : public Register<T> {
ajp109 1:a74f8c175f6c 78 friend class LSM303D;
ajp109 0:b07c3d3e3d9c 79 using Register<T>::parent_;
ajp109 0:b07c3d3e3d9c 80 using Register<T>::addr_;
ajp109 1:a74f8c175f6c 81 private:
ajp109 0:b07c3d3e3d9c 82 mutable T cache_;
ajp109 0:b07c3d3e3d9c 83 mutable bool cached_;
ajp109 1:a74f8c175f6c 84 protected:
ajp109 0:b07c3d3e3d9c 85 RWRegister(LSM303D &parent, unsigned char addr) :
ajp109 0:b07c3d3e3d9c 86 Register<T>(parent, addr), cached_(false) {}
ajp109 1:a74f8c175f6c 87 void write(T const &data) {
ajp109 0:b07c3d3e3d9c 88 char packet[sizeof(T)+1];
ajp109 0:b07c3d3e3d9c 89 packet[0] = addr_;
ajp109 0:b07c3d3e3d9c 90 memcpy(&packet[1], &data, sizeof(T));
ajp109 1:a74f8c175f6c 91 cache_ = data;
ajp109 1:a74f8c175f6c 92 cached_ = true;
ajp109 1:a74f8c175f6c 93 parent_.i2c_.lock();
ajp109 0:b07c3d3e3d9c 94 parent_.i2c_.write(parent_.addr_8bit_, packet, sizeof(T)+1);
ajp109 1:a74f8c175f6c 95 parent_.i2c_.unlock();
ajp109 0:b07c3d3e3d9c 96 }
ajp109 0:b07c3d3e3d9c 97 virtual T read() const {
ajp109 0:b07c3d3e3d9c 98 if (!cached_) {
ajp109 0:b07c3d3e3d9c 99 cache_ = Register<T>::read();
ajp109 0:b07c3d3e3d9c 100 cached_ = true;
ajp109 0:b07c3d3e3d9c 101 }
ajp109 0:b07c3d3e3d9c 102 return cache_;
ajp109 0:b07c3d3e3d9c 103 }
ajp109 1:a74f8c175f6c 104 inline RWRegister & operator= (T const &data) {
ajp109 0:b07c3d3e3d9c 105 write(data);
ajp109 0:b07c3d3e3d9c 106 return *this;
ajp109 0:b07c3d3e3d9c 107 }
ajp109 1:a74f8c175f6c 108 inline RWRegister & operator= (int const &data) {
ajp109 1:a74f8c175f6c 109 write(T(data));
ajp109 1:a74f8c175f6c 110 return *this;
ajp109 1:a74f8c175f6c 111 }
ajp109 1:a74f8c175f6c 112 inline RWRegister & operator|= (T const &data) {
ajp109 1:a74f8c175f6c 113 write(cache_ | data);
ajp109 1:a74f8c175f6c 114 return *this;
ajp109 1:a74f8c175f6c 115 }
ajp109 1:a74f8c175f6c 116 inline RWRegister & operator|= (int const &data) {
ajp109 1:a74f8c175f6c 117 write(cache_ | T(data));
ajp109 1:a74f8c175f6c 118 return *this;
ajp109 1:a74f8c175f6c 119 }
ajp109 1:a74f8c175f6c 120 inline RWRegister & operator&= (T const &data) {
ajp109 1:a74f8c175f6c 121 write(cache_ & data);
ajp109 1:a74f8c175f6c 122 return *this;
ajp109 1:a74f8c175f6c 123 }
ajp109 1:a74f8c175f6c 124 inline RWRegister & operator&= (int const &data) {
ajp109 1:a74f8c175f6c 125 write(cache_ & T(data));
ajp109 0:b07c3d3e3d9c 126 return *this;
ajp109 0:b07c3d3e3d9c 127 }
ajp109 0:b07c3d3e3d9c 128 };
ajp109 0:b07c3d3e3d9c 129
ajp109 1:a74f8c175f6c 130 template <class T>
ajp109 1:a74f8c175f6c 131 class Option {
ajp109 1:a74f8c175f6c 132 friend class LSM303D;
ajp109 1:a74f8c175f6c 133 protected:
ajp109 1:a74f8c175f6c 134 T const bits_;
ajp109 1:a74f8c175f6c 135 RWRegister<T> & reg_;
ajp109 1:a74f8c175f6c 136 Option(RWRegister<T> & reg, int bits) :
ajp109 1:a74f8c175f6c 137 reg_(reg), bits_(T(bits)) {}
ajp109 1:a74f8c175f6c 138 public:
ajp109 1:a74f8c175f6c 139 inline void operator() () const { reg_ |= bits_; };
ajp109 1:a74f8c175f6c 140 inline void operator() (bool sr) const
ajp109 1:a74f8c175f6c 141 { if (sr) reg_ |= bits_; else reg_ &= ~bits_; };
ajp109 1:a74f8c175f6c 142 };
ajp109 1:a74f8c175f6c 143
ajp109 1:a74f8c175f6c 144 protected:
ajp109 1:a74f8c175f6c 145 const Register <i16_3> OUT_M;
ajp109 1:a74f8c175f6c 146 const Register <i16_3> OFFSET_M;
ajp109 1:a74f8c175f6c 147 const Register <u8_3> REFERENCE;
ajp109 1:a74f8c175f6c 148 const Register <i16_3> OUT_A;
ajp109 1:a74f8c175f6c 149
ajp109 0:b07c3d3e3d9c 150 public:
ajp109 3:93343cd8cafe 151 const Register <i16> TEMP_OUT; ///< TEMP_OUT register, read-only
ajp109 3:93343cd8cafe 152 const Register <u8> STATUS_M; ///< STATUS_M register, read-only
ajp109 3:93343cd8cafe 153 const Register <i16> OUT_X_M; ///< OUT_X_M register, read-only
ajp109 3:93343cd8cafe 154 const Register <i16> OUT_Y_M; ///< OUT_Y_M register, read-only
ajp109 3:93343cd8cafe 155 const Register <i16> OUT_Z_M; ///< OUT_Z_M register, read-only
ajp109 3:93343cd8cafe 156 const Register <u8> WHO_AM_I; ///< WHO_AM_I register, read-only
ajp109 3:93343cd8cafe 157 const Register <u8> INT_SRC_M; ///< INT_SRC_M register, read-only
ajp109 3:93343cd8cafe 158 const Register <i16> OFFSET_X_M; ///< OFFSET_X_M register, read-only
ajp109 3:93343cd8cafe 159 const Register <i16> OFFSET_Y_M; ///< OFFSET_Y_M register, read-only
ajp109 3:93343cd8cafe 160 const Register <i16> OFFSET_Z_M; ///< OFFSET_Z_M register, read-only
ajp109 3:93343cd8cafe 161 const Register <u8> STATUS_A; ///< STATUS_A register, read-only
ajp109 3:93343cd8cafe 162 const Register <i16> OUT_X_A; ///< OUT_X_A register, read-only
ajp109 3:93343cd8cafe 163 const Register <i16> OUT_Y_A; ///< OUT_Y_A register, read-only
ajp109 3:93343cd8cafe 164 const Register <i16> OUT_Z_A; ///< OUT_Z_A register, read-only
ajp109 3:93343cd8cafe 165 const Register <u8> FIFO_SRC; ///< FIFO_SRC register, read-only
ajp109 3:93343cd8cafe 166 const Register <u8> IG_SRC1; ///< IG_SRC1 register, read-only
ajp109 3:93343cd8cafe 167 const Register <u8> IG_SRC2; ///< IG_SRC2 register, read-only
ajp109 3:93343cd8cafe 168 const Register <u8> CLICK_SRC; ///< CLICK_SRC register, read-only
ajp109 1:a74f8c175f6c 169
ajp109 3:93343cd8cafe 170 RWRegister <u8> INT_CTRL_M; ///< INT_CTRL_M register, read-write
ajp109 3:93343cd8cafe 171 RWRegister <u16> INT_THS_M; ///< INT_THS_M register, read-write
ajp109 3:93343cd8cafe 172 RWRegister <u8> REFERENCE_X; ///< REFERENCE_X register, read-write
ajp109 3:93343cd8cafe 173 RWRegister <u8> REFERENCE_Y; ///< REFERENCE_Y register, read-write
ajp109 3:93343cd8cafe 174 RWRegister <u8> REFERENCE_Z; ///< REFERENCE_Z register, read-write
ajp109 3:93343cd8cafe 175 RWRegister <u8> CTRL0; ///< CTRL0 register, read-write
ajp109 3:93343cd8cafe 176 RWRegister <u8> CTRL1; ///< CTRL1 register, read-write
ajp109 3:93343cd8cafe 177 RWRegister <u8> CTRL2; ///< CTRL2 register, read-write
ajp109 3:93343cd8cafe 178 RWRegister <u8> CTRL3; ///< CTRL3 register, read-write
ajp109 3:93343cd8cafe 179 RWRegister <u8> CTRL4; ///< CTRL4 register, read-write
ajp109 3:93343cd8cafe 180 RWRegister <u8> CTRL5; ///< CTRL5 register, read-write
ajp109 3:93343cd8cafe 181 RWRegister <u8> CTRL6; ///< CTRL6 register, read-write
ajp109 3:93343cd8cafe 182 RWRegister <u8> CTRL7; ///< CTRL7 register, read-write
ajp109 3:93343cd8cafe 183 RWRegister <u8> FIFO_CTRL; ///< FIFO_CTRL register, read-write
ajp109 3:93343cd8cafe 184 RWRegister <u8> IG_CFG1; ///< IG_CFG1 register, read-write
ajp109 3:93343cd8cafe 185 RWRegister <u8> IG_THS1; ///< IG_THS1 register, read-write
ajp109 3:93343cd8cafe 186 RWRegister <u8> IG_DUR1; ///< IG_DUR1 register, read-write
ajp109 3:93343cd8cafe 187 RWRegister <u8> IG_CFG2; ///< IG_CFG2 register, read-write
ajp109 3:93343cd8cafe 188 RWRegister <u8> IG_THS2; ///< IG_THS2 register, read-write
ajp109 3:93343cd8cafe 189 RWRegister <u8> IG_DUR2; ///< IG_DUR2 register, read-write
ajp109 3:93343cd8cafe 190 RWRegister <u8> CLICK_CFG; ///< CLICK_CFG register, read-write
ajp109 3:93343cd8cafe 191 RWRegister <u8> CLICK_THS; ///< CLICK_THS register, read-write
ajp109 3:93343cd8cafe 192 RWRegister <u8> TIME_LIMIT; ///< TIME_LIMIT register, read-write
ajp109 3:93343cd8cafe 193 RWRegister <u8> TIME_LATENCY;///< TIME_LATENCY register, read-write
ajp109 3:93343cd8cafe 194 RWRegister <u8> TIME_WINDOW; ///< TIME_WINDOW register, read-write
ajp109 3:93343cd8cafe 195 RWRegister <u8> Act_THS; ///< Act_THS register, read-write
ajp109 3:93343cd8cafe 196 RWRegister <u8> Act_DUR; ///< Act_DUR register, read-write
ajp109 1:a74f8c175f6c 197
ajp109 1:a74f8c175f6c 198 // CTRL1
ajp109 3:93343cd8cafe 199 /** AccelRate enum.
ajp109 3:93343cd8cafe 200 * Provides friendly names for the accelerometer update rates
ajp109 3:93343cd8cafe 201 * that are provided by the device.
ajp109 3:93343cd8cafe 202 */
ajp109 1:a74f8c175f6c 203 enum class AccelRate {
ajp109 3:93343cd8cafe 204 rate_3_125Hz = 0x10, ///< 3.125Hz update rate
ajp109 3:93343cd8cafe 205 rate_6_25Hz = 0x20, ///< 6.25Hz update rate
ajp109 3:93343cd8cafe 206 rate_12_5Hz = 0x30, ///< 12.5Hz update rate
ajp109 3:93343cd8cafe 207 rate_25Hz = 0x40, ///< 25Hz update rate
ajp109 3:93343cd8cafe 208 rate_50Hz = 0x50, ///< 50Hz update rate
ajp109 3:93343cd8cafe 209 rate_100Hz = 0x60, ///< 100Hz update rate
ajp109 3:93343cd8cafe 210 rate_200Hz = 0x70, ///< 200Hz update rate
ajp109 3:93343cd8cafe 211 rate_400Hz = 0x80, ///< 400Hz update rate
ajp109 3:93343cd8cafe 212 rate_800Hz = 0x90, ///< 800Hz update rate
ajp109 3:93343cd8cafe 213 rate_1600Hz = 0xA0 ///< 1600Hz update rate
ajp109 1:a74f8c175f6c 214 };
ajp109 3:93343cd8cafe 215 /** Sets the accelerometer update rate.
ajp109 3:93343cd8cafe 216 * Available values are:
ajp109 3:93343cd8cafe 217 * AccelRate::rate_3_125Hz (3.125 Hz)
ajp109 3:93343cd8cafe 218 * AccelRate::rate_6_25Hz (6.25 Hz)
ajp109 3:93343cd8cafe 219 * AccelRate::rate_12_5Hz (12.5 Hz)
ajp109 3:93343cd8cafe 220 * AccelRate::rate_25Hz (25 Hz)
ajp109 3:93343cd8cafe 221 * AccelRate::rate_50Hz (50 Hz)
ajp109 3:93343cd8cafe 222 * AccelRate::rate_100Hz (100 Hz)
ajp109 3:93343cd8cafe 223 * AccelRate::rate_200Hz (200 Hz)
ajp109 3:93343cd8cafe 224 * AccelRate::rate_400Hz (400 Hz)
ajp109 3:93343cd8cafe 225 * AccelRate::rate_800Hz (800 Hz)
ajp109 3:93343cd8cafe 226 * AccelRate::rate_1600Hz (1600 Hz)
ajp109 3:93343cd8cafe 227 */
ajp109 1:a74f8c175f6c 228 void setAccelRate(AccelRate const &rate);
ajp109 1:a74f8c175f6c 229
ajp109 1:a74f8c175f6c 230 const Option<u8> accel_enableX;
ajp109 1:a74f8c175f6c 231 const Option<u8> accel_enableY;
ajp109 1:a74f8c175f6c 232 const Option<u8> accel_enableZ;
ajp109 1:a74f8c175f6c 233 const Option<u8> accel_enableAll;
ajp109 1:a74f8c175f6c 234
ajp109 1:a74f8c175f6c 235 // CTRL2
ajp109 1:a74f8c175f6c 236 enum class AccelScale {
ajp109 1:a74f8c175f6c 237 scale_2g = 0x00,
ajp109 1:a74f8c175f6c 238 scale_4g = 0x08,
ajp109 1:a74f8c175f6c 239 scale_6g = 0x10,
ajp109 1:a74f8c175f6c 240 scale_8g = 0x18,
ajp109 1:a74f8c175f6c 241 scale_16g = 0x20
ajp109 1:a74f8c175f6c 242 };
ajp109 1:a74f8c175f6c 243 void setAccelScale(AccelScale const &scale);
ajp109 1:a74f8c175f6c 244
ajp109 1:a74f8c175f6c 245 // CTRL3
ajp109 1:a74f8c175f6c 246 const Option<u8> INT1_enable_BOOT;
ajp109 1:a74f8c175f6c 247 const Option<u8> INT1_enable_CLICK;
ajp109 1:a74f8c175f6c 248 const Option<u8> INT1_enable_IG1;
ajp109 1:a74f8c175f6c 249 const Option<u8> INT1_enable_IG2;
ajp109 1:a74f8c175f6c 250 const Option<u8> INT1_enable_IGM;
ajp109 1:a74f8c175f6c 251 const Option<u8> INT1_enable_DRDY_A;
ajp109 1:a74f8c175f6c 252 const Option<u8> INT1_enable_DRDY_M;
ajp109 1:a74f8c175f6c 253 const Option<u8> INT1_enable_EMPTY;
ajp109 1:a74f8c175f6c 254
ajp109 1:a74f8c175f6c 255 // CTRL4 concerns INT2 which is not broken out on the Pimoroni board
ajp109 1:a74f8c175f6c 256
ajp109 1:a74f8c175f6c 257 // CTRL5
ajp109 1:a74f8c175f6c 258 const Option<u8> temp_enable;
ajp109 1:a74f8c175f6c 259 const Option<u8> LIR1_enable;
ajp109 1:a74f8c175f6c 260
ajp109 1:a74f8c175f6c 261 enum class MagRes {
ajp109 1:a74f8c175f6c 262 high = 0x60,
ajp109 1:a74f8c175f6c 263 low = 0x00,
ajp109 1:a74f8c175f6c 264 };
ajp109 1:a74f8c175f6c 265 void setMagRes(MagRes const &res);
ajp109 1:a74f8c175f6c 266
ajp109 1:a74f8c175f6c 267 enum class MagRate {
ajp109 1:a74f8c175f6c 268 rate_3_125Hz = 0x00,
ajp109 1:a74f8c175f6c 269 rate_6_25Hz = 0x04,
ajp109 1:a74f8c175f6c 270 rate_12_5Hz = 0x08,
ajp109 1:a74f8c175f6c 271 rate_25Hz = 0x0C,
ajp109 1:a74f8c175f6c 272 rate_50Hz = 0x10,
ajp109 1:a74f8c175f6c 273 rate_100Hz = 0x14
ajp109 1:a74f8c175f6c 274 };
ajp109 1:a74f8c175f6c 275 void setMagRate(MagRate const &rate);
ajp109 1:a74f8c175f6c 276
ajp109 1:a74f8c175f6c 277 // CTRL6
ajp109 1:a74f8c175f6c 278 enum class MagScale {
ajp109 2:27d2a1082572 279 scale_2G = 0x00,
ajp109 2:27d2a1082572 280 scale_4G = 0x20,
ajp109 2:27d2a1082572 281 scale_8G = 0x40,
ajp109 2:27d2a1082572 282 scale_12G = 0x60
ajp109 1:a74f8c175f6c 283 };
ajp109 1:a74f8c175f6c 284 void setMagScale(MagScale const &scale);
ajp109 1:a74f8c175f6c 285
ajp109 1:a74f8c175f6c 286 // CTRL7
ajp109 1:a74f8c175f6c 287 enum class MagMode {
ajp109 1:a74f8c175f6c 288 continuous = 0x00,
ajp109 1:a74f8c175f6c 289 single = 0x01,
ajp109 1:a74f8c175f6c 290 off = 0x02
ajp109 1:a74f8c175f6c 291 };
ajp109 1:a74f8c175f6c 292 void setMagMode(MagMode const &mode);
ajp109 0:b07c3d3e3d9c 293
ajp109 0:b07c3d3e3d9c 294 LSM303D(PinName sda, PinName scl, unsigned char i2c_addr = 0x1D);
ajp109 0:b07c3d3e3d9c 295
ajp109 1:a74f8c175f6c 296 inline i16 getRawAccelX() { return OUT_X_A; };
ajp109 1:a74f8c175f6c 297 inline i16 getRawAccelY() { return OUT_Y_A; };
ajp109 1:a74f8c175f6c 298 inline i16 getRawAccelZ() { return OUT_Z_A; };
ajp109 1:a74f8c175f6c 299 inline float getAccelX() { return (float)OUT_X_A / accel_scale_ / 32768; };
ajp109 1:a74f8c175f6c 300 inline float getAccelY() { return (float)OUT_Y_A / accel_scale_ / 32768; };
ajp109 1:a74f8c175f6c 301 inline float getAccelZ() { return (float)OUT_Z_A / accel_scale_ / 32768; };
ajp109 0:b07c3d3e3d9c 302 void getRawAccelInto(short &x, short &y, short &z);
ajp109 0:b07c3d3e3d9c 303 void getAccelInto(float &x, float &y, float &z);
ajp109 0:b07c3d3e3d9c 304
ajp109 1:a74f8c175f6c 305 inline i16 getRawMagX() { return OUT_X_M; };
ajp109 1:a74f8c175f6c 306 inline i16 getRawMagY() { return OUT_Y_M; };
ajp109 1:a74f8c175f6c 307 inline i16 getRawMagZ() { return OUT_Z_M; };
ajp109 1:a74f8c175f6c 308 inline float getMagX() { return (float)OUT_X_M / mag_scale_ / 32768; };
ajp109 1:a74f8c175f6c 309 inline float getMagY() { return (float)OUT_Y_M / mag_scale_ / 32768; };
ajp109 1:a74f8c175f6c 310 inline float getMagZ() { return (float)OUT_Z_M / mag_scale_ / 32768; };
ajp109 0:b07c3d3e3d9c 311 void getRawMagInto(short &x, short &y, short &z);
ajp109 0:b07c3d3e3d9c 312 void getMagInto(float &x, float &y, float &z);
ajp109 0:b07c3d3e3d9c 313
ajp109 1:a74f8c175f6c 314 inline float getTemp() { return TEMP_OUT / 8.0 + 25; };
ajp109 0:b07c3d3e3d9c 315
ajp109 0:b07c3d3e3d9c 316 };
ajp109 0:b07c3d3e3d9c 317