ECE 2036 Project

Dependencies:   mbed wave_player 4DGL-uLCD-SE

Committer:
abraha2d
Date:
Thu Nov 21 16:10:57 2019 +0000
Revision:
2:2042f29de6b7
Parent:
0:cf4396614a79
Because other peeps is wanting dis...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rconnorlawson 0:cf4396614a79 1 #pragma once
rconnorlawson 0:cf4396614a79 2
rconnorlawson 0:cf4396614a79 3 // Authors: Ashley Mills, Nicholas Herriot
rconnorlawson 0:cf4396614a79 4 /* Copyright (c) 2013 Vodafone, MIT License
rconnorlawson 0:cf4396614a79 5 *
rconnorlawson 0:cf4396614a79 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
rconnorlawson 0:cf4396614a79 7 * and associated documentation files (the "Software"), to deal in the Software without restriction,
rconnorlawson 0:cf4396614a79 8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
rconnorlawson 0:cf4396614a79 9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
rconnorlawson 0:cf4396614a79 10 * furnished to do so, subject to the following conditions:
rconnorlawson 0:cf4396614a79 11 *
rconnorlawson 0:cf4396614a79 12 * The above copyright notice and this permission notice shall be included in all copies or
rconnorlawson 0:cf4396614a79 13 * substantial portions of the Software.
rconnorlawson 0:cf4396614a79 14 *
rconnorlawson 0:cf4396614a79 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
rconnorlawson 0:cf4396614a79 16 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
rconnorlawson 0:cf4396614a79 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
rconnorlawson 0:cf4396614a79 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rconnorlawson 0:cf4396614a79 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
rconnorlawson 0:cf4396614a79 20 */
rconnorlawson 0:cf4396614a79 21
rconnorlawson 0:cf4396614a79 22 // the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
rconnorlawson 0:cf4396614a79 23 // see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf
rconnorlawson 0:cf4396614a79 24
rconnorlawson 0:cf4396614a79 25 #include "mbed.h"
rconnorlawson 0:cf4396614a79 26
rconnorlawson 0:cf4396614a79 27 #define MMA8452_DEBUG 1
rconnorlawson 0:cf4396614a79 28
rconnorlawson 0:cf4396614a79 29 // More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
rconnorlawson 0:cf4396614a79 30 #define SA0 1
rconnorlawson 0:cf4396614a79 31 #if SA0
rconnorlawson 0:cf4396614a79 32 #define MMA8452_ADDRESS 0x3A // 0x1D<<1 // SA0 is high, 0x1C if low -
rconnorlawson 0:cf4396614a79 33 #else
rconnorlawson 0:cf4396614a79 34 #define MMA8452_ADDRESS 0x38 // 0x1C<<1
rconnorlawson 0:cf4396614a79 35 #endif
rconnorlawson 0:cf4396614a79 36
rconnorlawson 0:cf4396614a79 37 // Register descriptions found in section 6 of pdf
rconnorlawson 0:cf4396614a79 38 #define MMA8452_STATUS 0x00 // Type 'read' : Status of the data registers
rconnorlawson 0:cf4396614a79 39 #define MMA8452_OUT_X_MSB 0x01 // Type 'read' : x axis - MSB of 2 byte sample
rconnorlawson 0:cf4396614a79 40 #define MMA8452_OUT_X_LSB 0x02 // Type 'read' : x axis - LSB of 2 byte sample
rconnorlawson 0:cf4396614a79 41 #define MMA8452_OUT_Y_MSB 0x03 // Type 'read' : y axis - MSB of 2 byte sample
rconnorlawson 0:cf4396614a79 42 #define MMA8452_OUT_Y_LSB 0x04 // Type 'read' : y axis - LSB of 2 byte sample
rconnorlawson 0:cf4396614a79 43 #define MMA8452_OUT_Z_MSB 0x05 // Type 'read' : z axis - MSB of 2 byte sample
rconnorlawson 0:cf4396614a79 44 #define MMA8452_OUT_Z_LSB 0x06 // Type 'read' : z axis - LSB of 2 byte sample
rconnorlawson 0:cf4396614a79 45
rconnorlawson 0:cf4396614a79 46 // register definitions
rconnorlawson 0:cf4396614a79 47 #define MMA8452_XYZ_DATA_CFG 0x0E
rconnorlawson 0:cf4396614a79 48
rconnorlawson 0:cf4396614a79 49 #define MMA8452_SYSMOD 0x0B // Type 'read' : This tells you if device is active, sleep or standy 0x00=STANDBY 0x01=WAKE 0x02=SLEEP
rconnorlawson 0:cf4396614a79 50 #define MMA8452_WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A
rconnorlawson 0:cf4396614a79 51
rconnorlawson 0:cf4396614a79 52 #define MMA8452_PL_STATUS 0x10 // Type 'read' : This shows portrait landscape mode orientation
rconnorlawson 0:cf4396614a79 53 #define MMA8452_PL_CFG 0x11 // Type 'read/write' : This allows portrait landscape configuration
rconnorlawson 0:cf4396614a79 54 #define MMA8452_PL_COUNT 0x12 // Type 'read' : This is the portraint landscape debounce counter
rconnorlawson 0:cf4396614a79 55 #define MMA8452_PL_BF_ZCOMP 0x13 // Type 'read' :
rconnorlawson 0:cf4396614a79 56 #define MMA8452_PL_THS_REG 0x14 // Type 'read' :
rconnorlawson 0:cf4396614a79 57
rconnorlawson 0:cf4396614a79 58 #define MMA8452_FF_MT_CFG 0X15 // Type 'read/write' : Freefaul motion functional block configuration
rconnorlawson 0:cf4396614a79 59 #define MMA8452_FF_MT_SRC 0X16 // Type 'read' : Freefaul motion event source register
rconnorlawson 0:cf4396614a79 60 #define MMA8452_FF_MT_THS 0X17 // Type 'read' : Freefaul motion threshold register
rconnorlawson 0:cf4396614a79 61 #define MMA8452_FF_COUNT 0X18 // Type 'read' : Freefaul motion debouce counter
rconnorlawson 0:cf4396614a79 62
rconnorlawson 0:cf4396614a79 63 #define MMA8452_ASLP_COUNT 0x29 // Type 'read/write' : Counter settings for auto sleep
rconnorlawson 0:cf4396614a79 64 #define MMA8452_CTRL_REG_1 0x2A // Type 'read/write' :
rconnorlawson 0:cf4396614a79 65 #define MMA8452_CTRL_REG_2 0x2B // Type 'read/write' :
rconnorlawson 0:cf4396614a79 66 #define MMA8452_CTRL_REG_3 0x2C // Type 'read/write' :
rconnorlawson 0:cf4396614a79 67 #define MMA8452_CTRL_REG_4 0x2D // Type 'read/write' :
rconnorlawson 0:cf4396614a79 68 #define MMA8452_CTRL_REG_5 0x2E // Type 'read/write' :
rconnorlawson 0:cf4396614a79 69
rconnorlawson 0:cf4396614a79 70 // Defined in table 13 of the Freescale PDF
rconnorlawson 0:cf4396614a79 71 /// xxx these all need to have better names
rconnorlawson 0:cf4396614a79 72 #define STANDBY 0x00 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
rconnorlawson 0:cf4396614a79 73 #define WAKE 0x01 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
rconnorlawson 0:cf4396614a79 74 #define SLEEP 0x02 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
rconnorlawson 0:cf4396614a79 75 #define ACTIVE 0x01 // Stage value returned and set in Control Register 1, it can be STANDBY=00, or ACTIVE=01
rconnorlawson 0:cf4396614a79 76
rconnorlawson 0:cf4396614a79 77 #define TILT_STATUS 0x03 // Tilt Status (Read only)
rconnorlawson 0:cf4396614a79 78 #define SRST_STATUS 0x04 // Sample Rate Status Register (Read only)
rconnorlawson 0:cf4396614a79 79 #define SPCNT_STATUS 0x05 // Sleep Count Register (Read/Write)
rconnorlawson 0:cf4396614a79 80 #define INTSU_STATUS 0x06 // Interrupt Setup Register
rconnorlawson 0:cf4396614a79 81 #define MODE_STATUS 0x07 // Mode Register (Read/Write)
rconnorlawson 0:cf4396614a79 82 #define SR_STATUS 0x08 // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
rconnorlawson 0:cf4396614a79 83 #define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write)
rconnorlawson 0:cf4396614a79 84 #define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write)
rconnorlawson 0:cf4396614a79 85
rconnorlawson 0:cf4396614a79 86 // masks for enabling/disabling standby
rconnorlawson 0:cf4396614a79 87 #define MMA8452_ACTIVE_MASK 0x01
rconnorlawson 0:cf4396614a79 88 #define MMA8452_STANDBY_MASK 0xFE
rconnorlawson 0:cf4396614a79 89
rconnorlawson 0:cf4396614a79 90 // mask for dynamic range reading and writing
rconnorlawson 0:cf4396614a79 91 #define MMA8452_DYNAMIC_RANGE_MASK 0xFC
rconnorlawson 0:cf4396614a79 92
rconnorlawson 0:cf4396614a79 93 // mask and shift for data rate reading and writing
rconnorlawson 0:cf4396614a79 94 #define MMA8452_DATA_RATE_MASK 0xC7
rconnorlawson 0:cf4396614a79 95 #define MMA8452_DATA_RATE_MASK_SHIFT 0x03
rconnorlawson 0:cf4396614a79 96
rconnorlawson 0:cf4396614a79 97 // mask and shift for general reading and writing
rconnorlawson 0:cf4396614a79 98 #define MMA8452_WRITE_MASK 0xFE
rconnorlawson 0:cf4396614a79 99 #define MMA8452_READ_MASK 0x01
rconnorlawson 0:cf4396614a79 100
rconnorlawson 0:cf4396614a79 101 // mask and shift for bit depth reading and writing
rconnorlawson 0:cf4396614a79 102 #define MMA8452_BIT_DEPTH_MASK 0xFD
rconnorlawson 0:cf4396614a79 103 #define MMA8452_BIT_DEPTH_MASK_SHIFT 0x01
rconnorlawson 0:cf4396614a79 104
rconnorlawson 0:cf4396614a79 105 // status masks and shifts
rconnorlawson 0:cf4396614a79 106 #define MMA8452_STATUS_ZYXDR_MASK 0x08
rconnorlawson 0:cf4396614a79 107 #define MMA8452_STATUS_ZDR_MASK 0x04
rconnorlawson 0:cf4396614a79 108 #define MMA8452_STATUS_YDR_MASK 0x02
rconnorlawson 0:cf4396614a79 109 #define MMA8452_STATUS_XDR_MASK 0x01
rconnorlawson 0:cf4396614a79 110
rconnorlawson 0:cf4396614a79 111 /**
rconnorlawson 0:cf4396614a79 112 * Wrapper for the MMA8452 I2C driven accelerometer.
rconnorlawson 0:cf4396614a79 113 */
rconnorlawson 0:cf4396614a79 114 class MMA8452 {
rconnorlawson 0:cf4396614a79 115
rconnorlawson 0:cf4396614a79 116 public:
rconnorlawson 0:cf4396614a79 117
rconnorlawson 0:cf4396614a79 118 enum DynamicRange {
rconnorlawson 0:cf4396614a79 119 DYNAMIC_RANGE_2G=0x00,
rconnorlawson 0:cf4396614a79 120 DYNAMIC_RANGE_4G,
rconnorlawson 0:cf4396614a79 121 DYNAMIC_RANGE_8G,
rconnorlawson 0:cf4396614a79 122 DYNAMIC_RANGE_UNKNOWN
rconnorlawson 0:cf4396614a79 123 };
rconnorlawson 0:cf4396614a79 124
rconnorlawson 0:cf4396614a79 125 enum BitDepth {
rconnorlawson 0:cf4396614a79 126 BIT_DEPTH_12=0x00,
rconnorlawson 0:cf4396614a79 127 BIT_DEPTH_8, // 1 sets fast read mode, hence the inversion
rconnorlawson 0:cf4396614a79 128 BIT_DEPTH_UNKNOWN
rconnorlawson 0:cf4396614a79 129 };
rconnorlawson 0:cf4396614a79 130
rconnorlawson 0:cf4396614a79 131 enum DataRateHz {
rconnorlawson 0:cf4396614a79 132 RATE_800=0x00,
rconnorlawson 0:cf4396614a79 133 RATE_400,
rconnorlawson 0:cf4396614a79 134 RATE_200,
rconnorlawson 0:cf4396614a79 135 RATE_100,
rconnorlawson 0:cf4396614a79 136 RATE_50,
rconnorlawson 0:cf4396614a79 137 RATE_12_5,
rconnorlawson 0:cf4396614a79 138 RATE_6_25,
rconnorlawson 0:cf4396614a79 139 RATE_1_563,
rconnorlawson 0:cf4396614a79 140 RATE_UNKNOWN
rconnorlawson 0:cf4396614a79 141 };
rconnorlawson 0:cf4396614a79 142
rconnorlawson 0:cf4396614a79 143 /**
rconnorlawson 0:cf4396614a79 144 * Create an accelerometer object connected to the specified I2C pins.
rconnorlawson 0:cf4396614a79 145 *
rconnorlawson 0:cf4396614a79 146 * @param sda I2C data port
rconnorlawson 0:cf4396614a79 147 * @param scl I2C clock port
rconnorlawson 0:cf4396614a79 148 * @param frequency
rconnorlawson 0:cf4396614a79 149 *
rconnorlawson 0:cf4396614a79 150 */
rconnorlawson 0:cf4396614a79 151 MMA8452(PinName sda, PinName scl, int frequency);
rconnorlawson 0:cf4396614a79 152
rconnorlawson 0:cf4396614a79 153 /// Destructor
rconnorlawson 0:cf4396614a79 154 ~MMA8452();
rconnorlawson 0:cf4396614a79 155
rconnorlawson 0:cf4396614a79 156 /**
rconnorlawson 0:cf4396614a79 157 * Puts the MMA8452 in active mode.
rconnorlawson 0:cf4396614a79 158 * @return 0 on success, 1 on failure.
rconnorlawson 0:cf4396614a79 159 */
rconnorlawson 0:cf4396614a79 160 int activate();
rconnorlawson 0:cf4396614a79 161
rconnorlawson 0:cf4396614a79 162 /**
rconnorlawson 0:cf4396614a79 163 * Puts the MMA8452 in standby.
rconnorlawson 0:cf4396614a79 164 * @return 0 on success, 1 on failure.
rconnorlawson 0:cf4396614a79 165 */
rconnorlawson 0:cf4396614a79 166 int standby();
rconnorlawson 0:cf4396614a79 167
rconnorlawson 0:cf4396614a79 168 /**
rconnorlawson 0:cf4396614a79 169 * Read the device ID from the accelerometer (should be 0x2a)
rconnorlawson 0:cf4396614a79 170 *
rconnorlawson 0:cf4396614a79 171 * @param dst pointer to store the ID
rconnorlawson 0:cf4396614a79 172 * @return 0 on success, 1 on failure.
rconnorlawson 0:cf4396614a79 173 */
rconnorlawson 0:cf4396614a79 174 int getDeviceID(char* dst);
rconnorlawson 0:cf4396614a79 175
rconnorlawson 0:cf4396614a79 176 /**
rconnorlawson 0:cf4396614a79 177 * Read the MMA8452 status register.
rconnorlawson 0:cf4396614a79 178 *
rconnorlawson 0:cf4396614a79 179 * @param dst pointer to store the register value.
rconnorlawson 0:cf4396614a79 180 * @ return 0 on success, 1 on failure.
rconnorlawson 0:cf4396614a79 181 */
rconnorlawson 0:cf4396614a79 182 int getStatus(char* dst);
rconnorlawson 0:cf4396614a79 183
rconnorlawson 0:cf4396614a79 184 /**
rconnorlawson 0:cf4396614a79 185 * Read the raw x, y, an z registers of the MMA8452 in one operation.
rconnorlawson 0:cf4396614a79 186 * All three registers are read sequentially and stored in the provided buffer.
rconnorlawson 0:cf4396614a79 187 * The stored values are signed 2's complement left-aligned 12 or 8 bit integers.
rconnorlawson 0:cf4396614a79 188 *
rconnorlawson 0:cf4396614a79 189 * @param dst The destination buffer. Note that this needs to be 3 bytes for
rconnorlawson 0:cf4396614a79 190 * BIT_DEPTH_8 and 6 bytes for BIT_DEPTH_12. It is upto the caller to ensure this.
rconnorlawson 0:cf4396614a79 191 * @return 0 for success, and 1 for failure
rconnorlawson 0:cf4396614a79 192 * @sa setBitDepth
rconnorlawson 0:cf4396614a79 193 */
rconnorlawson 0:cf4396614a79 194 int readXYZRaw(char *dst);
rconnorlawson 0:cf4396614a79 195
rconnorlawson 0:cf4396614a79 196 /// Read the raw x register into the provided buffer. @sa readXYZRaw
rconnorlawson 0:cf4396614a79 197 int readXRaw(char *dst);
rconnorlawson 0:cf4396614a79 198 /// Read the raw y register into the provided buffer. @sa readXYZRaw
rconnorlawson 0:cf4396614a79 199 int readYRaw(char *dst);
rconnorlawson 0:cf4396614a79 200 /// Read the raw z register into the provided buffer. @sa readXYZRaw
rconnorlawson 0:cf4396614a79 201 int readZRaw(char *dst);
rconnorlawson 0:cf4396614a79 202
rconnorlawson 0:cf4396614a79 203 /**
rconnorlawson 0:cf4396614a79 204 * Read the x, y, and z signed counts of the MMA8452 axes.
rconnorlawson 0:cf4396614a79 205 *
rconnorlawson 0:cf4396614a79 206 * Count resolution is either 8 bits or 12 bits, and the range is either +-2G, +-4G, or +-8G
rconnorlawson 0:cf4396614a79 207 * depending on settings. The number of counts per G are 1024, 512, 256 for 2,4, and 8 G
rconnorlawson 0:cf4396614a79 208 * respectively at 12 bit resolution and 64, 32, 16 for 2, 4, and 8 G respectively at
rconnorlawson 0:cf4396614a79 209 * 8 bit resolution.
rconnorlawson 0:cf4396614a79 210 *
rconnorlawson 0:cf4396614a79 211 * This function queries the MMA8452 and returns the signed counts for each axes.
rconnorlawson 0:cf4396614a79 212 *
rconnorlawson 0:cf4396614a79 213 * @param x Pointer to integer to store x count
rconnorlawson 0:cf4396614a79 214 * @param y Pointer to integer to store y count
rconnorlawson 0:cf4396614a79 215 * @param z Pointer to integer to store z count
rconnorlawson 0:cf4396614a79 216 * @return 0 on success, 1 on failure.
rconnorlawson 0:cf4396614a79 217 */
rconnorlawson 0:cf4396614a79 218 int readXYZCounts(int *x, int *y, int *z);
rconnorlawson 0:cf4396614a79 219
rconnorlawson 0:cf4396614a79 220 /// Read the x axes signed count. @sa readXYZCounts
rconnorlawson 0:cf4396614a79 221 int readXCount(int *x);
rconnorlawson 0:cf4396614a79 222 /// Read the y axes signed count. @sa readXYZCounts
rconnorlawson 0:cf4396614a79 223 int readYCount(int *y);
rconnorlawson 0:cf4396614a79 224 /// Read the z axes signed count. @sa readXYZCounts
rconnorlawson 0:cf4396614a79 225 int readZCount(int *z);
rconnorlawson 0:cf4396614a79 226
rconnorlawson 0:cf4396614a79 227 /**
rconnorlawson 0:cf4396614a79 228 * Read the x, y, and z accelerations measured in G.
rconnorlawson 0:cf4396614a79 229 *
rconnorlawson 0:cf4396614a79 230 * The measurement resolution is controlled via setBitDepth which can
rconnorlawson 0:cf4396614a79 231 * be 8 or 12, and by setDynamicRange, which can be +-2G, +-4G, or +-8G.
rconnorlawson 0:cf4396614a79 232 *
rconnorlawson 0:cf4396614a79 233 * @param x A pointer to the double to store the x acceleration in.
rconnorlawson 0:cf4396614a79 234 * @param y A pointer to the double to store the y acceleration in.
rconnorlawson 0:cf4396614a79 235 * @param z A pointer to the double to store the z acceleration in.
rconnorlawson 0:cf4396614a79 236 *
rconnorlawson 0:cf4396614a79 237 * @return 0 on success, 1 on failure.
rconnorlawson 0:cf4396614a79 238 */
rconnorlawson 0:cf4396614a79 239 int readXYZGravity(double *x, double *y, double *z);
rconnorlawson 0:cf4396614a79 240
rconnorlawson 0:cf4396614a79 241 /// Read the x gravity in G into the provided double pointer. @sa readXYZGravity
rconnorlawson 0:cf4396614a79 242 int readXGravity(double *x);
rconnorlawson 0:cf4396614a79 243 /// Read the y gravity in G into the provided double pointer. @sa readXYZGravity
rconnorlawson 0:cf4396614a79 244 int readYGravity(double *y);
rconnorlawson 0:cf4396614a79 245 /// Read the z gravity in G into the provided double pointer. @sa readXYZGravity
rconnorlawson 0:cf4396614a79 246 int readZGravity(double *z);
rconnorlawson 0:cf4396614a79 247
rconnorlawson 0:cf4396614a79 248 /// Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.
rconnorlawson 0:cf4396614a79 249 int isXYZReady();
rconnorlawson 0:cf4396614a79 250 /// Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.
rconnorlawson 0:cf4396614a79 251 int isXReady();
rconnorlawson 0:cf4396614a79 252 /// Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.
rconnorlawson 0:cf4396614a79 253 int isYReady();
rconnorlawson 0:cf4396614a79 254 /// Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.
rconnorlawson 0:cf4396614a79 255 int isZReady();
rconnorlawson 0:cf4396614a79 256
rconnorlawson 0:cf4396614a79 257 /**
rconnorlawson 0:cf4396614a79 258 * Reads a single byte from the specified MMA8452 register.
rconnorlawson 0:cf4396614a79 259 *
rconnorlawson 0:cf4396614a79 260 * @param addr The internal register address.
rconnorlawson 0:cf4396614a79 261 * @param dst The destination buffer address.
rconnorlawson 0:cf4396614a79 262 * @return 1 on success, 0 on failure.
rconnorlawson 0:cf4396614a79 263 */
rconnorlawson 0:cf4396614a79 264 int readRegister(char addr, char *dst);
rconnorlawson 0:cf4396614a79 265
rconnorlawson 0:cf4396614a79 266 /**
rconnorlawson 0:cf4396614a79 267 * Reads n bytes from the specified MMA8452 register.
rconnorlawson 0:cf4396614a79 268 *
rconnorlawson 0:cf4396614a79 269 * @param addr The internal register address.
rconnorlawson 0:cf4396614a79 270 * @param dst The destination buffer address.
rconnorlawson 0:cf4396614a79 271 * @param nbytes The number of bytes to read.
rconnorlawson 0:cf4396614a79 272 * @return 1 on success, 0 on failure.
rconnorlawson 0:cf4396614a79 273 */
rconnorlawson 0:cf4396614a79 274 int readRegister(char addr, char *dst, int nbytes);
rconnorlawson 0:cf4396614a79 275
rconnorlawson 0:cf4396614a79 276 /**
rconnorlawson 0:cf4396614a79 277 * Write to the specified MMA8452 register.
rconnorlawson 0:cf4396614a79 278 *
rconnorlawson 0:cf4396614a79 279 * @param addr The internal register address
rconnorlawson 0:cf4396614a79 280 * @param data Data byte to write
rconnorlawson 0:cf4396614a79 281 */
rconnorlawson 0:cf4396614a79 282 int writeRegister(char addr, char data);
rconnorlawson 0:cf4396614a79 283
rconnorlawson 0:cf4396614a79 284 /**
rconnorlawson 0:cf4396614a79 285 * Write a data buffer to the specified MMA8452 register.
rconnorlawson 0:cf4396614a79 286 *
rconnorlawson 0:cf4396614a79 287 * @param addr The internal register address
rconnorlawson 0:cf4396614a79 288 * @param data Pointer to data buffer to write
rconnorlawson 0:cf4396614a79 289 * @param nbytes The length of the data buffer to write
rconnorlawson 0:cf4396614a79 290 */
rconnorlawson 0:cf4396614a79 291 int writeRegister(char addr, char *data, int nbytes);
rconnorlawson 0:cf4396614a79 292
rconnorlawson 0:cf4396614a79 293 int setDynamicRange(DynamicRange range, int toggleActivation=1);
rconnorlawson 0:cf4396614a79 294 int setBitDepth(BitDepth depth, int toggleActivation=1);
rconnorlawson 0:cf4396614a79 295 int setDataRate(DataRateHz dataRate, int toggleActivation=1);
rconnorlawson 0:cf4396614a79 296
rconnorlawson 0:cf4396614a79 297 DynamicRange getDynamicRange();
rconnorlawson 0:cf4396614a79 298 DataRateHz getDataRate();
rconnorlawson 0:cf4396614a79 299 BitDepth getBitDepth();
rconnorlawson 0:cf4396614a79 300
rconnorlawson 0:cf4396614a79 301 #ifdef MMA8452_DEBUG
rconnorlawson 0:cf4396614a79 302 void debugRegister(char reg);
rconnorlawson 0:cf4396614a79 303 #endif
rconnorlawson 0:cf4396614a79 304
rconnorlawson 0:cf4396614a79 305 private:
rconnorlawson 0:cf4396614a79 306 /**
rconnorlawson 0:cf4396614a79 307 * Reads the specified register, applies the mask with logical AND, logical ORs the value
rconnorlawson 0:cf4396614a79 308 * and writes back the result to the register. If toggleActivation is set to true then the
rconnorlawson 0:cf4396614a79 309 * device is put in standby before the operation, and activated at the end.
rconnorlawson 0:cf4396614a79 310 * Setting it to false is useful for setting options on a device that you want to keep in
rconnorlawson 0:cf4396614a79 311 * standby.
rconnorlawson 0:cf4396614a79 312 */
rconnorlawson 0:cf4396614a79 313 int maskAndApplyRegister(char reg, char mask, char value, int toggleActivation);
rconnorlawson 0:cf4396614a79 314
rconnorlawson 0:cf4396614a79 315 /// Reads the specified register, applies the mask with logical AND, and writes the result back.
rconnorlawson 0:cf4396614a79 316 int logicalANDRegister(char addr, char mask);
rconnorlawson 0:cf4396614a79 317 /// Reads the specified register, applies the mask with logical OR, and writes the result back.
rconnorlawson 0:cf4396614a79 318 int logicalORRegister(char addr, char mask);
rconnorlawson 0:cf4396614a79 319 /// Reads the specified register, applies the mask with logical XOR, and writes the result back.
rconnorlawson 0:cf4396614a79 320 int logicalXORRegister(char addr, char mask);
rconnorlawson 0:cf4396614a79 321
rconnorlawson 0:cf4396614a79 322 /// Converts the 12-bit two's complement number in buf to a signed integer. Returns the integer.
rconnorlawson 0:cf4396614a79 323 int twelveBitToSigned(char *buf);
rconnorlawson 0:cf4396614a79 324 /// Converts the 8-bit two's complement number in buf to a signed integer. Returns the integer.
rconnorlawson 0:cf4396614a79 325 int eightBitToSigned(char *buf);
rconnorlawson 0:cf4396614a79 326
rconnorlawson 0:cf4396614a79 327 /// Converts a count to a gravity using the supplied countsPerG. Returns the gravity.
rconnorlawson 0:cf4396614a79 328 double convertCountToGravity(int count, int countsPerG);
rconnorlawson 0:cf4396614a79 329
rconnorlawson 0:cf4396614a79 330 /// Reads the register at addr, applies the mask with logical AND, and returns the result.
rconnorlawson 0:cf4396614a79 331 char getMaskedRegister(int addr, char mask);
rconnorlawson 0:cf4396614a79 332
rconnorlawson 0:cf4396614a79 333 /// Get the counts per G for the current settings of bit depth and dynamic range.
rconnorlawson 0:cf4396614a79 334 int getCountsPerG();
rconnorlawson 0:cf4396614a79 335
rconnorlawson 0:cf4396614a79 336 I2C _i2c;
rconnorlawson 0:cf4396614a79 337 int _frequency;
rconnorlawson 0:cf4396614a79 338 int _readAddress;
rconnorlawson 0:cf4396614a79 339 int _writeAddress;
rconnorlawson 0:cf4396614a79 340
rconnorlawson 0:cf4396614a79 341 BitDepth _bitDepth;
rconnorlawson 0:cf4396614a79 342 DynamicRange _dynamicRange;
rconnorlawson 0:cf4396614a79 343 };