ECE_4180_Project / Mbed 2 deprecated Farkle_main

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Committer:
jwalker366
Date:
Tue Nov 30 18:58:54 2021 +0000
Revision:
0:09b7e6aa75a9
for sub;

Who changed what in which revision?

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