LAME

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem PinDetect

Committer:
kmillion3
Date:
Sun May 01 22:17:57 2022 +0000
Revision:
5:05f7a84d0078
LAME;

Who changed what in which revision?

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