/*
 * msp_Handset_TRXD_UTAT.c
 *
 *  Created on: 2024年2月20日
 *      Author: Administrator
 */
/* 遥控器发船段信息 66 66 01 00 08 B1 CB BA CF B1 CB B7 BD 7F 84 （彼合彼方） */

#include "MSP/msp_Handset_TRXD_UTAT.h"
#include "MSP/msp_Modbus_crc16.h"

void GF_Handset_TRXD_Unscramble_Callback();

UART_HandleTypeDef* Huart_Handset;
uint8_t _Handset_Filed[60];
SP_MSP_Handset_parameter* Handset_Filed_BING;
int Handset_TRXD_Flag=1;

int  Int_Size_bit=0;

uint8_t GF_Handset_TRXD_Init(UART_HandleTypeDef* huart,SP_MSP_Handset_parameter* _Handset_Filed_BING)
{

	Huart_Handset=huart;
	GF_BSP_Interrupt_Add_CallBack(DF_BSP_InterCall_E22_Serial_RxCpltCallback, GF_Handset_TRXD_Unscramble_Callback);//这句话的理解有问题
	HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,1);
	Handset_Filed_BING = _Handset_Filed_BING;
	return 1;
}

//机器人收，遥控器检查船段记录字符串
void GF_Handset_TRXD_Unscramble_Callback()
{
	unsigned short CRC_Value;
	switch(Handset_TRXD_Flag)
	{
	case 1:
		if(_Handset_Filed[0]==0X66)
		{
		HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,2);
		Handset_TRXD_Flag=2;
		}
		else
		{
		HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,1);
		Handset_TRXD_Flag=1;
		}
		break;
	case 2:
		if((_Handset_Filed[0]==0X66)&&(_Handset_Filed[1]==0X01))
		{
			HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,2);
			Handset_TRXD_Flag=3;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,1);
			Handset_TRXD_Flag=1;
		}
		break;
	case 3:
		//Recived data to struct
		Handset_Filed_BING->Handset_Filed[0]=0X66;
		Handset_Filed_BING->Handset_Filed[1]=0X66;
		Handset_Filed_BING->Handset_Filed[2]=0X01;
		Handset_Filed_BING->Handset_Filed[3]=_Handset_Filed[0];
		Handset_Filed_BING->Handset_Filed[4]=_Handset_Filed[1];

		Int_Size_bit=(int)(_Handset_Filed[0]<<8 | (_Handset_Filed[1]));
		HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,Int_Size_bit);
		Handset_TRXD_Flag=4;
		break;
	case 4:
		//Receive verification data
	    for(int i = 0; i < Int_Size_bit; i++)
	    {
	    	Handset_Filed_BING->Handset_Filed[5+ i] =_Handset_Filed[i];
	    }
		HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,2);
		Handset_TRXD_Flag=5;
		break;
	case 5:
		//Start CRC-16
		 CRC_Value=ModBusCRC16(Handset_Filed_BING->Handset_Filed, 5+Int_Size_bit); // CRC校验
		 Handset_Filed_BING->Handset_Filed[5+Int_Size_bit]= (uint8_t) CRC_Value;
		 Handset_Filed_BING->Handset_Filed[6+Int_Size_bit]= (uint8_t)( CRC_Value>>8);

		if((Handset_Filed_BING->Handset_Filed[5+Int_Size_bit]==_Handset_Filed[0])&&(Handset_Filed_BING->Handset_Filed[6+Int_Size_bit]==_Handset_Filed[1]))
		{
		}
		else
		{
			for (int i=0;i<(7+Int_Size_bit);i++)
			{
				Handset_Filed_BING->Handset_Filed[i]=0X00;
			}
		}
		HAL_UART_Receive_IT(Huart_Handset,_Handset_Filed,1);
		Handset_TRXD_Flag=1;
		break;

	default:
		break;
	}
}

// 机器人发送，机器人状态及IPC检测结果
void GF_MSP_Hangset_Ttransmit(int16_t Battery_level,int16_t Weld_Serial_Number,int16_t For_Back_Dist[],uint8_t Test_rec[])
{
	uint8_t Contronl_Ttr_Order[15]={0X66, 0X66, 0X04, 0X00, 0X08};
	unsigned short CRC_Value;
	int Test_rec_size=0;
	if((Test_rec[0]!=0x66)&&(Test_rec[1]!=0x66)&&(Test_rec[2]!=0x20))//机器人发“机器人状态”，ID=0x04，数据类型int16，3个变量，电量、序号、测距；
	{
		Contronl_Ttr_Order[5]=(uint8_t)(Battery_level>>8); Contronl_Ttr_Order[6]=(uint8_t)(Battery_level);
		Contronl_Ttr_Order[7]=(uint8_t)(Weld_Serial_Number>>8); Contronl_Ttr_Order[8]=(uint8_t)(Weld_Serial_Number);
		Contronl_Ttr_Order[9]=(uint8_t)(For_Back_Dist[0]>>8); Contronl_Ttr_Order[10]=(uint8_t)(For_Back_Dist[0]);
		Contronl_Ttr_Order[11]=(uint8_t)(For_Back_Dist[1]>>8); Contronl_Ttr_Order[12]=(uint8_t)(For_Back_Dist[1]);
		CRC_Value=ModBusCRC16(Contronl_Ttr_Order, 13); // CRC校验
		Contronl_Ttr_Order[13]= (uint8_t) CRC_Value;
		Contronl_Ttr_Order[14]= (uint8_t)( CRC_Value>>8);
		GF_BSP_UART_Transmit(5, Contronl_Ttr_Order, 15);//这句话的关联有问题
	}
	else//机器人发“检测记录“;
	{
		Test_rec_size=(int)(Test_rec[3]<<8 | (Test_rec[4]));
		GF_BSP_UART_Transmit(5, Test_rec, Test_rec_size+7);//这句话的关联有问题
	}
}









