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