/* Copyright (c) 2010-2011 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef MMA8451Q_H
#define MMA8451Q_H
#include "mbed.h"


#define ACCEL_I2C_ADDRESS 0x1D<<1
#define REG_STATUS        0x00
#define REG_F_SETUP_FIFO  0x09
#define REG_TRIG_CFG      0x0A
#define REG_XYZ_DATA_CFG  0x0E
#define REG_WHO_AM_I      0x0D
#define REG_PULSE_CFG     0x21
#define REG_PULSE_THSX    0x23
#define REG_PULSE_THSY    0x24
#define REG_PULSE_THSZ    0x25
#define REG_PULSE_TMLT    0x26
#define REG_PULSE_LTCY    0x27
#define REG_CTRL_REG1     0x2A
#define REG_CTRL_REG2     0x2B
#define REG_CTRL_REG3     0x2C
#define REG_CTRL_REG4     0x2D
#define REG_CTRL_REG5     0x2E
#define REG_OUT_X_MSB     0x01
#define REG_OUT_Y_MSB     0x03
#define REG_OUT_Z_MSB     0x05

#define UINT14_MAX        16383

extern DigitalOut test;
extern uint8_t buffer[10][192]; 
extern uint8_t ind1 , testX ;
class MMA8451Q
{
public:
 
  MMA8451Q(PinName sda, PinName scl, int addr);

  ~MMA8451Q();              //d'tor

  uint8_t getWhoAmI(); 

  void isr(void);
  
  void init_100Hz_logger(void);
  
  void init_400Hz_logger(void);
  
  void init_800Hz_logger(void);
  
  void init_tap(void);
  uint8_t xyz[192]; // for storing samples
  
  bool drdy(void) 
  { 
    if(data_ready) 
    { data_ready = false; return true;} 
    else return false;
  }
    void writeRegs(uint8_t adr1, uint8_t adr2);

private:
    
  volatile bool data_ready;

  I2C m_i2c;
  int m_addr;
  void readRegs(int addr, uint8_t * data, int len);
  void writeRegs(uint8_t * data, int len);
};

#endif
