/*
 * msp_BJ_Auto_IPC.c
 *
 *  Created on: Feb 1, 2024
 *      Author: Administrator
 */
#include "MSP/msp_BJ_Auto_IPC.h"
#include "MSP/msp_Modbus_crc16.h"

UART_HandleTypeDef* Huart_Cont_IPC;
SP_MSP_Controller_IPC_parameters *Control_IPC;
uint8_t Control_IPC_Val[10];
int Control_Flag=4;
void IPC_T_Cont_unscramble();
void  bytes_to_float(const unsigned char bytes[4], double *f_NUm);

uint8_t GF_MSP_BJ_IPC_Init(UART_HandleTypeDef* huart,SP_MSP_Controller_IPC_parameters *_Control_IPC)
{
	Huart_Cont_IPC=huart;
	GF_BSP_Interrupt_Add_CallBack(DF_BSP_InterCall_RS485_2_RxCpltCallback, IPC_T_Cont_unscramble);//这句话的理解有问题
	HAL_UART_Receive_IT(Huart_Cont_IPC,Control_IPC_Val,1);
	Control_IPC = _Control_IPC;
	return 1;
}


void GF_MSP_Cont_T_IPC_Value(int16_t battery_Level, int16_t Distance_Val[], int16_t Weld_Sequence, int16_t Arrival_Signal,int16_t Start_Signal, uint8_t Ship_Weld_mARK[])
{
	uint8_t Order[200];
	int Array_Num=0;
	Order[0]=0X66;Order[1]=0X66;
	unsigned short CRC_Value;
	switch(Control_Flag)
	{
		case 4:                //机器人状态信息已发送完毕，检查无问题
			Order[2]=0X04;
			Order[3]=0X00;//Data_Length High
			Order[4]=0X08;//Data_Length Low

			Order[5]=(uint8_t)(battery_Level>>8);
			Order[6]=(uint8_t)(battery_Level);

			Order[7]=(uint8_t)(Distance_Val[0]>>8);
			Order[8]=(uint8_t)(Distance_Val[0]);

			Order[9]=(uint8_t)(Distance_Val[1]>>8);
			Order[10]=(uint8_t)(Distance_Val[1]);

			Order[11]=(uint8_t)(Weld_Sequence>>8);
			Order[12]=(uint8_t)(Weld_Sequence);

			CRC_Value=ModBusCRC16(Order, 13); // CRC校验
			Order[13]= (uint8_t)( CRC_Value>>8);
			Order[14]= (uint8_t)CRC_Value;
			GF_BSP_UART_Transmit(2, Order, 15);//这句话的关联有问题
			break;
		case 3:           //到位采集信息发送完毕，检查无问题
			if(Arrival_Signal!=0)
			{
			Order[2]=0X03;
			Order[3]=0X00;//Data_Length High
			Order[4]=0X00;//Data_Length Low

			 CRC_Value=ModBusCRC16(Order, 5); // CRC校验
			 Order[5]= (uint8_t) ( CRC_Value>>8);
			 Order[6]= (uint8_t)CRC_Value;
			 GF_BSP_UART_Transmit(2, Order, 7);//这句话的关联有问题
			}
			break;
		case 2:    //检测开始信息发送完毕，检查无问题
			if(Start_Signal!=0)
			{
			Order[2]=0X02;
			Order[3]=0X00;//Data_Length High
			Order[4]=0X00;//Data_Length Low

			CRC_Value=ModBusCRC16(Order, 5); // CRC校验
			Order[5]= (uint8_t)( CRC_Value>>8);
			Order[6]= (uint8_t)CRC_Value;
			GF_BSP_UART_Transmit(2, Order, 6);//这句话的关联有问题
			}

			break;
		case 1: //检测船段信息（由遥控器发送给控制器的字段）
			if((Ship_Weld_mARK[0]==0X66)&&(Ship_Weld_mARK[1]==0X66)&&(Ship_Weld_mARK[2]==0X01))
			{
				Array_Num=(int)((Ship_Weld_mARK[3]<<8)|(Ship_Weld_mARK[4]));
				for(int i=0;i<Array_Num+7;i++)
				{
					Order[i]=(uint8_t)(Ship_Weld_mARK[i]);
				}
				 GF_BSP_UART_Transmit(2, Order, 7+Array_Num);//这句话的关联有问题 */
			}
			break;
		default:   //错误时将状态全部置位
			Order[2]=0X04;
			Order[3]=0X00;
			Order[4]=0X00;
			Order[5]=0;
			Order[6]=0;
			Order[7]=0;
			Order[8]=0;
			Order[9]=0;
			Order[10]=0;
			Order[11]=0;
			Order[12]=0;
			CRC_Value=ModBusCRC16(Order, 13); // CRC校验
			Order[13]= (uint8_t) ( CRC_Value>>8);
			Order[14]= (uint8_t)CRC_Value;
			GF_BSP_UART_Transmit(2, Order, 15);//这句话的关联有问题
			break;
	}
	Control_Flag=Control_Flag-1;
	if(Control_Flag<1)
	{
	Control_Flag=4;
	}
}

int IPC_Falg=1;
uint8_t IPC_stas[20]={0};
unsigned short CRC_Value_IPC;
int Data_Length_Val=0;

//正确函数__添加校验
void IPC_T_Cont_unscramble()
{
	unsigned char Bytes_Num[] = {0xF0, 0xA7, 0x06, 0xBF}; // 示例字节数组，代表某个float值
	double Num_value[1];
	switch(IPC_Falg)
	{
	case 1:
		if(IPC_stas[0]==0X66)
		{
			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);//接收RS485数据
			IPC_Falg=2;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);//接收RS485数据
			IPC_Falg=1;
		}
		break;
	case 2:
		if(IPC_stas[0]==0X66)
		{
			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);//接收RS485数据
			IPC_Falg=3;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);//接收RS485数据
			IPC_Falg=1;
		}
		break;
	case 3:
		if(IPC_stas[0]==0X20)//焊缝检测结果
		{
			Control_IPC->Weld_Dete_Res[0]=0x66;
			Control_IPC->Weld_Dete_Res[1]=0x66;
			Control_IPC->Weld_Dete_Res[2]=IPC_stas[0];

			Control_IPC->Weld_Dete_Res_IPC[0]=0x66;
			Control_IPC->Weld_Dete_Res_IPC[1]=0x66;
			Control_IPC->Weld_Dete_Res_IPC[2]=IPC_stas[0];

			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,4);//接收数据长度
			IPC_Falg=4;
		}
		else if(IPC_stas[0]==0X21)//偏移量，偏移角
		{
			Control_IPC->Weld_Dete_Res_IPC[0]=0x66;
			Control_IPC->Weld_Dete_Res_IPC[1]=0x66;
			Control_IPC->Weld_Dete_Res_IPC[2]=IPC_stas[0];

			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,4);//接收数据长度
			IPC_Falg=5;
		}
		else if(IPC_stas[0]==0X24)
		{
			Control_IPC->IPC_State[0]=0x66;
			Control_IPC->IPC_State[1]=0x66;
			Control_IPC->IPC_State[2]=IPC_stas[0];

			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,2);//接收数据长度
			IPC_Falg=6;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);//接收RS485数据
			IPC_Falg=1;
		}
		break;
	case 4:
		Data_Length_Val=(int)(IPC_stas[0]|(IPC_stas[1]<<8)|(IPC_stas[2]<<16)|(IPC_stas[3]<<24));

		Control_IPC->Weld_Dete_Res_IPC[3]=IPC_stas[0];
		Control_IPC->Weld_Dete_Res_IPC[4]=IPC_stas[1];

		Control_IPC->Weld_Dete_Res_IPC[5]=IPC_stas[2];
		Control_IPC->Weld_Dete_Res_IPC[6]=IPC_stas[3];

		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,Data_Length_Val);
		IPC_Falg=7;
		break;
	case 7:
		for (int i=0 ; i<(Data_Length_Val) ;i++)
		{
			Control_IPC->Weld_Dete_Res_IPC[7+i]=IPC_stas[i];
		}
		CRC_Value_IPC=ModBusCRC16(Control_IPC->Weld_Dete_Res_IPC, 7+Data_Length_Val); // CRC校验

		Control_IPC->Weld_Dete_Res_IPC[7+Data_Length_Val]= (uint8_t) CRC_Value_IPC;
		Control_IPC->Weld_Dete_Res_IPC[8+Data_Length_Val]= (uint8_t)(CRC_Value_IPC>>8);

		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,2);//接收CRC Verify
		IPC_Falg=10;
		break;
	case 10:   //需要将数据转换为串口所需要数据格式,数据重组
		if((Control_IPC->Weld_Dete_Res_IPC[7+Data_Length_Val]==IPC_stas[0])&&(Control_IPC->Weld_Dete_Res_IPC[8+Data_Length_Val]==IPC_stas[1]))
		{
			Control_IPC->Weld_Dete_Res[3]=(uint8_t) ((Data_Length_Val-1)>>8);
			Control_IPC->Weld_Dete_Res[4]=(uint8_t) ((Data_Length_Val-1));

			for(int i=0 ; i<(Data_Length_Val-1);i++)
			{
				Control_IPC->Weld_Dete_Res[i+5]=Control_IPC->Weld_Dete_Res_IPC[i+8];
			}
			CRC_Value_IPC=ModBusCRC16(Control_IPC->Weld_Dete_Res, 4+Data_Length_Val); // CRC校验
			Control_IPC->Weld_Dete_Res[4+Data_Length_Val]= (uint8_t) CRC_Value_IPC;
			Control_IPC->Weld_Dete_Res[5+Data_Length_Val]= (uint8_t)(CRC_Value_IPC>>8);
		}
		else
		{
			Control_IPC->Weld_Dete_Res[1]=0X00;
			Control_IPC->Weld_Dete_Res[2]=0X00;
		}
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);
		IPC_Falg=1;
		break;
	case 5:
		Data_Length_Val=(int)(IPC_stas[0]|(IPC_stas[1]<<8)|(IPC_stas[2]<<16)|(IPC_stas[3]<<24));
		Control_IPC->Weld_Dete_Res_IPC[3]=IPC_stas[0];
		Control_IPC->Weld_Dete_Res_IPC[4]=IPC_stas[1];
		Control_IPC->Weld_Dete_Res_IPC[5]=IPC_stas[2];
		Control_IPC->Weld_Dete_Res_IPC[6]=IPC_stas[3];
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,Data_Length_Val);//接收偏移量和偏移角
		IPC_Falg=8;
		break;
	case 8:
		for (int i=0 ; i<(Data_Length_Val) ;i++)
		{
			Control_IPC->Weld_Dete_Res_IPC[7+i]=IPC_stas[i];
		}
		CRC_Value_IPC=ModBusCRC16(Control_IPC->Weld_Dete_Res_IPC, 7+Data_Length_Val); // CRC校验

		Control_IPC->Weld_Dete_Res_IPC[7+Data_Length_Val]= (uint8_t) CRC_Value_IPC;
		Control_IPC->Weld_Dete_Res_IPC[8+Data_Length_Val]= (uint8_t)(CRC_Value_IPC>>8);

		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,2);//接收CRC Verify
		IPC_Falg=11;
		break;
	case 11:
		if((Control_IPC->Weld_Dete_Res_IPC[7+Data_Length_Val]==IPC_stas[0])&&(Control_IPC->Weld_Dete_Res_IPC[8+Data_Length_Val]==IPC_stas[1]))
		{
			Control_IPC->Weld_status=Control_IPC->Weld_Dete_Res_IPC[7];//焊缝状态

			Bytes_Num[0] = Control_IPC->Weld_Dete_Res_IPC[8];
			Bytes_Num[1] = Control_IPC->Weld_Dete_Res_IPC[9];
			Bytes_Num[2] = Control_IPC->Weld_Dete_Res_IPC[10];
			Bytes_Num[3] = Control_IPC->Weld_Dete_Res_IPC[11];

			bytes_to_float(Bytes_Num,Num_value);//数据映射到偏移量
			Control_IPC->Offset_angle[0]=(int16_t)Num_value[0];

			Bytes_Num[0] = Control_IPC->Weld_Dete_Res_IPC[12];
			Bytes_Num[1] = Control_IPC->Weld_Dete_Res_IPC[13];
			Bytes_Num[2] = Control_IPC->Weld_Dete_Res_IPC[14];
			Bytes_Num[3] = Control_IPC->Weld_Dete_Res_IPC[15];

			bytes_to_float(Bytes_Num,Num_value);//数据映射到偏移角
			Control_IPC->Offset_angle[1]=(int16_t)Num_value[0];
		}
		else
		{
		}
		Control_IPC->Weld_Dete_Res_IPC[0]=0X00;
		Control_IPC->Weld_Dete_Res_IPC[1]=0X00;
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);
		IPC_Falg=1;
		break;

	case 6:
		Data_Length_Val=(int)(IPC_stas[0]<<8|(IPC_stas[1]));
		Control_IPC->IPC_State[3]=IPC_stas[0];
		Control_IPC->IPC_State[4]=IPC_stas[1];
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,Data_Length_Val);
		IPC_Falg=9;
		break;
	case 9:
		for (int i=0 ; i<(Data_Length_Val) ;i++)
		{
			Control_IPC->IPC_State[5+i]=IPC_stas[i];
		}
		CRC_Value_IPC=ModBusCRC16(Control_IPC->IPC_State, 5+Data_Length_Val); // CRC校验
		Control_IPC->IPC_State[5+Data_Length_Val]= (uint8_t) CRC_Value_IPC;
		Control_IPC->IPC_State[6+Data_Length_Val]= (uint8_t)(CRC_Value_IPC>>8);
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,2);//接收CRC Verify
		IPC_Falg=12;
		break;
	case 12:
		if((Control_IPC->IPC_State[5+Data_Length_Val]==IPC_stas[0])&&(Control_IPC->IPC_State[6+Data_Length_Val]==IPC_stas[1]))
		{
		}
		else
		{
			Control_IPC->IPC_State[0]=0X00;
			Control_IPC->IPC_State[1]=0X00;
		}
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);
		IPC_Falg=1;
		break;
	default:
		HAL_UART_Receive_IT(Huart_Cont_IPC,IPC_stas,1);//接收RS485数据
		IPC_Falg=1;
		break;
	}
}



void  bytes_to_float(const unsigned char bytes[4], double *f_NUm) {
    float f;
    memcpy(&f, bytes, sizeof(f));
    f_NUm[0]=(double)f;
}

