ECE 4180 Final Project MP3 player code

Dependencies:   mbed mbed-rtos wave_player_appbd 4DGL-uLCD-SE SDFileSystem PinDetect

Committer:
lfink6
Date:
Fri Dec 10 17:59:57 2021 +0000
Revision:
3:8fd82fb378d5
Parent:
0:57a32b7102e8
test again;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lfink6 0:57a32b7102e8 1 /******************************************************************************
lfink6 0:57a32b7102e8 2 SFE_LSM9DS1.h
lfink6 0:57a32b7102e8 3 SFE_LSM9DS1 Library Header File
lfink6 0:57a32b7102e8 4 Jim Lindblom @ SparkFun Electronics
lfink6 0:57a32b7102e8 5 Original Creation Date: February 27, 2015
lfink6 0:57a32b7102e8 6 https://github.com/sparkfun/LSM9DS1_Breakout
lfink6 0:57a32b7102e8 7
lfink6 0:57a32b7102e8 8 This file prototypes the LSM9DS1 class, implemented in SFE_LSM9DS1.cpp. In
lfink6 0:57a32b7102e8 9 addition, it defines every register in the LSM9DS1 (both the Gyro and Accel/
lfink6 0:57a32b7102e8 10 Magnetometer registers).
lfink6 0:57a32b7102e8 11
lfink6 0:57a32b7102e8 12 Development environment specifics:
lfink6 0:57a32b7102e8 13 IDE: Arduino 1.6.0
lfink6 0:57a32b7102e8 14 Hardware Platform: Arduino Uno
lfink6 0:57a32b7102e8 15 LSM9DS1 Breakout Version: 1.0
lfink6 0:57a32b7102e8 16
lfink6 0:57a32b7102e8 17 This code is beerware; if you see me (or any other SparkFun employee) at the
lfink6 0:57a32b7102e8 18 local, and you've found our code helpful, please buy us a round!
lfink6 0:57a32b7102e8 19
lfink6 0:57a32b7102e8 20 Distributed as-is; no warranty is given.
lfink6 0:57a32b7102e8 21 ******************************************************************************/
lfink6 0:57a32b7102e8 22 #ifndef __SparkFunLSM9DS1_H__
lfink6 0:57a32b7102e8 23 #define __SparkFunLSM9DS1_H__
lfink6 0:57a32b7102e8 24
lfink6 0:57a32b7102e8 25 //#if defined(ARDUINO) && ARDUINO >= 100
lfink6 0:57a32b7102e8 26 // #include "Arduino.h"
lfink6 0:57a32b7102e8 27 //#else
lfink6 0:57a32b7102e8 28 // #include "WProgram.h"
lfink6 0:57a32b7102e8 29 // #include "pins_arduino.h"
lfink6 0:57a32b7102e8 30 //#endif
lfink6 0:57a32b7102e8 31
lfink6 0:57a32b7102e8 32 #include "mbed.h"
lfink6 0:57a32b7102e8 33 #include <stdint.h>
lfink6 0:57a32b7102e8 34 #include "LSM9DS1_Registers.h"
lfink6 0:57a32b7102e8 35 #include "LSM9DS1_Types.h"
lfink6 0:57a32b7102e8 36
lfink6 0:57a32b7102e8 37 #define LSM9DS1_AG_ADDR(sa0) ((sa0) == 0 ? 0x6A : 0x6B)
lfink6 0:57a32b7102e8 38 #define LSM9DS1_M_ADDR(sa1) ((sa1) == 0 ? 0x1C : 0x1E)
lfink6 0:57a32b7102e8 39
lfink6 0:57a32b7102e8 40 enum lsm9ds1_axis {
lfink6 0:57a32b7102e8 41 X_AXIS,
lfink6 0:57a32b7102e8 42 Y_AXIS,
lfink6 0:57a32b7102e8 43 Z_AXIS,
lfink6 0:57a32b7102e8 44 ALL_AXIS
lfink6 0:57a32b7102e8 45 };
lfink6 0:57a32b7102e8 46
lfink6 0:57a32b7102e8 47 class LSM9DS1
lfink6 0:57a32b7102e8 48 {
lfink6 0:57a32b7102e8 49 public:
lfink6 0:57a32b7102e8 50 IMUSettings settings;
lfink6 0:57a32b7102e8 51
lfink6 0:57a32b7102e8 52 // We'll store the gyro, accel, and magnetometer readings in a series of
lfink6 0:57a32b7102e8 53 // public class variables. Each sensor gets three variables -- one for each
lfink6 0:57a32b7102e8 54 // axis. Call readGyro(), readAccel(), and readMag() first, before using
lfink6 0:57a32b7102e8 55 // these variables!
lfink6 0:57a32b7102e8 56 // These values are the RAW signed 16-bit readings from the sensors.
lfink6 0:57a32b7102e8 57 int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
lfink6 0:57a32b7102e8 58 int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
lfink6 0:57a32b7102e8 59 int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
lfink6 0:57a32b7102e8 60 int16_t temperature; // Chip temperature
lfink6 0:57a32b7102e8 61 float gBias[3], aBias[3], mBias[3];
lfink6 0:57a32b7102e8 62 int16_t gBiasRaw[3], aBiasRaw[3], mBiasRaw[3];
lfink6 0:57a32b7102e8 63
lfink6 0:57a32b7102e8 64 // LSM9DS1 -- LSM9DS1 class constructor
lfink6 0:57a32b7102e8 65 // The constructor will set up a handful of private variables, and set the
lfink6 0:57a32b7102e8 66 // communication mode as well.
lfink6 0:57a32b7102e8 67 /**Input:
lfink6 0:57a32b7102e8 68 * - interface = Either IMU_MODE_SPI or IMU_MODE_I2C, whichever you're using
lfink6 0:57a32b7102e8 69 * to talk to the IC.
lfink6 0:57a32b7102e8 70 * - xgAddr = If IMU_MODE_I2C, this is the I2C address of the accel/gyroscope.
lfink6 0:57a32b7102e8 71 * If IMU_MODE_SPI, this is the chip select pin of the gyro (CS_AG)
lfink6 0:57a32b7102e8 72 * - mAddr = If IMU_MODE_I2C, this is the I2C address of the magnetometer.
lfink6 0:57a32b7102e8 73 * If IMU_MODE_SPI, this is the cs pin of the magnetometer (CS_M)
lfink6 0:57a32b7102e8 74
lfink6 0:57a32b7102e8 75 */
lfink6 0:57a32b7102e8 76 LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr);
lfink6 0:57a32b7102e8 77 //LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
lfink6 0:57a32b7102e8 78 //LSM9DS1();
lfink6 0:57a32b7102e8 79
lfink6 0:57a32b7102e8 80
lfink6 0:57a32b7102e8 81 /** begin() -- Initialize the gyro, accelerometer, and magnetometer.
lfink6 0:57a32b7102e8 82 *This will set up the scale and output rate of each sensor. The values set
lfink6 0:57a32b7102e8 83 * in the IMUSettings struct will take effect after calling this function.
lfink6 0:57a32b7102e8 84 */
lfink6 0:57a32b7102e8 85 uint16_t begin();
lfink6 0:57a32b7102e8 86
lfink6 0:57a32b7102e8 87 void calibrate(bool autoCalc = true);
lfink6 0:57a32b7102e8 88 void calibrateMag(bool loadIn = true);
lfink6 0:57a32b7102e8 89 void magOffset(uint8_t axis, int16_t offset);
lfink6 0:57a32b7102e8 90
lfink6 0:57a32b7102e8 91 /** accelAvailable() -- Polls the accelerometer status register to check
lfink6 0:57a32b7102e8 92 * if new data is available.
lfink6 0:57a32b7102e8 93 * Output: 1 - New data available
lfink6 0:57a32b7102e8 94 * 0 - No new data available
lfink6 0:57a32b7102e8 95 */
lfink6 0:57a32b7102e8 96 uint8_t accelAvailable();
lfink6 0:57a32b7102e8 97
lfink6 0:57a32b7102e8 98 /** gyroAvailable() -- Polls the gyroscope status register to check
lfink6 0:57a32b7102e8 99 * if new data is available.
lfink6 0:57a32b7102e8 100 * Output: 1 - New data available
lfink6 0:57a32b7102e8 101 * 0 - No new data available
lfink6 0:57a32b7102e8 102 */
lfink6 0:57a32b7102e8 103 uint8_t gyroAvailable();
lfink6 0:57a32b7102e8 104
lfink6 0:57a32b7102e8 105 /** gyroAvailable() -- Polls the temperature status register to check
lfink6 0:57a32b7102e8 106 * if new data is available.
lfink6 0:57a32b7102e8 107 * Output: 1 - New data available
lfink6 0:57a32b7102e8 108 * 0 - No new data available
lfink6 0:57a32b7102e8 109 */
lfink6 0:57a32b7102e8 110 uint8_t tempAvailable();
lfink6 0:57a32b7102e8 111
lfink6 0:57a32b7102e8 112 /** magAvailable() -- Polls the accelerometer status register to check
lfink6 0:57a32b7102e8 113 * if new data is available.
lfink6 0:57a32b7102e8 114 * Input:
lfink6 0:57a32b7102e8 115 * - axis can be either X_AXIS, Y_AXIS, Z_AXIS, to check for new data
lfink6 0:57a32b7102e8 116 * on one specific axis. Or ALL_AXIS (default) to check for new data
lfink6 0:57a32b7102e8 117 * on all axes.
lfink6 0:57a32b7102e8 118 * Output: 1 - New data available
lfink6 0:57a32b7102e8 119 * 0 - No new data available
lfink6 0:57a32b7102e8 120 */
lfink6 0:57a32b7102e8 121 uint8_t magAvailable(lsm9ds1_axis axis = ALL_AXIS);
lfink6 0:57a32b7102e8 122
lfink6 0:57a32b7102e8 123 /** readGyro() -- Read the gyroscope output registers.
lfink6 0:57a32b7102e8 124 * This function will read all six gyroscope output registers.
lfink6 0:57a32b7102e8 125 * The readings are stored in the class' gx, gy, and gz variables. Read
lfink6 0:57a32b7102e8 126 * those _after_ calling readGyro().
lfink6 0:57a32b7102e8 127 */
lfink6 0:57a32b7102e8 128 void readGyro();
lfink6 0:57a32b7102e8 129
lfink6 0:57a32b7102e8 130 /** int16_t readGyro(axis) -- Read a specific axis of the gyroscope.
lfink6 0:57a32b7102e8 131 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
lfink6 0:57a32b7102e8 132 * Input:
lfink6 0:57a32b7102e8 133 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
lfink6 0:57a32b7102e8 134 * Output:
lfink6 0:57a32b7102e8 135 * A 16-bit signed integer with sensor data on requested axis.
lfink6 0:57a32b7102e8 136 */
lfink6 0:57a32b7102e8 137 int16_t readGyro(lsm9ds1_axis axis);
lfink6 0:57a32b7102e8 138
lfink6 0:57a32b7102e8 139 /** readAccel() -- Read the accelerometer output registers.
lfink6 0:57a32b7102e8 140 * This function will read all six accelerometer output registers.
lfink6 0:57a32b7102e8 141 * The readings are stored in the class' ax, ay, and az variables. Read
lfink6 0:57a32b7102e8 142 * those _after_ calling readAccel().
lfink6 0:57a32b7102e8 143 */
lfink6 0:57a32b7102e8 144 void readAccel();
lfink6 0:57a32b7102e8 145
lfink6 0:57a32b7102e8 146 /** int16_t readAccel(axis) -- Read a specific axis of the accelerometer.
lfink6 0:57a32b7102e8 147 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
lfink6 0:57a32b7102e8 148 * Input:
lfink6 0:57a32b7102e8 149 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
lfink6 0:57a32b7102e8 150 * Output:
lfink6 0:57a32b7102e8 151 * A 16-bit signed integer with sensor data on requested axis.
lfink6 0:57a32b7102e8 152 */
lfink6 0:57a32b7102e8 153 int16_t readAccel(lsm9ds1_axis axis);
lfink6 0:57a32b7102e8 154
lfink6 0:57a32b7102e8 155 /** readMag() -- Read the magnetometer output registers.
lfink6 0:57a32b7102e8 156 * This function will read all six magnetometer output registers.
lfink6 0:57a32b7102e8 157 * The readings are stored in the class' mx, my, and mz variables. Read
lfink6 0:57a32b7102e8 158 * those _after_ calling readMag().
lfink6 0:57a32b7102e8 159 */
lfink6 0:57a32b7102e8 160 void readMag();
lfink6 0:57a32b7102e8 161
lfink6 0:57a32b7102e8 162 /** int16_t readMag(axis) -- Read a specific axis of the magnetometer.
lfink6 0:57a32b7102e8 163 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
lfink6 0:57a32b7102e8 164 * Input:
lfink6 0:57a32b7102e8 165 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
lfink6 0:57a32b7102e8 166 * Output:
lfink6 0:57a32b7102e8 167 * A 16-bit signed integer with sensor data on requested axis.
lfink6 0:57a32b7102e8 168 */
lfink6 0:57a32b7102e8 169 int16_t readMag(lsm9ds1_axis axis);
lfink6 0:57a32b7102e8 170
lfink6 0:57a32b7102e8 171 /** readTemp() -- Read the temperature output register.
lfink6 0:57a32b7102e8 172 * This function will read two temperature output registers.
lfink6 0:57a32b7102e8 173 * The combined readings are stored in the class' temperature variables. Read
lfink6 0:57a32b7102e8 174 * those _after_ calling readTemp().
lfink6 0:57a32b7102e8 175 */
lfink6 0:57a32b7102e8 176 void readTemp();
lfink6 0:57a32b7102e8 177
lfink6 0:57a32b7102e8 178 /** calcGyro() -- Convert from RAW signed 16-bit value to degrees per second
lfink6 0:57a32b7102e8 179 * This function reads in a signed 16-bit value and returns the scaled
lfink6 0:57a32b7102e8 180 * DPS. This function relies on gScale and gRes being correct.
lfink6 0:57a32b7102e8 181 * Input:
lfink6 0:57a32b7102e8 182 * - gyro = A signed 16-bit raw reading from the gyroscope.
lfink6 0:57a32b7102e8 183 */
lfink6 0:57a32b7102e8 184 float calcGyro(int16_t gyro);
lfink6 0:57a32b7102e8 185
lfink6 0:57a32b7102e8 186 /** calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's).
lfink6 0:57a32b7102e8 187 * This function reads in a signed 16-bit value and returns the scaled
lfink6 0:57a32b7102e8 188 * g's. This function relies on aScale and aRes being correct.
lfink6 0:57a32b7102e8 189 * Input:
lfink6 0:57a32b7102e8 190 * - accel = A signed 16-bit raw reading from the accelerometer.
lfink6 0:57a32b7102e8 191 */
lfink6 0:57a32b7102e8 192 float calcAccel(int16_t accel);
lfink6 0:57a32b7102e8 193
lfink6 0:57a32b7102e8 194 /** calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs)
lfink6 0:57a32b7102e8 195 * This function reads in a signed 16-bit value and returns the scaled
lfink6 0:57a32b7102e8 196 * Gs. This function relies on mScale and mRes being correct.
lfink6 0:57a32b7102e8 197 * Input:
lfink6 0:57a32b7102e8 198 * - mag = A signed 16-bit raw reading from the magnetometer.
lfink6 0:57a32b7102e8 199 */
lfink6 0:57a32b7102e8 200 float calcMag(int16_t mag);
lfink6 0:57a32b7102e8 201
lfink6 0:57a32b7102e8 202 /** setGyroScale() -- Set the full-scale range of the gyroscope.
lfink6 0:57a32b7102e8 203 * This function can be called to set the scale of the gyroscope to
lfink6 0:57a32b7102e8 204 * 245, 500, or 200 degrees per second.
lfink6 0:57a32b7102e8 205 * Input:
lfink6 0:57a32b7102e8 206 * - gScl = The desired gyroscope scale. Must be one of three possible
lfink6 0:57a32b7102e8 207 * values from the gyro_scale.
lfink6 0:57a32b7102e8 208 */
lfink6 0:57a32b7102e8 209 void setGyroScale(uint16_t gScl);
lfink6 0:57a32b7102e8 210
lfink6 0:57a32b7102e8 211 /** setAccelScale() -- Set the full-scale range of the accelerometer.
lfink6 0:57a32b7102e8 212 * This function can be called to set the scale of the accelerometer to
lfink6 0:57a32b7102e8 213 * 2, 4, 6, 8, or 16 g's.
lfink6 0:57a32b7102e8 214 * Input:
lfink6 0:57a32b7102e8 215 * - aScl = The desired accelerometer scale. Must be one of five possible
lfink6 0:57a32b7102e8 216 * values from the accel_scale.
lfink6 0:57a32b7102e8 217 */
lfink6 0:57a32b7102e8 218 void setAccelScale(uint8_t aScl);
lfink6 0:57a32b7102e8 219
lfink6 0:57a32b7102e8 220 /** setMagScale() -- Set the full-scale range of the magnetometer.
lfink6 0:57a32b7102e8 221 * This function can be called to set the scale of the magnetometer to
lfink6 0:57a32b7102e8 222 * 2, 4, 8, or 12 Gs.
lfink6 0:57a32b7102e8 223 * Input:
lfink6 0:57a32b7102e8 224 * - mScl = The desired magnetometer scale. Must be one of four possible
lfink6 0:57a32b7102e8 225 * values from the mag_scale.
lfink6 0:57a32b7102e8 226 */
lfink6 0:57a32b7102e8 227 void setMagScale(uint8_t mScl);
lfink6 0:57a32b7102e8 228
lfink6 0:57a32b7102e8 229 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
lfink6 0:57a32b7102e8 230 * Input:
lfink6 0:57a32b7102e8 231 * - gRate = The desired output rate and cutoff frequency of the gyro.
lfink6 0:57a32b7102e8 232 */
lfink6 0:57a32b7102e8 233 void setGyroODR(uint8_t gRate);
lfink6 0:57a32b7102e8 234
lfink6 0:57a32b7102e8 235 // setAccelODR() -- Set the output data rate of the accelerometer
lfink6 0:57a32b7102e8 236 // Input:
lfink6 0:57a32b7102e8 237 // - aRate = The desired output rate of the accel.
lfink6 0:57a32b7102e8 238 void setAccelODR(uint8_t aRate);
lfink6 0:57a32b7102e8 239
lfink6 0:57a32b7102e8 240 // setMagODR() -- Set the output data rate of the magnetometer
lfink6 0:57a32b7102e8 241 // Input:
lfink6 0:57a32b7102e8 242 // - mRate = The desired output rate of the mag.
lfink6 0:57a32b7102e8 243 void setMagODR(uint8_t mRate);
lfink6 0:57a32b7102e8 244
lfink6 0:57a32b7102e8 245 // configInactivity() -- Configure inactivity interrupt parameters
lfink6 0:57a32b7102e8 246 // Input:
lfink6 0:57a32b7102e8 247 // - duration = Inactivity duration - actual value depends on gyro ODR
lfink6 0:57a32b7102e8 248 // - threshold = Activity Threshold
lfink6 0:57a32b7102e8 249 // - sleepOn = Gyroscope operating mode during inactivity.
lfink6 0:57a32b7102e8 250 // true: gyroscope in sleep mode
lfink6 0:57a32b7102e8 251 // false: gyroscope in power-down
lfink6 0:57a32b7102e8 252 void configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn);
lfink6 0:57a32b7102e8 253
lfink6 0:57a32b7102e8 254 // configAccelInt() -- Configure Accelerometer Interrupt Generator
lfink6 0:57a32b7102e8 255 // Input:
lfink6 0:57a32b7102e8 256 // - generator = Interrupt axis/high-low events
lfink6 0:57a32b7102e8 257 // Any OR'd combination of ZHIE_XL, ZLIE_XL, YHIE_XL, YLIE_XL, XHIE_XL, XLIE_XL
lfink6 0:57a32b7102e8 258 // - andInterrupts = AND/OR combination of interrupt events
lfink6 0:57a32b7102e8 259 // true: AND combination
lfink6 0:57a32b7102e8 260 // false: OR combination
lfink6 0:57a32b7102e8 261 void configAccelInt(uint8_t generator, bool andInterrupts = false);
lfink6 0:57a32b7102e8 262
lfink6 0:57a32b7102e8 263 // configAccelThs() -- Configure the threshold of an accelereomter axis
lfink6 0:57a32b7102e8 264 // Input:
lfink6 0:57a32b7102e8 265 // - threshold = Interrupt threshold. Possible values: 0-255.
lfink6 0:57a32b7102e8 266 // Multiply by 128 to get the actual raw accel value.
lfink6 0:57a32b7102e8 267 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
lfink6 0:57a32b7102e8 268 // - duration = Duration value must be above or below threshold to trigger interrupt
lfink6 0:57a32b7102e8 269 // - wait = Wait function on duration counter
lfink6 0:57a32b7102e8 270 // true: Wait for duration samples before exiting interrupt
lfink6 0:57a32b7102e8 271 // false: Wait function off
lfink6 0:57a32b7102e8 272 void configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration = 0, bool wait = 0);
lfink6 0:57a32b7102e8 273
lfink6 0:57a32b7102e8 274 // configGyroInt() -- Configure Gyroscope Interrupt Generator
lfink6 0:57a32b7102e8 275 // Input:
lfink6 0:57a32b7102e8 276 // - generator = Interrupt axis/high-low events
lfink6 0:57a32b7102e8 277 // Any OR'd combination of ZHIE_G, ZLIE_G, YHIE_G, YLIE_G, XHIE_G, XLIE_G
lfink6 0:57a32b7102e8 278 // - aoi = AND/OR combination of interrupt events
lfink6 0:57a32b7102e8 279 // true: AND combination
lfink6 0:57a32b7102e8 280 // false: OR combination
lfink6 0:57a32b7102e8 281 // - latch: latch gyroscope interrupt request.
lfink6 0:57a32b7102e8 282 void configGyroInt(uint8_t generator, bool aoi, bool latch);
lfink6 0:57a32b7102e8 283
lfink6 0:57a32b7102e8 284 // configGyroThs() -- Configure the threshold of a gyroscope axis
lfink6 0:57a32b7102e8 285 // Input:
lfink6 0:57a32b7102e8 286 // - threshold = Interrupt threshold. Possible values: 0-0x7FF.
lfink6 0:57a32b7102e8 287 // Value is equivalent to raw gyroscope value.
lfink6 0:57a32b7102e8 288 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
lfink6 0:57a32b7102e8 289 // - duration = Duration value must be above or below threshold to trigger interrupt
lfink6 0:57a32b7102e8 290 // - wait = Wait function on duration counter
lfink6 0:57a32b7102e8 291 // true: Wait for duration samples before exiting interrupt
lfink6 0:57a32b7102e8 292 // false: Wait function off
lfink6 0:57a32b7102e8 293 void configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait);
lfink6 0:57a32b7102e8 294
lfink6 0:57a32b7102e8 295 // configInt() -- Configure INT1 or INT2 (Gyro and Accel Interrupts only)
lfink6 0:57a32b7102e8 296 // Input:
lfink6 0:57a32b7102e8 297 // - interrupt = Select INT1 or INT2
lfink6 0:57a32b7102e8 298 // Possible values: XG_INT1 or XG_INT2
lfink6 0:57a32b7102e8 299 // - generator = Or'd combination of interrupt generators.
lfink6 0:57a32b7102e8 300 // Possible values: INT_DRDY_XL, INT_DRDY_G, INT1_BOOT (INT1 only), INT2_DRDY_TEMP (INT2 only)
lfink6 0:57a32b7102e8 301 // INT_FTH, INT_OVR, INT_FSS5, INT_IG_XL (INT1 only), INT1_IG_G (INT1 only), INT2_INACT (INT2 only)
lfink6 0:57a32b7102e8 302 // - activeLow = Interrupt active configuration
lfink6 0:57a32b7102e8 303 // Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
lfink6 0:57a32b7102e8 304 // - pushPull = Push-pull or open drain interrupt configuration
lfink6 0:57a32b7102e8 305 // Can be either INT_PUSH_PULL or INT_OPEN_DRAIN
lfink6 0:57a32b7102e8 306 void configInt(interrupt_select interupt, uint8_t generator,
lfink6 0:57a32b7102e8 307 h_lactive activeLow = INT_ACTIVE_LOW, pp_od pushPull = INT_PUSH_PULL);
lfink6 0:57a32b7102e8 308
lfink6 0:57a32b7102e8 309 /** configMagInt() -- Configure Magnetometer Interrupt Generator
lfink6 0:57a32b7102e8 310 * Input:
lfink6 0:57a32b7102e8 311 * - generator = Interrupt axis/high-low events
lfink6 0:57a32b7102e8 312 * Any OR'd combination of ZIEN, YIEN, XIEN
lfink6 0:57a32b7102e8 313 * - activeLow = Interrupt active configuration
lfink6 0:57a32b7102e8 314 * Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
lfink6 0:57a32b7102e8 315 * - latch: latch gyroscope interrupt request.
lfink6 0:57a32b7102e8 316 */
lfink6 0:57a32b7102e8 317 void configMagInt(uint8_t generator, h_lactive activeLow, bool latch = true);
lfink6 0:57a32b7102e8 318
lfink6 0:57a32b7102e8 319 /** configMagThs() -- Configure the threshold of a gyroscope axis
lfink6 0:57a32b7102e8 320 * Input:
lfink6 0:57a32b7102e8 321 * - threshold = Interrupt threshold. Possible values: 0-0x7FF.
lfink6 0:57a32b7102e8 322 * Value is equivalent to raw magnetometer value.
lfink6 0:57a32b7102e8 323 */
lfink6 0:57a32b7102e8 324 void configMagThs(uint16_t threshold);
lfink6 0:57a32b7102e8 325
lfink6 0:57a32b7102e8 326 //! getGyroIntSrc() -- Get contents of Gyroscope interrupt source register
lfink6 0:57a32b7102e8 327 uint8_t getGyroIntSrc();
lfink6 0:57a32b7102e8 328
lfink6 0:57a32b7102e8 329 //! getGyroIntSrc() -- Get contents of accelerometer interrupt source register
lfink6 0:57a32b7102e8 330 uint8_t getAccelIntSrc();
lfink6 0:57a32b7102e8 331
lfink6 0:57a32b7102e8 332 //! getGyroIntSrc() -- Get contents of magnetometer interrupt source register
lfink6 0:57a32b7102e8 333 uint8_t getMagIntSrc();
lfink6 0:57a32b7102e8 334
lfink6 0:57a32b7102e8 335 //! getGyroIntSrc() -- Get status of inactivity interrupt
lfink6 0:57a32b7102e8 336 uint8_t getInactivity();
lfink6 0:57a32b7102e8 337
lfink6 0:57a32b7102e8 338 /** sleepGyro() -- Sleep or wake the gyroscope
lfink6 0:57a32b7102e8 339 * Input:
lfink6 0:57a32b7102e8 340 * - enable: True = sleep gyro. False = wake gyro.
lfink6 0:57a32b7102e8 341 */
lfink6 0:57a32b7102e8 342 void sleepGyro(bool enable = true);
lfink6 0:57a32b7102e8 343
lfink6 0:57a32b7102e8 344 /** enableFIFO() - Enable or disable the FIFO
lfink6 0:57a32b7102e8 345 * Input:
lfink6 0:57a32b7102e8 346 * - enable: true = enable, false = disable.
lfink6 0:57a32b7102e8 347 */
lfink6 0:57a32b7102e8 348 void enableFIFO(bool enable = true);
lfink6 0:57a32b7102e8 349
lfink6 0:57a32b7102e8 350 /** setFIFO() - Configure FIFO mode and Threshold
lfink6 0:57a32b7102e8 351 * Input:
lfink6 0:57a32b7102e8 352 * - fifoMode: Set FIFO mode to off, FIFO (stop when full), continuous, bypass
lfink6 0:57a32b7102e8 353 * Possible inputs: FIFO_OFF, FIFO_THS, FIFO_CONT_TRIGGER, FIFO_OFF_TRIGGER, FIFO_CONT
lfink6 0:57a32b7102e8 354 * - fifoThs: FIFO threshold level setting
lfink6 0:57a32b7102e8 355 * Any value from 0-0x1F is acceptable.
lfink6 0:57a32b7102e8 356 */
lfink6 0:57a32b7102e8 357 void setFIFO(fifoMode_type fifoMode, uint8_t fifoThs);
lfink6 0:57a32b7102e8 358
lfink6 0:57a32b7102e8 359 //! getFIFOSamples() - Get number of FIFO samples
lfink6 0:57a32b7102e8 360 uint8_t getFIFOSamples();
lfink6 0:57a32b7102e8 361
lfink6 0:57a32b7102e8 362
lfink6 0:57a32b7102e8 363 protected:
lfink6 0:57a32b7102e8 364 // x_mAddress and gAddress store the I2C address or SPI chip select pin
lfink6 0:57a32b7102e8 365 // for each sensor.
lfink6 0:57a32b7102e8 366 uint8_t _mAddress, _xgAddress;
lfink6 0:57a32b7102e8 367
lfink6 0:57a32b7102e8 368 // gRes, aRes, and mRes store the current resolution for each sensor.
lfink6 0:57a32b7102e8 369 // Units of these values would be DPS (or g's or Gs's) per ADC tick.
lfink6 0:57a32b7102e8 370 // This value is calculated as (sensor scale) / (2^15).
lfink6 0:57a32b7102e8 371 float gRes, aRes, mRes;
lfink6 0:57a32b7102e8 372
lfink6 0:57a32b7102e8 373 // _autoCalc keeps track of whether we're automatically subtracting off
lfink6 0:57a32b7102e8 374 // accelerometer and gyroscope bias calculated in calibrate().
lfink6 0:57a32b7102e8 375 bool _autoCalc;
lfink6 0:57a32b7102e8 376
lfink6 0:57a32b7102e8 377 // init() -- Sets up gyro, accel, and mag settings to default.
lfink6 0:57a32b7102e8 378 // - interface - Sets the interface mode (IMU_MODE_I2C or IMU_MODE_SPI)
lfink6 0:57a32b7102e8 379 // - xgAddr - Sets either the I2C address of the accel/gyro or SPI chip
lfink6 0:57a32b7102e8 380 // select pin connected to the CS_XG pin.
lfink6 0:57a32b7102e8 381 // - mAddr - Sets either the I2C address of the magnetometer or SPI chip
lfink6 0:57a32b7102e8 382 // select pin connected to the CS_M pin.
lfink6 0:57a32b7102e8 383 void init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
lfink6 0:57a32b7102e8 384
lfink6 0:57a32b7102e8 385 // initGyro() -- Sets up the gyroscope to begin reading.
lfink6 0:57a32b7102e8 386 // This function steps through all five gyroscope control registers.
lfink6 0:57a32b7102e8 387 // Upon exit, the following parameters will be set:
lfink6 0:57a32b7102e8 388 // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled.
lfink6 0:57a32b7102e8 389 // 95 Hz ODR, 12.5 Hz cutoff frequency.
lfink6 0:57a32b7102e8 390 // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
lfink6 0:57a32b7102e8 391 // set to 7.2 Hz (depends on ODR).
lfink6 0:57a32b7102e8 392 // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
lfink6 0:57a32b7102e8 393 // active high). Data-ready output enabled on DRDY_G.
lfink6 0:57a32b7102e8 394 // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
lfink6 0:57a32b7102e8 395 // address. Scale set to 245 DPS. SPI mode set to 4-wire.
lfink6 0:57a32b7102e8 396 // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
lfink6 0:57a32b7102e8 397 void initGyro();
lfink6 0:57a32b7102e8 398
lfink6 0:57a32b7102e8 399 // initAccel() -- Sets up the accelerometer to begin reading.
lfink6 0:57a32b7102e8 400 // This function steps through all accelerometer related control registers.
lfink6 0:57a32b7102e8 401 // Upon exit these registers will be set as:
lfink6 0:57a32b7102e8 402 // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
lfink6 0:57a32b7102e8 403 // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
lfink6 0:57a32b7102e8 404 // all axes enabled.
lfink6 0:57a32b7102e8 405 // - CTRL_REG2_XM = 0x00: 2g scale. 773 Hz anti-alias filter BW.
lfink6 0:57a32b7102e8 406 // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
lfink6 0:57a32b7102e8 407 void initAccel();
lfink6 0:57a32b7102e8 408
lfink6 0:57a32b7102e8 409 // initMag() -- Sets up the magnetometer to begin reading.
lfink6 0:57a32b7102e8 410 // This function steps through all magnetometer-related control registers.
lfink6 0:57a32b7102e8 411 // Upon exit these registers will be set as:
lfink6 0:57a32b7102e8 412 // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
lfink6 0:57a32b7102e8 413 // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
lfink6 0:57a32b7102e8 414 // requests don't latch. Temperature sensor disabled.
lfink6 0:57a32b7102e8 415 // - CTRL_REG6_XM = 0x00: 2 Gs scale.
lfink6 0:57a32b7102e8 416 // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
lfink6 0:57a32b7102e8 417 // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
lfink6 0:57a32b7102e8 418 void initMag();
lfink6 0:57a32b7102e8 419
lfink6 0:57a32b7102e8 420 // gReadByte() -- Reads a byte from a specified gyroscope register.
lfink6 0:57a32b7102e8 421 // Input:
lfink6 0:57a32b7102e8 422 // - subAddress = Register to be read from.
lfink6 0:57a32b7102e8 423 // Output:
lfink6 0:57a32b7102e8 424 // - An 8-bit value read from the requested address.
lfink6 0:57a32b7102e8 425 uint8_t mReadByte(uint8_t subAddress);
lfink6 0:57a32b7102e8 426
lfink6 0:57a32b7102e8 427 // gReadBytes() -- Reads a number of bytes -- beginning at an address
lfink6 0:57a32b7102e8 428 // and incrementing from there -- from the gyroscope.
lfink6 0:57a32b7102e8 429 // Input:
lfink6 0:57a32b7102e8 430 // - subAddress = Register to be read from.
lfink6 0:57a32b7102e8 431 // - * dest = A pointer to an array of uint8_t's. Values read will be
lfink6 0:57a32b7102e8 432 // stored in here on return.
lfink6 0:57a32b7102e8 433 // - count = The number of bytes to be read.
lfink6 0:57a32b7102e8 434 // Output: No value is returned, but the `dest` array will store
lfink6 0:57a32b7102e8 435 // the data read upon exit.
lfink6 0:57a32b7102e8 436 void mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
lfink6 0:57a32b7102e8 437
lfink6 0:57a32b7102e8 438 // gWriteByte() -- Write a byte to a register in the gyroscope.
lfink6 0:57a32b7102e8 439 // Input:
lfink6 0:57a32b7102e8 440 // - subAddress = Register to be written to.
lfink6 0:57a32b7102e8 441 // - data = data to be written to the register.
lfink6 0:57a32b7102e8 442 void mWriteByte(uint8_t subAddress, uint8_t data);
lfink6 0:57a32b7102e8 443
lfink6 0:57a32b7102e8 444 // xmReadByte() -- Read a byte from a register in the accel/mag sensor
lfink6 0:57a32b7102e8 445 // Input:
lfink6 0:57a32b7102e8 446 // - subAddress = Register to be read from.
lfink6 0:57a32b7102e8 447 // Output:
lfink6 0:57a32b7102e8 448 // - An 8-bit value read from the requested register.
lfink6 0:57a32b7102e8 449 uint8_t xgReadByte(uint8_t subAddress);
lfink6 0:57a32b7102e8 450
lfink6 0:57a32b7102e8 451 // xmReadBytes() -- Reads a number of bytes -- beginning at an address
lfink6 0:57a32b7102e8 452 // and incrementing from there -- from the accelerometer/magnetometer.
lfink6 0:57a32b7102e8 453 // Input:
lfink6 0:57a32b7102e8 454 // - subAddress = Register to be read from.
lfink6 0:57a32b7102e8 455 // - * dest = A pointer to an array of uint8_t's. Values read will be
lfink6 0:57a32b7102e8 456 // stored in here on return.
lfink6 0:57a32b7102e8 457 // - count = The number of bytes to be read.
lfink6 0:57a32b7102e8 458 // Output: No value is returned, but the `dest` array will store
lfink6 0:57a32b7102e8 459 // the data read upon exit.
lfink6 0:57a32b7102e8 460 void xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
lfink6 0:57a32b7102e8 461
lfink6 0:57a32b7102e8 462 // xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
lfink6 0:57a32b7102e8 463 // Input:
lfink6 0:57a32b7102e8 464 // - subAddress = Register to be written to.
lfink6 0:57a32b7102e8 465 // - data = data to be written to the register.
lfink6 0:57a32b7102e8 466 void xgWriteByte(uint8_t subAddress, uint8_t data);
lfink6 0:57a32b7102e8 467
lfink6 0:57a32b7102e8 468 // calcgRes() -- Calculate the resolution of the gyroscope.
lfink6 0:57a32b7102e8 469 // This function will set the value of the gRes variable. gScale must
lfink6 0:57a32b7102e8 470 // be set prior to calling this function.
lfink6 0:57a32b7102e8 471 void calcgRes();
lfink6 0:57a32b7102e8 472
lfink6 0:57a32b7102e8 473 // calcmRes() -- Calculate the resolution of the magnetometer.
lfink6 0:57a32b7102e8 474 // This function will set the value of the mRes variable. mScale must
lfink6 0:57a32b7102e8 475 // be set prior to calling this function.
lfink6 0:57a32b7102e8 476 void calcmRes();
lfink6 0:57a32b7102e8 477
lfink6 0:57a32b7102e8 478 // calcaRes() -- Calculate the resolution of the accelerometer.
lfink6 0:57a32b7102e8 479 // This function will set the value of the aRes variable. aScale must
lfink6 0:57a32b7102e8 480 // be set prior to calling this function.
lfink6 0:57a32b7102e8 481 void calcaRes();
lfink6 0:57a32b7102e8 482
lfink6 0:57a32b7102e8 483 //////////////////////
lfink6 0:57a32b7102e8 484 // Helper Functions //
lfink6 0:57a32b7102e8 485 //////////////////////
lfink6 0:57a32b7102e8 486 void constrainScales();
lfink6 0:57a32b7102e8 487
lfink6 0:57a32b7102e8 488 ///////////////////
lfink6 0:57a32b7102e8 489 // SPI Functions //
lfink6 0:57a32b7102e8 490 ///////////////////
lfink6 0:57a32b7102e8 491 // initSPI() -- Initialize the SPI hardware.
lfink6 0:57a32b7102e8 492 // This function will setup all SPI pins and related hardware.
lfink6 0:57a32b7102e8 493 void initSPI();
lfink6 0:57a32b7102e8 494
lfink6 0:57a32b7102e8 495 // SPIwriteByte() -- Write a byte out of SPI to a register in the device
lfink6 0:57a32b7102e8 496 // Input:
lfink6 0:57a32b7102e8 497 // - csPin = The chip select pin of the slave device.
lfink6 0:57a32b7102e8 498 // - subAddress = The register to be written to.
lfink6 0:57a32b7102e8 499 // - data = Byte to be written to the register.
lfink6 0:57a32b7102e8 500 void SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data);
lfink6 0:57a32b7102e8 501
lfink6 0:57a32b7102e8 502 // SPIreadByte() -- Read a single byte from a register over SPI.
lfink6 0:57a32b7102e8 503 // Input:
lfink6 0:57a32b7102e8 504 // - csPin = The chip select pin of the slave device.
lfink6 0:57a32b7102e8 505 // - subAddress = The register to be read from.
lfink6 0:57a32b7102e8 506 // Output:
lfink6 0:57a32b7102e8 507 // - The byte read from the requested address.
lfink6 0:57a32b7102e8 508 uint8_t SPIreadByte(uint8_t csPin, uint8_t subAddress);
lfink6 0:57a32b7102e8 509
lfink6 0:57a32b7102e8 510 // SPIreadBytes() -- Read a series of bytes, starting at a register via SPI
lfink6 0:57a32b7102e8 511 // Input:
lfink6 0:57a32b7102e8 512 // - csPin = The chip select pin of a slave device.
lfink6 0:57a32b7102e8 513 // - subAddress = The register to begin reading.
lfink6 0:57a32b7102e8 514 // - * dest = Pointer to an array where we'll store the readings.
lfink6 0:57a32b7102e8 515 // - count = Number of registers to be read.
lfink6 0:57a32b7102e8 516 // Output: No value is returned by the function, but the registers read are
lfink6 0:57a32b7102e8 517 // all stored in the *dest array given.
lfink6 0:57a32b7102e8 518 void SPIreadBytes(uint8_t csPin, uint8_t subAddress,
lfink6 0:57a32b7102e8 519 uint8_t * dest, uint8_t count);
lfink6 0:57a32b7102e8 520
lfink6 0:57a32b7102e8 521 ///////////////////
lfink6 0:57a32b7102e8 522 // I2C Functions //
lfink6 0:57a32b7102e8 523 ///////////////////
lfink6 0:57a32b7102e8 524 // initI2C() -- Initialize the I2C hardware.
lfink6 0:57a32b7102e8 525 // This function will setup all I2C pins and related hardware.
lfink6 0:57a32b7102e8 526 void initI2C();
lfink6 0:57a32b7102e8 527
lfink6 0:57a32b7102e8 528 // I2CwriteByte() -- Write a byte out of I2C to a register in the device
lfink6 0:57a32b7102e8 529 // Input:
lfink6 0:57a32b7102e8 530 // - address = The 7-bit I2C address of the slave device.
lfink6 0:57a32b7102e8 531 // - subAddress = The register to be written to.
lfink6 0:57a32b7102e8 532 // - data = Byte to be written to the register.
lfink6 0:57a32b7102e8 533 void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data);
lfink6 0:57a32b7102e8 534
lfink6 0:57a32b7102e8 535 // I2CreadByte() -- Read a single byte from a register over I2C.
lfink6 0:57a32b7102e8 536 // Input:
lfink6 0:57a32b7102e8 537 // - address = The 7-bit I2C address of the slave device.
lfink6 0:57a32b7102e8 538 // - subAddress = The register to be read from.
lfink6 0:57a32b7102e8 539 // Output:
lfink6 0:57a32b7102e8 540 // - The byte read from the requested address.
lfink6 0:57a32b7102e8 541 uint8_t I2CreadByte(uint8_t address, uint8_t subAddress);
lfink6 0:57a32b7102e8 542
lfink6 0:57a32b7102e8 543 // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI
lfink6 0:57a32b7102e8 544 // Input:
lfink6 0:57a32b7102e8 545 // - address = The 7-bit I2C address of the slave device.
lfink6 0:57a32b7102e8 546 // - subAddress = The register to begin reading.
lfink6 0:57a32b7102e8 547 // - * dest = Pointer to an array where we'll store the readings.
lfink6 0:57a32b7102e8 548 // - count = Number of registers to be read.
lfink6 0:57a32b7102e8 549 // Output: No value is returned by the function, but the registers read are
lfink6 0:57a32b7102e8 550 // all stored in the *dest array given.
lfink6 0:57a32b7102e8 551 uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
lfink6 0:57a32b7102e8 552
lfink6 0:57a32b7102e8 553 private:
lfink6 0:57a32b7102e8 554 I2C i2c;
lfink6 0:57a32b7102e8 555 };
lfink6 0:57a32b7102e8 556
lfink6 0:57a32b7102e8 557 #endif // SFE_LSM9DS1_H //