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