#include "user.h"
#include "Ultrasonic.h"
u8 count = 0;
u8 test = 0;

u8 ddd = 0;
void Timer4Deal(void)
{
    static uint32_t s_1=0,s_2=0,s_4=0,ms_10;
    if(1){if(LedStr.Cnt)LedStr.Cnt--;};
    if(UARTTEST){if(UartStr.Cnt)UartStr.Cnt--;};
    if(wireless_init.InitDelay)wireless_init.InitDelay--;
    if(wireless_init.SendDelay)wireless_init.SendDelay--;
    if(UartTimeOut)UartTimeOut--;
    if(DelayMs)DelayMs--;
    s_1++;s_2++;s_4++;ms_10++;
    
    if(ms_10>=10)
    {
         if(CadDecTime)CadDecTime--;
         if(Sx1278RxTimeout)Sx1278RxTimeout--;
         if(Sx1278SleepTime)Sx1278SleepTime--;
         BeepCheck();
         if(CycleDetectionTime)CycleDetectionTime--;
         if(MotorStr.delay_ms200)MotorStr.delay_ms200--;
          if(MotorStr.delay_ms500)MotorStr.delay_ms500--;
         SX78Str.SX7xDelay = TRUE;
         ms_10=0;
    }
    if(s_1>=1000)
    {
         if(Delay_2s--);
         if(ddd)ddd--;
         //if(DelayTime4s)DelayTime4s--;
         if(MotorStr.motor_delay)MotorStr.motor_delay--;
         s_1=0;if(DelayTime)DelayTime--;
    }
    if(s_2>=1000*2)
    {
         s_2 = 0;
         if(LockCarState.HostReportDelay)LockCarState.HostReportDelay--;
    }
    if(s_4>=1000*4)
    {
         if(DelayTime4s)DelayTime4s--;
         //QMC5883LStr.DataOkFlag=0;
         //QMC5883LStr.DataOkFlag=0;
//         static uint8_t MotorTestCnt = 0;
         static uint8_t TxSecond60Cnt = 0;
         s_4 = 0;
//         if(TxModel)
//		{
//				char *BUFF = "hello world!\n";
//				SX78Str.SX7xTxFlag = TRUE;
//				SX78Str.SX7xCmd = CmdTX;
//				memcpy(SX78Str.SX7xTxStr,BUFF,SX78Str.SX7xTxlen = strlen(BUFF));		//
//		}
         //if(MotorTestCnt++==1){motor_cmd = motor_fwd;}
//                if((TxModel)&&(TxSecond60Cnt++>=2))
//		{
//                  static int i = 0;
//                  GPIO_ToggleBits(GPIOA,GPIO_Pin_5);
//                  TxSecond60Cnt = 0;
//                  //uint8_t BUFF[2][25] = {{0x35,0x19,0x13,0x7B,0x05,0x35,0x19,0x06,0x02,0x00,0x56,0x7D},{0x35,0x19,0x13,0x7B,0x05,0x35,0x19,0x06,0x02,0x01,0x57,0x7D}};
//                  uint8_t BUFF[2][25] = {{0x35,0x19,0x13,0x7B,0x04,0x35,0x19,0x03,0xFF,0x50,0x7D},{0x35,0x19,0x13,0x7B,0x04,0x35,0x19,0x03,0xFF,0x50,0x7D}};
//				SX78Str.SX7xTxFlag = TRUE;
//				SX78Str.SX7xCmd = CmdTX;
//				memcpy(SX78Str.SX7xTxStr,BUFF[i++],SX78Str.SX7xTxlen = 11*sizeof(uint8_t));
//                                if(i>=2)i=0;
//		}
//                if((TxModel)&&(TxSecond60Cnt++>=7))
//		{
//                  static int i = 0;
//                  TxSecond60Cnt = 0;
//                  uint8_t BUFF[2][25] = {{0x7B,0x05,0x35,0x19,0x06,0x02,0x00,0x56,0x7D},{0x7B,0x05,0x35,0x19,0x06,0x02,0x01,0x57,0x7D}};
//				SX78Str.SX7xTxFlag = TRUE;
//				SX78Str.SX7xCmd = CmdTX;
//				memcpy(SX78Str.SX7xTxStr,BUFF[i++],SX78Str.SX7xTxlen = 9*sizeof(uint8_t));
//                                if(i>=2)i=0;
//		}
//                if((TxModel)&&(TxSecond60Cnt++>=2))
//		{
//                  static int i = 0;
//                  GPIO_ToggleBits(GPIOA,GPIO_Pin_5);
//                  TxSecond60Cnt = 0;
//                  //uint8_t BUFF[2][25] = {{0x35,0x19,0x13,0x7B,0x05,0x35,0x19,0x06,0x02,0x00,0x56,0x7D},{0x35,0x19,0x13,0x7B,0x05,0x35,0x19,0x06,0x02,0x01,0x57,0x7D}};
//                  uint8_t BUFF[2][25] = {{0x35,0x19,0x13,0x7B,0x04,0x35,0x19,0x03,0xFF,0x50,0x7D},{0x35,0x19,0x13,0x7B,0x04,0x35,0x19,0x03,0xFF,0x50,0x7D}};
//				SX78Str.SX7xTxFlag = TRUE;
//				SX78Str.SX7xCmd = CmdTX;
//				memcpy(SX78Str.SX7xTxStr,BUFF[i++],SX78Str.SX7xTxlen = 11*sizeof(uint8_t));
//                                if(i>=2)i=0;
//		}
    }
}

void UserTest(void)
{
     //LedTest();
     CmdDeal();
     
     motor_contro_act(&LockCarState.MotorCmd);
     HostPCSend();
     QmcDeal();
     SX7xTest(&SX78Str.SX7xCmd);
     CycleDetection();
     SUPERSONIC_hanld();
     time_state();   
}

uint8_t Bubble_Sort(uint8_t a[],int n)//n为数组a的元素个数
{
	uint8_t 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;
}

double BatVoltage[2] = {0};

void BatVolDec(u8 state)
{
     int i=0,BatValue=0;
     double temp=0;
     ADC_Cmd(ADC1,ENABLE);
     ADC_ChannelCmd(ADC1,ADC_Channel_22,DISABLE);
     ADC_ChannelCmd(ADC1,ADC_Channel_2,DISABLE);
     ADC_ChannelCmd(ADC1, ADC_Channel_0, ENABLE);
     for(i=0;i<5;i++)
     {
         ADC_SoftwareStartConv(ADC1);
         while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);
         BatValue += ADC_GetConversionValue(ADC1);
     }
     BatValue = BatValue/5;    
     if(state==1)
     {
          BatVoltage[0] = BatValue*3*10.0/308.0;
     }
     ADC_ChannelCmd(ADC1,ADC_Channel_22,DISABLE);
     ADC_ChannelCmd(ADC1,ADC_Channel_2,DISABLE);
     ADC_ChannelCmd(ADC1, ADC_Channel_0, DISABLE);
     ADC_Cmd(ADC1,DISABLE);
}
