alarm system

Dependencies:   mbed xbee_lib

Fork of rtos_mutex by mbed official

Committer:
sreddy47
Date:
Fri Apr 29 06:59:56 2016 +0000
Revision:
5:802a7c5a4b27
alarm;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sreddy47 5:802a7c5a4b27 1 /******************************************************************************
sreddy47 5:802a7c5a4b27 2 SFE_LSM9DS1.cpp
sreddy47 5:802a7c5a4b27 3 SFE_LSM9DS1 Library Source File
sreddy47 5:802a7c5a4b27 4 Jim Lindblom @ SparkFun Electronics
sreddy47 5:802a7c5a4b27 5 Original Creation Date: February 27, 2015
sreddy47 5:802a7c5a4b27 6 https://github.com/sparkfun/LSM9DS1_Breakout
sreddy47 5:802a7c5a4b27 7
sreddy47 5:802a7c5a4b27 8 This file implements all functions of the LSM9DS1 class. Functions here range
sreddy47 5:802a7c5a4b27 9 from higher level stuff, like reading/writing LSM9DS1 registers to low-level,
sreddy47 5:802a7c5a4b27 10 hardware reads and writes. Both SPI and I2C handler functions can be found
sreddy47 5:802a7c5a4b27 11 towards the bottom of this file.
sreddy47 5:802a7c5a4b27 12
sreddy47 5:802a7c5a4b27 13 Development environment specifics:
sreddy47 5:802a7c5a4b27 14 IDE: Arduino 1.6
sreddy47 5:802a7c5a4b27 15 Hardware Platform: Arduino Uno
sreddy47 5:802a7c5a4b27 16 LSM9DS1 Breakout Version: 1.0
sreddy47 5:802a7c5a4b27 17
sreddy47 5:802a7c5a4b27 18 This code is beerware; if you see me (or any other SparkFun employee) at the
sreddy47 5:802a7c5a4b27 19 local, and you've found our code helpful, please buy us a round!
sreddy47 5:802a7c5a4b27 20
sreddy47 5:802a7c5a4b27 21 Distributed as-is; no warranty is given.
sreddy47 5:802a7c5a4b27 22 ******************************************************************************/
sreddy47 5:802a7c5a4b27 23
sreddy47 5:802a7c5a4b27 24 #include "LSM9DS1.h"
sreddy47 5:802a7c5a4b27 25 #include "LSM9DS1_Registers.h"
sreddy47 5:802a7c5a4b27 26 #include "LSM9DS1_Types.h"
sreddy47 5:802a7c5a4b27 27 //#include <Wire.h> // Wire library is used for I2C
sreddy47 5:802a7c5a4b27 28 //#include <SPI.h> // SPI library is used for...SPI.
sreddy47 5:802a7c5a4b27 29
sreddy47 5:802a7c5a4b27 30 //#if defined(ARDUINO) && ARDUINO >= 100
sreddy47 5:802a7c5a4b27 31 // #include "Arduino.h"
sreddy47 5:802a7c5a4b27 32 //#else
sreddy47 5:802a7c5a4b27 33 // #include "WProgram.h"
sreddy47 5:802a7c5a4b27 34 //#endif
sreddy47 5:802a7c5a4b27 35
sreddy47 5:802a7c5a4b27 36 #define LSM9DS1_COMMUNICATION_TIMEOUT 1000
sreddy47 5:802a7c5a4b27 37
sreddy47 5:802a7c5a4b27 38 float magSensitivity[4] = {0.00014, 0.00029, 0.00043, 0.00058};
sreddy47 5:802a7c5a4b27 39 extern Serial pc;
sreddy47 5:802a7c5a4b27 40
sreddy47 5:802a7c5a4b27 41 LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr)
sreddy47 5:802a7c5a4b27 42 :i2c(sda, scl)
sreddy47 5:802a7c5a4b27 43 {
sreddy47 5:802a7c5a4b27 44 init(IMU_MODE_I2C, xgAddr, mAddr); // dont know about 0xD6 or 0x3B
sreddy47 5:802a7c5a4b27 45 }
sreddy47 5:802a7c5a4b27 46 /*
sreddy47 5:802a7c5a4b27 47 LSM9DS1::LSM9DS1()
sreddy47 5:802a7c5a4b27 48 {
sreddy47 5:802a7c5a4b27 49 init(IMU_MODE_I2C, LSM9DS1_AG_ADDR(1), LSM9DS1_M_ADDR(1));
sreddy47 5:802a7c5a4b27 50 }
sreddy47 5:802a7c5a4b27 51
sreddy47 5:802a7c5a4b27 52 LSM9DS1::LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr)
sreddy47 5:802a7c5a4b27 53 {
sreddy47 5:802a7c5a4b27 54 init(interface, xgAddr, mAddr);
sreddy47 5:802a7c5a4b27 55 }
sreddy47 5:802a7c5a4b27 56 */
sreddy47 5:802a7c5a4b27 57
sreddy47 5:802a7c5a4b27 58 void LSM9DS1::init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr)
sreddy47 5:802a7c5a4b27 59 {
sreddy47 5:802a7c5a4b27 60 settings.device.commInterface = interface;
sreddy47 5:802a7c5a4b27 61 settings.device.agAddress = xgAddr;
sreddy47 5:802a7c5a4b27 62 settings.device.mAddress = mAddr;
sreddy47 5:802a7c5a4b27 63
sreddy47 5:802a7c5a4b27 64 settings.gyro.enabled = true;
sreddy47 5:802a7c5a4b27 65 settings.gyro.enableX = true;
sreddy47 5:802a7c5a4b27 66 settings.gyro.enableY = true;
sreddy47 5:802a7c5a4b27 67 settings.gyro.enableZ = true;
sreddy47 5:802a7c5a4b27 68 // gyro scale can be 245, 500, or 2000
sreddy47 5:802a7c5a4b27 69 settings.gyro.scale = 245;
sreddy47 5:802a7c5a4b27 70 // gyro sample rate: value between 1-6
sreddy47 5:802a7c5a4b27 71 // 1 = 14.9 4 = 238
sreddy47 5:802a7c5a4b27 72 // 2 = 59.5 5 = 476
sreddy47 5:802a7c5a4b27 73 // 3 = 119 6 = 952
sreddy47 5:802a7c5a4b27 74 settings.gyro.sampleRate = 6;
sreddy47 5:802a7c5a4b27 75 // gyro cutoff frequency: value between 0-3
sreddy47 5:802a7c5a4b27 76 // Actual value of cutoff frequency depends
sreddy47 5:802a7c5a4b27 77 // on sample rate.
sreddy47 5:802a7c5a4b27 78 settings.gyro.bandwidth = 0;
sreddy47 5:802a7c5a4b27 79 settings.gyro.lowPowerEnable = false;
sreddy47 5:802a7c5a4b27 80 settings.gyro.HPFEnable = false;
sreddy47 5:802a7c5a4b27 81 // Gyro HPF cutoff frequency: value between 0-9
sreddy47 5:802a7c5a4b27 82 // Actual value depends on sample rate. Only applies
sreddy47 5:802a7c5a4b27 83 // if gyroHPFEnable is true.
sreddy47 5:802a7c5a4b27 84 settings.gyro.HPFCutoff = 0;
sreddy47 5:802a7c5a4b27 85 settings.gyro.flipX = false;
sreddy47 5:802a7c5a4b27 86 settings.gyro.flipY = false;
sreddy47 5:802a7c5a4b27 87 settings.gyro.flipZ = false;
sreddy47 5:802a7c5a4b27 88 settings.gyro.orientation = 0;
sreddy47 5:802a7c5a4b27 89 settings.gyro.latchInterrupt = true;
sreddy47 5:802a7c5a4b27 90
sreddy47 5:802a7c5a4b27 91 settings.accel.enabled = true;
sreddy47 5:802a7c5a4b27 92 settings.accel.enableX = true;
sreddy47 5:802a7c5a4b27 93 settings.accel.enableY = true;
sreddy47 5:802a7c5a4b27 94 settings.accel.enableZ = true;
sreddy47 5:802a7c5a4b27 95 // accel scale can be 2, 4, 8, or 16
sreddy47 5:802a7c5a4b27 96 settings.accel.scale = 2;
sreddy47 5:802a7c5a4b27 97 // accel sample rate can be 1-6
sreddy47 5:802a7c5a4b27 98 // 1 = 10 Hz 4 = 238 Hz
sreddy47 5:802a7c5a4b27 99 // 2 = 50 Hz 5 = 476 Hz
sreddy47 5:802a7c5a4b27 100 // 3 = 119 Hz 6 = 952 Hz
sreddy47 5:802a7c5a4b27 101 settings.accel.sampleRate = 6;
sreddy47 5:802a7c5a4b27 102 // Accel cutoff freqeuncy can be any value between -1 - 3.
sreddy47 5:802a7c5a4b27 103 // -1 = bandwidth determined by sample rate
sreddy47 5:802a7c5a4b27 104 // 0 = 408 Hz 2 = 105 Hz
sreddy47 5:802a7c5a4b27 105 // 1 = 211 Hz 3 = 50 Hz
sreddy47 5:802a7c5a4b27 106 settings.accel.bandwidth = -1;
sreddy47 5:802a7c5a4b27 107 settings.accel.highResEnable = false;
sreddy47 5:802a7c5a4b27 108 // accelHighResBandwidth can be any value between 0-3
sreddy47 5:802a7c5a4b27 109 // LP cutoff is set to a factor of sample rate
sreddy47 5:802a7c5a4b27 110 // 0 = ODR/50 2 = ODR/9
sreddy47 5:802a7c5a4b27 111 // 1 = ODR/100 3 = ODR/400
sreddy47 5:802a7c5a4b27 112 settings.accel.highResBandwidth = 0;
sreddy47 5:802a7c5a4b27 113
sreddy47 5:802a7c5a4b27 114 settings.mag.enabled = true;
sreddy47 5:802a7c5a4b27 115 // mag scale can be 4, 8, 12, or 16
sreddy47 5:802a7c5a4b27 116 settings.mag.scale = 4;
sreddy47 5:802a7c5a4b27 117 // mag data rate can be 0-7
sreddy47 5:802a7c5a4b27 118 // 0 = 0.625 Hz 4 = 10 Hz
sreddy47 5:802a7c5a4b27 119 // 1 = 1.25 Hz 5 = 20 Hz
sreddy47 5:802a7c5a4b27 120 // 2 = 2.5 Hz 6 = 40 Hz
sreddy47 5:802a7c5a4b27 121 // 3 = 5 Hz 7 = 80 Hz
sreddy47 5:802a7c5a4b27 122 settings.mag.sampleRate = 7;
sreddy47 5:802a7c5a4b27 123 settings.mag.tempCompensationEnable = false;
sreddy47 5:802a7c5a4b27 124 // magPerformance can be any value between 0-3
sreddy47 5:802a7c5a4b27 125 // 0 = Low power mode 2 = high performance
sreddy47 5:802a7c5a4b27 126 // 1 = medium performance 3 = ultra-high performance
sreddy47 5:802a7c5a4b27 127 settings.mag.XYPerformance = 3;
sreddy47 5:802a7c5a4b27 128 settings.mag.ZPerformance = 3;
sreddy47 5:802a7c5a4b27 129 settings.mag.lowPowerEnable = false;
sreddy47 5:802a7c5a4b27 130 // magOperatingMode can be 0-2
sreddy47 5:802a7c5a4b27 131 // 0 = continuous conversion
sreddy47 5:802a7c5a4b27 132 // 1 = single-conversion
sreddy47 5:802a7c5a4b27 133 // 2 = power down
sreddy47 5:802a7c5a4b27 134 settings.mag.operatingMode = 0;
sreddy47 5:802a7c5a4b27 135
sreddy47 5:802a7c5a4b27 136 settings.temp.enabled = true;
sreddy47 5:802a7c5a4b27 137 for (int i=0; i<3; i++)
sreddy47 5:802a7c5a4b27 138 {
sreddy47 5:802a7c5a4b27 139 gBias[i] = 0;
sreddy47 5:802a7c5a4b27 140 aBias[i] = 0;
sreddy47 5:802a7c5a4b27 141 mBias[i] = 0;
sreddy47 5:802a7c5a4b27 142 gBiasRaw[i] = 0;
sreddy47 5:802a7c5a4b27 143 aBiasRaw[i] = 0;
sreddy47 5:802a7c5a4b27 144 mBiasRaw[i] = 0;
sreddy47 5:802a7c5a4b27 145 }
sreddy47 5:802a7c5a4b27 146 _autoCalc = false;
sreddy47 5:802a7c5a4b27 147 }
sreddy47 5:802a7c5a4b27 148
sreddy47 5:802a7c5a4b27 149
sreddy47 5:802a7c5a4b27 150 uint16_t LSM9DS1::begin()
sreddy47 5:802a7c5a4b27 151 {
sreddy47 5:802a7c5a4b27 152 //! Todo: don't use _xgAddress or _mAddress, duplicating memory
sreddy47 5:802a7c5a4b27 153 _xgAddress = settings.device.agAddress;
sreddy47 5:802a7c5a4b27 154 _mAddress = settings.device.mAddress;
sreddy47 5:802a7c5a4b27 155
sreddy47 5:802a7c5a4b27 156 constrainScales();
sreddy47 5:802a7c5a4b27 157 // Once we have the scale values, we can calculate the resolution
sreddy47 5:802a7c5a4b27 158 // of each sensor. That's what these functions are for. One for each sensor
sreddy47 5:802a7c5a4b27 159 calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable
sreddy47 5:802a7c5a4b27 160 calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable
sreddy47 5:802a7c5a4b27 161 calcaRes(); // Calculate g / ADC tick, stored in aRes variable
sreddy47 5:802a7c5a4b27 162
sreddy47 5:802a7c5a4b27 163 // Now, initialize our hardware interface.
sreddy47 5:802a7c5a4b27 164 if (settings.device.commInterface == IMU_MODE_I2C) // If we're using I2C
sreddy47 5:802a7c5a4b27 165 initI2C(); // Initialize I2C
sreddy47 5:802a7c5a4b27 166 else if (settings.device.commInterface == IMU_MODE_SPI) // else, if we're using SPI
sreddy47 5:802a7c5a4b27 167 initSPI(); // Initialize SPI
sreddy47 5:802a7c5a4b27 168
sreddy47 5:802a7c5a4b27 169 // To verify communication, we can read from the WHO_AM_I register of
sreddy47 5:802a7c5a4b27 170 // each device. Store those in a variable so we can return them.
sreddy47 5:802a7c5a4b27 171 uint8_t mTest = mReadByte(WHO_AM_I_M); // Read the gyro WHO_AM_I
sreddy47 5:802a7c5a4b27 172 uint8_t xgTest = xgReadByte(WHO_AM_I_XG); // Read the accel/mag WHO_AM_I
sreddy47 5:802a7c5a4b27 173 pc.printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress);
sreddy47 5:802a7c5a4b27 174 uint16_t whoAmICombined = (xgTest << 8) | mTest;
sreddy47 5:802a7c5a4b27 175
sreddy47 5:802a7c5a4b27 176 if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP))
sreddy47 5:802a7c5a4b27 177 return 0;
sreddy47 5:802a7c5a4b27 178
sreddy47 5:802a7c5a4b27 179 // Gyro initialization stuff:
sreddy47 5:802a7c5a4b27 180 initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc.
sreddy47 5:802a7c5a4b27 181
sreddy47 5:802a7c5a4b27 182 // Accelerometer initialization stuff:
sreddy47 5:802a7c5a4b27 183 initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc.
sreddy47 5:802a7c5a4b27 184
sreddy47 5:802a7c5a4b27 185 // Magnetometer initialization stuff:
sreddy47 5:802a7c5a4b27 186 initMag(); // "Turn on" all axes of the mag. Set up interrupts, etc.
sreddy47 5:802a7c5a4b27 187
sreddy47 5:802a7c5a4b27 188 // Once everything is initialized, return the WHO_AM_I registers we read:
sreddy47 5:802a7c5a4b27 189 return whoAmICombined;
sreddy47 5:802a7c5a4b27 190 }
sreddy47 5:802a7c5a4b27 191
sreddy47 5:802a7c5a4b27 192 void LSM9DS1::initGyro()
sreddy47 5:802a7c5a4b27 193 {
sreddy47 5:802a7c5a4b27 194 uint8_t tempRegValue = 0;
sreddy47 5:802a7c5a4b27 195
sreddy47 5:802a7c5a4b27 196 // CTRL_REG1_G (Default value: 0x00)
sreddy47 5:802a7c5a4b27 197 // [ODR_G2][ODR_G1][ODR_G0][FS_G1][FS_G0][0][BW_G1][BW_G0]
sreddy47 5:802a7c5a4b27 198 // ODR_G[2:0] - Output data rate selection
sreddy47 5:802a7c5a4b27 199 // FS_G[1:0] - Gyroscope full-scale selection
sreddy47 5:802a7c5a4b27 200 // BW_G[1:0] - Gyroscope bandwidth selection
sreddy47 5:802a7c5a4b27 201
sreddy47 5:802a7c5a4b27 202 // To disable gyro, set sample rate bits to 0. We'll only set sample
sreddy47 5:802a7c5a4b27 203 // rate if the gyro is enabled.
sreddy47 5:802a7c5a4b27 204 if (settings.gyro.enabled)
sreddy47 5:802a7c5a4b27 205 {
sreddy47 5:802a7c5a4b27 206 tempRegValue = (settings.gyro.sampleRate & 0x07) << 5;
sreddy47 5:802a7c5a4b27 207 }
sreddy47 5:802a7c5a4b27 208 switch (settings.gyro.scale)
sreddy47 5:802a7c5a4b27 209 {
sreddy47 5:802a7c5a4b27 210 case 500:
sreddy47 5:802a7c5a4b27 211 tempRegValue |= (0x1 << 3);
sreddy47 5:802a7c5a4b27 212 break;
sreddy47 5:802a7c5a4b27 213 case 2000:
sreddy47 5:802a7c5a4b27 214 tempRegValue |= (0x3 << 3);
sreddy47 5:802a7c5a4b27 215 break;
sreddy47 5:802a7c5a4b27 216 // Otherwise we'll set it to 245 dps (0x0 << 4)
sreddy47 5:802a7c5a4b27 217 }
sreddy47 5:802a7c5a4b27 218 tempRegValue |= (settings.gyro.bandwidth & 0x3);
sreddy47 5:802a7c5a4b27 219 xgWriteByte(CTRL_REG1_G, tempRegValue);
sreddy47 5:802a7c5a4b27 220
sreddy47 5:802a7c5a4b27 221 // CTRL_REG2_G (Default value: 0x00)
sreddy47 5:802a7c5a4b27 222 // [0][0][0][0][INT_SEL1][INT_SEL0][OUT_SEL1][OUT_SEL0]
sreddy47 5:802a7c5a4b27 223 // INT_SEL[1:0] - INT selection configuration
sreddy47 5:802a7c5a4b27 224 // OUT_SEL[1:0] - Out selection configuration
sreddy47 5:802a7c5a4b27 225 xgWriteByte(CTRL_REG2_G, 0x00);
sreddy47 5:802a7c5a4b27 226
sreddy47 5:802a7c5a4b27 227 // CTRL_REG3_G (Default value: 0x00)
sreddy47 5:802a7c5a4b27 228 // [LP_mode][HP_EN][0][0][HPCF3_G][HPCF2_G][HPCF1_G][HPCF0_G]
sreddy47 5:802a7c5a4b27 229 // LP_mode - Low-power mode enable (0: disabled, 1: enabled)
sreddy47 5:802a7c5a4b27 230 // HP_EN - HPF enable (0:disabled, 1: enabled)
sreddy47 5:802a7c5a4b27 231 // HPCF_G[3:0] - HPF cutoff frequency
sreddy47 5:802a7c5a4b27 232 tempRegValue = settings.gyro.lowPowerEnable ? (1<<7) : 0;
sreddy47 5:802a7c5a4b27 233 if (settings.gyro.HPFEnable)
sreddy47 5:802a7c5a4b27 234 {
sreddy47 5:802a7c5a4b27 235 tempRegValue |= (1<<6) | (settings.gyro.HPFCutoff & 0x0F);
sreddy47 5:802a7c5a4b27 236 }
sreddy47 5:802a7c5a4b27 237 xgWriteByte(CTRL_REG3_G, tempRegValue);
sreddy47 5:802a7c5a4b27 238
sreddy47 5:802a7c5a4b27 239 // CTRL_REG4 (Default value: 0x38)
sreddy47 5:802a7c5a4b27 240 // [0][0][Zen_G][Yen_G][Xen_G][0][LIR_XL1][4D_XL1]
sreddy47 5:802a7c5a4b27 241 // Zen_G - Z-axis output enable (0:disable, 1:enable)
sreddy47 5:802a7c5a4b27 242 // Yen_G - Y-axis output enable (0:disable, 1:enable)
sreddy47 5:802a7c5a4b27 243 // Xen_G - X-axis output enable (0:disable, 1:enable)
sreddy47 5:802a7c5a4b27 244 // LIR_XL1 - Latched interrupt (0:not latched, 1:latched)
sreddy47 5:802a7c5a4b27 245 // 4D_XL1 - 4D option on interrupt (0:6D used, 1:4D used)
sreddy47 5:802a7c5a4b27 246 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 247 if (settings.gyro.enableZ) tempRegValue |= (1<<5);
sreddy47 5:802a7c5a4b27 248 if (settings.gyro.enableY) tempRegValue |= (1<<4);
sreddy47 5:802a7c5a4b27 249 if (settings.gyro.enableX) tempRegValue |= (1<<3);
sreddy47 5:802a7c5a4b27 250 if (settings.gyro.latchInterrupt) tempRegValue |= (1<<1);
sreddy47 5:802a7c5a4b27 251 xgWriteByte(CTRL_REG4, tempRegValue);
sreddy47 5:802a7c5a4b27 252
sreddy47 5:802a7c5a4b27 253 // ORIENT_CFG_G (Default value: 0x00)
sreddy47 5:802a7c5a4b27 254 // [0][0][SignX_G][SignY_G][SignZ_G][Orient_2][Orient_1][Orient_0]
sreddy47 5:802a7c5a4b27 255 // SignX_G - Pitch axis (X) angular rate sign (0: positive, 1: negative)
sreddy47 5:802a7c5a4b27 256 // Orient [2:0] - Directional user orientation selection
sreddy47 5:802a7c5a4b27 257 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 258 if (settings.gyro.flipX) tempRegValue |= (1<<5);
sreddy47 5:802a7c5a4b27 259 if (settings.gyro.flipY) tempRegValue |= (1<<4);
sreddy47 5:802a7c5a4b27 260 if (settings.gyro.flipZ) tempRegValue |= (1<<3);
sreddy47 5:802a7c5a4b27 261 xgWriteByte(ORIENT_CFG_G, tempRegValue);
sreddy47 5:802a7c5a4b27 262 }
sreddy47 5:802a7c5a4b27 263
sreddy47 5:802a7c5a4b27 264 void LSM9DS1::initAccel()
sreddy47 5:802a7c5a4b27 265 {
sreddy47 5:802a7c5a4b27 266 uint8_t tempRegValue = 0;
sreddy47 5:802a7c5a4b27 267
sreddy47 5:802a7c5a4b27 268 // CTRL_REG5_XL (0x1F) (Default value: 0x38)
sreddy47 5:802a7c5a4b27 269 // [DEC_1][DEC_0][Zen_XL][Yen_XL][Zen_XL][0][0][0]
sreddy47 5:802a7c5a4b27 270 // DEC[0:1] - Decimation of accel data on OUT REG and FIFO.
sreddy47 5:802a7c5a4b27 271 // 00: None, 01: 2 samples, 10: 4 samples 11: 8 samples
sreddy47 5:802a7c5a4b27 272 // Zen_XL - Z-axis output enabled
sreddy47 5:802a7c5a4b27 273 // Yen_XL - Y-axis output enabled
sreddy47 5:802a7c5a4b27 274 // Xen_XL - X-axis output enabled
sreddy47 5:802a7c5a4b27 275 if (settings.accel.enableZ) tempRegValue |= (1<<5);
sreddy47 5:802a7c5a4b27 276 if (settings.accel.enableY) tempRegValue |= (1<<4);
sreddy47 5:802a7c5a4b27 277 if (settings.accel.enableX) tempRegValue |= (1<<3);
sreddy47 5:802a7c5a4b27 278
sreddy47 5:802a7c5a4b27 279 xgWriteByte(CTRL_REG5_XL, tempRegValue);
sreddy47 5:802a7c5a4b27 280
sreddy47 5:802a7c5a4b27 281 // CTRL_REG6_XL (0x20) (Default value: 0x00)
sreddy47 5:802a7c5a4b27 282 // [ODR_XL2][ODR_XL1][ODR_XL0][FS1_XL][FS0_XL][BW_SCAL_ODR][BW_XL1][BW_XL0]
sreddy47 5:802a7c5a4b27 283 // ODR_XL[2:0] - Output data rate & power mode selection
sreddy47 5:802a7c5a4b27 284 // FS_XL[1:0] - Full-scale selection
sreddy47 5:802a7c5a4b27 285 // BW_SCAL_ODR - Bandwidth selection
sreddy47 5:802a7c5a4b27 286 // BW_XL[1:0] - Anti-aliasing filter bandwidth selection
sreddy47 5:802a7c5a4b27 287 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 288 // To disable the accel, set the sampleRate bits to 0.
sreddy47 5:802a7c5a4b27 289 if (settings.accel.enabled)
sreddy47 5:802a7c5a4b27 290 {
sreddy47 5:802a7c5a4b27 291 tempRegValue |= (settings.accel.sampleRate & 0x07) << 5;
sreddy47 5:802a7c5a4b27 292 }
sreddy47 5:802a7c5a4b27 293 switch (settings.accel.scale)
sreddy47 5:802a7c5a4b27 294 {
sreddy47 5:802a7c5a4b27 295 case 4:
sreddy47 5:802a7c5a4b27 296 tempRegValue |= (0x2 << 3);
sreddy47 5:802a7c5a4b27 297 break;
sreddy47 5:802a7c5a4b27 298 case 8:
sreddy47 5:802a7c5a4b27 299 tempRegValue |= (0x3 << 3);
sreddy47 5:802a7c5a4b27 300 break;
sreddy47 5:802a7c5a4b27 301 case 16:
sreddy47 5:802a7c5a4b27 302 tempRegValue |= (0x1 << 3);
sreddy47 5:802a7c5a4b27 303 break;
sreddy47 5:802a7c5a4b27 304 // Otherwise it'll be set to 2g (0x0 << 3)
sreddy47 5:802a7c5a4b27 305 }
sreddy47 5:802a7c5a4b27 306 if (settings.accel.bandwidth >= 0)
sreddy47 5:802a7c5a4b27 307 {
sreddy47 5:802a7c5a4b27 308 tempRegValue |= (1<<2); // Set BW_SCAL_ODR
sreddy47 5:802a7c5a4b27 309 tempRegValue |= (settings.accel.bandwidth & 0x03);
sreddy47 5:802a7c5a4b27 310 }
sreddy47 5:802a7c5a4b27 311 xgWriteByte(CTRL_REG6_XL, tempRegValue);
sreddy47 5:802a7c5a4b27 312
sreddy47 5:802a7c5a4b27 313 // CTRL_REG7_XL (0x21) (Default value: 0x00)
sreddy47 5:802a7c5a4b27 314 // [HR][DCF1][DCF0][0][0][FDS][0][HPIS1]
sreddy47 5:802a7c5a4b27 315 // HR - High resolution mode (0: disable, 1: enable)
sreddy47 5:802a7c5a4b27 316 // DCF[1:0] - Digital filter cutoff frequency
sreddy47 5:802a7c5a4b27 317 // FDS - Filtered data selection
sreddy47 5:802a7c5a4b27 318 // HPIS1 - HPF enabled for interrupt function
sreddy47 5:802a7c5a4b27 319 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 320 if (settings.accel.highResEnable)
sreddy47 5:802a7c5a4b27 321 {
sreddy47 5:802a7c5a4b27 322 tempRegValue |= (1<<7); // Set HR bit
sreddy47 5:802a7c5a4b27 323 tempRegValue |= (settings.accel.highResBandwidth & 0x3) << 5;
sreddy47 5:802a7c5a4b27 324 }
sreddy47 5:802a7c5a4b27 325 xgWriteByte(CTRL_REG7_XL, tempRegValue);
sreddy47 5:802a7c5a4b27 326 }
sreddy47 5:802a7c5a4b27 327
sreddy47 5:802a7c5a4b27 328 // This is a function that uses the FIFO to accumulate sample of accelerometer and gyro data, average
sreddy47 5:802a7c5a4b27 329 // them, scales them to gs and deg/s, respectively, and then passes the biases to the main sketch
sreddy47 5:802a7c5a4b27 330 // for subtraction from all subsequent data. There are no gyro and accelerometer bias registers to store
sreddy47 5:802a7c5a4b27 331 // the data as there are in the ADXL345, a precursor to the LSM9DS0, or the MPU-9150, so we have to
sreddy47 5:802a7c5a4b27 332 // subtract the biases ourselves. This results in a more accurate measurement in general and can
sreddy47 5:802a7c5a4b27 333 // remove errors due to imprecise or varying initial placement. Calibration of sensor data in this manner
sreddy47 5:802a7c5a4b27 334 // is good practice.
sreddy47 5:802a7c5a4b27 335 void LSM9DS1::calibrate(bool autoCalc)
sreddy47 5:802a7c5a4b27 336 {
sreddy47 5:802a7c5a4b27 337 uint8_t data[6] = {0, 0, 0, 0, 0, 0};
sreddy47 5:802a7c5a4b27 338 uint8_t samples = 0;
sreddy47 5:802a7c5a4b27 339 int ii;
sreddy47 5:802a7c5a4b27 340 int32_t aBiasRawTemp[3] = {0, 0, 0};
sreddy47 5:802a7c5a4b27 341 int32_t gBiasRawTemp[3] = {0, 0, 0};
sreddy47 5:802a7c5a4b27 342
sreddy47 5:802a7c5a4b27 343 // Turn on FIFO and set threshold to 32 samples
sreddy47 5:802a7c5a4b27 344 enableFIFO(true);
sreddy47 5:802a7c5a4b27 345 setFIFO(FIFO_THS, 0x1F);
sreddy47 5:802a7c5a4b27 346 while (samples < 0x1F)
sreddy47 5:802a7c5a4b27 347 {
sreddy47 5:802a7c5a4b27 348 samples = (xgReadByte(FIFO_SRC) & 0x3F); // Read number of stored samples
sreddy47 5:802a7c5a4b27 349 }
sreddy47 5:802a7c5a4b27 350 for(ii = 0; ii < samples ; ii++)
sreddy47 5:802a7c5a4b27 351 { // Read the gyro data stored in the FIFO
sreddy47 5:802a7c5a4b27 352 readGyro();
sreddy47 5:802a7c5a4b27 353 gBiasRawTemp[0] += gx;
sreddy47 5:802a7c5a4b27 354 gBiasRawTemp[1] += gy;
sreddy47 5:802a7c5a4b27 355 gBiasRawTemp[2] += gz;
sreddy47 5:802a7c5a4b27 356 readAccel();
sreddy47 5:802a7c5a4b27 357 aBiasRawTemp[0] += ax;
sreddy47 5:802a7c5a4b27 358 aBiasRawTemp[1] += ay;
sreddy47 5:802a7c5a4b27 359 aBiasRawTemp[2] += az - (int16_t)(1./aRes); // Assumes sensor facing up!
sreddy47 5:802a7c5a4b27 360 }
sreddy47 5:802a7c5a4b27 361 for (ii = 0; ii < 3; ii++)
sreddy47 5:802a7c5a4b27 362 {
sreddy47 5:802a7c5a4b27 363 gBiasRaw[ii] = gBiasRawTemp[ii] / samples;
sreddy47 5:802a7c5a4b27 364 gBias[ii] = calcGyro(gBiasRaw[ii]);
sreddy47 5:802a7c5a4b27 365 aBiasRaw[ii] = aBiasRawTemp[ii] / samples;
sreddy47 5:802a7c5a4b27 366 aBias[ii] = calcAccel(aBiasRaw[ii]);
sreddy47 5:802a7c5a4b27 367 }
sreddy47 5:802a7c5a4b27 368
sreddy47 5:802a7c5a4b27 369 enableFIFO(false);
sreddy47 5:802a7c5a4b27 370 setFIFO(FIFO_OFF, 0x00);
sreddy47 5:802a7c5a4b27 371
sreddy47 5:802a7c5a4b27 372 if (autoCalc) _autoCalc = true;
sreddy47 5:802a7c5a4b27 373 }
sreddy47 5:802a7c5a4b27 374
sreddy47 5:802a7c5a4b27 375 void LSM9DS1::calibrateMag(bool loadIn)
sreddy47 5:802a7c5a4b27 376 {
sreddy47 5:802a7c5a4b27 377 int i, j;
sreddy47 5:802a7c5a4b27 378 int16_t magMin[3] = {0, 0, 0};
sreddy47 5:802a7c5a4b27 379 int16_t magMax[3] = {0, 0, 0}; // The road warrior
sreddy47 5:802a7c5a4b27 380
sreddy47 5:802a7c5a4b27 381 for (i=0; i<128; i++)
sreddy47 5:802a7c5a4b27 382 {
sreddy47 5:802a7c5a4b27 383 while (!magAvailable())
sreddy47 5:802a7c5a4b27 384 ;
sreddy47 5:802a7c5a4b27 385 readMag();
sreddy47 5:802a7c5a4b27 386 int16_t magTemp[3] = {0, 0, 0};
sreddy47 5:802a7c5a4b27 387 magTemp[0] = mx;
sreddy47 5:802a7c5a4b27 388 magTemp[1] = my;
sreddy47 5:802a7c5a4b27 389 magTemp[2] = mz;
sreddy47 5:802a7c5a4b27 390 for (j = 0; j < 3; j++)
sreddy47 5:802a7c5a4b27 391 {
sreddy47 5:802a7c5a4b27 392 if (magTemp[j] > magMax[j]) magMax[j] = magTemp[j];
sreddy47 5:802a7c5a4b27 393 if (magTemp[j] < magMin[j]) magMin[j] = magTemp[j];
sreddy47 5:802a7c5a4b27 394 }
sreddy47 5:802a7c5a4b27 395 }
sreddy47 5:802a7c5a4b27 396 for (j = 0; j < 3; j++)
sreddy47 5:802a7c5a4b27 397 {
sreddy47 5:802a7c5a4b27 398 mBiasRaw[j] = (magMax[j] + magMin[j]) / 2;
sreddy47 5:802a7c5a4b27 399 mBias[j] = calcMag(mBiasRaw[j]);
sreddy47 5:802a7c5a4b27 400 if (loadIn)
sreddy47 5:802a7c5a4b27 401 magOffset(j, mBiasRaw[j]);
sreddy47 5:802a7c5a4b27 402 }
sreddy47 5:802a7c5a4b27 403
sreddy47 5:802a7c5a4b27 404 }
sreddy47 5:802a7c5a4b27 405 void LSM9DS1::magOffset(uint8_t axis, int16_t offset)
sreddy47 5:802a7c5a4b27 406 {
sreddy47 5:802a7c5a4b27 407 if (axis > 2)
sreddy47 5:802a7c5a4b27 408 return;
sreddy47 5:802a7c5a4b27 409 uint8_t msb, lsb;
sreddy47 5:802a7c5a4b27 410 msb = (offset & 0xFF00) >> 8;
sreddy47 5:802a7c5a4b27 411 lsb = offset & 0x00FF;
sreddy47 5:802a7c5a4b27 412 mWriteByte(OFFSET_X_REG_L_M + (2 * axis), lsb);
sreddy47 5:802a7c5a4b27 413 mWriteByte(OFFSET_X_REG_H_M + (2 * axis), msb);
sreddy47 5:802a7c5a4b27 414 }
sreddy47 5:802a7c5a4b27 415
sreddy47 5:802a7c5a4b27 416 void LSM9DS1::initMag()
sreddy47 5:802a7c5a4b27 417 {
sreddy47 5:802a7c5a4b27 418 uint8_t tempRegValue = 0;
sreddy47 5:802a7c5a4b27 419
sreddy47 5:802a7c5a4b27 420 // CTRL_REG1_M (Default value: 0x10)
sreddy47 5:802a7c5a4b27 421 // [TEMP_COMP][OM1][OM0][DO2][DO1][DO0][0][ST]
sreddy47 5:802a7c5a4b27 422 // TEMP_COMP - Temperature compensation
sreddy47 5:802a7c5a4b27 423 // OM[1:0] - X & Y axes op mode selection
sreddy47 5:802a7c5a4b27 424 // 00:low-power, 01:medium performance
sreddy47 5:802a7c5a4b27 425 // 10: high performance, 11:ultra-high performance
sreddy47 5:802a7c5a4b27 426 // DO[2:0] - Output data rate selection
sreddy47 5:802a7c5a4b27 427 // ST - Self-test enable
sreddy47 5:802a7c5a4b27 428 if (settings.mag.tempCompensationEnable) tempRegValue |= (1<<7);
sreddy47 5:802a7c5a4b27 429 tempRegValue |= (settings.mag.XYPerformance & 0x3) << 5;
sreddy47 5:802a7c5a4b27 430 tempRegValue |= (settings.mag.sampleRate & 0x7) << 2;
sreddy47 5:802a7c5a4b27 431 mWriteByte(CTRL_REG1_M, tempRegValue);
sreddy47 5:802a7c5a4b27 432
sreddy47 5:802a7c5a4b27 433 // CTRL_REG2_M (Default value 0x00)
sreddy47 5:802a7c5a4b27 434 // [0][FS1][FS0][0][REBOOT][SOFT_RST][0][0]
sreddy47 5:802a7c5a4b27 435 // FS[1:0] - Full-scale configuration
sreddy47 5:802a7c5a4b27 436 // REBOOT - Reboot memory content (0:normal, 1:reboot)
sreddy47 5:802a7c5a4b27 437 // SOFT_RST - Reset config and user registers (0:default, 1:reset)
sreddy47 5:802a7c5a4b27 438 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 439 switch (settings.mag.scale)
sreddy47 5:802a7c5a4b27 440 {
sreddy47 5:802a7c5a4b27 441 case 8:
sreddy47 5:802a7c5a4b27 442 tempRegValue |= (0x1 << 5);
sreddy47 5:802a7c5a4b27 443 break;
sreddy47 5:802a7c5a4b27 444 case 12:
sreddy47 5:802a7c5a4b27 445 tempRegValue |= (0x2 << 5);
sreddy47 5:802a7c5a4b27 446 break;
sreddy47 5:802a7c5a4b27 447 case 16:
sreddy47 5:802a7c5a4b27 448 tempRegValue |= (0x3 << 5);
sreddy47 5:802a7c5a4b27 449 break;
sreddy47 5:802a7c5a4b27 450 // Otherwise we'll default to 4 gauss (00)
sreddy47 5:802a7c5a4b27 451 }
sreddy47 5:802a7c5a4b27 452 mWriteByte(CTRL_REG2_M, tempRegValue); // +/-4Gauss
sreddy47 5:802a7c5a4b27 453
sreddy47 5:802a7c5a4b27 454 // CTRL_REG3_M (Default value: 0x03)
sreddy47 5:802a7c5a4b27 455 // [I2C_DISABLE][0][LP][0][0][SIM][MD1][MD0]
sreddy47 5:802a7c5a4b27 456 // I2C_DISABLE - Disable I2C interace (0:enable, 1:disable)
sreddy47 5:802a7c5a4b27 457 // LP - Low-power mode cofiguration (1:enable)
sreddy47 5:802a7c5a4b27 458 // SIM - SPI mode selection (0:write-only, 1:read/write enable)
sreddy47 5:802a7c5a4b27 459 // MD[1:0] - Operating mode
sreddy47 5:802a7c5a4b27 460 // 00:continuous conversion, 01:single-conversion,
sreddy47 5:802a7c5a4b27 461 // 10,11: Power-down
sreddy47 5:802a7c5a4b27 462 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 463 if (settings.mag.lowPowerEnable) tempRegValue |= (1<<5);
sreddy47 5:802a7c5a4b27 464 tempRegValue |= (settings.mag.operatingMode & 0x3);
sreddy47 5:802a7c5a4b27 465 mWriteByte(CTRL_REG3_M, tempRegValue); // Continuous conversion mode
sreddy47 5:802a7c5a4b27 466
sreddy47 5:802a7c5a4b27 467 // CTRL_REG4_M (Default value: 0x00)
sreddy47 5:802a7c5a4b27 468 // [0][0][0][0][OMZ1][OMZ0][BLE][0]
sreddy47 5:802a7c5a4b27 469 // OMZ[1:0] - Z-axis operative mode selection
sreddy47 5:802a7c5a4b27 470 // 00:low-power mode, 01:medium performance
sreddy47 5:802a7c5a4b27 471 // 10:high performance, 10:ultra-high performance
sreddy47 5:802a7c5a4b27 472 // BLE - Big/little endian data
sreddy47 5:802a7c5a4b27 473 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 474 tempRegValue = (settings.mag.ZPerformance & 0x3) << 2;
sreddy47 5:802a7c5a4b27 475 mWriteByte(CTRL_REG4_M, tempRegValue);
sreddy47 5:802a7c5a4b27 476
sreddy47 5:802a7c5a4b27 477 // CTRL_REG5_M (Default value: 0x00)
sreddy47 5:802a7c5a4b27 478 // [0][BDU][0][0][0][0][0][0]
sreddy47 5:802a7c5a4b27 479 // BDU - Block data update for magnetic data
sreddy47 5:802a7c5a4b27 480 // 0:continuous, 1:not updated until MSB/LSB are read
sreddy47 5:802a7c5a4b27 481 tempRegValue = 0;
sreddy47 5:802a7c5a4b27 482 mWriteByte(CTRL_REG5_M, tempRegValue);
sreddy47 5:802a7c5a4b27 483 }
sreddy47 5:802a7c5a4b27 484
sreddy47 5:802a7c5a4b27 485 uint8_t LSM9DS1::accelAvailable()
sreddy47 5:802a7c5a4b27 486 {
sreddy47 5:802a7c5a4b27 487 uint8_t status = xgReadByte(STATUS_REG_1);
sreddy47 5:802a7c5a4b27 488
sreddy47 5:802a7c5a4b27 489 return (status & (1<<0));
sreddy47 5:802a7c5a4b27 490 }
sreddy47 5:802a7c5a4b27 491
sreddy47 5:802a7c5a4b27 492 uint8_t LSM9DS1::gyroAvailable()
sreddy47 5:802a7c5a4b27 493 {
sreddy47 5:802a7c5a4b27 494 uint8_t status = xgReadByte(STATUS_REG_1);
sreddy47 5:802a7c5a4b27 495
sreddy47 5:802a7c5a4b27 496 return ((status & (1<<1)) >> 1);
sreddy47 5:802a7c5a4b27 497 }
sreddy47 5:802a7c5a4b27 498
sreddy47 5:802a7c5a4b27 499 uint8_t LSM9DS1::tempAvailable()
sreddy47 5:802a7c5a4b27 500 {
sreddy47 5:802a7c5a4b27 501 uint8_t status = xgReadByte(STATUS_REG_1);
sreddy47 5:802a7c5a4b27 502
sreddy47 5:802a7c5a4b27 503 return ((status & (1<<2)) >> 2);
sreddy47 5:802a7c5a4b27 504 }
sreddy47 5:802a7c5a4b27 505
sreddy47 5:802a7c5a4b27 506 uint8_t LSM9DS1::magAvailable(lsm9ds1_axis axis)
sreddy47 5:802a7c5a4b27 507 {
sreddy47 5:802a7c5a4b27 508 uint8_t status;
sreddy47 5:802a7c5a4b27 509 status = mReadByte(STATUS_REG_M);
sreddy47 5:802a7c5a4b27 510
sreddy47 5:802a7c5a4b27 511 return ((status & (1<<axis)) >> axis);
sreddy47 5:802a7c5a4b27 512 }
sreddy47 5:802a7c5a4b27 513
sreddy47 5:802a7c5a4b27 514 void LSM9DS1::readAccel()
sreddy47 5:802a7c5a4b27 515 {
sreddy47 5:802a7c5a4b27 516 uint8_t temp[6]; // We'll read six bytes from the accelerometer into temp
sreddy47 5:802a7c5a4b27 517 xgReadBytes(OUT_X_L_XL, temp, 6); // Read 6 bytes, beginning at OUT_X_L_XL
sreddy47 5:802a7c5a4b27 518 ax = (temp[1] << 8) | temp[0]; // Store x-axis values into ax
sreddy47 5:802a7c5a4b27 519 ay = (temp[3] << 8) | temp[2]; // Store y-axis values into ay
sreddy47 5:802a7c5a4b27 520 az = (temp[5] << 8) | temp[4]; // Store z-axis values into az
sreddy47 5:802a7c5a4b27 521 if (_autoCalc)
sreddy47 5:802a7c5a4b27 522 {
sreddy47 5:802a7c5a4b27 523 ax -= aBiasRaw[X_AXIS];
sreddy47 5:802a7c5a4b27 524 ay -= aBiasRaw[Y_AXIS];
sreddy47 5:802a7c5a4b27 525 az -= aBiasRaw[Z_AXIS];
sreddy47 5:802a7c5a4b27 526 }
sreddy47 5:802a7c5a4b27 527 }
sreddy47 5:802a7c5a4b27 528
sreddy47 5:802a7c5a4b27 529 int16_t LSM9DS1::readAccel(lsm9ds1_axis axis)
sreddy47 5:802a7c5a4b27 530 {
sreddy47 5:802a7c5a4b27 531 uint8_t temp[2];
sreddy47 5:802a7c5a4b27 532 int16_t value;
sreddy47 5:802a7c5a4b27 533 xgReadBytes(OUT_X_L_XL + (2 * axis), temp, 2);
sreddy47 5:802a7c5a4b27 534 value = (temp[1] << 8) | temp[0];
sreddy47 5:802a7c5a4b27 535
sreddy47 5:802a7c5a4b27 536 if (_autoCalc)
sreddy47 5:802a7c5a4b27 537 value -= aBiasRaw[axis];
sreddy47 5:802a7c5a4b27 538
sreddy47 5:802a7c5a4b27 539 return value;
sreddy47 5:802a7c5a4b27 540 }
sreddy47 5:802a7c5a4b27 541
sreddy47 5:802a7c5a4b27 542 void LSM9DS1::readMag()
sreddy47 5:802a7c5a4b27 543 {
sreddy47 5:802a7c5a4b27 544 uint8_t temp[6]; // We'll read six bytes from the mag into temp
sreddy47 5:802a7c5a4b27 545 mReadBytes(OUT_X_L_M, temp, 6); // Read 6 bytes, beginning at OUT_X_L_M
sreddy47 5:802a7c5a4b27 546 mx = (temp[1] << 8) | temp[0]; // Store x-axis values into mx
sreddy47 5:802a7c5a4b27 547 my = (temp[3] << 8) | temp[2]; // Store y-axis values into my
sreddy47 5:802a7c5a4b27 548 mz = (temp[5] << 8) | temp[4]; // Store z-axis values into mz
sreddy47 5:802a7c5a4b27 549 }
sreddy47 5:802a7c5a4b27 550
sreddy47 5:802a7c5a4b27 551 int16_t LSM9DS1::readMag(lsm9ds1_axis axis)
sreddy47 5:802a7c5a4b27 552 {
sreddy47 5:802a7c5a4b27 553 uint8_t temp[2];
sreddy47 5:802a7c5a4b27 554 mReadBytes(OUT_X_L_M + (2 * axis), temp, 2);
sreddy47 5:802a7c5a4b27 555 return (temp[1] << 8) | temp[0];
sreddy47 5:802a7c5a4b27 556 }
sreddy47 5:802a7c5a4b27 557
sreddy47 5:802a7c5a4b27 558 void LSM9DS1::readTemp()
sreddy47 5:802a7c5a4b27 559 {
sreddy47 5:802a7c5a4b27 560 uint8_t temp[2]; // We'll read two bytes from the temperature sensor into temp
sreddy47 5:802a7c5a4b27 561 xgReadBytes(OUT_TEMP_L, temp, 2); // Read 2 bytes, beginning at OUT_TEMP_L
sreddy47 5:802a7c5a4b27 562 temperature = ((int16_t)temp[1] << 8) | temp[0];
sreddy47 5:802a7c5a4b27 563 }
sreddy47 5:802a7c5a4b27 564
sreddy47 5:802a7c5a4b27 565 void LSM9DS1::readGyro()
sreddy47 5:802a7c5a4b27 566 {
sreddy47 5:802a7c5a4b27 567 uint8_t temp[6]; // We'll read six bytes from the gyro into temp
sreddy47 5:802a7c5a4b27 568 xgReadBytes(OUT_X_L_G, temp, 6); // Read 6 bytes, beginning at OUT_X_L_G
sreddy47 5:802a7c5a4b27 569 gx = (temp[1] << 8) | temp[0]; // Store x-axis values into gx
sreddy47 5:802a7c5a4b27 570 gy = (temp[3] << 8) | temp[2]; // Store y-axis values into gy
sreddy47 5:802a7c5a4b27 571 gz = (temp[5] << 8) | temp[4]; // Store z-axis values into gz
sreddy47 5:802a7c5a4b27 572 if (_autoCalc)
sreddy47 5:802a7c5a4b27 573 {
sreddy47 5:802a7c5a4b27 574 gx -= gBiasRaw[X_AXIS];
sreddy47 5:802a7c5a4b27 575 gy -= gBiasRaw[Y_AXIS];
sreddy47 5:802a7c5a4b27 576 gz -= gBiasRaw[Z_AXIS];
sreddy47 5:802a7c5a4b27 577 }
sreddy47 5:802a7c5a4b27 578 }
sreddy47 5:802a7c5a4b27 579
sreddy47 5:802a7c5a4b27 580 int16_t LSM9DS1::readGyro(lsm9ds1_axis axis)
sreddy47 5:802a7c5a4b27 581 {
sreddy47 5:802a7c5a4b27 582 uint8_t temp[2];
sreddy47 5:802a7c5a4b27 583 int16_t value;
sreddy47 5:802a7c5a4b27 584
sreddy47 5:802a7c5a4b27 585 xgReadBytes(OUT_X_L_G + (2 * axis), temp, 2);
sreddy47 5:802a7c5a4b27 586
sreddy47 5:802a7c5a4b27 587 value = (temp[1] << 8) | temp[0];
sreddy47 5:802a7c5a4b27 588
sreddy47 5:802a7c5a4b27 589 if (_autoCalc)
sreddy47 5:802a7c5a4b27 590 value -= gBiasRaw[axis];
sreddy47 5:802a7c5a4b27 591
sreddy47 5:802a7c5a4b27 592 return value;
sreddy47 5:802a7c5a4b27 593 }
sreddy47 5:802a7c5a4b27 594
sreddy47 5:802a7c5a4b27 595 float LSM9DS1::calcGyro(int16_t gyro)
sreddy47 5:802a7c5a4b27 596 {
sreddy47 5:802a7c5a4b27 597 // Return the gyro raw reading times our pre-calculated DPS / (ADC tick):
sreddy47 5:802a7c5a4b27 598 return gRes * gyro;
sreddy47 5:802a7c5a4b27 599 }
sreddy47 5:802a7c5a4b27 600
sreddy47 5:802a7c5a4b27 601 float LSM9DS1::calcAccel(int16_t accel)
sreddy47 5:802a7c5a4b27 602 {
sreddy47 5:802a7c5a4b27 603 // Return the accel raw reading times our pre-calculated g's / (ADC tick):
sreddy47 5:802a7c5a4b27 604 return aRes * accel;
sreddy47 5:802a7c5a4b27 605 }
sreddy47 5:802a7c5a4b27 606
sreddy47 5:802a7c5a4b27 607 float LSM9DS1::calcMag(int16_t mag)
sreddy47 5:802a7c5a4b27 608 {
sreddy47 5:802a7c5a4b27 609 // Return the mag raw reading times our pre-calculated Gs / (ADC tick):
sreddy47 5:802a7c5a4b27 610 return mRes * mag;
sreddy47 5:802a7c5a4b27 611 }
sreddy47 5:802a7c5a4b27 612
sreddy47 5:802a7c5a4b27 613 void LSM9DS1::setGyroScale(uint16_t gScl)
sreddy47 5:802a7c5a4b27 614 {
sreddy47 5:802a7c5a4b27 615 // Read current value of CTRL_REG1_G:
sreddy47 5:802a7c5a4b27 616 uint8_t ctrl1RegValue = xgReadByte(CTRL_REG1_G);
sreddy47 5:802a7c5a4b27 617 // Mask out scale bits (3 & 4):
sreddy47 5:802a7c5a4b27 618 ctrl1RegValue &= 0xE7;
sreddy47 5:802a7c5a4b27 619 switch (gScl)
sreddy47 5:802a7c5a4b27 620 {
sreddy47 5:802a7c5a4b27 621 case 500:
sreddy47 5:802a7c5a4b27 622 ctrl1RegValue |= (0x1 << 3);
sreddy47 5:802a7c5a4b27 623 settings.gyro.scale = 500;
sreddy47 5:802a7c5a4b27 624 break;
sreddy47 5:802a7c5a4b27 625 case 2000:
sreddy47 5:802a7c5a4b27 626 ctrl1RegValue |= (0x3 << 3);
sreddy47 5:802a7c5a4b27 627 settings.gyro.scale = 2000;
sreddy47 5:802a7c5a4b27 628 break;
sreddy47 5:802a7c5a4b27 629 default: // Otherwise we'll set it to 245 dps (0x0 << 4)
sreddy47 5:802a7c5a4b27 630 settings.gyro.scale = 245;
sreddy47 5:802a7c5a4b27 631 break;
sreddy47 5:802a7c5a4b27 632 }
sreddy47 5:802a7c5a4b27 633 xgWriteByte(CTRL_REG1_G, ctrl1RegValue);
sreddy47 5:802a7c5a4b27 634
sreddy47 5:802a7c5a4b27 635 calcgRes();
sreddy47 5:802a7c5a4b27 636 }
sreddy47 5:802a7c5a4b27 637
sreddy47 5:802a7c5a4b27 638 void LSM9DS1::setAccelScale(uint8_t aScl)
sreddy47 5:802a7c5a4b27 639 {
sreddy47 5:802a7c5a4b27 640 // We need to preserve the other bytes in CTRL_REG6_XL. So, first read it:
sreddy47 5:802a7c5a4b27 641 uint8_t tempRegValue = xgReadByte(CTRL_REG6_XL);
sreddy47 5:802a7c5a4b27 642 // Mask out accel scale bits:
sreddy47 5:802a7c5a4b27 643 tempRegValue &= 0xE7;
sreddy47 5:802a7c5a4b27 644
sreddy47 5:802a7c5a4b27 645 switch (aScl)
sreddy47 5:802a7c5a4b27 646 {
sreddy47 5:802a7c5a4b27 647 case 4:
sreddy47 5:802a7c5a4b27 648 tempRegValue |= (0x2 << 3);
sreddy47 5:802a7c5a4b27 649 settings.accel.scale = 4;
sreddy47 5:802a7c5a4b27 650 break;
sreddy47 5:802a7c5a4b27 651 case 8:
sreddy47 5:802a7c5a4b27 652 tempRegValue |= (0x3 << 3);
sreddy47 5:802a7c5a4b27 653 settings.accel.scale = 8;
sreddy47 5:802a7c5a4b27 654 break;
sreddy47 5:802a7c5a4b27 655 case 16:
sreddy47 5:802a7c5a4b27 656 tempRegValue |= (0x1 << 3);
sreddy47 5:802a7c5a4b27 657 settings.accel.scale = 16;
sreddy47 5:802a7c5a4b27 658 break;
sreddy47 5:802a7c5a4b27 659 default: // Otherwise it'll be set to 2g (0x0 << 3)
sreddy47 5:802a7c5a4b27 660 settings.accel.scale = 2;
sreddy47 5:802a7c5a4b27 661 break;
sreddy47 5:802a7c5a4b27 662 }
sreddy47 5:802a7c5a4b27 663 xgWriteByte(CTRL_REG6_XL, tempRegValue);
sreddy47 5:802a7c5a4b27 664
sreddy47 5:802a7c5a4b27 665 // Then calculate a new aRes, which relies on aScale being set correctly:
sreddy47 5:802a7c5a4b27 666 calcaRes();
sreddy47 5:802a7c5a4b27 667 }
sreddy47 5:802a7c5a4b27 668
sreddy47 5:802a7c5a4b27 669 void LSM9DS1::setMagScale(uint8_t mScl)
sreddy47 5:802a7c5a4b27 670 {
sreddy47 5:802a7c5a4b27 671 // We need to preserve the other bytes in CTRL_REG6_XM. So, first read it:
sreddy47 5:802a7c5a4b27 672 uint8_t temp = mReadByte(CTRL_REG2_M);
sreddy47 5:802a7c5a4b27 673 // Then mask out the mag scale bits:
sreddy47 5:802a7c5a4b27 674 temp &= 0xFF^(0x3 << 5);
sreddy47 5:802a7c5a4b27 675
sreddy47 5:802a7c5a4b27 676 switch (mScl)
sreddy47 5:802a7c5a4b27 677 {
sreddy47 5:802a7c5a4b27 678 case 8:
sreddy47 5:802a7c5a4b27 679 temp |= (0x1 << 5);
sreddy47 5:802a7c5a4b27 680 settings.mag.scale = 8;
sreddy47 5:802a7c5a4b27 681 break;
sreddy47 5:802a7c5a4b27 682 case 12:
sreddy47 5:802a7c5a4b27 683 temp |= (0x2 << 5);
sreddy47 5:802a7c5a4b27 684 settings.mag.scale = 12;
sreddy47 5:802a7c5a4b27 685 break;
sreddy47 5:802a7c5a4b27 686 case 16:
sreddy47 5:802a7c5a4b27 687 temp |= (0x3 << 5);
sreddy47 5:802a7c5a4b27 688 settings.mag.scale = 16;
sreddy47 5:802a7c5a4b27 689 break;
sreddy47 5:802a7c5a4b27 690 default: // Otherwise we'll default to 4 gauss (00)
sreddy47 5:802a7c5a4b27 691 settings.mag.scale = 4;
sreddy47 5:802a7c5a4b27 692 break;
sreddy47 5:802a7c5a4b27 693 }
sreddy47 5:802a7c5a4b27 694
sreddy47 5:802a7c5a4b27 695 // And write the new register value back into CTRL_REG6_XM:
sreddy47 5:802a7c5a4b27 696 mWriteByte(CTRL_REG2_M, temp);
sreddy47 5:802a7c5a4b27 697
sreddy47 5:802a7c5a4b27 698 // We've updated the sensor, but we also need to update our class variables
sreddy47 5:802a7c5a4b27 699 // First update mScale:
sreddy47 5:802a7c5a4b27 700 //mScale = mScl;
sreddy47 5:802a7c5a4b27 701 // Then calculate a new mRes, which relies on mScale being set correctly:
sreddy47 5:802a7c5a4b27 702 calcmRes();
sreddy47 5:802a7c5a4b27 703 }
sreddy47 5:802a7c5a4b27 704
sreddy47 5:802a7c5a4b27 705 void LSM9DS1::setGyroODR(uint8_t gRate)
sreddy47 5:802a7c5a4b27 706 {
sreddy47 5:802a7c5a4b27 707 // Only do this if gRate is not 0 (which would disable the gyro)
sreddy47 5:802a7c5a4b27 708 if ((gRate & 0x07) != 0)
sreddy47 5:802a7c5a4b27 709 {
sreddy47 5:802a7c5a4b27 710 // We need to preserve the other bytes in CTRL_REG1_G. So, first read it:
sreddy47 5:802a7c5a4b27 711 uint8_t temp = xgReadByte(CTRL_REG1_G);
sreddy47 5:802a7c5a4b27 712 // Then mask out the gyro ODR bits:
sreddy47 5:802a7c5a4b27 713 temp &= 0xFF^(0x7 << 5);
sreddy47 5:802a7c5a4b27 714 temp |= (gRate & 0x07) << 5;
sreddy47 5:802a7c5a4b27 715 // Update our settings struct
sreddy47 5:802a7c5a4b27 716 settings.gyro.sampleRate = gRate & 0x07;
sreddy47 5:802a7c5a4b27 717 // And write the new register value back into CTRL_REG1_G:
sreddy47 5:802a7c5a4b27 718 xgWriteByte(CTRL_REG1_G, temp);
sreddy47 5:802a7c5a4b27 719 }
sreddy47 5:802a7c5a4b27 720 }
sreddy47 5:802a7c5a4b27 721
sreddy47 5:802a7c5a4b27 722 void LSM9DS1::setAccelODR(uint8_t aRate)
sreddy47 5:802a7c5a4b27 723 {
sreddy47 5:802a7c5a4b27 724 // Only do this if aRate is not 0 (which would disable the accel)
sreddy47 5:802a7c5a4b27 725 if ((aRate & 0x07) != 0)
sreddy47 5:802a7c5a4b27 726 {
sreddy47 5:802a7c5a4b27 727 // We need to preserve the other bytes in CTRL_REG1_XM. So, first read it:
sreddy47 5:802a7c5a4b27 728 uint8_t temp = xgReadByte(CTRL_REG6_XL);
sreddy47 5:802a7c5a4b27 729 // Then mask out the accel ODR bits:
sreddy47 5:802a7c5a4b27 730 temp &= 0x1F;
sreddy47 5:802a7c5a4b27 731 // Then shift in our new ODR bits:
sreddy47 5:802a7c5a4b27 732 temp |= ((aRate & 0x07) << 5);
sreddy47 5:802a7c5a4b27 733 settings.accel.sampleRate = aRate & 0x07;
sreddy47 5:802a7c5a4b27 734 // And write the new register value back into CTRL_REG1_XM:
sreddy47 5:802a7c5a4b27 735 xgWriteByte(CTRL_REG6_XL, temp);
sreddy47 5:802a7c5a4b27 736 }
sreddy47 5:802a7c5a4b27 737 }
sreddy47 5:802a7c5a4b27 738
sreddy47 5:802a7c5a4b27 739 void LSM9DS1::setMagODR(uint8_t mRate)
sreddy47 5:802a7c5a4b27 740 {
sreddy47 5:802a7c5a4b27 741 // We need to preserve the other bytes in CTRL_REG5_XM. So, first read it:
sreddy47 5:802a7c5a4b27 742 uint8_t temp = mReadByte(CTRL_REG1_M);
sreddy47 5:802a7c5a4b27 743 // Then mask out the mag ODR bits:
sreddy47 5:802a7c5a4b27 744 temp &= 0xFF^(0x7 << 2);
sreddy47 5:802a7c5a4b27 745 // Then shift in our new ODR bits:
sreddy47 5:802a7c5a4b27 746 temp |= ((mRate & 0x07) << 2);
sreddy47 5:802a7c5a4b27 747 settings.mag.sampleRate = mRate & 0x07;
sreddy47 5:802a7c5a4b27 748 // And write the new register value back into CTRL_REG5_XM:
sreddy47 5:802a7c5a4b27 749 mWriteByte(CTRL_REG1_M, temp);
sreddy47 5:802a7c5a4b27 750 }
sreddy47 5:802a7c5a4b27 751
sreddy47 5:802a7c5a4b27 752 void LSM9DS1::calcgRes()
sreddy47 5:802a7c5a4b27 753 {
sreddy47 5:802a7c5a4b27 754 gRes = ((float) settings.gyro.scale) / 32768.0;
sreddy47 5:802a7c5a4b27 755 }
sreddy47 5:802a7c5a4b27 756
sreddy47 5:802a7c5a4b27 757 void LSM9DS1::calcaRes()
sreddy47 5:802a7c5a4b27 758 {
sreddy47 5:802a7c5a4b27 759 aRes = ((float) settings.accel.scale) / 32768.0;
sreddy47 5:802a7c5a4b27 760 }
sreddy47 5:802a7c5a4b27 761
sreddy47 5:802a7c5a4b27 762 void LSM9DS1::calcmRes()
sreddy47 5:802a7c5a4b27 763 {
sreddy47 5:802a7c5a4b27 764 //mRes = ((float) settings.mag.scale) / 32768.0;
sreddy47 5:802a7c5a4b27 765 switch (settings.mag.scale)
sreddy47 5:802a7c5a4b27 766 {
sreddy47 5:802a7c5a4b27 767 case 4:
sreddy47 5:802a7c5a4b27 768 mRes = magSensitivity[0];
sreddy47 5:802a7c5a4b27 769 break;
sreddy47 5:802a7c5a4b27 770 case 8:
sreddy47 5:802a7c5a4b27 771 mRes = magSensitivity[1];
sreddy47 5:802a7c5a4b27 772 break;
sreddy47 5:802a7c5a4b27 773 case 12:
sreddy47 5:802a7c5a4b27 774 mRes = magSensitivity[2];
sreddy47 5:802a7c5a4b27 775 break;
sreddy47 5:802a7c5a4b27 776 case 16:
sreddy47 5:802a7c5a4b27 777 mRes = magSensitivity[3];
sreddy47 5:802a7c5a4b27 778 break;
sreddy47 5:802a7c5a4b27 779 }
sreddy47 5:802a7c5a4b27 780
sreddy47 5:802a7c5a4b27 781 }
sreddy47 5:802a7c5a4b27 782
sreddy47 5:802a7c5a4b27 783 void LSM9DS1::configInt(interrupt_select interrupt, uint8_t generator,
sreddy47 5:802a7c5a4b27 784 h_lactive activeLow, pp_od pushPull)
sreddy47 5:802a7c5a4b27 785 {
sreddy47 5:802a7c5a4b27 786 // Write to INT1_CTRL or INT2_CTRL. [interupt] should already be one of
sreddy47 5:802a7c5a4b27 787 // those two values.
sreddy47 5:802a7c5a4b27 788 // [generator] should be an OR'd list of values from the interrupt_generators enum
sreddy47 5:802a7c5a4b27 789 xgWriteByte(interrupt, generator);
sreddy47 5:802a7c5a4b27 790
sreddy47 5:802a7c5a4b27 791 // Configure CTRL_REG8
sreddy47 5:802a7c5a4b27 792 uint8_t temp;
sreddy47 5:802a7c5a4b27 793 temp = xgReadByte(CTRL_REG8);
sreddy47 5:802a7c5a4b27 794
sreddy47 5:802a7c5a4b27 795 if (activeLow) temp |= (1<<5);
sreddy47 5:802a7c5a4b27 796 else temp &= ~(1<<5);
sreddy47 5:802a7c5a4b27 797
sreddy47 5:802a7c5a4b27 798 if (pushPull) temp &= ~(1<<4);
sreddy47 5:802a7c5a4b27 799 else temp |= (1<<4);
sreddy47 5:802a7c5a4b27 800
sreddy47 5:802a7c5a4b27 801 xgWriteByte(CTRL_REG8, temp);
sreddy47 5:802a7c5a4b27 802 }
sreddy47 5:802a7c5a4b27 803
sreddy47 5:802a7c5a4b27 804 void LSM9DS1::configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn)
sreddy47 5:802a7c5a4b27 805 {
sreddy47 5:802a7c5a4b27 806 uint8_t temp = 0;
sreddy47 5:802a7c5a4b27 807
sreddy47 5:802a7c5a4b27 808 temp = threshold & 0x7F;
sreddy47 5:802a7c5a4b27 809 if (sleepOn) temp |= (1<<7);
sreddy47 5:802a7c5a4b27 810 xgWriteByte(ACT_THS, temp);
sreddy47 5:802a7c5a4b27 811
sreddy47 5:802a7c5a4b27 812 xgWriteByte(ACT_DUR, duration);
sreddy47 5:802a7c5a4b27 813 }
sreddy47 5:802a7c5a4b27 814
sreddy47 5:802a7c5a4b27 815 uint8_t LSM9DS1::getInactivity()
sreddy47 5:802a7c5a4b27 816 {
sreddy47 5:802a7c5a4b27 817 uint8_t temp = xgReadByte(STATUS_REG_0);
sreddy47 5:802a7c5a4b27 818 temp &= (0x10);
sreddy47 5:802a7c5a4b27 819 return temp;
sreddy47 5:802a7c5a4b27 820 }
sreddy47 5:802a7c5a4b27 821
sreddy47 5:802a7c5a4b27 822 void LSM9DS1::configAccelInt(uint8_t generator, bool andInterrupts)
sreddy47 5:802a7c5a4b27 823 {
sreddy47 5:802a7c5a4b27 824 // Use variables from accel_interrupt_generator, OR'd together to create
sreddy47 5:802a7c5a4b27 825 // the [generator]value.
sreddy47 5:802a7c5a4b27 826 uint8_t temp = generator;
sreddy47 5:802a7c5a4b27 827 if (andInterrupts) temp |= 0x80;
sreddy47 5:802a7c5a4b27 828 xgWriteByte(INT_GEN_CFG_XL, temp);
sreddy47 5:802a7c5a4b27 829 }
sreddy47 5:802a7c5a4b27 830
sreddy47 5:802a7c5a4b27 831 void LSM9DS1::configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait)
sreddy47 5:802a7c5a4b27 832 {
sreddy47 5:802a7c5a4b27 833 // Write threshold value to INT_GEN_THS_?_XL.
sreddy47 5:802a7c5a4b27 834 // axis will be 0, 1, or 2 (x, y, z respectively)
sreddy47 5:802a7c5a4b27 835 xgWriteByte(INT_GEN_THS_X_XL + axis, threshold);
sreddy47 5:802a7c5a4b27 836
sreddy47 5:802a7c5a4b27 837 // Write duration and wait to INT_GEN_DUR_XL
sreddy47 5:802a7c5a4b27 838 uint8_t temp;
sreddy47 5:802a7c5a4b27 839 temp = (duration & 0x7F);
sreddy47 5:802a7c5a4b27 840 if (wait) temp |= 0x80;
sreddy47 5:802a7c5a4b27 841 xgWriteByte(INT_GEN_DUR_XL, temp);
sreddy47 5:802a7c5a4b27 842 }
sreddy47 5:802a7c5a4b27 843
sreddy47 5:802a7c5a4b27 844 uint8_t LSM9DS1::getAccelIntSrc()
sreddy47 5:802a7c5a4b27 845 {
sreddy47 5:802a7c5a4b27 846 uint8_t intSrc = xgReadByte(INT_GEN_SRC_XL);
sreddy47 5:802a7c5a4b27 847
sreddy47 5:802a7c5a4b27 848 // Check if the IA_XL (interrupt active) bit is set
sreddy47 5:802a7c5a4b27 849 if (intSrc & (1<<6))
sreddy47 5:802a7c5a4b27 850 {
sreddy47 5:802a7c5a4b27 851 return (intSrc & 0x3F);
sreddy47 5:802a7c5a4b27 852 }
sreddy47 5:802a7c5a4b27 853
sreddy47 5:802a7c5a4b27 854 return 0;
sreddy47 5:802a7c5a4b27 855 }
sreddy47 5:802a7c5a4b27 856
sreddy47 5:802a7c5a4b27 857 void LSM9DS1::configGyroInt(uint8_t generator, bool aoi, bool latch)
sreddy47 5:802a7c5a4b27 858 {
sreddy47 5:802a7c5a4b27 859 // Use variables from accel_interrupt_generator, OR'd together to create
sreddy47 5:802a7c5a4b27 860 // the [generator]value.
sreddy47 5:802a7c5a4b27 861 uint8_t temp = generator;
sreddy47 5:802a7c5a4b27 862 if (aoi) temp |= 0x80;
sreddy47 5:802a7c5a4b27 863 if (latch) temp |= 0x40;
sreddy47 5:802a7c5a4b27 864 xgWriteByte(INT_GEN_CFG_G, temp);
sreddy47 5:802a7c5a4b27 865 }
sreddy47 5:802a7c5a4b27 866
sreddy47 5:802a7c5a4b27 867 void LSM9DS1::configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait)
sreddy47 5:802a7c5a4b27 868 {
sreddy47 5:802a7c5a4b27 869 uint8_t buffer[2];
sreddy47 5:802a7c5a4b27 870 buffer[0] = (threshold & 0x7F00) >> 8;
sreddy47 5:802a7c5a4b27 871 buffer[1] = (threshold & 0x00FF);
sreddy47 5:802a7c5a4b27 872 // Write threshold value to INT_GEN_THS_?H_G and INT_GEN_THS_?L_G.
sreddy47 5:802a7c5a4b27 873 // axis will be 0, 1, or 2 (x, y, z respectively)
sreddy47 5:802a7c5a4b27 874 xgWriteByte(INT_GEN_THS_XH_G + (axis * 2), buffer[0]);
sreddy47 5:802a7c5a4b27 875 xgWriteByte(INT_GEN_THS_XH_G + 1 + (axis * 2), buffer[1]);
sreddy47 5:802a7c5a4b27 876
sreddy47 5:802a7c5a4b27 877 // Write duration and wait to INT_GEN_DUR_XL
sreddy47 5:802a7c5a4b27 878 uint8_t temp;
sreddy47 5:802a7c5a4b27 879 temp = (duration & 0x7F);
sreddy47 5:802a7c5a4b27 880 if (wait) temp |= 0x80;
sreddy47 5:802a7c5a4b27 881 xgWriteByte(INT_GEN_DUR_G, temp);
sreddy47 5:802a7c5a4b27 882 }
sreddy47 5:802a7c5a4b27 883
sreddy47 5:802a7c5a4b27 884 uint8_t LSM9DS1::getGyroIntSrc()
sreddy47 5:802a7c5a4b27 885 {
sreddy47 5:802a7c5a4b27 886 uint8_t intSrc = xgReadByte(INT_GEN_SRC_G);
sreddy47 5:802a7c5a4b27 887
sreddy47 5:802a7c5a4b27 888 // Check if the IA_G (interrupt active) bit is set
sreddy47 5:802a7c5a4b27 889 if (intSrc & (1<<6))
sreddy47 5:802a7c5a4b27 890 {
sreddy47 5:802a7c5a4b27 891 return (intSrc & 0x3F);
sreddy47 5:802a7c5a4b27 892 }
sreddy47 5:802a7c5a4b27 893
sreddy47 5:802a7c5a4b27 894 return 0;
sreddy47 5:802a7c5a4b27 895 }
sreddy47 5:802a7c5a4b27 896
sreddy47 5:802a7c5a4b27 897 void LSM9DS1::configMagInt(uint8_t generator, h_lactive activeLow, bool latch)
sreddy47 5:802a7c5a4b27 898 {
sreddy47 5:802a7c5a4b27 899 // Mask out non-generator bits (0-4)
sreddy47 5:802a7c5a4b27 900 uint8_t config = (generator & 0xE0);
sreddy47 5:802a7c5a4b27 901 // IEA bit is 0 for active-low, 1 for active-high.
sreddy47 5:802a7c5a4b27 902 if (activeLow == INT_ACTIVE_HIGH) config |= (1<<2);
sreddy47 5:802a7c5a4b27 903 // IEL bit is 0 for latched, 1 for not-latched
sreddy47 5:802a7c5a4b27 904 if (!latch) config |= (1<<1);
sreddy47 5:802a7c5a4b27 905 // As long as we have at least 1 generator, enable the interrupt
sreddy47 5:802a7c5a4b27 906 if (generator != 0) config |= (1<<0);
sreddy47 5:802a7c5a4b27 907
sreddy47 5:802a7c5a4b27 908 mWriteByte(INT_CFG_M, config);
sreddy47 5:802a7c5a4b27 909 }
sreddy47 5:802a7c5a4b27 910
sreddy47 5:802a7c5a4b27 911 void LSM9DS1::configMagThs(uint16_t threshold)
sreddy47 5:802a7c5a4b27 912 {
sreddy47 5:802a7c5a4b27 913 // Write high eight bits of [threshold] to INT_THS_H_M
sreddy47 5:802a7c5a4b27 914 mWriteByte(INT_THS_H_M, uint8_t((threshold & 0x7F00) >> 8));
sreddy47 5:802a7c5a4b27 915 // Write low eight bits of [threshold] to INT_THS_L_M
sreddy47 5:802a7c5a4b27 916 mWriteByte(INT_THS_L_M, uint8_t(threshold & 0x00FF));
sreddy47 5:802a7c5a4b27 917 }
sreddy47 5:802a7c5a4b27 918
sreddy47 5:802a7c5a4b27 919 uint8_t LSM9DS1::getMagIntSrc()
sreddy47 5:802a7c5a4b27 920 {
sreddy47 5:802a7c5a4b27 921 uint8_t intSrc = mReadByte(INT_SRC_M);
sreddy47 5:802a7c5a4b27 922
sreddy47 5:802a7c5a4b27 923 // Check if the INT (interrupt active) bit is set
sreddy47 5:802a7c5a4b27 924 if (intSrc & (1<<0))
sreddy47 5:802a7c5a4b27 925 {
sreddy47 5:802a7c5a4b27 926 return (intSrc & 0xFE);
sreddy47 5:802a7c5a4b27 927 }
sreddy47 5:802a7c5a4b27 928
sreddy47 5:802a7c5a4b27 929 return 0;
sreddy47 5:802a7c5a4b27 930 }
sreddy47 5:802a7c5a4b27 931
sreddy47 5:802a7c5a4b27 932 void LSM9DS1::sleepGyro(bool enable)
sreddy47 5:802a7c5a4b27 933 {
sreddy47 5:802a7c5a4b27 934 uint8_t temp = xgReadByte(CTRL_REG9);
sreddy47 5:802a7c5a4b27 935 if (enable) temp |= (1<<6);
sreddy47 5:802a7c5a4b27 936 else temp &= ~(1<<6);
sreddy47 5:802a7c5a4b27 937 xgWriteByte(CTRL_REG9, temp);
sreddy47 5:802a7c5a4b27 938 }
sreddy47 5:802a7c5a4b27 939
sreddy47 5:802a7c5a4b27 940 void LSM9DS1::enableFIFO(bool enable)
sreddy47 5:802a7c5a4b27 941 {
sreddy47 5:802a7c5a4b27 942 uint8_t temp = xgReadByte(CTRL_REG9);
sreddy47 5:802a7c5a4b27 943 if (enable) temp |= (1<<1);
sreddy47 5:802a7c5a4b27 944 else temp &= ~(1<<1);
sreddy47 5:802a7c5a4b27 945 xgWriteByte(CTRL_REG9, temp);
sreddy47 5:802a7c5a4b27 946 }
sreddy47 5:802a7c5a4b27 947
sreddy47 5:802a7c5a4b27 948 void LSM9DS1::setFIFO(fifoMode_type fifoMode, uint8_t fifoThs)
sreddy47 5:802a7c5a4b27 949 {
sreddy47 5:802a7c5a4b27 950 // Limit threshold - 0x1F (31) is the maximum. If more than that was asked
sreddy47 5:802a7c5a4b27 951 // limit it to the maximum.
sreddy47 5:802a7c5a4b27 952 uint8_t threshold = fifoThs <= 0x1F ? fifoThs : 0x1F;
sreddy47 5:802a7c5a4b27 953 xgWriteByte(FIFO_CTRL, ((fifoMode & 0x7) << 5) | (threshold & 0x1F));
sreddy47 5:802a7c5a4b27 954 }
sreddy47 5:802a7c5a4b27 955
sreddy47 5:802a7c5a4b27 956 uint8_t LSM9DS1::getFIFOSamples()
sreddy47 5:802a7c5a4b27 957 {
sreddy47 5:802a7c5a4b27 958 return (xgReadByte(FIFO_SRC) & 0x3F);
sreddy47 5:802a7c5a4b27 959 }
sreddy47 5:802a7c5a4b27 960
sreddy47 5:802a7c5a4b27 961 void LSM9DS1::constrainScales()
sreddy47 5:802a7c5a4b27 962 {
sreddy47 5:802a7c5a4b27 963 if ((settings.gyro.scale != 245) && (settings.gyro.scale != 500) &&
sreddy47 5:802a7c5a4b27 964 (settings.gyro.scale != 2000))
sreddy47 5:802a7c5a4b27 965 {
sreddy47 5:802a7c5a4b27 966 settings.gyro.scale = 245;
sreddy47 5:802a7c5a4b27 967 }
sreddy47 5:802a7c5a4b27 968
sreddy47 5:802a7c5a4b27 969 if ((settings.accel.scale != 2) && (settings.accel.scale != 4) &&
sreddy47 5:802a7c5a4b27 970 (settings.accel.scale != 8) && (settings.accel.scale != 16))
sreddy47 5:802a7c5a4b27 971 {
sreddy47 5:802a7c5a4b27 972 settings.accel.scale = 2;
sreddy47 5:802a7c5a4b27 973 }
sreddy47 5:802a7c5a4b27 974
sreddy47 5:802a7c5a4b27 975 if ((settings.mag.scale != 4) && (settings.mag.scale != 8) &&
sreddy47 5:802a7c5a4b27 976 (settings.mag.scale != 12) && (settings.mag.scale != 16))
sreddy47 5:802a7c5a4b27 977 {
sreddy47 5:802a7c5a4b27 978 settings.mag.scale = 4;
sreddy47 5:802a7c5a4b27 979 }
sreddy47 5:802a7c5a4b27 980 }
sreddy47 5:802a7c5a4b27 981
sreddy47 5:802a7c5a4b27 982 void LSM9DS1::xgWriteByte(uint8_t subAddress, uint8_t data)
sreddy47 5:802a7c5a4b27 983 {
sreddy47 5:802a7c5a4b27 984 // Whether we're using I2C or SPI, write a byte using the
sreddy47 5:802a7c5a4b27 985 // gyro-specific I2C address or SPI CS pin.
sreddy47 5:802a7c5a4b27 986 if (settings.device.commInterface == IMU_MODE_I2C) {
sreddy47 5:802a7c5a4b27 987 printf("yo");
sreddy47 5:802a7c5a4b27 988 I2CwriteByte(_xgAddress, subAddress, data);
sreddy47 5:802a7c5a4b27 989 } else if (settings.device.commInterface == IMU_MODE_SPI) {
sreddy47 5:802a7c5a4b27 990 SPIwriteByte(_xgAddress, subAddress, data);
sreddy47 5:802a7c5a4b27 991 }
sreddy47 5:802a7c5a4b27 992 }
sreddy47 5:802a7c5a4b27 993
sreddy47 5:802a7c5a4b27 994 void LSM9DS1::mWriteByte(uint8_t subAddress, uint8_t data)
sreddy47 5:802a7c5a4b27 995 {
sreddy47 5:802a7c5a4b27 996 // Whether we're using I2C or SPI, write a byte using the
sreddy47 5:802a7c5a4b27 997 // accelerometer-specific I2C address or SPI CS pin.
sreddy47 5:802a7c5a4b27 998 if (settings.device.commInterface == IMU_MODE_I2C)
sreddy47 5:802a7c5a4b27 999 return I2CwriteByte(_mAddress, subAddress, data);
sreddy47 5:802a7c5a4b27 1000 else if (settings.device.commInterface == IMU_MODE_SPI)
sreddy47 5:802a7c5a4b27 1001 return SPIwriteByte(_mAddress, subAddress, data);
sreddy47 5:802a7c5a4b27 1002 }
sreddy47 5:802a7c5a4b27 1003
sreddy47 5:802a7c5a4b27 1004 uint8_t LSM9DS1::xgReadByte(uint8_t subAddress)
sreddy47 5:802a7c5a4b27 1005 {
sreddy47 5:802a7c5a4b27 1006 // Whether we're using I2C or SPI, read a byte using the
sreddy47 5:802a7c5a4b27 1007 // gyro-specific I2C address or SPI CS pin.
sreddy47 5:802a7c5a4b27 1008 if (settings.device.commInterface == IMU_MODE_I2C)
sreddy47 5:802a7c5a4b27 1009 return I2CreadByte(_xgAddress, subAddress);
sreddy47 5:802a7c5a4b27 1010 else if (settings.device.commInterface == IMU_MODE_SPI)
sreddy47 5:802a7c5a4b27 1011 return SPIreadByte(_xgAddress, subAddress);
sreddy47 5:802a7c5a4b27 1012 }
sreddy47 5:802a7c5a4b27 1013
sreddy47 5:802a7c5a4b27 1014 void LSM9DS1::xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
sreddy47 5:802a7c5a4b27 1015 {
sreddy47 5:802a7c5a4b27 1016 // Whether we're using I2C or SPI, read multiple bytes using the
sreddy47 5:802a7c5a4b27 1017 // gyro-specific I2C address or SPI CS pin.
sreddy47 5:802a7c5a4b27 1018 if (settings.device.commInterface == IMU_MODE_I2C) {
sreddy47 5:802a7c5a4b27 1019 I2CreadBytes(_xgAddress, subAddress, dest, count);
sreddy47 5:802a7c5a4b27 1020 } else if (settings.device.commInterface == IMU_MODE_SPI) {
sreddy47 5:802a7c5a4b27 1021 SPIreadBytes(_xgAddress, subAddress, dest, count);
sreddy47 5:802a7c5a4b27 1022 }
sreddy47 5:802a7c5a4b27 1023 }
sreddy47 5:802a7c5a4b27 1024
sreddy47 5:802a7c5a4b27 1025 uint8_t LSM9DS1::mReadByte(uint8_t subAddress)
sreddy47 5:802a7c5a4b27 1026 {
sreddy47 5:802a7c5a4b27 1027 // Whether we're using I2C or SPI, read a byte using the
sreddy47 5:802a7c5a4b27 1028 // accelerometer-specific I2C address or SPI CS pin.
sreddy47 5:802a7c5a4b27 1029 if (settings.device.commInterface == IMU_MODE_I2C)
sreddy47 5:802a7c5a4b27 1030 return I2CreadByte(_mAddress, subAddress);
sreddy47 5:802a7c5a4b27 1031 else if (settings.device.commInterface == IMU_MODE_SPI)
sreddy47 5:802a7c5a4b27 1032 return SPIreadByte(_mAddress, subAddress);
sreddy47 5:802a7c5a4b27 1033 }
sreddy47 5:802a7c5a4b27 1034
sreddy47 5:802a7c5a4b27 1035 void LSM9DS1::mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
sreddy47 5:802a7c5a4b27 1036 {
sreddy47 5:802a7c5a4b27 1037 // Whether we're using I2C or SPI, read multiple bytes using the
sreddy47 5:802a7c5a4b27 1038 // accelerometer-specific I2C address or SPI CS pin.
sreddy47 5:802a7c5a4b27 1039 if (settings.device.commInterface == IMU_MODE_I2C)
sreddy47 5:802a7c5a4b27 1040 I2CreadBytes(_mAddress, subAddress, dest, count);
sreddy47 5:802a7c5a4b27 1041 else if (settings.device.commInterface == IMU_MODE_SPI)
sreddy47 5:802a7c5a4b27 1042 SPIreadBytes(_mAddress, subAddress, dest, count);
sreddy47 5:802a7c5a4b27 1043 }
sreddy47 5:802a7c5a4b27 1044
sreddy47 5:802a7c5a4b27 1045 void LSM9DS1::initSPI()
sreddy47 5:802a7c5a4b27 1046 {
sreddy47 5:802a7c5a4b27 1047 /*
sreddy47 5:802a7c5a4b27 1048 pinMode(_xgAddress, OUTPUT);
sreddy47 5:802a7c5a4b27 1049 digitalWrite(_xgAddress, HIGH);
sreddy47 5:802a7c5a4b27 1050 pinMode(_mAddress, OUTPUT);
sreddy47 5:802a7c5a4b27 1051 digitalWrite(_mAddress, HIGH);
sreddy47 5:802a7c5a4b27 1052
sreddy47 5:802a7c5a4b27 1053 SPI.begin();
sreddy47 5:802a7c5a4b27 1054 // Maximum SPI frequency is 10MHz, could divide by 2 here:
sreddy47 5:802a7c5a4b27 1055 SPI.setClockDivider(SPI_CLOCK_DIV2);
sreddy47 5:802a7c5a4b27 1056 // Data is read and written MSb first.
sreddy47 5:802a7c5a4b27 1057 SPI.setBitOrder(MSBFIRST);
sreddy47 5:802a7c5a4b27 1058 // Data is captured on rising edge of clock (CPHA = 0)
sreddy47 5:802a7c5a4b27 1059 // Base value of the clock is HIGH (CPOL = 1)
sreddy47 5:802a7c5a4b27 1060 SPI.setDataMode(SPI_MODE0);
sreddy47 5:802a7c5a4b27 1061 */
sreddy47 5:802a7c5a4b27 1062 }
sreddy47 5:802a7c5a4b27 1063
sreddy47 5:802a7c5a4b27 1064 void LSM9DS1::SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data)
sreddy47 5:802a7c5a4b27 1065 {
sreddy47 5:802a7c5a4b27 1066 /*
sreddy47 5:802a7c5a4b27 1067 digitalWrite(csPin, LOW); // Initiate communication
sreddy47 5:802a7c5a4b27 1068
sreddy47 5:802a7c5a4b27 1069 // If write, bit 0 (MSB) should be 0
sreddy47 5:802a7c5a4b27 1070 // If single write, bit 1 should be 0
sreddy47 5:802a7c5a4b27 1071 SPI.transfer(subAddress & 0x3F); // Send Address
sreddy47 5:802a7c5a4b27 1072 SPI.transfer(data); // Send data
sreddy47 5:802a7c5a4b27 1073
sreddy47 5:802a7c5a4b27 1074 digitalWrite(csPin, HIGH); // Close communication
sreddy47 5:802a7c5a4b27 1075 */
sreddy47 5:802a7c5a4b27 1076 }
sreddy47 5:802a7c5a4b27 1077
sreddy47 5:802a7c5a4b27 1078 uint8_t LSM9DS1::SPIreadByte(uint8_t csPin, uint8_t subAddress)
sreddy47 5:802a7c5a4b27 1079 {
sreddy47 5:802a7c5a4b27 1080 uint8_t temp;
sreddy47 5:802a7c5a4b27 1081 // Use the multiple read function to read 1 byte.
sreddy47 5:802a7c5a4b27 1082 // Value is returned to `temp`.
sreddy47 5:802a7c5a4b27 1083 SPIreadBytes(csPin, subAddress, &temp, 1);
sreddy47 5:802a7c5a4b27 1084 return temp;
sreddy47 5:802a7c5a4b27 1085 }
sreddy47 5:802a7c5a4b27 1086
sreddy47 5:802a7c5a4b27 1087 void LSM9DS1::SPIreadBytes(uint8_t csPin, uint8_t subAddress,
sreddy47 5:802a7c5a4b27 1088 uint8_t * dest, uint8_t count)
sreddy47 5:802a7c5a4b27 1089 {
sreddy47 5:802a7c5a4b27 1090 // To indicate a read, set bit 0 (msb) of first byte to 1
sreddy47 5:802a7c5a4b27 1091 uint8_t rAddress = 0x80 | (subAddress & 0x3F);
sreddy47 5:802a7c5a4b27 1092 // Mag SPI port is different. If we're reading multiple bytes,
sreddy47 5:802a7c5a4b27 1093 // set bit 1 to 1. The remaining six bytes are the address to be read
sreddy47 5:802a7c5a4b27 1094 if ((csPin == _mAddress) && count > 1)
sreddy47 5:802a7c5a4b27 1095 rAddress |= 0x40;
sreddy47 5:802a7c5a4b27 1096
sreddy47 5:802a7c5a4b27 1097 /*
sreddy47 5:802a7c5a4b27 1098 digitalWrite(csPin, LOW); // Initiate communication
sreddy47 5:802a7c5a4b27 1099 SPI.transfer(rAddress);
sreddy47 5:802a7c5a4b27 1100 for (int i=0; i<count; i++)
sreddy47 5:802a7c5a4b27 1101 {
sreddy47 5:802a7c5a4b27 1102 dest[i] = SPI.transfer(0x00); // Read into destination array
sreddy47 5:802a7c5a4b27 1103 }
sreddy47 5:802a7c5a4b27 1104 digitalWrite(csPin, HIGH); // Close communication
sreddy47 5:802a7c5a4b27 1105 */
sreddy47 5:802a7c5a4b27 1106 }
sreddy47 5:802a7c5a4b27 1107
sreddy47 5:802a7c5a4b27 1108 void LSM9DS1::initI2C()
sreddy47 5:802a7c5a4b27 1109 {
sreddy47 5:802a7c5a4b27 1110 /*
sreddy47 5:802a7c5a4b27 1111 Wire.begin(); // Initialize I2C library
sreddy47 5:802a7c5a4b27 1112 */
sreddy47 5:802a7c5a4b27 1113
sreddy47 5:802a7c5a4b27 1114 //already initialized in constructor!
sreddy47 5:802a7c5a4b27 1115 }
sreddy47 5:802a7c5a4b27 1116
sreddy47 5:802a7c5a4b27 1117 // Wire.h read and write protocols
sreddy47 5:802a7c5a4b27 1118 void LSM9DS1::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data)
sreddy47 5:802a7c5a4b27 1119 {
sreddy47 5:802a7c5a4b27 1120 /*
sreddy47 5:802a7c5a4b27 1121 Wire.beginTransmission(address); // Initialize the Tx buffer
sreddy47 5:802a7c5a4b27 1122 Wire.write(subAddress); // Put slave register address in Tx buffer
sreddy47 5:802a7c5a4b27 1123 Wire.write(data); // Put data in Tx buffer
sreddy47 5:802a7c5a4b27 1124 Wire.endTransmission(); // Send the Tx buffer
sreddy47 5:802a7c5a4b27 1125 */
sreddy47 5:802a7c5a4b27 1126 char temp_data[2] = {subAddress, data};
sreddy47 5:802a7c5a4b27 1127 i2c.write(address, temp_data, 2);
sreddy47 5:802a7c5a4b27 1128 }
sreddy47 5:802a7c5a4b27 1129
sreddy47 5:802a7c5a4b27 1130 uint8_t LSM9DS1::I2CreadByte(uint8_t address, uint8_t subAddress)
sreddy47 5:802a7c5a4b27 1131 {
sreddy47 5:802a7c5a4b27 1132 /*
sreddy47 5:802a7c5a4b27 1133 int timeout = LSM9DS1_COMMUNICATION_TIMEOUT;
sreddy47 5:802a7c5a4b27 1134 uint8_t data; // `data` will store the register data
sreddy47 5:802a7c5a4b27 1135
sreddy47 5:802a7c5a4b27 1136 Wire.beginTransmission(address); // Initialize the Tx buffer
sreddy47 5:802a7c5a4b27 1137 Wire.write(subAddress); // Put slave register address in Tx buffer
sreddy47 5:802a7c5a4b27 1138 Wire.endTransmission(true); // Send the Tx buffer, but send a restart to keep connection alive
sreddy47 5:802a7c5a4b27 1139 Wire.requestFrom(address, (uint8_t) 1); // Read one byte from slave register address
sreddy47 5:802a7c5a4b27 1140 while ((Wire.available() < 1) && (timeout-- > 0))
sreddy47 5:802a7c5a4b27 1141 delay(1);
sreddy47 5:802a7c5a4b27 1142
sreddy47 5:802a7c5a4b27 1143 if (timeout <= 0)
sreddy47 5:802a7c5a4b27 1144 return 255; //! Bad! 255 will be misinterpreted as a good value.
sreddy47 5:802a7c5a4b27 1145
sreddy47 5:802a7c5a4b27 1146 data = Wire.read(); // Fill Rx buffer with result
sreddy47 5:802a7c5a4b27 1147 return data; // Return data read from slave register
sreddy47 5:802a7c5a4b27 1148 */
sreddy47 5:802a7c5a4b27 1149 char data;
sreddy47 5:802a7c5a4b27 1150 char temp[1] = {subAddress};
sreddy47 5:802a7c5a4b27 1151
sreddy47 5:802a7c5a4b27 1152 i2c.write(address, temp, 1);
sreddy47 5:802a7c5a4b27 1153 //i2c.write(address & 0xFE);
sreddy47 5:802a7c5a4b27 1154 temp[1] = 0x00;
sreddy47 5:802a7c5a4b27 1155 i2c.write(address, temp, 1);
sreddy47 5:802a7c5a4b27 1156 //i2c.write( address | 0x01);
sreddy47 5:802a7c5a4b27 1157 int a = i2c.read(address, &data, 1);
sreddy47 5:802a7c5a4b27 1158 return data;
sreddy47 5:802a7c5a4b27 1159 }
sreddy47 5:802a7c5a4b27 1160
sreddy47 5:802a7c5a4b27 1161 uint8_t LSM9DS1::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count)
sreddy47 5:802a7c5a4b27 1162 {
sreddy47 5:802a7c5a4b27 1163 /*
sreddy47 5:802a7c5a4b27 1164 int timeout = LSM9DS1_COMMUNICATION_TIMEOUT;
sreddy47 5:802a7c5a4b27 1165 Wire.beginTransmission(address); // Initialize the Tx buffer
sreddy47 5:802a7c5a4b27 1166 // Next send the register to be read. OR with 0x80 to indicate multi-read.
sreddy47 5:802a7c5a4b27 1167 Wire.write(subAddress | 0x80); // Put slave register address in Tx buffer
sreddy47 5:802a7c5a4b27 1168
sreddy47 5:802a7c5a4b27 1169 Wire.endTransmission(true); // Send the Tx buffer, but send a restart to keep connection alive
sreddy47 5:802a7c5a4b27 1170 uint8_t i = 0;
sreddy47 5:802a7c5a4b27 1171 Wire.requestFrom(address, count); // Read bytes from slave register address
sreddy47 5:802a7c5a4b27 1172 while ((Wire.available() < count) && (timeout-- > 0))
sreddy47 5:802a7c5a4b27 1173 delay(1);
sreddy47 5:802a7c5a4b27 1174 if (timeout <= 0)
sreddy47 5:802a7c5a4b27 1175 return -1;
sreddy47 5:802a7c5a4b27 1176
sreddy47 5:802a7c5a4b27 1177 for (int i=0; i<count;)
sreddy47 5:802a7c5a4b27 1178 {
sreddy47 5:802a7c5a4b27 1179 if (Wire.available())
sreddy47 5:802a7c5a4b27 1180 {
sreddy47 5:802a7c5a4b27 1181 dest[i++] = Wire.read();
sreddy47 5:802a7c5a4b27 1182 }
sreddy47 5:802a7c5a4b27 1183 }
sreddy47 5:802a7c5a4b27 1184 return count;
sreddy47 5:802a7c5a4b27 1185 */
sreddy47 5:802a7c5a4b27 1186 int i;
sreddy47 5:802a7c5a4b27 1187 char temp_dest[count];
sreddy47 5:802a7c5a4b27 1188 char temp[1] = {subAddress};
sreddy47 5:802a7c5a4b27 1189 i2c.write(address, temp, 1);
sreddy47 5:802a7c5a4b27 1190 i2c.read(address, temp_dest, count);
sreddy47 5:802a7c5a4b27 1191
sreddy47 5:802a7c5a4b27 1192 //i2c doesn't take uint8_ts, but rather chars so do this nasty af conversion
sreddy47 5:802a7c5a4b27 1193 for (i=0; i < count; i++) {
sreddy47 5:802a7c5a4b27 1194 dest[i] = temp_dest[i];
sreddy47 5:802a7c5a4b27 1195 }
sreddy47 5:802a7c5a4b27 1196 return count;
sreddy47 5:802a7c5a4b27 1197 }