LIS3MDL I2C Mbed class with compass heading function

Committer:
gpmbed
Date:
Tue Apr 13 13:46:21 2021 +0000
Revision:
0:4aa6b3804281
Child:
1:fe199024ddfe
LIS3MDL lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gpmbed 0:4aa6b3804281 1 #include "LIS3MDL.h"
gpmbed 0:4aa6b3804281 2
gpmbed 0:4aa6b3804281 3
gpmbed 0:4aa6b3804281 4 LIS3MDL::LIS3MDL(PinName sda, PinName scl) : i2c(sda, scl)
gpmbed 0:4aa6b3804281 5 {
gpmbed 0:4aa6b3804281 6 _address = LIS3MDL_ADDRESS;//LIS3MDL_DEFAULT_ADDRESS;
gpmbed 0:4aa6b3804281 7 }
gpmbed 0:4aa6b3804281 8
gpmbed 0:4aa6b3804281 9 /* ****
gpmbed 0:4aa6b3804281 10 addr data len
gpmbed 0:4aa6b3804281 11 i2c.write(LSM6DS0_ADDRESS, data_write, 2);
gpmbed 0:4aa6b3804281 12 */
gpmbed 0:4aa6b3804281 13
gpmbed 0:4aa6b3804281 14 int LIS3MDL::i2c_write(uint8_t DeviceAddr, uint8_t RegisterAddr,uint8_t value, uint16_t NumByteToWrite)
gpmbed 0:4aa6b3804281 15 {
gpmbed 0:4aa6b3804281 16 int ret;
gpmbed 0:4aa6b3804281 17 int TEMP_BUF_SIZE = 5;
gpmbed 0:4aa6b3804281 18 uint8_t tmp[TEMP_BUF_SIZE];
gpmbed 0:4aa6b3804281 19
gpmbed 0:4aa6b3804281 20 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
gpmbed 0:4aa6b3804281 21
gpmbed 0:4aa6b3804281 22 /* First, send device address. Then, send data and STOP condition */
gpmbed 0:4aa6b3804281 23 tmp[0] = RegisterAddr;
gpmbed 0:4aa6b3804281 24 tmp[1] = value;
gpmbed 0:4aa6b3804281 25 //memcpy(tmp+1, pBuffer, NumByteToWrite);
gpmbed 0:4aa6b3804281 26
gpmbed 0:4aa6b3804281 27 ret = i2c.write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
gpmbed 0:4aa6b3804281 28
gpmbed 0:4aa6b3804281 29 if(ret) return -1;
gpmbed 0:4aa6b3804281 30 return 0;
gpmbed 0:4aa6b3804281 31 }
gpmbed 0:4aa6b3804281 32
gpmbed 0:4aa6b3804281 33 int LIS3MDL::i2c_read(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead) {
gpmbed 0:4aa6b3804281 34 int ret;
gpmbed 0:4aa6b3804281 35
gpmbed 0:4aa6b3804281 36 /* Send device address, with no STOP condition */
gpmbed 0:4aa6b3804281 37 ret = i2c.write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
gpmbed 0:4aa6b3804281 38 if(!ret) {
gpmbed 0:4aa6b3804281 39 /* Read data, with STOP condition */
gpmbed 0:4aa6b3804281 40 ret = i2c.read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
gpmbed 0:4aa6b3804281 41 }
gpmbed 0:4aa6b3804281 42
gpmbed 0:4aa6b3804281 43 if(ret) return -1;
gpmbed 0:4aa6b3804281 44 return 0;
gpmbed 0:4aa6b3804281 45 }
gpmbed 0:4aa6b3804281 46
gpmbed 0:4aa6b3804281 47 bool LIS3MDL::init(void) {
gpmbed 0:4aa6b3804281 48
gpmbed 0:4aa6b3804281 49
gpmbed 0:4aa6b3804281 50 // Choose device mode (bits 1:0 = 00 = continuous data read, 01 = single conversion, 10 & 11 = default power down)
gpmbed 0:4aa6b3804281 51 //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG3, 0x00); // Enable continuous data read mode (bits 1:0 = 00)
gpmbed 0:4aa6b3804281 52 i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG3,0x00,1);
gpmbed 0:4aa6b3804281 53 // Enable temperature sensor (bit 7 = 1)
gpmbed 0:4aa6b3804281 54 // Set magnetometer operative mode for x and y axes (bits 6:5)
gpmbed 0:4aa6b3804281 55 // Set magnetometer ODR (bits 4:2)
gpmbed 0:4aa6b3804281 56 //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG1, 0x80 | Mopmode << 5 | Modr << 2);
gpmbed 0:4aa6b3804281 57 //i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG1, 0x80 | Mopmode << 5 | Modr << 2,1);
gpmbed 0:4aa6b3804281 58
gpmbed 0:4aa6b3804281 59 //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG2, Mscale << 5); // Set magnetometer full scale range
gpmbed 0:4aa6b3804281 60 //i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG2, Mscale << 5,1);
gpmbed 0:4aa6b3804281 61
gpmbed 0:4aa6b3804281 62 //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG4, Mopmode << 2); // Set magnetometer operative mode for z axis
gpmbed 0:4aa6b3804281 63 //i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG4, Mopmode << 2,1); // Set magnetometer operative mode for z axis
gpmbed 0:4aa6b3804281 64
gpmbed 0:4aa6b3804281 65 //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG5, 0x40); // output registers not updated until both data bytes have been read
gpmbed 0:4aa6b3804281 66 i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG5, 0x40,1); // output registers not updated until both data bytes have been read
gpmbed 0:4aa6b3804281 67
gpmbed 0:4aa6b3804281 68
gpmbed 0:4aa6b3804281 69 //Put into the correct operating mode
gpmbed 0:4aa6b3804281 70 //disableLowPower();
gpmbed 0:4aa6b3804281 71 //enableAxisXYZ();
gpmbed 0:4aa6b3804281 72 //setDataRate(2);
gpmbed 0:4aa6b3804281 73
gpmbed 0:4aa6b3804281 74 return 1;
gpmbed 0:4aa6b3804281 75 }
gpmbed 0:4aa6b3804281 76
gpmbed 0:4aa6b3804281 77 bool LIS3MDL::writeRegister(const uint8_t register_addr, const uint8_t value) {
gpmbed 0:4aa6b3804281 78 //send write call to sensor address
gpmbed 0:4aa6b3804281 79 //send register address to sensor
gpmbed 0:4aa6b3804281 80 //send value to register
gpmbed 0:4aa6b3804281 81 /*
gpmbed 0:4aa6b3804281 82 bool write_status = 0;
gpmbed 0:4aa6b3804281 83 Wire.beginTransmission(_address); //open communication with
gpmbed 0:4aa6b3804281 84 Wire.write(register_addr);
gpmbed 0:4aa6b3804281 85 Wire.write(value);
gpmbed 0:4aa6b3804281 86 Wire.endTransmission();
gpmbed 0:4aa6b3804281 87 */
gpmbed 0:4aa6b3804281 88 i2c_write(_address,register_addr,value,1);
gpmbed 0:4aa6b3804281 89
gpmbed 0:4aa6b3804281 90 return 1; //returns whether the write succeeded or failed
gpmbed 0:4aa6b3804281 91 }
gpmbed 0:4aa6b3804281 92
gpmbed 0:4aa6b3804281 93 uint8_t LIS3MDL::readRegister(const uint8_t register_addr)
gpmbed 0:4aa6b3804281 94 {
gpmbed 0:4aa6b3804281 95 uint8_t reg_val = 0;
gpmbed 0:4aa6b3804281 96 i2c_read(_address, register_addr, &reg_val, 1);
gpmbed 0:4aa6b3804281 97
gpmbed 0:4aa6b3804281 98 return reg_val;
gpmbed 0:4aa6b3804281 99 }
gpmbed 0:4aa6b3804281 100
gpmbed 0:4aa6b3804281 101 uint16_t LIS3MDL::readRegisters(const uint8_t msb_register, const uint8_t lsb_register) {
gpmbed 0:4aa6b3804281 102 uint8_t msb = readRegister(msb_register);
gpmbed 0:4aa6b3804281 103 uint8_t lsb = readRegister(lsb_register);
gpmbed 0:4aa6b3804281 104 return (((int16_t)msb) << 8) | lsb;
gpmbed 0:4aa6b3804281 105 }
gpmbed 0:4aa6b3804281 106
gpmbed 0:4aa6b3804281 107 bool LIS3MDL::writeMaskedRegister(const uint8_t register_addr, const uint8_t mask, const bool value) {
gpmbed 0:4aa6b3804281 108 uint8_t data = readRegister(register_addr);
gpmbed 0:4aa6b3804281 109 uint8_t combo;
gpmbed 0:4aa6b3804281 110 if(value) {
gpmbed 0:4aa6b3804281 111 combo = (mask | data);
gpmbed 0:4aa6b3804281 112 } else {
gpmbed 0:4aa6b3804281 113 combo = ((~mask) & data);
gpmbed 0:4aa6b3804281 114 }
gpmbed 0:4aa6b3804281 115 return writeRegister(register_addr, combo);
gpmbed 0:4aa6b3804281 116 }
gpmbed 0:4aa6b3804281 117
gpmbed 0:4aa6b3804281 118 bool LIS3MDL::writeMaskedRegister(const int register_addr, const int mask, const int value) {
gpmbed 0:4aa6b3804281 119 uint8_t data = readRegister(register_addr);
gpmbed 0:4aa6b3804281 120 uint8_t masked_value = (data | (mask & value)); //Not sure if right...
gpmbed 0:4aa6b3804281 121 return writeRegister(register_addr, masked_value);
gpmbed 0:4aa6b3804281 122 }
gpmbed 0:4aa6b3804281 123
gpmbed 0:4aa6b3804281 124 uint8_t LIS3MDL::readMaskedRegister(const uint8_t register_addr, const uint8_t mask) {
gpmbed 0:4aa6b3804281 125 uint8_t data = readRegister(register_addr);
gpmbed 0:4aa6b3804281 126 return (data & mask);
gpmbed 0:4aa6b3804281 127 }
gpmbed 0:4aa6b3804281 128
gpmbed 0:4aa6b3804281 129
gpmbed 0:4aa6b3804281 130 /** Read the X axis registers
gpmbed 0:4aa6b3804281 131 * @see LIS3MDL_OUT_X_H
gpmbed 0:4aa6b3804281 132 * @see LIS3MDL_OUT_X_L
gpmbed 0:4aa6b3804281 133 */
gpmbed 0:4aa6b3804281 134 int16_t LIS3MDL::getAxisX(void) {
gpmbed 0:4aa6b3804281 135 return readRegisters(LIS3MDL_OUT_X_H, LIS3MDL_OUT_X_L);
gpmbed 0:4aa6b3804281 136 }
gpmbed 0:4aa6b3804281 137
gpmbed 0:4aa6b3804281 138 float LIS3MDL::getAxisX_mag()
gpmbed 0:4aa6b3804281 139 {
gpmbed 0:4aa6b3804281 140 float res = 0;
gpmbed 0:4aa6b3804281 141 float factor = 6842;
gpmbed 0:4aa6b3804281 142 res = (int16_t)getAxisX();
gpmbed 0:4aa6b3804281 143 res = res / factor;
gpmbed 0:4aa6b3804281 144
gpmbed 0:4aa6b3804281 145 return res;
gpmbed 0:4aa6b3804281 146 }
gpmbed 0:4aa6b3804281 147
gpmbed 0:4aa6b3804281 148
gpmbed 0:4aa6b3804281 149 /** Read the Y axis registers
gpmbed 0:4aa6b3804281 150 * @see LIS3MDL_OUT_Y_H
gpmbed 0:4aa6b3804281 151 * @see LIS3MDL_OUT_Y_L
gpmbed 0:4aa6b3804281 152 */
gpmbed 0:4aa6b3804281 153 int16_t LIS3MDL::getAxisY(void) {
gpmbed 0:4aa6b3804281 154 return readRegisters(LIS3MDL_OUT_Y_H, LIS3MDL_OUT_Y_L);
gpmbed 0:4aa6b3804281 155 }
gpmbed 0:4aa6b3804281 156
gpmbed 0:4aa6b3804281 157 float LIS3MDL::getAxisY_mag()
gpmbed 0:4aa6b3804281 158 {
gpmbed 0:4aa6b3804281 159 float res = 0;
gpmbed 0:4aa6b3804281 160 float factor = 6842;
gpmbed 0:4aa6b3804281 161 res = (int16_t)getAxisY();
gpmbed 0:4aa6b3804281 162 res = res / factor;
gpmbed 0:4aa6b3804281 163
gpmbed 0:4aa6b3804281 164 return res;
gpmbed 0:4aa6b3804281 165 }
gpmbed 0:4aa6b3804281 166
gpmbed 0:4aa6b3804281 167 /** Read the Z axis registers
gpmbed 0:4aa6b3804281 168 * @see LIS3MDL_OUT_Z_H
gpmbed 0:4aa6b3804281 169 * @see LIS3MDL_OUT_Z_L
gpmbed 0:4aa6b3804281 170 */
gpmbed 0:4aa6b3804281 171 int16_t LIS3MDL::getAxisZ(void) {
gpmbed 0:4aa6b3804281 172 return readRegisters(LIS3MDL_OUT_Z_H, LIS3MDL_OUT_Z_L);
gpmbed 0:4aa6b3804281 173 }
gpmbed 0:4aa6b3804281 174
gpmbed 0:4aa6b3804281 175 int16_t LIS3MDL::getTemperature(void) {
gpmbed 0:4aa6b3804281 176 return readRegisters(LIS3MDL_TEMP_OUT_H,LIS3MDL_TEMP_OUT_L);
gpmbed 0:4aa6b3804281 177 }
gpmbed 0:4aa6b3804281 178
gpmbed 0:4aa6b3804281 179
gpmbed 0:4aa6b3804281 180 float LIS3MDL::getHeading()
gpmbed 0:4aa6b3804281 181 {
gpmbed 0:4aa6b3804281 182 float xH = getAxisX_mag();
gpmbed 0:4aa6b3804281 183 float yH = getAxisY_mag();
gpmbed 0:4aa6b3804281 184 //float xH = (float)mag_axes[0]; //milliGauss
gpmbed 0:4aa6b3804281 185 //float yH = (float)mag_axes[1]; //milliGauss
gpmbed 0:4aa6b3804281 186 xH *=1000; //milliGauss
gpmbed 0:4aa6b3804281 187 yH *=1000; //milliGauss
gpmbed 0:4aa6b3804281 188
gpmbed 0:4aa6b3804281 189 xH = xH - 190; //taratura manuale
gpmbed 0:4aa6b3804281 190 yH = yH + 550;
gpmbed 0:4aa6b3804281 191
gpmbed 0:4aa6b3804281 192 float heading = atan(yH/xH)*LIS3MDL_Rad2Degree;
gpmbed 0:4aa6b3804281 193
gpmbed 0:4aa6b3804281 194
gpmbed 0:4aa6b3804281 195 if(xH<0){heading=180-heading;}
gpmbed 0:4aa6b3804281 196 else if(xH>0 && yH<0){heading=-heading;}
gpmbed 0:4aa6b3804281 197 else if(xH>0 && yH>0){heading=360-heading; }
gpmbed 0:4aa6b3804281 198 else if(xH==0 && yH<0){heading=90; }
gpmbed 0:4aa6b3804281 199 else if(xH==0 && yH>0){heading=270; }
gpmbed 0:4aa6b3804281 200
gpmbed 0:4aa6b3804281 201 heading = (360-heading)+180;
gpmbed 0:4aa6b3804281 202
gpmbed 0:4aa6b3804281 203 if(heading<0){ heading +=360; }
gpmbed 0:4aa6b3804281 204 else if(heading>360){ heading -=360; }
gpmbed 0:4aa6b3804281 205
gpmbed 0:4aa6b3804281 206
gpmbed 0:4aa6b3804281 207 return heading;
gpmbed 0:4aa6b3804281 208 }
gpmbed 0:4aa6b3804281 209
gpmbed 0:4aa6b3804281 210 void getMag(int16_t* ax, int16_t* ay, int16_t* az) {
gpmbed 0:4aa6b3804281 211 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 212 }
gpmbed 0:4aa6b3804281 213
gpmbed 0:4aa6b3804281 214 bool whoAmI(void) {
gpmbed 0:4aa6b3804281 215 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 216 }
gpmbed 0:4aa6b3804281 217
gpmbed 0:4aa6b3804281 218 bool getTempEnabled(void) {
gpmbed 0:4aa6b3804281 219 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 220 }
gpmbed 0:4aa6b3804281 221
gpmbed 0:4aa6b3804281 222 bool setTempEnabled(bool enable) {
gpmbed 0:4aa6b3804281 223 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 224 }
gpmbed 0:4aa6b3804281 225
gpmbed 0:4aa6b3804281 226 uint8_t getOperativeMode(void) {
gpmbed 0:4aa6b3804281 227 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 228 }
gpmbed 0:4aa6b3804281 229
gpmbed 0:4aa6b3804281 230 bool setOperativeMode(uint8_t mode) {
gpmbed 0:4aa6b3804281 231 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 232 }
gpmbed 0:4aa6b3804281 233
gpmbed 0:4aa6b3804281 234 uint8_t getDataRate(void) {
gpmbed 0:4aa6b3804281 235 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 236 }
gpmbed 0:4aa6b3804281 237
gpmbed 0:4aa6b3804281 238 bool setDataRate(uint8_t rate) {
gpmbed 0:4aa6b3804281 239 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 240 }
gpmbed 0:4aa6b3804281 241
gpmbed 0:4aa6b3804281 242 bool getSelfTestEnabled(void) {
gpmbed 0:4aa6b3804281 243 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 244 }
gpmbed 0:4aa6b3804281 245
gpmbed 0:4aa6b3804281 246 bool setSelfTestEnabled(bool enable) {
gpmbed 0:4aa6b3804281 247 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 248 }
gpmbed 0:4aa6b3804281 249
gpmbed 0:4aa6b3804281 250 uint8_t getFullScale(void) {
gpmbed 0:4aa6b3804281 251 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 252 }
gpmbed 0:4aa6b3804281 253
gpmbed 0:4aa6b3804281 254 bool setFullScale(uint8_t sccale) {
gpmbed 0:4aa6b3804281 255 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 256 }
gpmbed 0:4aa6b3804281 257
gpmbed 0:4aa6b3804281 258 bool reboot(void) {
gpmbed 0:4aa6b3804281 259 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 260 }
gpmbed 0:4aa6b3804281 261
gpmbed 0:4aa6b3804281 262 bool softReset(void) {
gpmbed 0:4aa6b3804281 263 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 264 }
gpmbed 0:4aa6b3804281 265
gpmbed 0:4aa6b3804281 266 bool getLowPowerMode(void) {
gpmbed 0:4aa6b3804281 267 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 268 }
gpmbed 0:4aa6b3804281 269
gpmbed 0:4aa6b3804281 270 bool setLowPowerMode(bool mode) {
gpmbed 0:4aa6b3804281 271 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 272 }
gpmbed 0:4aa6b3804281 273
gpmbed 0:4aa6b3804281 274 bool getSPIMode(void) {
gpmbed 0:4aa6b3804281 275 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 276 }
gpmbed 0:4aa6b3804281 277
gpmbed 0:4aa6b3804281 278 bool setSPIMode(bool mode) {
gpmbed 0:4aa6b3804281 279 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 280 }
gpmbed 0:4aa6b3804281 281
gpmbed 0:4aa6b3804281 282 uint8_t getOperatingMode(void) {
gpmbed 0:4aa6b3804281 283 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 284 }
gpmbed 0:4aa6b3804281 285
gpmbed 0:4aa6b3804281 286 bool setOperatingMode(uint8_t mode) {
gpmbed 0:4aa6b3804281 287 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 288 }
gpmbed 0:4aa6b3804281 289
gpmbed 0:4aa6b3804281 290 uint8_t getOMZ(void) {
gpmbed 0:4aa6b3804281 291 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 292 }
gpmbed 0:4aa6b3804281 293
gpmbed 0:4aa6b3804281 294 bool setOMZ(uint8_t mode) {
gpmbed 0:4aa6b3804281 295 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 296 }
gpmbed 0:4aa6b3804281 297
gpmbed 0:4aa6b3804281 298 bool getEndian(void) {
gpmbed 0:4aa6b3804281 299 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 300 }
gpmbed 0:4aa6b3804281 301
gpmbed 0:4aa6b3804281 302 bool setEndian(bool selection) {
gpmbed 0:4aa6b3804281 303 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 304 }
gpmbed 0:4aa6b3804281 305
gpmbed 0:4aa6b3804281 306 bool disableDataUpdate(void) {
gpmbed 0:4aa6b3804281 307 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 308 }
gpmbed 0:4aa6b3804281 309
gpmbed 0:4aa6b3804281 310 bool enableDataUpdate(void) {
gpmbed 0:4aa6b3804281 311 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 312 }
gpmbed 0:4aa6b3804281 313
gpmbed 0:4aa6b3804281 314 bool getDataUpdateStatus(void) {
gpmbed 0:4aa6b3804281 315 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 316 }
gpmbed 0:4aa6b3804281 317
gpmbed 0:4aa6b3804281 318 bool getXYZOverrun(void) {
gpmbed 0:4aa6b3804281 319 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 320 }
gpmbed 0:4aa6b3804281 321
gpmbed 0:4aa6b3804281 322 bool getZOverrun(void) {
gpmbed 0:4aa6b3804281 323 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 324 }
gpmbed 0:4aa6b3804281 325
gpmbed 0:4aa6b3804281 326 bool getYOverrun(void) {
gpmbed 0:4aa6b3804281 327 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 328 }
gpmbed 0:4aa6b3804281 329
gpmbed 0:4aa6b3804281 330 bool getXOverrun(void) {
gpmbed 0:4aa6b3804281 331 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 332 }
gpmbed 0:4aa6b3804281 333
gpmbed 0:4aa6b3804281 334 bool getXYZDataAvailable(void) {
gpmbed 0:4aa6b3804281 335 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 336 }
gpmbed 0:4aa6b3804281 337
gpmbed 0:4aa6b3804281 338 bool getZDataAvailable(void) {
gpmbed 0:4aa6b3804281 339 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 340 }
gpmbed 0:4aa6b3804281 341
gpmbed 0:4aa6b3804281 342 bool getYDataAvailabl(void) {
gpmbed 0:4aa6b3804281 343 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 344 }
gpmbed 0:4aa6b3804281 345
gpmbed 0:4aa6b3804281 346 bool getXDataAvailable(void) {
gpmbed 0:4aa6b3804281 347 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 348 }
gpmbed 0:4aa6b3804281 349
gpmbed 0:4aa6b3804281 350 bool enableXInterupt(void) {
gpmbed 0:4aa6b3804281 351 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 352 }
gpmbed 0:4aa6b3804281 353
gpmbed 0:4aa6b3804281 354 bool disableXInterupt(void) {
gpmbed 0:4aa6b3804281 355 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 356 }
gpmbed 0:4aa6b3804281 357
gpmbed 0:4aa6b3804281 358 bool isXIntEnabled(void) {
gpmbed 0:4aa6b3804281 359 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 360 }
gpmbed 0:4aa6b3804281 361
gpmbed 0:4aa6b3804281 362 bool enableYInterupt(void) {
gpmbed 0:4aa6b3804281 363 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 364 }
gpmbed 0:4aa6b3804281 365
gpmbed 0:4aa6b3804281 366 bool disableYInterupt(void) {
gpmbed 0:4aa6b3804281 367 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 368 }
gpmbed 0:4aa6b3804281 369
gpmbed 0:4aa6b3804281 370 bool isYIntEnabled(void) {
gpmbed 0:4aa6b3804281 371 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 372 }
gpmbed 0:4aa6b3804281 373
gpmbed 0:4aa6b3804281 374 bool enableZInterupt(void) {
gpmbed 0:4aa6b3804281 375 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 376 }
gpmbed 0:4aa6b3804281 377
gpmbed 0:4aa6b3804281 378 bool disableZInterupt(void) {
gpmbed 0:4aa6b3804281 379 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 380 }
gpmbed 0:4aa6b3804281 381
gpmbed 0:4aa6b3804281 382 bool isZIntEnabled(void) {
gpmbed 0:4aa6b3804281 383 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 384 }
gpmbed 0:4aa6b3804281 385
gpmbed 0:4aa6b3804281 386 bool setIntHigh(void) {
gpmbed 0:4aa6b3804281 387 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 388 }
gpmbed 0:4aa6b3804281 389
gpmbed 0:4aa6b3804281 390 bool setIntLow(void) {
gpmbed 0:4aa6b3804281 391 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 392 }
gpmbed 0:4aa6b3804281 393
gpmbed 0:4aa6b3804281 394 bool checkIntConfig(void) {
gpmbed 0:4aa6b3804281 395 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 396 }
gpmbed 0:4aa6b3804281 397
gpmbed 0:4aa6b3804281 398 bool setInteruptLatch(bool latch) {
gpmbed 0:4aa6b3804281 399 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 400 }
gpmbed 0:4aa6b3804281 401
gpmbed 0:4aa6b3804281 402 bool checkInteruptLatch(void) {
gpmbed 0:4aa6b3804281 403 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 404 }
gpmbed 0:4aa6b3804281 405
gpmbed 0:4aa6b3804281 406 bool enableIntInterupt(void) {
gpmbed 0:4aa6b3804281 407 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 408 }
gpmbed 0:4aa6b3804281 409
gpmbed 0:4aa6b3804281 410 bool disableIntInterupt(void) {
gpmbed 0:4aa6b3804281 411 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 412 }
gpmbed 0:4aa6b3804281 413
gpmbed 0:4aa6b3804281 414 bool checkIntInterupt(void) {
gpmbed 0:4aa6b3804281 415 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 416 }
gpmbed 0:4aa6b3804281 417
gpmbed 0:4aa6b3804281 418
gpmbed 0:4aa6b3804281 419 bool posXThreshold(void) {
gpmbed 0:4aa6b3804281 420 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 421 }
gpmbed 0:4aa6b3804281 422
gpmbed 0:4aa6b3804281 423 bool posYThreshold(void) {
gpmbed 0:4aa6b3804281 424 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 425 }
gpmbed 0:4aa6b3804281 426
gpmbed 0:4aa6b3804281 427 bool posZThreshold(void) {
gpmbed 0:4aa6b3804281 428 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 429 }
gpmbed 0:4aa6b3804281 430
gpmbed 0:4aa6b3804281 431 bool negXThreshold(void) {
gpmbed 0:4aa6b3804281 432 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 433 }
gpmbed 0:4aa6b3804281 434
gpmbed 0:4aa6b3804281 435 bool negYThreshold(void) {
gpmbed 0:4aa6b3804281 436 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 437 }
gpmbed 0:4aa6b3804281 438
gpmbed 0:4aa6b3804281 439 bool negZThreshold(void) {
gpmbed 0:4aa6b3804281 440 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 441 }
gpmbed 0:4aa6b3804281 442
gpmbed 0:4aa6b3804281 443 bool measurementOverflow(void) {
gpmbed 0:4aa6b3804281 444 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 445 }
gpmbed 0:4aa6b3804281 446
gpmbed 0:4aa6b3804281 447 bool checkInteruptEvent(void) {
gpmbed 0:4aa6b3804281 448 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 449 }
gpmbed 0:4aa6b3804281 450
gpmbed 0:4aa6b3804281 451
gpmbed 0:4aa6b3804281 452 bool setInteruptThreshold(uint16_t threshold) {
gpmbed 0:4aa6b3804281 453 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 454 }
gpmbed 0:4aa6b3804281 455
gpmbed 0:4aa6b3804281 456 uint16_t getInteruptThreshold(void) {
gpmbed 0:4aa6b3804281 457 //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0);
gpmbed 0:4aa6b3804281 458 }
gpmbed 0:4aa6b3804281 459
gpmbed 0:4aa6b3804281 460
gpmbed 0:4aa6b3804281 461
gpmbed 0:4aa6b3804281 462
gpmbed 0:4aa6b3804281 463
gpmbed 0:4aa6b3804281 464 /** Read the all axis registers
gpmbed 0:4aa6b3804281 465 * @see getAxisZ()
gpmbed 0:4aa6b3804281 466 * @see getAxisY()
gpmbed 0:4aa6b3804281 467 * @see getAxisZ()
gpmbed 0:4aa6b3804281 468 */
gpmbed 0:4aa6b3804281 469 void LIS3MDL::getMotion(int16_t* ax, int16_t* ay, int16_t* az) {
gpmbed 0:4aa6b3804281 470 *ax = getAxisX();
gpmbed 0:4aa6b3804281 471 *ay = getAxisY();
gpmbed 0:4aa6b3804281 472 *az = getAxisZ();
gpmbed 0:4aa6b3804281 473 }
gpmbed 0:4aa6b3804281 474
gpmbed 0:4aa6b3804281 475 //bool LIS3MDL::tempHasOverrun(void) {
gpmbed 0:4aa6b3804281 476 // uint8_t overrun = readMaskedRegister(LIS3MDL_STATUS_REG_AUX, LIS3MDL_TOR_MASK);
gpmbed 0:4aa6b3804281 477 // return (overrun != 0);
gpmbed 0:4aa6b3804281 478 //}
gpmbed 0:4aa6b3804281 479
gpmbed 0:4aa6b3804281 480 //bool LIS3MDL::tempDataAvailable(void) {
gpmbed 0:4aa6b3804281 481 // uint8_t data = readMaskedRegister(LIS3MDL_STATUS_REG_AUX, LIS3MDL_TDA_MASK);
gpmbed 0:4aa6b3804281 482 // return (data != 0);
gpmbed 0:4aa6b3804281 483 //}
gpmbed 0:4aa6b3804281 484
gpmbed 0:4aa6b3804281 485 //uint16_t LIS3MDL::getTemperature(void) {
gpmbed 0:4aa6b3804281 486 // if(tempDataAvailable()){
gpmbed 0:4aa6b3804281 487 // return readRegisters(LIS3MDL_OUT_TEMP_H, LIS3MDL_OUT_TEMP_L);
gpmbed 0:4aa6b3804281 488 // } else {
gpmbed 0:4aa6b3804281 489 // //if new data isn't available
gpmbed 0:4aa6b3804281 490 // return 0;
gpmbed 0:4aa6b3804281 491 // }
gpmbed 0:4aa6b3804281 492 //}
gpmbed 0:4aa6b3804281 493
gpmbed 0:4aa6b3804281 494 bool LIS3MDL::whoAmI(void) {
gpmbed 0:4aa6b3804281 495 return (LIS3MDL_I_AM_MASK == readRegister(LIS3MDL_WHO_AM_I));
gpmbed 0:4aa6b3804281 496 }
gpmbed 0:4aa6b3804281 497
gpmbed 0:4aa6b3804281 498 bool LIS3MDL::getTempEnabled(void) {
gpmbed 0:4aa6b3804281 499 //return (readMaskedRegister(LIS3MDL_TEMP_CFG_REG, LIS3MDL_TEMP_EN_MASK) != 0);
gpmbed 0:4aa6b3804281 500 }
gpmbed 0:4aa6b3804281 501
gpmbed 0:4aa6b3804281 502 bool LIS3MDL::setTempEnabled(bool enable) {
gpmbed 0:4aa6b3804281 503 //return writeRegister(LIS3MDL_TEMP_CFG_REG, enable ? LIS3MDL_TEMP_EN_MASK : 0);
gpmbed 0:4aa6b3804281 504 }
gpmbed 0:4aa6b3804281 505
gpmbed 0:4aa6b3804281 506 uint8_t LIS3MDL::getDataRate(void) {
gpmbed 0:4aa6b3804281 507 //return readMaskedRegister(LIS3MDL_CTRL_REG1, LIS3MDL_ODR_MASK);
gpmbed 0:4aa6b3804281 508 }
gpmbed 0:4aa6b3804281 509
gpmbed 0:4aa6b3804281 510 bool LIS3MDL::setDataRate(uint8_t rate) {
gpmbed 0:4aa6b3804281 511 if(rate > 9) {
gpmbed 0:4aa6b3804281 512 return 0;
gpmbed 0:4aa6b3804281 513 }
gpmbed 0:4aa6b3804281 514 uint8_t data_rate = rate << 4;
gpmbed 0:4aa6b3804281 515 // return writeMaskedRegister(LIS3MDL_CTRL_REG1, LIS3MDL_ODR_MASK, data_rate);
gpmbed 0:4aa6b3804281 516 }
gpmbed 0:4aa6b3804281 517
gpmbed 0:4aa6b3804281 518
gpmbed 0:4aa6b3804281 519
gpmbed 0:4aa6b3804281 520