
#include "ALL_Includes.h"//包含所需的头文件
#include "math.h"

void ALL_Config(void);
static void MX_GPIO_Init(void);

#define  Rate2HZ   0x02
#define  Rate4HZ   0x03
#define  Rate8HZ   0x04
#define  Rate16HZ  0x05
#define  Rate32HZ  0x06

#define  MLX_I2C_ADDR 0x33
#define	 RefreshRate Rate8HZ 
#define  TA_SHIFT 8 //Default shift for MLX90640 in open air


int main(void)
{
		static uint16_t eeMLX90640[832];  
    uint16_t frame[834];
    float Ta,tr;
    static float mlx90640To[768];
		u16 LcdBuff[49152];
		u16 TimeCount=0;	
		u16 i=0;
	  float emissivity=0.95;
		int mode;
	
		ALL_Config();
					
		CoefficientInit();
		
    MLX90640_SetRefreshRate(MLX_I2C_ADDR, RefreshRate);//可以不用，系统默认初始化
    MLX90640_SetChessMode(MLX_I2C_ADDR);//可以不用，系统默认初始化
		paramsMLX90640 mlx90640;
    MLX90640_DumpEE(MLX_I2C_ADDR, eeMLX90640);
    MLX90640_ExtractParameters(eeMLX90640, &mlx90640);
		mode = MLX90640_GetCurMode(MLX_I2C_ADDR);
	
		for(i=0;i<3;i++)//丢去开始帧
		{
				MLX90640_GetFrameData(MLX_I2C_ADDR, frame);
				delay_ms(500);
		}
		
		while(1)
		{
				MLX90640_GetFrameData(MLX_I2C_ADDR, frame);
				Ta = MLX90640_GetTa(frame, &mlx90640);
				tr = Ta - TA_SHIFT;
				MLX90640_CalculateTo(frame, &mlx90640, emissivity, tr, mlx90640To);							
				MLX90640_BadPixelsCorrection((&mlx90640)->brokenPixels, mlx90640To, mode,  &mlx90640);
				MLX90640_BadPixelsCorrection((&mlx90640)->outlierPixels, mlx90640To,  mode, &mlx90640); 
			
				FindMaxMinTemp(mlx90640To);
				TempToColor(mlx90640To,LcdBuff); //768点温度转换成图像
				SetDispBuf(LcdBuff);//显示图像		
		}
}



/************************
函数功能：总初始化
输入参数：无
输出参数：无
备    注：无
************************/

void ALL_Config(void)
{	
	Delay_Init(168);//延时初始化
	delay_ms(1000);
	MX_GPIO_Init();
	LCD_init(); 
	IIC_GPIO_Init();
}

static void MX_GPIO_Init(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
	
  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA |RCC_AHB1Periph_GPIOB |  RCC_AHB1Periph_GPIOC |  RCC_AHB1Periph_GPIOE, ENABLE);
	
		//Key2、Key3
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
		//Key1
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  	GPIO_Init(GPIOC, &GPIO_InitStructure);
}