Dependencies:   4DGL-uLCD-SE PinDetect mbed

Committer:
Philipjp
Date:
Tue Nov 14 18:03:31 2017 +0000
Revision:
0:73375f76583c
Asteroid

Who changed what in which revision?

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