/*
 * msp_JTBATTERY.c
 *
 *  Created on: Oct 26, 2023
 *      Author: shiya
 */
#include "MSP/msp_JTBATTERY.h"

UART_HandleTypeDef* Huart_Bat4006;

SP_MSP_Battery_parameters* P_Bat_4006;
int8_t Battery_Quit_Falg=1;

uint8_t Bat_stas[4];
uint8_t Bat_stas_Val[10];
int32_t Battery_Falg=1;


void Battery_Command_Comp(int8_t Battery_Status);
void Data_Value_1_Bit(uint8_t Length, uint8_t *Data_Value);
void Data_Value_2_Bit(uint8_t Length, uint8_t *Data_Value);
void Data_Value_4_Bit(uint8_t Length, uint8_t *Data_Value);
void Battery_Alarm(uint8_t Data_Value);


uint8_t GF_MSP_Bat_4006_Init(UART_HandleTypeDef* huart,SP_MSP_Battery_parameters *_Bat_4006)
{
	Battery_Quit_Falg=1;
	Huart_Bat4006=huart;
	GF_BSP_Interrupt_Add_CallBack(DF_BSP_InterCall_RS485_1_RxCpltCallback, GF_Battery_Unscramble_Callback);//这句话的理解有问题
	HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas,1);
	P_Bat_4006 = _Bat_4006;
	return 1;
}

void GF_MSP_Battery_Inquiry()
{
	uint8_t Order[8];
	Order[0]=0X3A;Order[1]=0X16;
	switch(Battery_Quit_Falg)
	{
		case 1:
			Order[2]=0X01;
			break;
		case 2:
			Order[2]=0X06;
			break;
		case 3:
			Order[2]=0X07;
			break;
		case 4:
			Order[2]=0X08;
			break;
		case 5:
			Order[2]=0X09;
			break;
		case 6:
			Order[2]=0X0A;
			break;
		case 7:
			Order[2]=0X0D;
			break;
		case 8:
			Order[2]=0X0F;
			break;
		case 9:
			Order[2]=0X10;
			break;
		default:
			Order[2]=0X01;
			break;
	}
	Battery_Quit_Falg=Battery_Quit_Falg+1;
	if(Battery_Quit_Falg>9)
	{
	Battery_Quit_Falg=1;
	}

	Order[3]=0X00;
	int16_t  Value_Total=(int16_t)Order[1]+(int16_t)Order[2]+(int16_t)Order[3];
			  Order[4]=(uint8_t)Value_Total;
			  Order[5]=(uint8_t)(Value_Total>>8);
			  Order[6]=0X0D;
			  Order[7]=0X0A;
	GF_BSP_UART_Transmit(1, Order, 8);//这句话的关联有问题
}


void  GF_Battery_Unscramble_Callback()
{
		switch(Battery_Falg)
		{
		case 1:
			if(Bat_stas[0]==0X3A)
			{
			HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas,3);
			Battery_Falg=2;
			}
			else
			{
			HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas,1);
			Battery_Falg=1;
			}
			break;
		case 2:
			if(Bat_stas[0]==0X16)
			{
			HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas_Val,Bat_stas[2]+4);
			Battery_Falg=3;
			}
			else
			{
			Battery_Falg=1;
			HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas,1);
			}
			break;
		case 3:
			if((Bat_stas_Val[Bat_stas[2]+3]==0X0A)&&(Bat_stas_Val[Bat_stas[2]+2]==0X0D))
			{
			Battery_Command_Comp(Bat_stas[1]);
			HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas,1);
			Battery_Falg=1;
			}
			else
			{
			Battery_Falg=1;
			HAL_UART_Receive_IT(Huart_Bat4006,Bat_stas,1);
			}
			break;

		default:
			break;
		}
}

void  Battery_Command_Comp(int8_t Battery_Status)
{
	uint8_t  Data_Value_all[5];
	double  Median_Int;

	switch(Battery_Status)
	{
	case 0x01:
		Data_Value_1_Bit(Bat_stas[2], Data_Value_all);
		Battery_Alarm(Data_Value_all[0]);
		break;
	case 0x06:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		P_Bat_4006->BMS_MOS_Temp=(int32_t)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0])-(int16_t)2731;
		break;
	case 0x07:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		Median_Int=(double)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0]);
		if(((Data_Value_all[1])&(0X80))>0)
		{
			Median_Int=-1*(65536-Median_Int);
		}
		P_Bat_4006->Battery_Min_Temp=((int32_t)Median_Int-(int16_t)2731)*0.1;//正确
		break;
	case 0x08:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		P_Bat_4006->Battery_Max_Temp=(int32_t)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0])-(int16_t)2731;
		break;
	case 0x09:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		P_Bat_4006->Battery_Tot_Vol=(int32_t)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0]); //错误 41577*0.001
		break;
	case 0x0A:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		Median_Int=(double)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0]);
		if(((Data_Value_all[1])&(0X80))>0)
		{
			Median_Int=-1*(65536-Median_Int);
		}
		P_Bat_4006->Battery_Cur=(int32_t)Median_Int;//正确FF4D -179
		break;
	case 0x0D:
		Data_Value_1_Bit(Bat_stas[2], Data_Value_all);
		P_Bat_4006->Battery_SOC_Perc=(int32_t)Data_Value_all[0];
		break;
	case 0x0F:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		P_Bat_4006->Battery_SOC=(int32_t)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0]);
		break;
	case 0x10:
		Data_Value_2_Bit(Bat_stas[2], Data_Value_all);
		P_Bat_4006->Battery_Full_SOC=(int32_t)((int16_t)Data_Value_all[1]<<8|Data_Value_all[0]);
		break;

	default:
		break;
	}
}

void Data_Value_1_Bit(uint8_t Length, uint8_t *Data_Value)
{
	int16_t Checksum_Value;int32_t Data_Sum=0X00;
	Checksum_Value=(int16_t)((int16_t)Bat_stas_Val[Length+1]<<8|Bat_stas_Val[Length]);

	Data_Sum=(int32_t)Bat_stas[0]+(int32_t)Bat_stas[1]+(int32_t)Bat_stas[2]+(int32_t)Bat_stas_Val[0];
	if (Checksum_Value==Data_Sum)
	{
		for(uint8_t i=0;i<Length;i++)
		{
			Data_Value[i]=Bat_stas_Val[i];
		}
	}
	else
	{
		Battery_Falg=1;
	}
}

void Data_Value_2_Bit(uint8_t Length, uint8_t *Data_Value)
{
	int32_t Checksum_Value;int32_t Data_Sum=0X00;
	Checksum_Value=(int32_t)((int16_t)Bat_stas_Val[Length+1]<<8|Bat_stas_Val[Length]);

//	int16_t Checksum_Sum=(int32_t)((int16_t)Bat_stas_Val[1]<<8|Bat_stas_Val[0]);

	int16_t Checksum_Sum=(int32_t)Bat_stas_Val[1]+(int32_t)Bat_stas_Val[0];
	Data_Sum=(int32_t)Bat_stas[0]+(int32_t)Bat_stas[1]+(int32_t)Bat_stas[2]+(int32_t)Checksum_Sum;
	if (Checksum_Value==Data_Sum)
	{
		for(int8_t i=0;i<Length;i++)
		{
			Data_Value[i]=Bat_stas_Val[i];
		}
	}
	else
	{
		Battery_Falg=1;
	}
}

void Data_Value_4_Bit(uint8_t Length, uint8_t *Data_Value)
{
	int32_t Checksum_Value;int32_t Data_Sum=0X00;
	Checksum_Value=(int32_t)((int16_t)Bat_stas_Val[Length+1]<<8|Bat_stas_Val[Length]);

//	int32_t Checksum_Sum=(int32_t)((int32_t)Bat_stas_Val[3]<<24|Bat_stas_Val[2]<<16|Bat_stas_Val[1]<<8|Bat_stas_Val[0]);

	int32_t Checksum_Sum=(int32_t)Bat_stas_Val[3]  +(int32_t)Bat_stas_Val[2]+ (int32_t)Bat_stas_Val[1]+(int32_t)Bat_stas_Val[0];

	Data_Sum=(int32_t)Bat_stas[0]+(int32_t)Bat_stas[1]+(int32_t)Bat_stas[2]+(int32_t)Checksum_Sum;
	if (Checksum_Value==Data_Sum)
	{
		for(uint8_t i=0;i<Length;i++)
		{
			Data_Value[i]=Bat_stas_Val[i];
		}
	}
	else
	{
		Battery_Falg=1;
	}
}



void Battery_Alarm(uint8_t Data_Value)
{

	if(((Data_Value)&(0X01))>0)
	{
		P_Bat_4006->Charg_Over=1;
	}
	else
	{
		P_Bat_4006->Charg_Over=0;
	}

	if(((Data_Value)&(0X02))>0)
	{
		P_Bat_4006->Dscharg_Over=1;
	}
	else
	{
		P_Bat_4006->Dscharg_Over=0;
	}

	if(((Data_Value)&(0X04))>0)
	{
		P_Bat_4006->Charg_Short_Cir=1;
	}
	else
	{
		P_Bat_4006->Charg_Short_Cir=0;
	}

	if(((Data_Value)&(0X08))>0)
	{
		P_Bat_4006->Charg_Short_Cir=1;
	}
	else
	{
		P_Bat_4006->Charg_Short_Cir=0;
	}

	if(((Data_Value)&(0X10))>0)
	{
		P_Bat_4006->High_Temp=1;
	}
	else
	{
		P_Bat_4006->High_Temp=0;
	}

	if(((Data_Value)&(0X20))>0)
	{
		P_Bat_4006->Over_load=1;
	}
	else
	{
		P_Bat_4006->Over_load=0;
	}


	if(((Data_Value)&(0X40))>0)
	{
		P_Bat_4006->Over_Voltage=1;
	}
	else
	{
		P_Bat_4006->Over_Voltage=0;
	}

	if(((Data_Value)&(0X80))>0)
	{
		P_Bat_4006->Under_Voltage=1;
	}
	else
	{
		P_Bat_4006->Under_Voltage=0;
	}

}






