/**
  ******************************************************************************
  * @file 		Accelerometer3B
  * @author  	Innovati Team
  * @version  	V0.1.0
  * @date     	07/18/2011
  * @brief  	Main program body
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, Innovati SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 Innovati, Inc</center></h2>
  */

/* Includes ------------------------------------------------------------------*/
#include "arm32f.h"
#include "I2CRoutines.h"
#include "Accelerometer3B.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*----------------------command list------------------------------------------*/                                                                
/*----------------------------------------------------------------------------*/
Accelerometer3B::Accelerometer3B(uint8_t mySlaveID)
{
	SlaveID = mySlaveID;
    InitArmCommander();
}
void Accelerometer3B::GetXYZForce(int16_t &XForce, int16_t &YForce, int16_t &ZForce)
{
  __innoCommandBuff__[0] = 93;
  __innoNumByteToRead__ = 7;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  XForce = *((int16_t *)&__innoCommandBuff__);
	  YForce = *((int16_t *)&__innoCommandBuff__[2]);
	  ZForce = *((int16_t *)&__innoCommandBuff__[4]);
  }	  	        	  
}
void Accelerometer3B::GetAngle2D(uint16_t &Angle)
{
#ifdef __CHECK__
	char *ptr = (char *)0x1f0007B6;
	if(*ptr != 'C') return;
#endif  
  __innoCommandBuff__[0] = 95;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  	Angle = *((uint16_t *)&__innoCommandBuff__);
}
void Accelerometer3B::GetAngle3D (uint16_t &Angle1, uint8_t &Angle2)
{
  __innoCommandBuff__[0] = 99;
  __innoNumByteToRead__ = 4;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Angle1 = *((uint16_t *)&__innoCommandBuff__);   
	  Angle2 = __innoCommandBuff__[2];
  }	        
}
void Accelerometer3B::SetAngle2D(uint16_t Angle)
{
  __innoCommandBuff__[0] = 96;
  *((uint16_t *)&__innoCommandBuff__[2]) = Angle;    	 
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 6);
}
void Accelerometer3B::GetDevAngle2D(int16_t &Angle)
{
  __innoCommandBuff__[0] = 97;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
    Angle = *((int16_t *)&__innoCommandBuff__);
}
void Accelerometer3B::SetAngle3D(uint16_t Angle1, uint8_t Angle2)
{
  __innoCommandBuff__[0] = 100;
  *((uint16_t *)&__innoCommandBuff__[2]) = Angle1;  
  *((uint8_t *)&__innoCommandBuff__[4]) = Angle2;     	 
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 7);
}
void Accelerometer3B::GetDevAngle3D(int16_t &Angle1, char &Angle2)
{
  __innoCommandBuff__[0] = 101;
  __innoNumByteToRead__ = 4;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__)) 
  {
    Angle1 = *((int16_t *)&__innoCommandBuff__);
    Angle2 = *((int8_t *)&__innoCommandBuff__[2]);
  }
}
void Accelerometer3B::GetDevAngle3D(int16_t &Angle1, int8_t &Angle2)
{
  __innoCommandBuff__[0] = 101;
  __innoNumByteToRead__ = 4;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__)) 
  {
    Angle1 = *((int16_t *)&__innoCommandBuff__);
    Angle2 = *((int8_t *)&__innoCommandBuff__[2]);
  }
}
void Accelerometer3B::SetAxis2D(uint8_t Axis2D)
{
  __innoCommandBuff__[0] = 141;
  __innoCommandBuff__[2] = Axis2D;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 5);
}
void Accelerometer3B::GetXADVal(uint16_t &ADVal)
{
  __innoCommandBuff__[0] = 133;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  	ADVal = *((uint16_t *)&__innoCommandBuff__);
}
void Accelerometer3B::GetYADVal(uint16_t &ADVal)
{
  __innoCommandBuff__[0] = 134;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  	ADVal = *((uint16_t *)&__innoCommandBuff__);
}
void Accelerometer3B::GetZADVal(uint16_t &ADVal)
{
  __innoCommandBuff__[0] = 135;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  	ADVal = *((uint16_t *)&__innoCommandBuff__);
}

void Accelerometer3B::SetRefreshFreq(uint8_t RefreshFreq)
{
  __innoCommandBuff__[0] = 102;
  __innoCommandBuff__[2] = RefreshFreq;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 5);
}


void Accelerometer3B::GetXForce(int16_t& Force)
{
  __innoCommandBuff__[0] = 90;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((int16_t *)&__innoCommandBuff__[0]);
  }	
}
void Accelerometer3B::GetYForce(int16_t& Force)
{
  __innoCommandBuff__[0] = 91;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((int16_t *)&__innoCommandBuff__[0]);
  }	
}
void Accelerometer3B::GetZForce(int16_t& Force)
{
  __innoCommandBuff__[0] = 92;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((int16_t *)&__innoCommandBuff__[0]);
  }	
}
void Accelerometer3B::GetForce2D(int16_t& Force, int16_t& Angle)
{
  __innoCommandBuff__[0] = 94;
  __innoNumByteToRead__ = 5;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((uint16_t *)&__innoCommandBuff__);   
	  Angle = *((uint16_t *)&__innoCommandBuff__[2]); 
  }	
}
void Accelerometer3B::GetForce3D(int16_t& Force, int16_t& Angle1, int8_t& Angle2)
{
  __innoCommandBuff__[0] = 98;
  __innoNumByteToRead__ = 6;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((uint16_t *)&__innoCommandBuff__);   
      Angle1 = *((uint16_t *)&__innoCommandBuff__[2]);
	  Angle2 = __innoCommandBuff__[4];
  }	
}
void Accelerometer3B::GetRefreshFreq(uint8_t&Freq)
{
  __innoCommandBuff__[0] = 103;
  __innoNumByteToRead__ = 2;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Freq = __innoCommandBuff__[0];
  }	
}
void Accelerometer3B::SetTimerFreq(uint16_t Freq)
{	        
  __innoCommandBuff__[0] = 104;
  *((uint16_t *)&__innoCommandBuff__[2]) = Freq;    	 
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 6);
}
void Accelerometer3B::StartTimer(void)
{       
  __innoCommandBuff__[0] = 105;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 3);
}
void Accelerometer3B::StopTimer(void)
{       
  __innoCommandBuff__[0] = 106;	 
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 3);
}
uint8_t Accelerometer3B::GetTimerStatus(void)
{
  __innoCommandBuff__[0] = 107;
  __innoNumByteToRead__ = 2;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	 return(__innoCommandBuff__[0]); 
  }	        
  return 0;
}
void Accelerometer3B::SetXForceLimit(uint8_t Number, uint16_t Limit)
{
  __innoCommandBuff__[0] = 110;
  __innoCommandBuff__[2] = Number;
  *((int16_t *)&__innoCommandBuff__[3]) = Limit;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 6);  
}     
void Accelerometer3B::GetXForceLimit(uint8_t Number, uint16_t& Limit)
{
  __innoCommandBuff__[0] = 111;
  __innoCommandBuff__[2] = Number;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 5, &__innoNumByteToRead__))
  {
	  Limit = *((uint16_t *)&__innoCommandBuff__);   
  }	
}     
uint8_t Accelerometer3B::GetXForceLimitStatus(void)
{
  __innoCommandBuff__[0] = 112;
  __innoNumByteToRead__ = 2;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  return(__innoCommandBuff__[0]);   
  }	
  return 0;
}     
void Accelerometer3B::SetYForceLimit(uint8_t Number, uint16_t Limit)
{
  __innoCommandBuff__[0] = 115;
  __innoCommandBuff__[2] = Number;
  *((int16_t *)&__innoCommandBuff__[3]) = Limit;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 6);   
}     
void Accelerometer3B::GetYForceLimit(uint8_t Number, uint16_t& Limit)
{
  __innoCommandBuff__[0] = 116;
  __innoCommandBuff__[2] = Number;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 5, &__innoNumByteToRead__))
  {
	  Limit = *((uint16_t *)&__innoCommandBuff__);   
  }  
}     
uint8_t Accelerometer3B::GetYForceLimitStatus(void)
{
  __innoCommandBuff__[0] = 117;
  __innoNumByteToRead__ = 2;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  return(__innoCommandBuff__[0]);   
  }	
  return 0;  
}     
void Accelerometer3B::SetZForceLimit(uint8_t Number, uint16_t Limit)
{
  __innoCommandBuff__[0] = 120;
  __innoCommandBuff__[2] = Number;
  *((int16_t *)&__innoCommandBuff__[3]) = Limit;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 6);   
}      
void Accelerometer3B::GetZForceLimit(uint8_t Number, uint16_t& Limit)
{
  __innoCommandBuff__[0] = 121;
  __innoCommandBuff__[2] = Number;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 5, &__innoNumByteToRead__))
  {
	  Limit = *((uint16_t *)&__innoCommandBuff__);   
  }  
}     
uint8_t Accelerometer3B::GetZForceLimitStatus(void)
{
  __innoCommandBuff__[0] = 122;
  __innoNumByteToRead__ = 2;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  return(__innoCommandBuff__[0]);
  }	
  return 0;
}     
     
         
void Accelerometer3B::GetMaxXForce(int16_t& Force)
{
  __innoCommandBuff__[0] = 125;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((int16_t *)&__innoCommandBuff__);   
  }	
}      
void Accelerometer3B::GetMaxYForce(int16_t& Force)
{
  __innoCommandBuff__[0] = 126;
  __innoNumByteToRead__ = 3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	 Force = *((int16_t *)&__innoCommandBuff__);  
  }	
}         
void Accelerometer3B::GetMaxZForce(int16_t& Force)
{
  __innoCommandBuff__[0] = 127;
  __innoNumByteToRead__ =  3;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 3, &__innoNumByteToRead__))
  {
	  Force = *((uint16_t *)&__innoCommandBuff__);  
  }	
}         
      
void Accelerometer3B::ClrMaxXForce(void)
{
  __innoCommandBuff__[0] = 128;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 3);  
}     
void Accelerometer3B::ClrMaxYForce(void)
{
  __innoCommandBuff__[0] = 129;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 3);  
}      
void Accelerometer3B::ClrMaxZForce(void)
{
  __innoCommandBuff__[0] = 130;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 3);  
}      
      
void Accelerometer3B::SaveCalVal(uint8_t mode, uint16_t X0G, uint16_t X1G, uint16_t X_1G, 
                                               uint16_t Y0G, uint16_t Y1G, uint16_t Y_1G, 
                                               uint16_t Z0G, uint16_t Z1G, uint16_t Z_1G)
{
  __innoCommandBuff__[0] = 136;
  __innoCommandBuff__[2] = mode;
  *((uint16_t *)&__innoCommandBuff__[3]) = X0G;    
  *((uint16_t *)&__innoCommandBuff__[5]) = X1G;
  *((uint16_t *)&__innoCommandBuff__[7]) = X_1G;    
  *((uint16_t *)&__innoCommandBuff__[9]) = Y0G;
  *((uint16_t *)&__innoCommandBuff__[11]) = Y1G;
  *((uint16_t *)&__innoCommandBuff__[13]) = Y_1G;
  *((uint16_t *)&__innoCommandBuff__[15]) = Z0G;
  *((uint16_t *)&__innoCommandBuff__[17]) = Z1G;
  *((uint16_t *)&__innoCommandBuff__[19]) = Z_1G;    
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 21);  
}     
void Accelerometer3B::LoadCalVal(uint8_t mode, uint16_t& X0G, uint16_t& X1G, uint16_t& X_1G, 
                                               uint16_t& Y0G, uint16_t& Y1G, uint16_t& Y_1G, 
                                               uint16_t& Z0G, uint16_t& Z1G, uint16_t& Z_1G)
{
  __innoCommandBuff__[0] = 137;
  __innoCommandBuff__[2] = mode;    
  __innoNumByteToRead__ = 19;
  if(!__i2cReadBuffer(__innoCommandBuff__, SlaveID, 5, &__innoNumByteToRead__))
  {
    X0G = *((uint16_t *)&__innoCommandBuff__[0]);    
    X1G = *((uint16_t *)&__innoCommandBuff__[2]); 
    X_1G = *((uint16_t *)&__innoCommandBuff__[4]);     
    Y0G = *((uint16_t *)&__innoCommandBuff__[6]); 
    Y1G = *((uint16_t *)&__innoCommandBuff__[8]);
    Y_1G = *((uint16_t *)&__innoCommandBuff__[10]);
    Z0G = *((uint16_t *)&__innoCommandBuff__[12]);
    Z1G = *((uint16_t *)&__innoCommandBuff__[14]);
    Z_1G = *((uint16_t *)&__innoCommandBuff__[16]);    
  }	
}     
void Accelerometer3B::RestoreCalVal(void)
{
  __innoCommandBuff__[0] = 140;
  __i2cWriteBuffer(__innoCommandBuff__, SlaveID, 3);  
}


