Lab 2 for ECE 2036 (work in progress)

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem PinDetect

Committer:
jmcmath
Date:
Mon Feb 11 04:31:13 2019 +0000
Revision:
0:f10415100c39
work in progress

Who changed what in which revision?

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