WITB2

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
Nydrel
Date:
Thu Nov 29 18:37:00 2018 +0000
Revision:
5:ed52ac77cdf9
Parent:
0:5e90b8577fef
witb 2;

Who changed what in which revision?

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