2036 mbed lab4

Dependencies:   4DGL-uLCD-SE PinDetect

Committer:
lhanks02
Date:
Mon Mar 28 18:44:30 2022 +0000
Revision:
0:bbda88bee65a
lab4

Who changed what in which revision?

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