Lab4

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
ckaminsky7
Date:
Fri Oct 22 20:30:07 2021 +0000
Revision:
0:42967dd26360
Lab4 Farkle Game

Who changed what in which revision?

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