Lab 6 for ECE 2036,a lame version of Galiga that needs to have the bullet fixed, but focuses on the concept of polymorphism

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
nasiromar
Date:
Wed Apr 27 05:31:20 2022 +0000
Revision:
0:660af2d0e42d
Lame Game Lab 6

Who changed what in which revision?

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