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