/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：distancer.c
 * 描  述  ：测距程序     
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/08/04 
*********************************************************************************/
#include "distancer.h"
#include "timer.h"
#include "delay.h"
#include "my_math.h"
#include "data_typedef.h"
#include "key.h"

// uint8_t distancerTpye = ULTRASONIC_DISTANCER;					//测距传感器的类型
uint8_t distancerTpye = IR_DISTANCER;									//测距传感器的类型

/*
***********************************************************************
*	函 数 名: IR_GetDistanceTask
*	功能说明: 获取红外测距任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void IR_GetDistanceTask(void)
{
	static uint8_t getDataStatus1 = 0x00;
	static uint8_t getDataStatus2 = 0x00;
	static uint8_t getDataStatus3 = 0x00;
	static uint8_t getDataStatus4 = 0x00;
	
	static timer waitingTimer1;
	static timer waitingTimer2;
	static timer waitingTimer3;
	static timer waitingTimer4;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&waitingTimer1, CLOCK_SECOND*2);
		timer_set(&waitingTimer2, CLOCK_SECOND*2);
		timer_set(&waitingTimer3, CLOCK_SECOND*2);
		timer_set(&waitingTimer4, CLOCK_SECOND*2);
	}
	
//---------第1路测距采集
	switch(getDataStatus1)
	{
		case 0x00:
			if(timer_expired(&waitingTimer1))
			{
				timer_restart(&waitingTimer1);
				getDataStatus1 = 0x01;
				break;
			}
			break;
			
		case 0x01:
			IR_GetDistanceTask_01();
			if(getIrDistanceOkMark_1 == 1)			//第一路测距数据采集成功
			{
				getIrDistanceOkMark_1 = 0;
				getDistance1 = ir_Distance1;
				
// 				printf("\r\n测距：No1 = %3.1f cm\r\n", (float)getDistance1);
// 				printf("\r\n第1路采集OK\r\n");
				getDataStatus1 = 0x00;
				break;
			}
			break;
	}
	
//---------第2路测距采集
	switch(getDataStatus2)
	{
		case 0x00:
			if(timer_expired(&waitingTimer2))
			{
				timer_restart(&waitingTimer2);
				getDataStatus2 = 0x01;
				break;
			}
			break;
			
		case 0x01:
			IR_GetDistanceTask_02();
			if(getIrDistanceOkMark_2 == 1)			//第2路测距数据采集成功
			{
				getIrDistanceOkMark_2 = 0;
				getDistance2 = ir_Distance2;
				
// 				printf("\r\n测距：No2 = %3.1f cm\r\n", (float)getDistance2);
// 				printf("\r\n第2路采集OK\r\n");
				getDataStatus2 = 0x00;
				break;
			}
			break;
	}
	
//---------第3路测距采集
	switch(getDataStatus3)
	{
		case 0x00:
			if(timer_expired(&waitingTimer3))
			{
				timer_restart(&waitingTimer3);
				getDataStatus3 = 0x01;
			}
			break;
			
		case 0x01:
			IR_GetDistanceTask_03();
			if(getIrDistanceOkMark_3 == 1)			//第3路测距数据采集成功
			{
				getIrDistanceOkMark_3 = 0;
				getDistance3 = ir_Distance3;
				
// 				printf("\r\n测距：No3 = %3.1f cm\r\n", (float)getDistance3);
// 				printf("\r\n第2路采集OK\r\n");
				getDataStatus3 = 0x00;
				break;
			}
			break;
	}
	
//---------第4路测距采集
	switch(getDataStatus4)
	{
		case 0x00:
			if(timer_expired(&waitingTimer4))
			{
				timer_restart(&waitingTimer4);
				getDataStatus4 = 0x01;
				break;
			}
			break;
			
		case 0x01:
			IR_GetDistanceTask_04();
			if(getIrDistanceOkMark_4 == 1)			//第4路测距数据采集成功
			{
				getIrDistanceOkMark_4 = 0;
				getDistance4 = ir_Distance4;
				
// 				printf("\r\n测距：No4 = %3.1f cm\r\n", (float)getDistance4);
// 				printf("\r\n第4路采集OK\r\n");
				getDataStatus4 = 0x00;
				break;
			}
			break;
	}
}

/*
***********************************************************************
*	函 数 名: GetDistanceTask
*	功能说明: 测距任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void GetDistanceTask(void)
{
	static timer waitingTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&waitingTimer, CLOCK_SECOND*5);
	}
	
	IR_GetDistanceTask();
	
	if(timer_expired(&waitingTimer))
	{
		timer_restart(&waitingTimer);
		
		printf("\r\n测距：No1 = %3.1f cm, No2 = %3.1f cm, No3 = %3.1f cm, No4 = %3.1f cm\r\n", 
		(float)getDistance1,
		(float)getDistance2,
		(float)getDistance3,
		(float)getDistance4);
	}
}

/******************** (C) COPYRIGHT 2017 CXHY *****END OF FILE*******************/
