
#include "Include.h"
//_GaugesInfo MeterInfo[Num_Of_Piecewise_Linear_App];
//Motor_Struct     Motor;  
//uint8_t HalfSelfSts;

//ÿ�����̵��������Ͳ�����Ӧ��ϵ��д
void MeterInfo_Init(void)
{

//���0������д
//ˮ��	

	MeterInfo[Motor_1].Parameter_Num = 8;

	MeterInfo[Motor_1].Physical_Quantity[0] = 40;
	MeterInfo[Motor_1].Physical_Quantity[1] = 60;   
	MeterInfo[Motor_1].Physical_Quantity[2] = 80;   
	MeterInfo[Motor_1].Physical_Quantity[3] = 95;
	MeterInfo[Motor_1].Physical_Quantity[4] = 110;
	MeterInfo[Motor_1].Physical_Quantity[5] = 114;
	MeterInfo[Motor_1].Physical_Quantity[6] = 120;//ConfFun_Backup.CoolantTem_Warning;
	MeterInfo[Motor_1].Physical_Quantity[7] = 125;//ConfFun_Backup.CoolantTem_H;

	
	MeterInfo[Motor_1].MicroStep[0] = 0;
	MeterInfo[Motor_1].MicroStep[1] = 335;
	MeterInfo[Motor_1].MicroStep[2] = 640;
	MeterInfo[Motor_1].MicroStep[3] = 641;
	MeterInfo[Motor_1].MicroStep[4] = 800;
	MeterInfo[Motor_1].MicroStep[5] = 950;
	MeterInfo[Motor_1].MicroStep[6] = 1100;
	MeterInfo[Motor_1].MicroStep[7] = 1270;
//���1������д	
//����
    	MeterInfo[Motor_2].Parameter_Num = 8;
	MeterInfo[Motor_2].Physical_Quantity[0] = 0;
	MeterInfo[Motor_2].Physical_Quantity[1] = 500;
	MeterInfo[Motor_2].Physical_Quantity[2] = 750;
	MeterInfo[Motor_2].Physical_Quantity[3] = 1000;
	MeterInfo[Motor_2].Physical_Quantity[4] = 1500;
	MeterInfo[Motor_2].Physical_Quantity[5] = 1750;
	MeterInfo[Motor_2].Physical_Quantity[6] = 2000;
	MeterInfo[Motor_2].Physical_Quantity[7] = 2500;

	MeterInfo[Motor_2].MicroStep[0] = 0;
	MeterInfo[Motor_2].MicroStep[1] = 545;//715;	  
	MeterInfo[Motor_2].MicroStep[2] = 830;//1050;  
	MeterInfo[Motor_2].MicroStep[3] = 1100;//1390;   
	MeterInfo[Motor_2].MicroStep[4] = 1640;//1720;   
	MeterInfo[Motor_2].MicroStep[5] = 1900;//2060;    
	MeterInfo[Motor_2].MicroStep[6] = 2180;//2390; 
	MeterInfo[Motor_2].MicroStep[7] = 2730;//2720; 
	
	

//���2������д
//ȼ��
	MeterInfo[Motor_3].Parameter_Num = 8;

	MeterInfo[Motor_3].Physical_Quantity[0] = 10;
	MeterInfo[Motor_3].Physical_Quantity[1] = 30;
	MeterInfo[Motor_3].Physical_Quantity[2] = 50;
	MeterInfo[Motor_3].Physical_Quantity[3] = 60;
	MeterInfo[Motor_3].Physical_Quantity[4] = 70;
	MeterInfo[Motor_3].Physical_Quantity[5] = 80;
	MeterInfo[Motor_3].Physical_Quantity[6] = 90;
	MeterInfo[Motor_3].Physical_Quantity[7] = 100;

	
	MeterInfo[Motor_3].MicroStep[0] = 0;
	MeterInfo[Motor_3].MicroStep[1] = 340;
	MeterInfo[Motor_3].MicroStep[2] = 635;
	MeterInfo[Motor_3].MicroStep[3] = 780;
	MeterInfo[Motor_3].MicroStep[4] = 925;
	MeterInfo[Motor_3].MicroStep[5] = 1080;
	MeterInfo[Motor_3].MicroStep[6] = 1250;
	MeterInfo[Motor_3].MicroStep[7] = 1280;

	//���3������д
	//����
	MeterInfo[Motor_4].Parameter_Num = 8;

	MeterInfo[Motor_4].Physical_Quantity[0] = 0;
	MeterInfo[Motor_4].Physical_Quantity[1] = 200;
	MeterInfo[Motor_4].Physical_Quantity[2] = 400;
	MeterInfo[Motor_4].Physical_Quantity[3] = 600;
	MeterInfo[Motor_4].Physical_Quantity[4] = 800;
	MeterInfo[Motor_4].Physical_Quantity[5] = 1000;
	MeterInfo[Motor_4].Physical_Quantity[6] = 1200;
	MeterInfo[Motor_4].Physical_Quantity[7] = 1400;

	MeterInfo[Motor_4].MicroStep[0] = 0;
	MeterInfo[Motor_4].MicroStep[1] = 380; 
	MeterInfo[Motor_4].MicroStep[2] = 760;	 
	MeterInfo[Motor_4].MicroStep[3] = 1150;
	MeterInfo[Motor_4].MicroStep[4] = 1530;
	MeterInfo[Motor_4].MicroStep[5] = 1920;
	MeterInfo[Motor_4].MicroStep[6] = 2300;
	MeterInfo[Motor_4].MicroStep[7] = 2690; 	
}



/******************************************************************************
  ��������Motor_SelfCheck
  ��  ��: ����Լ캯��
  ��  ��: ��
  ����ֵ����
 ******************************************************************************/
void Motor_SelfCheck(void)
{
    if(SYS_OPR_STAT_IGN_ON && (g_u8SelfLampTimes < LED_SELF_CHECK_TIME))
    {
        App_Get_Motor_Status();
        if( (Motor.workMode_Motor1 == MaxStepKeep)&&(Motor.workMode_Motor2 == MaxStepKeep)&&(Motor.workMode_Motor4 == MaxStepKeep)  )
        {
            //Sound_Delete(SND_SRC_Self);
            HalfSelfSts = 1;
            bsp_SetMotorMode(Motor_1,MinStep);
            bsp_SetMotorMode(Motor_2,MinStep);
            bsp_SetMotorMode(Motor_3,MinStep);
            bsp_SetMotorMode(Motor_4,MinStep);
        }
        else;  
    }
}

/******************************************************************************
  ��������Motor_Init_Poweron_System
  ��  ��: ֱ�����������30��ʱ����
  ��  ��: ��
  ����ֵ����
 ******************************************************************************/

void Motor_Init_Poweron_System(void)
{
        	Motor.MotorLib_Version = bsp_MotorGetVersion();
         MeterInfo_Init();
	bsp_MeterConfig1();
	bsp_SetMotorMode(Motor_1, MaxReset);
	bsp_SetMotorMode(Motor_2, MaxReset);
	bsp_SetMotorMode(Motor_3, MaxReset);
	bsp_SetMotorMode(Motor_4, MaxReset);

}

/******************************************************************************
  ��������Get_Motor_Status
  ��  ��: ��ȡ���״̬
  ��  ��: ��
  ����ֵ����
 ******************************************************************************/
void App_Get_Motor_Status(void)
{
    Motor.workMode_Motor1 = bsp_GetMotorState(Motor_1);
    Motor.workMode_Motor2 = bsp_GetMotorState(Motor_2);
    Motor.workMode_Motor3 = bsp_GetMotorState(Motor_3);
    Motor.workMode_Motor4 = bsp_GetMotorState(Motor_4);
}

/******************************************************************************
  ��������Motor_Init_Poweron_IGN
  ��  ��: ֱ�����������15��ʱ����
  ��  ��: ��
  ����ֵ����
 ******************************************************************************/
void Motor_Init_Poweron_IGN(void)
{
    bsp_MeterConfig1();
    bsp_SetMaxSpeed(Motor_1,1);
    bsp_SetMaxSpeed(Motor_2,1);
    bsp_SetMaxSpeed(Motor_3,1);
    bsp_SetMaxSpeed(Motor_4,1);
    App_Get_Motor_Status();
    while((Motor.workMode_Motor1 != Normal)||(Motor.workMode_Motor2 != Normal)||\
    (Motor.workMode_Motor3 != Normal) || (Motor.workMode_Motor4 != Normal) ) 
    {
        App_Get_Motor_Status();
        __RESET_WATCHDOG(); 
    }
    bsp_SetMotorMode(Motor_1,MinReset);   
    bsp_SetMotorMode(Motor_2,MinReset);
    bsp_SetMotorMode(Motor_3,MinReset);
    bsp_SetMotorMode(Motor_4,MinReset);
    App_Get_Motor_Status();
    while((Motor.workMode_Motor1 != Normal)||(Motor.workMode_Motor2 != Normal)||\
    (Motor.workMode_Motor3 != Normal)||(Motor.workMode_Motor4 != Normal) ) 
    {
        App_Get_Motor_Status();
        __RESET_WATCHDOG();   
    }  
//    bsp_SetMotorMode(Motor_1,MaxStep);  
//    bsp_SetMotorMode(Motor_2,MaxStep); 
  //  bsp_SetMotorMode(Motor_3,MaxStep);
  //  bsp_SetMotorMode(Motor_4,MaxStep);
}
/******************************************************************************
  ��������Motor_IGNOFF
  ��  ��: ֱ��IGNOFF���㺯��
  ��  ��: ��
  ����ֵ����
 ******************************************************************************/
void Motor_IGNOFF(void)
{
//	bsp_SetMotorMode(Motor_1,MinStep);
//	bsp_SetMotorMode(Motor_2,MinStep);
//	bsp_SetMotorMode(Motor_3,MinStep);
//	bsp_SetMotorMode(Motor_4,MinStep);
	        /*ˮ��*/
		App_SetMotorStep(0,Motor_1);

		/*ת��*/
		App_SetMotorStep(0,Motor_2);

		/*ȼ��*/
		App_SetMotorStep(0,Motor_3);
		
		/*����*/
		App_SetMotorStep(0,Motor_4 );

}

/******************************************************************************
  ������?AApp_SetMotorStep
  ��  ��: ��Ŀ�경�����뵽����������
  ��  ��:  Physical_Data:��Ӧ���������ֵ   index:������
  ����ֵ����
 ******************************************************************************/
void App_SetMotorStep(uint16_t Physical_Data,uint8_t index)
{
    uint8_t Sector;
    uint16_t destPosition,stepsub;
    uint16_t physub,physubcur;
    uint32_t DesPositionStep;
    switch(index)
    {
        case Motor_1:
             Sector = App_FindSector(Physical_Data,index);//��ȡ��ǰֵ���ڿ�
             if(Sector == 8)
                destPosition =  MeterInfo[Motor_1].MicroStep[MeterInfo[Motor_1].Parameter_Num - 1];
             else if(Sector == 9)
                destPosition =  MeterInfo[Motor_1].MicroStep[0];
             else
             {   
                 stepsub  = MeterInfo[Motor_1].MicroStep[Sector] - MeterInfo[Motor_1].MicroStep[Sector-1];//���ڿ�Ĳ�������ֵ
                 physub   = MeterInfo[Motor_1].Physical_Quantity[Sector] - MeterInfo[Motor_1].Physical_Quantity[Sector-1];//���ڿ����������ֵ
                 physubcur= Physical_Data - MeterInfo[Motor_1].Physical_Quantity[Sector-1];//��ǰֵ��ռ�����Ĵ�С
                 DesPositionStep  =  stepsub;
                 DesPositionStep *=  physubcur;
                 DesPositionStep /=  physub;
                 DesPositionStep +=  MeterInfo[Motor_1].MicroStep[Sector-1];
                 destPosition =  (uint16_t)DesPositionStep;
             }
             if(bsp_GetMotorState(Motor_1) == Normal) bsp_SetMotorStep(Motor_1,destPosition);
        break;
        case Motor_2:
             Sector = App_FindSector(Physical_Data,index);//��ȡ��ǰֵ���ڿ�
             if(Sector == 8)
                destPosition =  MeterInfo[Motor_2].MicroStep[MeterInfo[Motor_2].Parameter_Num - 1];
             else if(Sector == 9)
                destPosition =  MeterInfo[Motor_2].MicroStep[0];
             else
             {   
                 stepsub  = MeterInfo[Motor_2].MicroStep[Sector] - MeterInfo[Motor_2].MicroStep[Sector-1];//���ڿ�Ĳ�������ֵ
                 physub   = MeterInfo[Motor_2].Physical_Quantity[Sector] - MeterInfo[Motor_2].Physical_Quantity[Sector-1];//���ڿ����������ֵ
                 physubcur= Physical_Data - MeterInfo[Motor_2].Physical_Quantity[Sector-1];//��ǰֵ��ռ�����Ĵ�С
                 DesPositionStep  =  stepsub;
                 DesPositionStep *=  physubcur;
                 DesPositionStep /=  physub;
                 DesPositionStep +=  MeterInfo[Motor_2].MicroStep[Sector-1];
                 destPosition =  (uint16_t)DesPositionStep;
             }
             if(bsp_GetMotorState(Motor_2) == Normal) bsp_SetMotorStep(Motor_2,destPosition);
        break;
        case Motor_3:
             Sector = App_FindSector(Physical_Data,index);//��ȡ��ǰֵ���ڿ�
             if(Sector == 8)
                destPosition =  MeterInfo[Motor_3].MicroStep[MeterInfo[Motor_3].Parameter_Num - 1];
             else if(Sector == 9)
                destPosition =  MeterInfo[Motor_3].MicroStep[0];
             else
             {   
                 stepsub  = MeterInfo[Motor_3].MicroStep[Sector] - MeterInfo[Motor_3].MicroStep[Sector-1];//���ڿ�Ĳ�������ֵ
                 physub   = MeterInfo[Motor_3].Physical_Quantity[Sector] - MeterInfo[Motor_3].Physical_Quantity[Sector-1];//���ڿ����������ֵ
                 physubcur= Physical_Data - MeterInfo[Motor_3].Physical_Quantity[Sector-1];//��ǰֵ��ռ�����Ĵ�С
                 DesPositionStep  =  stepsub;
                 DesPositionStep *=  physubcur;
                 DesPositionStep /=  physub;
                 DesPositionStep +=  MeterInfo[Motor_3].MicroStep[Sector-1];
                 destPosition =  (uint16_t)DesPositionStep;
             }
             if(bsp_GetMotorState(Motor_3) == Normal) bsp_SetMotorStep(Motor_3,destPosition);
        break;
        case Motor_4:
             Sector = App_FindSector(Physical_Data,index);//��ȡ��ǰֵ���ڿ�
             if(Sector == 8)
                destPosition =  MeterInfo[Motor_4].MicroStep[MeterInfo[Motor_4].Parameter_Num - 1];
             else if(Sector == 9)
                destPosition =  MeterInfo[Motor_4].MicroStep[0];
             else
             {   
                 stepsub  = MeterInfo[Motor_4].MicroStep[Sector] - MeterInfo[Motor_4].MicroStep[Sector-1];//���ڿ�Ĳ�������ֵ
                 physub   = MeterInfo[Motor_4].Physical_Quantity[Sector] - MeterInfo[Motor_4].Physical_Quantity[Sector-1];//���ڿ����������ֵ
                 physubcur= Physical_Data - MeterInfo[Motor_4].Physical_Quantity[Sector-1];//��ǰֵ��ռ�����Ĵ�С
                 DesPositionStep  =  stepsub;
                 DesPositionStep *=  physubcur;
                 DesPositionStep /=  physub;
                 DesPositionStep +=  MeterInfo[Motor_4].MicroStep[Sector-1];
                 destPosition =  (uint16_t)DesPositionStep;
             }
             if(bsp_GetMotorState(Motor_4) == Normal) bsp_SetMotorStep(Motor_4,destPosition);
        break;
        default:
        break;
    }
}
/******************************************************************************
  ������:	App_FindSector
  ��  ��: �������ǰֵ�����ĸ�����
  ��  ��: Physical_Data:��Ӧ���������ֵ   index:������
  ����ֵ������ֵ���ڿ飬����ֵ1-(Parameter_Num - 1)��8Ϊ���볬�����ֵ��9Ϊ����С����Сֵ
******************************************************************************/
INT8U App_FindSector(INT16U Physical_Data, INT8U Index)
{
	uint8_t i;
	uint8_t SectorID = 0;
	if(Physical_Data>MeterInfo[Index].Physical_Quantity[MeterInfo[Index].Parameter_Num - 1])
	{
		return 8;//����ֵ�������ֵ
	}
	if(Physical_Data<MeterInfo[Index].Physical_Quantity[0]) 
	{
		return 9;//����ֵС����Сֵ
	}
	for (i = 0; i < (MeterInfo[Index].Parameter_Num - 1); i++)
	{
		if (Physical_Data >= MeterInfo[Index].Physical_Quantity[i])
		{
			SectorID++;
		}
	}   
	return SectorID;    
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





#pragma CODE_SEG NON_BANKED
#pragma TRAP_PROC

interrupt void Api_Motor_isr(void)
{
	bsp_MotorMove_50us_main();
	MCCTL0_MCTOIF = 1;
}
#pragma CODE_SEG DEFAULT



void Gauges_Sleep_Init(void)
{
	MotorDriveOff();
}

void Gauges_Wake_Up_Init(void)
{
}

//INT16U  CCC;
//INT16U  DDD;

void Gauges_Management_Service(void)
{
	if (SYS_OPR_STAT_HALT)
	{
		Motor_IGNOFF();
        App_SetMotorStep(1000,Motor_2);
        return;
	}

	if (SYS_OPR_STAT_IGN_ON)
	{
      		 //bsp_SetMotorStep(Motor_2 ,  CCC);
      		 //App_SetMotorStep(CCC,Motor_2) ;

	    /*ˮ��*/
	    App_SetMotorStep(Coolant_Temperature.Value,Motor_1);

		/*����*/
		App_SetMotorStep(DataConsPwr.u8CANSignal_InputVal,Motor_2);

		/*ȼ��*/
		App_SetMotorStep(FuelData.Step_Get,Motor_3);
		
		/*����*/
		App_SetMotorStep(DataVSpeedDisp,Motor_4 );
	}
	else
	{
	}
}