Andrew Shi / Mbed 2 deprecated Lab4

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
ashi31
Date:
Fri Oct 22 03:57:47 2021 +0000
Revision:
0:7d8ffdfdb16e
Lab4

Who changed what in which revision?

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