A simple asteroids-like game utilizing various Mbed-compatible sensors

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
sralph3
Date:
Thu Jan 03 19:56:27 2019 +0000
Revision:
8:137330cfe63d
Parent:
0:f2cc64948895
Jan 3, 2019

Who changed what in which revision?

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