Library for driving the MMA8452 accelerometer over I2C

Dependents:   MMA8452_Test MMA8452_Demo Dualing_Tanks IMU-Controlled_MP3_Player ... more

Here is a simple example:

#include "mbed.h"
#include "MMA8452.h"

int main() {
   Serial pc(USBTX,USBRX);
   pc.baud(115200);
   double x = 0, y = 0, z = 0;

   MMA8452 acc(p28, p27, 40000);
   acc.setBitDepth(MMA8452::BIT_DEPTH_12);
   acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
   acc.setDataRate(MMA8452::RATE_100);
   
   while(1) {
      if(!acc.isXYZReady()) {
         wait(0.01);
         continue;
      }
      acc.readXYZGravity(&x,&y,&z);
      pc.printf("Gravities: %lf %lf %lf\r\n",x,y,z);
   }
}

An easy way to test that this actually works is to run the loop above and hold the MMA8452 parallel to the ground along the respective axis (and upsidedown in each axis). You will see 1G on the respective axis and 0G on the others.

Committer:
ashleymills
Date:
Wed Mar 05 15:04:13 2014 +0000
Revision:
13:4bd8b4cd479d
Parent:
12:172540ff6b8b
Child:
14:0602b45ca70f
Has routines to read raw, counts, and gravity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 10:ca9ba7ad4e94 1 // Author: Nicholas Herriot, Ashley Mills
nherriot 0:bcf2aa85d7f9 2 /* Copyright (c) 2013 Vodafone, MIT License
nherriot 0:bcf2aa85d7f9 3 *
nherriot 0:bcf2aa85d7f9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
nherriot 0:bcf2aa85d7f9 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
nherriot 0:bcf2aa85d7f9 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
nherriot 0:bcf2aa85d7f9 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
nherriot 0:bcf2aa85d7f9 8 * furnished to do so, subject to the following conditions:
nherriot 0:bcf2aa85d7f9 9 *
nherriot 0:bcf2aa85d7f9 10 * The above copyright notice and this permission notice shall be included in all copies or
nherriot 0:bcf2aa85d7f9 11 * substantial portions of the Software.
nherriot 0:bcf2aa85d7f9 12 *
nherriot 0:bcf2aa85d7f9 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
nherriot 0:bcf2aa85d7f9 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
nherriot 0:bcf2aa85d7f9 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
nherriot 0:bcf2aa85d7f9 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nherriot 0:bcf2aa85d7f9 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nherriot 0:bcf2aa85d7f9 18 */
nherriot 0:bcf2aa85d7f9 19
nherriot 0:bcf2aa85d7f9 20 // the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
nherriot 0:bcf2aa85d7f9 21 // see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf
nherriot 0:bcf2aa85d7f9 22
ashleymills 11:dfd1e0afcb7b 23
ashleymills 11:dfd1e0afcb7b 24 #define DBG(...) pc.printf(__VA_ARGS__); pc.printf("\r\n");
ashleymills 11:dfd1e0afcb7b 25 #define DBGX(...) pc.print(__VA_ARGS__);
ashleymills 11:dfd1e0afcb7b 26
nherriot 0:bcf2aa85d7f9 27 //#define SA0 1
nherriot 0:bcf2aa85d7f9 28 //#if SA0
nherriot 0:bcf2aa85d7f9 29 //#define MMA8452_ADDRESS 0x3A // SA0 is high, 0x1C if low - it should be 0x1D, but we shift now to save shifting in the code
nherriot 0:bcf2aa85d7f9 30 //#else
nherriot 0:bcf2aa85d7f9 31 //#define MMA8452_ADDRESS 0x1C
nherriot 0:bcf2aa85d7f9 32 //#endif
nherriot 0:bcf2aa85d7f9 33
nherriot 0:bcf2aa85d7f9 34 //#ifndef MBED_MMA8452
nherriot 0:bcf2aa85d7f9 35
nherriot 0:bcf2aa85d7f9 36 //#define MBED_MMA8452
nherriot 0:bcf2aa85d7f9 37
nherriot 0:bcf2aa85d7f9 38 #include "mbed.h"
nherriot 0:bcf2aa85d7f9 39
nherriot 0:bcf2aa85d7f9 40 /** Accelerometer MMA8452 class
nherriot 0:bcf2aa85d7f9 41 *
nherriot 0:bcf2aa85d7f9 42 * Example:
nherriot 0:bcf2aa85d7f9 43 * @code
nherriot 0:bcf2aa85d7f9 44 *
nherriot 0:bcf2aa85d7f9 45 * #include "mbed.h"
nherriot 0:bcf2aa85d7f9 46 * #include "MMA8452.h"
nherriot 0:bcf2aa85d7f9 47 *
nherriot 0:bcf2aa85d7f9 48 *
nherriot 0:bcf2aa85d7f9 49 * Accelerometer_MMA8452 Acc(p28, p27);
nherriot 0:bcf2aa85d7f9 50 * serial pc(USBTX, USBRX);
nherriot 0:bcf2aa85d7f9 51 *
nherriot 0:bcf2aa85d7f9 52 * int main()
nherriot 0:bcf2aa85d7f9 53 * {
nherriot 0:bcf2aa85d7f9 54 * Acc.init();
nherriot 0:bcf2aa85d7f9 55 * while(1)
nherriot 0:bcf2aa85d7f9 56 * {
nherriot 0:bcf2aa85d7f9 57 * int x=0, y=0, z=0;
nherriot 0:bcf2aa85d7f9 58 * Acc.read_Tilt(&x, &y, &z);
nherriot 0:bcf2aa85d7f9 59 * pc.printf("Tilt x: %2.2f degree \n", x); // Print the tilt orientation of the X axis
nherriot 0:bcf2aa85d7f9 60 * pc.printf("Tilt y: %2.2f degree \n", y); // Print the tilt orientation of the Y axis
nherriot 0:bcf2aa85d7f9 61 * pc.printf("Tilt z: %2.2f degree \n", z); // Print the tilt orientation of the Z axis
nherriot 0:bcf2aa85d7f9 62 * wait(1);
nherriot 0:bcf2aa85d7f9 63 * }
nherriot 0:bcf2aa85d7f9 64 * }
nherriot 0:bcf2aa85d7f9 65 * @endcode
nherriot 0:bcf2aa85d7f9 66 */
nherriot 0:bcf2aa85d7f9 67
nherriot 0:bcf2aa85d7f9 68 // More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
nherriot 0:bcf2aa85d7f9 69 #define SA0 1
nherriot 0:bcf2aa85d7f9 70 #if SA0
ashleymills 11:dfd1e0afcb7b 71 #define MMA8452_ADDRESS 0x3A // 0x1D<<1 // SA0 is high, 0x1C if low -
nherriot 0:bcf2aa85d7f9 72 #else
ashleymills 11:dfd1e0afcb7b 73 #define MMA8452_ADDRESS 0x38 // 0x1C<<1
nherriot 0:bcf2aa85d7f9 74 #endif
nherriot 0:bcf2aa85d7f9 75
nherriot 0:bcf2aa85d7f9 76 // Register descriptions found in section 6 of pdf
ashleymills 12:172540ff6b8b 77 #define MMA8452_STATUS 0x00 // Type 'read' : Status of the data registers
ashleymills 12:172540ff6b8b 78 #define MMA8452_OUT_X_MSB 0x01 // Type 'read' : x axis - MSB of 2 byte sample
ashleymills 12:172540ff6b8b 79 #define MMA8452_OUT_X_LSB 0x02 // Type 'read' : x axis - LSB of 2 byte sample
ashleymills 12:172540ff6b8b 80 #define MMA8452_OUT_Y_MSB 0x03 // Type 'read' : y axis - MSB of 2 byte sample
ashleymills 12:172540ff6b8b 81 #define MMA8452_OUT_Y_LSB 0x04 // Type 'read' : y axis - LSB of 2 byte sample
ashleymills 12:172540ff6b8b 82 #define MMA8452_OUT_Z_MSB 0x05 // Type 'read' : z axis - MSB of 2 byte sample
ashleymills 12:172540ff6b8b 83 #define MMA8452_OUT_Z_LSB 0x06 // Type 'read' : z axis - LSB of 2 byte sample
nherriot 0:bcf2aa85d7f9 84
ashleymills 12:172540ff6b8b 85 // register definitions
ashleymills 12:172540ff6b8b 86 #define MMA8452_XYZ_DATA_CFG 0x0E
ashleymills 12:172540ff6b8b 87
ashleymills 12:172540ff6b8b 88 #define MMA8452_SYSMOD 0x0B // Type 'read' : This tells you if device is active, sleep or standy 0x00=STANDBY 0x01=WAKE 0x02=SLEEP
ashleymills 12:172540ff6b8b 89 #define MMA8452_WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A
nherriot 0:bcf2aa85d7f9 90
ashleymills 12:172540ff6b8b 91 #define MMA8452_PL_STATUS 0x10 // Type 'read' : This shows portrait landscape mode orientation
ashleymills 12:172540ff6b8b 92 #define MMA8452_PL_CFG 0x11 // Type 'read/write' : This allows portrait landscape configuration
ashleymills 12:172540ff6b8b 93 #define MMA8452_PL_COUNT 0x12 // Type 'read' : This is the portraint landscape debounce counter
ashleymills 12:172540ff6b8b 94 #define MMA8452_PL_BF_ZCOMP 0x13 // Type 'read' :
ashleymills 12:172540ff6b8b 95 #define MMA8452_PL_THS_REG 0x14 // Type 'read' :
nherriot 0:bcf2aa85d7f9 96
ashleymills 12:172540ff6b8b 97 #define MMA8452_FF_MT_CFG 0X15 // Type 'read/write' : Freefaul motion functional block configuration
ashleymills 12:172540ff6b8b 98 #define MMA8452_FF_MT_SRC 0X16 // Type 'read' : Freefaul motion event source register
ashleymills 12:172540ff6b8b 99 #define MMA8452_FF_MT_THS 0X17 // Type 'read' : Freefaul motion threshold register
ashleymills 12:172540ff6b8b 100 #define MMA8452_FF_COUNT 0X18 // Type 'read' : Freefaul motion debouce counter
nherriot 0:bcf2aa85d7f9 101
ashleymills 12:172540ff6b8b 102 #define MMA8452_ASLP_COUNT 0x29 // Type 'read/write' : Counter settings for auto sleep
ashleymills 12:172540ff6b8b 103 #define MMA8452_CTRL_REG_1 0x2A // Type 'read/write' :
ashleymills 12:172540ff6b8b 104 #define MMA8452_CTRL_REG_2 0x2B // Type 'read/write' :
ashleymills 12:172540ff6b8b 105 #define MMA8452_CTRL_REG_3 0x2C // Type 'read/write' :
ashleymills 12:172540ff6b8b 106 #define MMA8452_CTRL_REG_4 0x2D // Type 'read/write' :
ashleymills 12:172540ff6b8b 107 #define MMA8452_CTRL_REG_5 0x2E // Type 'read/write' :
nherriot 0:bcf2aa85d7f9 108
nherriot 3:ffb0b1650ca2 109 // Defined in table 13 of the Freescale PDF
ashleymills 12:172540ff6b8b 110 /// xxx these all need to have better names
nherriot 3:ffb0b1650ca2 111 #define STANDBY 0x00 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 112 #define WAKE 0x01 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 113 #define SLEEP 0x02 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 114 #define ACTIVE 0x01 // Stage value returned and set in Control Register 1, it can be STANDBY=00, or ACTIVE=01
nherriot 0:bcf2aa85d7f9 115
nherriot 0:bcf2aa85d7f9 116 #define TILT_STATUS 0x03 // Tilt Status (Read only)
nherriot 0:bcf2aa85d7f9 117 #define SRST_STATUS 0x04 // Sample Rate Status Register (Read only)
nherriot 0:bcf2aa85d7f9 118 #define SPCNT_STATUS 0x05 // Sleep Count Register (Read/Write)
nherriot 0:bcf2aa85d7f9 119 #define INTSU_STATUS 0x06 // Interrupt Setup Register
nherriot 0:bcf2aa85d7f9 120 #define MODE_STATUS 0x07 // Mode Register (Read/Write)
nherriot 0:bcf2aa85d7f9 121 #define SR_STATUS 0x08 // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
nherriot 0:bcf2aa85d7f9 122 #define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write)
nherriot 0:bcf2aa85d7f9 123 #define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write)
nherriot 0:bcf2aa85d7f9 124
ashleymills 12:172540ff6b8b 125 // masks for enabling/disabling standby
ashleymills 11:dfd1e0afcb7b 126 #define MMA8452_ACTIVE_MASK 0x01
ashleymills 11:dfd1e0afcb7b 127 #define MMA8452_STANDBY_MASK 0xFE
ashleymills 11:dfd1e0afcb7b 128
ashleymills 12:172540ff6b8b 129 // mask for dynamic range reading and writing
ashleymills 11:dfd1e0afcb7b 130 #define MMA8452_DYNAMIC_RANGE_MASK 0xFC
ashleymills 11:dfd1e0afcb7b 131
ashleymills 12:172540ff6b8b 132 // mask and shift for data rate reading and writing
ashleymills 11:dfd1e0afcb7b 133 #define MMA8452_DATA_RATE_MASK 0xC7
ashleymills 11:dfd1e0afcb7b 134 #define MMA8452_DATA_RATE_MASK_SHIFT 0x03
ashleymills 11:dfd1e0afcb7b 135
ashleymills 12:172540ff6b8b 136 // mask and shift for general reading and writing
ashleymills 11:dfd1e0afcb7b 137 #define MMA8452_WRITE_MASK 0xFE
ashleymills 11:dfd1e0afcb7b 138 #define MMA8452_READ_MASK 0x01
ashleymills 11:dfd1e0afcb7b 139
ashleymills 12:172540ff6b8b 140 // mask and shift for bit depth reading and writing
ashleymills 11:dfd1e0afcb7b 141 #define MMA8452_BIT_DEPTH_MASK 0xFD
ashleymills 11:dfd1e0afcb7b 142 #define MMA8452_BIT_DEPTH_MASK_SHIFT 0x01
ashleymills 12:172540ff6b8b 143
ashleymills 12:172540ff6b8b 144 // status masks and shifts
ashleymills 12:172540ff6b8b 145 #define MMA8452_STATUS_ZYXDR_MASK 0x08
ashleymills 11:dfd1e0afcb7b 146
ashleymills 10:ca9ba7ad4e94 147 class MMA8452
nherriot 0:bcf2aa85d7f9 148 {
nherriot 0:bcf2aa85d7f9 149 public:
ashleymills 10:ca9ba7ad4e94 150
ashleymills 11:dfd1e0afcb7b 151 enum DynamicRange {
ashleymills 11:dfd1e0afcb7b 152 DYNAMIC_RANGE_2G=0x00,
ashleymills 11:dfd1e0afcb7b 153 DYNAMIC_RANGE_4G,
ashleymills 11:dfd1e0afcb7b 154 DYNAMIC_RANGE_8G,
ashleymills 11:dfd1e0afcb7b 155 DYNAMIC_RANGE_UNKNOWN
ashleymills 10:ca9ba7ad4e94 156 };
nherriot 0:bcf2aa85d7f9 157
ashleymills 10:ca9ba7ad4e94 158 enum BitDepth {
ashleymills 11:dfd1e0afcb7b 159 BIT_DEPTH_12=0x00,
ashleymills 12:172540ff6b8b 160 BIT_DEPTH_8, // 1 sets fast read mode, hence the inversion
ashleymills 12:172540ff6b8b 161 BIT_DEPTH_UNKNOWN
ashleymills 10:ca9ba7ad4e94 162 };
ashleymills 11:dfd1e0afcb7b 163
ashleymills 11:dfd1e0afcb7b 164 enum DataRateHz {
ashleymills 11:dfd1e0afcb7b 165 RATE_800=0x00,
ashleymills 11:dfd1e0afcb7b 166 RATE_400,
ashleymills 11:dfd1e0afcb7b 167 RATE_200,
ashleymills 11:dfd1e0afcb7b 168 RATE_100,
ashleymills 11:dfd1e0afcb7b 169 RATE_50,
ashleymills 11:dfd1e0afcb7b 170 RATE_12_5,
ashleymills 11:dfd1e0afcb7b 171 RATE_6_25,
ashleymills 11:dfd1e0afcb7b 172 RATE_1_563,
ashleymills 11:dfd1e0afcb7b 173 RATE_UNKNOWN
ashleymills 11:dfd1e0afcb7b 174 };
ashleymills 11:dfd1e0afcb7b 175
ashleymills 11:dfd1e0afcb7b 176 //void setDynamicRange(
nherriot 0:bcf2aa85d7f9 177
nherriot 0:bcf2aa85d7f9 178 /** Create an accelerometer object connected to the specified I2C object
nherriot 0:bcf2aa85d7f9 179 *
nherriot 0:bcf2aa85d7f9 180 * @param sda I2C data port
nherriot 0:bcf2aa85d7f9 181 * @param scl I2C8452 clock port
nherriot 0:bcf2aa85d7f9 182 *
nherriot 0:bcf2aa85d7f9 183 */
ashleymills 10:ca9ba7ad4e94 184 MMA8452(PinName sda, PinName scl, int frequency);
nherriot 1:ef026bf28798 185 //Accelerometer_MMA8452(PinName sda, PinName scl);
nherriot 0:bcf2aa85d7f9 186
nherriot 0:bcf2aa85d7f9 187 /** Destroys an MMA8452 object
nherriot 0:bcf2aa85d7f9 188 *
nherriot 0:bcf2aa85d7f9 189 */
ashleymills 10:ca9ba7ad4e94 190 ~MMA8452();
nherriot 3:ffb0b1650ca2 191
ashleymills 11:dfd1e0afcb7b 192 //int setOperationMode(OperationMode m);
nherriot 3:ffb0b1650ca2 193
nherriot 3:ffb0b1650ca2 194
nherriot 0:bcf2aa85d7f9 195 /** Activate the MMA8452 (required)
nherriot 1:ef026bf28798 196 * returns 0 for success in activating the chip
nherriot 1:ef026bf28798 197 * returns 1 for failure in activating the chip
nherriot 1:ef026bf28798 198 * -currrently no retries or waiting is done, this method tries 1 time the exits.
nherriot 3:ffb0b1650ca2 199 *
nherriot 3:ffb0b1650ca2 200 * This will set the device 'active' even if it's already active. It's just a way to force that state.
nherriot 1:ef026bf28798 201 */
nherriot 0:bcf2aa85d7f9 202 int activate();
nherriot 3:ffb0b1650ca2 203
nherriot 3:ffb0b1650ca2 204
nherriot 3:ffb0b1650ca2 205 /** Standby the MMA8452 (not required)
nherriot 3:ffb0b1650ca2 206 * returns 0 for success in activating the chip
nherriot 3:ffb0b1650ca2 207 * returns 1 for failure in activating the chip
nherriot 3:ffb0b1650ca2 208 * -currrently no retries or waiting is done, this method tries 1 time the exits.
nherriot 3:ffb0b1650ca2 209 *
nherriot 3:ffb0b1650ca2 210 * This will set the device 'standby' even if it's already in standby. It's just a way to force that state.
nherriot 3:ffb0b1650ca2 211 */
nherriot 3:ffb0b1650ca2 212 int standby();
nherriot 3:ffb0b1650ca2 213
nherriot 3:ffb0b1650ca2 214
nherriot 5:b3d0abd97e55 215 /** get_CTRL_Reg1 the MMA8452 (not required)
nherriot 5:b3d0abd97e55 216 * returns 0 for success in activating the chip
nherriot 5:b3d0abd97e55 217 * returns 1 for failure in activating the chip
nherriot 5:b3d0abd97e55 218 * -currrently no retries or waiting is done, this method tries 1 time the exits.
nherriot 5:b3d0abd97e55 219 *
nherriot 5:b3d0abd97e55 220 * This will return the state of the control register 1. This holds and sets values for auto wake, sleep mode
nherriot 5:b3d0abd97e55 221 * output data rate, fast read mode and active/standby. More info on 6.7 of pdf for MMA8452 Freescale doc.
nherriot 5:b3d0abd97e55 222 */
ashleymills 11:dfd1e0afcb7b 223 int get_CTRL_Reg1(char* dst);
nherriot 5:b3d0abd97e55 224
nherriot 0:bcf2aa85d7f9 225
nherriot 0:bcf2aa85d7f9 226 /** Initialization of device MMA8452 (required)
nherriot 0:bcf2aa85d7f9 227 */
nherriot 0:bcf2aa85d7f9 228 void init();
nherriot 0:bcf2aa85d7f9 229
nherriot 1:ef026bf28798 230 /** Read the device ID from the accelerometer
nherriot 1:ef026bf28798 231 *
nherriot 1:ef026bf28798 232 * @param *deviceID Value of device - is should be 0x2A
nherriot 1:ef026bf28798 233 * return 0 for success or
nherriot 1:ef026bf28798 234 * return 1 for failure.
nherriot 1:ef026bf28798 235 */
ashleymills 11:dfd1e0afcb7b 236 int getDeviceID(char* dst);
nherriot 0:bcf2aa85d7f9 237
ashleymills 12:172540ff6b8b 238 int getStatus(char* dst);
nherriot 3:ffb0b1650ca2 239
nherriot 0:bcf2aa85d7f9 240 /** Read the x register of the MMA8452
nherriot 0:bcf2aa85d7f9 241 *
nherriot 0:bcf2aa85d7f9 242 * @returns The value of x acceleration
nherriot 0:bcf2aa85d7f9 243 */
ashleymills 12:172540ff6b8b 244 int readRawX(char *dst);
ashleymills 12:172540ff6b8b 245
nherriot 0:bcf2aa85d7f9 246 /** Read the y register of the MMA8452
ashleymills 12:172540ff6b8b 247 * @param dst The destination buffer
ashleymills 12:172540ff6b8b 248 * @returns The value of y acceleration
ashleymills 12:172540ff6b8b 249 */
ashleymills 12:172540ff6b8b 250 int readRawY(char *dst);
nherriot 0:bcf2aa85d7f9 251
nherriot 0:bcf2aa85d7f9 252 /** Read the z register of the MMA8452
nherriot 0:bcf2aa85d7f9 253 *
nherriot 0:bcf2aa85d7f9 254 * @returns The value of z acceleration
nherriot 0:bcf2aa85d7f9 255 */
ashleymills 10:ca9ba7ad4e94 256 int readRawZ(char * zaxis);
nherriot 1:ef026bf28798 257
ashleymills 12:172540ff6b8b 258 /**
ashleymills 12:172540ff6b8b 259 * Read the x,y, and z registers of the MMA8452.
nherriot 1:ef026bf28798 260 *
ashleymills 12:172540ff6b8b 261 * @param dst The destination buffer. Note that this needs to be 3 bytes for
ashleymills 12:172540ff6b8b 262 * BIT_DEPTH_8 and 6 bytes for BIT_DEPTH_12. It is upto the caller to ensure this.
ashleymills 12:172540ff6b8b 263 * @return 0 for success, and 1 for failure
nherriot 1:ef026bf28798 264 */
ashleymills 13:4bd8b4cd479d 265 int readXYZRaw(char *dst);
ashleymills 13:4bd8b4cd479d 266 int readXYZCounts(int *x, int *y, int *z);
ashleymills 13:4bd8b4cd479d 267 int readXYZGravity(double *x, double *y, double *z);
ashleymills 12:172540ff6b8b 268
ashleymills 13:4bd8b4cd479d 269 int isXReady();
ashleymills 13:4bd8b4cd479d 270 int isYReady();
ashleymills 13:4bd8b4cd479d 271 int isZReady();
ashleymills 12:172540ff6b8b 272 int isXYZReady();
nherriot 0:bcf2aa85d7f9 273
ashleymills 12:172540ff6b8b 274 /** Read from specified MMA8452 register
ashleymills 12:172540ff6b8b 275 *
ashleymills 12:172540ff6b8b 276 * @param addr The internal registeraddress of the MMA8452
ashleymills 12:172540ff6b8b 277 * @return The value of the register
ashleymills 12:172540ff6b8b 278 */
ashleymills 11:dfd1e0afcb7b 279 int readRegister(char addr, char *dst);
ashleymills 11:dfd1e0afcb7b 280
ashleymills 11:dfd1e0afcb7b 281 int readRegister(char addr, char *dst, int nbytes);
nherriot 0:bcf2aa85d7f9 282
ashleymills 12:172540ff6b8b 283 /**
ashleymills 12:172540ff6b8b 284 * Write to the specified MMA8452 register.
nherriot 0:bcf2aa85d7f9 285 *
ashleymills 12:172540ff6b8b 286 * @param addr The internal register address
ashleymills 12:172540ff6b8b 287 * @param data Data to write
nherriot 0:bcf2aa85d7f9 288 */
ashleymills 11:dfd1e0afcb7b 289 int writeRegister(char addr, char data);
ashleymills 12:172540ff6b8b 290
ashleymills 12:172540ff6b8b 291 /**
ashleymills 12:172540ff6b8b 292 * Write a data buffer to the specified MMA8452 register.
ashleymills 12:172540ff6b8b 293 *
ashleymills 12:172540ff6b8b 294 * @param addr The internal register address
ashleymills 12:172540ff6b8b 295 * @param data Data buffer to write
ashleymills 12:172540ff6b8b 296 * @param nbytes The length of the data buffer to write
ashleymills 12:172540ff6b8b 297 */
ashleymills 11:dfd1e0afcb7b 298 int writeRegister(char addr, char *data, int nbytes);
ashleymills 11:dfd1e0afcb7b 299
ashleymills 11:dfd1e0afcb7b 300 int logicalANDRegister(char addr, char mask);
ashleymills 11:dfd1e0afcb7b 301 int logicalORRegister(char addr, char mask);
ashleymills 11:dfd1e0afcb7b 302 int logicalXORRegister(char addr, char mask);
nherriot 0:bcf2aa85d7f9 303
ashleymills 11:dfd1e0afcb7b 304 int setDynamicRange(DynamicRange range, int toggleActivation=1);
ashleymills 11:dfd1e0afcb7b 305 int setBitDepth(BitDepth depth, int toggleActivation=1);
ashleymills 11:dfd1e0afcb7b 306 int setDataRate(DataRateHz dataRate, int toggleActivation=1);
ashleymills 11:dfd1e0afcb7b 307 DynamicRange getDynamicRange();
ashleymills 11:dfd1e0afcb7b 308 DataRateHz getDataRate();
ashleymills 12:172540ff6b8b 309 BitDepth getBitDepth();
ashleymills 11:dfd1e0afcb7b 310
ashleymills 11:dfd1e0afcb7b 311 void debugRegister(char reg);
nherriot 0:bcf2aa85d7f9 312
nherriot 0:bcf2aa85d7f9 313 private:
ashleymills 10:ca9ba7ad4e94 314 int readRaw(char src, char *dst, int len);
ashleymills 11:dfd1e0afcb7b 315 int maskAndApplyRegister(char reg, char mask, char value, int toggleActivation);
ashleymills 13:4bd8b4cd479d 316
ashleymills 13:4bd8b4cd479d 317 int twelveBitToSigned(char *buf);
ashleymills 13:4bd8b4cd479d 318 int eightBitToSigned(char *buf);
ashleymills 13:4bd8b4cd479d 319 double convertCountToGravity(int count, int countsPerG);
ashleymills 8:89272163f395 320
ashleymills 11:dfd1e0afcb7b 321 I2C _i2c;
ashleymills 11:dfd1e0afcb7b 322 int _frequency;
ashleymills 6:f6bde04bf8be 323 int _readAddress;
ashleymills 6:f6bde04bf8be 324 int _writeAddress;
ashleymills 12:172540ff6b8b 325
ashleymills 12:172540ff6b8b 326 BitDepth _bitDepth;
ashleymills 13:4bd8b4cd479d 327 DynamicRange _dynamicRange;
nherriot 0:bcf2aa85d7f9 328
nherriot 0:bcf2aa85d7f9 329 };
nherriot 0:bcf2aa85d7f9 330
nherriot 0:bcf2aa85d7f9 331 //#endif