#include "user.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(UartRecLen)FeedWatchDog();
         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(s_4>=1000*4)
    {
         static uint8_t TxSecond60Cnt = 0;
         s_4 = 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)
{
     FeedWatchDog();
     SX7xTest(&SX78Str.SX7xCmd);
     LedTest();
     HostPCSend();
}

void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen)
{
	short i;
	unsigned char highByte, lowByte;
	for (i = 0; i < sourceLen; i++)
	{
		highByte = source[i] >> 4;
		lowByte = source[i] & 0x0f;

		highByte += 0x30;

		if (highByte > 0x39)
			dest[i * 2] = highByte + 0x07;
		else
			dest[i * 2] = highByte;

		lowByte += 0x30;
		if (lowByte > 0x39)
			dest[i * 2 + 1] = lowByte + 0x07;
		else
			dest[i * 2 + 1] = lowByte;
	}
	return;
}
void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
{
	short i;
	unsigned char highByte, lowByte;
	for (i = 0; i < sourceLen; i += 2)
	{
		highByte = toupper(source[i]);
		lowByte = toupper(source[i + 1]);

		if (highByte > 0x39)
			highByte -= 0x37;
		else
			highByte -= 0x30;

		if (lowByte > 0x39)
			lowByte -= 0x37;
		else
			lowByte -= 0x30;

		dest[i / 2] = (highByte << 4) | lowByte;
	}
	return;
}
