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