I think there was a bug with debug mode

Dependents:   Generic_Platformer

Committer:
Yehowshua
Date:
Sat Mar 16 05:24:59 2019 +0000
Revision:
22:62055aa5fd8a
Parent:
21:a92a632a0cc7
added inputs and associated libraries

Who changed what in which revision?

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