/*
  MMA8452Q.h -- interface for MMA accelerometer.
   Need to #include <Wire.h> in your main program to get access to I2C.

  This code is in some domain, probably.
 */
#ifndef MMA8452Q_h
#define MMA8452Q_h

#include "Arduino.h"

// Sets full-scale range to +/-2, 4, or 8g. Used to calc real g values.
#define GSCALE 2
// Output Data Rate from ODR set below
#define USEODR	ODR050		// 50Hz -- seems useful...
//#define USEODR	ODR800		// 800Hz seems too fast...

// System Output Data Rate Selection -- from spec sheet Table 55.
// Values for CTRL_REG1
// DR2 DR1 DR0	shifted into correct byte position
// 								Value	ODR Period
#define ODR800	(0x00 << 3)	// 0 0 0	800 Hz 1.25 ms
#define ODR400	(0x01 << 3)	// 0 0 1	400 Hz 2.5 ms
#define ODR200	(0x02 << 3)	// 0 1 0	200 Hz 5 ms
#define ODR100	(0x03 << 3)	// 0 1 1	100 Hz 10 ms
#define ODR050	(0x04 << 3)	// 1 0 0	50 Hz 20 ms
#define ODR012	(0x05 << 3)	// 1 0 1	12.5 Hz 80 ms
#define ODR006	(0x06 << 3)	// 1 1 0	6.25 Hz 160 ms
#define ODR001	(0x07 << 3)	// 1 1 1	1.56 Hz 640 ms


// externs from MMA8452Q.cpp library
class MMA8452Q
{
  public:
	// empty constructor
	MMA8452Q(void);
	// start Wire I2C lib and initialize Accel to default condition
	//  returns 1 if OK or 0 if not
	byte init(void);
	// read a raw data packet of 3 signed integers X,Y,Z
	//  return pointer to buffer or null on error
	int* readData( int *destination );
	// convert raw data to actual acceleration values
	void toAccelG( int *data, float *accel );
  private:
	void standby(void);
	void active(void);
	byte readRegisters(byte addressToRead, byte bytesToRead, byte * dest);
	byte readRegister(byte addressToRead);
	void writeRegister(byte addressToWrite, byte dataToWrite);
	byte wego;	// true if initialized correctly
};

#endif //MMA8452Q_h
