/**
  ******************************************************************************
  * @file    bma250e.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    24-11-2014
  * @brief
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */
#include "hmc5883.h"

#include "stm8l15x.h"

u8 mag3[4];
int Mag_x_stable = 0;
int Mag_y_stable = 0;

u8 BUF[8];           		//接收数据缓存区
u8 MagGain;
u8 mag_time_count;
u8 set_parking_space_status;
extern u8 SlaveAddress;   //IIC写入时的地址字节数据，+1为读取
extern int magX=0, magY=0, magZ=0;    //hmc最原始数据
/*初始化HMC5883-----------------------------------------*/

//////////////////////////////////////////////////////////////////////
u8 magflagtemp;
u8 MagChangeFlag_read;
int mag_x_OPT,mag_y_OPT;
int mag_x_temp[10],mag_y_temp[10];
int mag_x_filter,mag_y_filter;
int geo_wave_flag = 0;
int geo_wave_count = 0;

void HMC5883L_Init(void)
{
  
        I2C_GPIO_Init();
	SlaveAddress = HMC5883_Addr;
	Single_WriteI2C(0x09,0x1d);  //控制寄存器配置:8G
	Single_WriteI2C(0x0b,0x01);  //设置清除时间寄存器
        	Single_WriteI2C(0x20,0x40);  //
	Single_WriteI2C(0x21,0x01);  //
}
void HMC5883L_MeasureMode(void)
{
	u8 crtl_reg;

	Single_WriteI2C(0x0b,0x01);

	bsp_DelayMS(10);

	SlaveAddress = HMC5883_Addr;

	crtl_reg = Single_ReadI2C(0x09);

	crtl_reg |= 0x01;//连续测量模式

	Single_WriteI2C(0x09,crtl_reg);

	bsp_DelayMS(10);

}

/*HMC5883闲置-----------------------------------------*/
void HMC5883L_IdleMode(void)
{
	u8 crtl_reg;

	SlaveAddress = HMC5883_Addr;

	crtl_reg = Single_ReadI2C(0x09);

	crtl_reg  &= ~0x01;//待机模式

	Single_WriteI2C(0x09,crtl_reg);
}
#define MAG_COUNT 16
/*从HMC5883连续读取16个数据放在BUF中-----------------------------------------*/
void updateHMC5883(void)
{
	static int  MagX_Save[MAG_COUNT] ;
	static uint8_t 	MagXWrite;
	static int  MagY_Save[MAG_COUNT] ;
	static uint8_t 	MagYWrite;

	HMC5883L_MeasureMode();
	Multiple_Read_QMC5883();
	magZ=((BUF[2] << 8) | BUF[1]); //使用XZ，平躺在电池盒上面
	magY=((BUF[4] << 8) | BUF[3]);
	magX=((BUF[6] << 8) | BUF[5]);

	if(magX&0x8000)
	{
		magX&=0x7fff;
		magX^=0x7fff;
		magX+=1;
		magX=-magX;
	}
	if(magY&0x8000)
	{
		magY&=0x7fff;
		magY^=0x7fff;
		magY+=1;
		magY=-magY;
	}
	HMC5883L_IdleMode();

//将采样值进行保存
	MagX_Save[MagXWrite] = magX ;
	if (++MagXWrite >= MAG_COUNT)
	{
		MagXWrite = 0;
	}

	MagY_Save[MagYWrite] = magY ;
	if (++MagYWrite >= MAG_COUNT)
	{
		MagYWrite = 0;
	}

}

/*设置增益值*/
void HMC5883L_Set_Gain(u8 value)
{

}

/*读取增益值*/
void HMC5883L_Get_Gain(void)
{
	// EEPROM_ReadByte(EEP_ADD_MAG_GAIN,&MagGain,1);
}
/**
*地磁更新
*/
void MagUpdate(int *p1,int *p2,u8 len)
{
	u8 i=0;
	HMC5883L_MeasureMode();
	for(i=0; i<len; i++,p1++,p2++)
	{
		bsp_DelayMS(20);
		updateHMC5883();
		*p1=magX;
		*p2=magY;
	}
	HMC5883L_IdleMode();
}

/**
*数组中去大去小取平均
*/
/*int Bubble_Sort(int a[],int n)//n为数组a的元素个数
{
	int i,j,temp;
	long     int sum = 0;
	for(j=0; j<n-1; j++)
		for(i=0; i<n-1-j; i++)
		{
			if(a[i]>a[i+1])//数组元素大小按升序排列
			{
				temp=a[i];
				a[i]=a[i+1];
				a[i+1]=temp;
			}
		}
	for(i=1,temp=0; i<(n-1); i++)
	{
		sum=a[i]+sum;
	}
	temp=sum/(n-2);

	return temp;
}*/
int Bubble_Sort(int a[],int n)//n为数组a的元素个数
{
	int i,temp;
	long int sum = 0;

	for(i=1,temp=0; i<(n-1); i++)
	{
		sum=a[i]+sum;
	}
	temp=sum/(n-2);

	return temp;
}

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

/**
*地磁检测
*/
#define STABLE_NUM 5
///////////////////////读取地磁值/////////////////////////////////
/***
功能：得到地磁值
***/
void get_geomag(void)
{
	MagUpdate(mag_x_temp,mag_y_temp,5);//10次修改为5次20170511
	mag_x_filter = Bubble_Sort(mag_x_temp,5);//10次修改为5次20170511
	mag_y_filter = Bubble_Sort(mag_y_temp,5);//10次修改为5次20170511
	mag3[1]=(char)(mag_x_filter&0xff);
	mag3[0]=(char)((mag_x_filter&0xff00)>>8);
	mag3[3]=(char)(mag_y_filter&0xff);
	mag3[2]=(char)((mag_y_filter&0xff00)>>8);
}
