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

UART_HandleTypeDef* Huart_Bat_SF4314;

SP_MSP_Battery_parameters* P_Bat_SF4314;


uint8_t Bat_stas[3];
uint8_t Bat_stas_head[3];
uint8_t Bat_stas_Val[10];
uint8_t Bat_stas_Val_percentage[10];

uint8_t Battery_stas_check[2];
double_t Battery_result_sum;
uint16_t Batter_percentage;
uint8_t Battery_crc[10];
uint8_t Battery_crc_H;
uint8_t Battery_crc_L;
uint16_t Battery_integer;
uint16_t Battery_decimal;

int8_t Battery_Quit_Falg = 1;
uint8_t Bat_stas_Falg = 1;
int8_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_SF4314_Init(UART_HandleTypeDef* huart,SP_MSP_Battery_parameters *_Bat_SF4314)
{

	Huart_Bat_SF4314=huart;
	GF_BSP_Interrupt_Add_CallBack(DF_BSP_InterCall_RS485_1_RxCpltCallback, GF_Battery_Unscramble_Callback);//这句话的理解有问题
	HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
	P_Bat_SF4314 = _Bat_SF4314;
	return 1;
}

void GF_MSP_Battery_Inquiry()
{
	uint8_t Order[8];
	Order[0]=0X01;Order[1]=0X03;Order[2]=0x00;
	switch(Battery_Quit_Falg)
	{
		case 1:
			Bat_stas_Falg = 1;
			Order[3]=0X07;
			Order[4]=0X00;
			Order[5]=0x02;
			Order[6]=0x75;
			Order[7]=0xCA;

			break;
		case 2:
			Bat_stas_Falg = 2;
			Order[3]=0X05;
			Order[4]=0X00;
			Order[5]=0x02;
			Order[6]=0xD4;
			Order[7]=0x0A;

			break;
		case 3:
			Bat_stas_Falg = 3;
			Order[3]=0X09;
			Order[4]=0X00;
			Order[5]=0x01;
			Order[6]=0x54;
			Order[7]=0x08;

			break;
		default:
			Bat_stas_Falg = 1;
			break;
	}
	Battery_Quit_Falg=Battery_Quit_Falg+1;
	if(Battery_Quit_Falg>3)
	{
		Battery_Quit_Falg=1;
	}
	GF_BSP_UART_Transmit(1, Order, 8);//这句话的关联有问题
}


void  GF_Battery_Unscramble_Callback()
{

	unsigned short CRC_Value;

	switch(Battery_Falg)
	{
	case 1:
		if(Bat_stas[0]==0X01)
		{
			Bat_stas_head[0] = Bat_stas[0];
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
			Battery_Falg=2;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
			Battery_Falg=1;
		}
		break;
	case 2:
		if(Bat_stas[0]==0X03)
		{
			Bat_stas_head[1] = Bat_stas[0];
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
			Battery_Falg=3;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
			Battery_Falg=1;
		}
		break;
	case 3:
		if(Bat_stas[0]==0X04)
		{
			Bat_stas_head[2] = Bat_stas[0];
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas_Val,4);
			Battery_Falg=4;
		}
		else if(Bat_stas[0]==0x02)
		{
			Bat_stas_head[2] = Bat_stas[0];
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas_Val_percentage,2);
			Battery_Falg=6;
		}
		else
		{
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
			Battery_Falg=1;
		}
		break;
	case 4:
			HAL_UART_Receive_IT(Huart_Bat_SF4314,Battery_stas_check,2);
			Battery_Falg=5;
		break;
	case 5:
		for(int i=0;i<3;i++)
		{
			Battery_crc[i] = Bat_stas_head[i];
		}
		for(int i=0;i<4;i++)
		{
			Battery_crc[i+3] = Bat_stas_Val[i];
		}

		CRC_Value=ModBusCRC16(Battery_crc,7); // CRC校验
		Battery_crc_H = (uint8_t)CRC_Value;
		Battery_crc_L = (uint8_t)( CRC_Value>>8);

		if(Battery_crc_H==Battery_stas_check[0] && Battery_crc_L==Battery_stas_check[1])
			{
				Battery_Command_Comp(Bat_stas_Falg);
				Battery_Falg=1;
			}
		else
			{
				Battery_Falg=1;

			}
		HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
		break;
	case 6:

		HAL_UART_Receive_IT(Huart_Bat_SF4314,Battery_stas_check,2);

		Battery_Falg=7;
		break;

	case 7:
		for(int i=0;i<3;i++)
		{
			Battery_crc[i] = Bat_stas_head[i];
		}
		for(int i=0;i<2;i++)
		{
			Battery_crc[i+3] = Bat_stas_Val_percentage[i];
		}

		CRC_Value=ModBusCRC16(Battery_crc,5); // CRC校验
		Battery_crc_H = (uint8_t)CRC_Value;
		Battery_crc_L = (uint8_t)( CRC_Value>>8);

		if(Battery_crc_H==Battery_stas_check[0] && Battery_crc_L==Battery_stas_check[1])
			{
				Battery_Command_Comp(Bat_stas_Falg);
				Battery_Falg=1;
			}
		else
			{
				Battery_Falg=1;

			}
		HAL_UART_Receive_IT(Huart_Bat_SF4314,Bat_stas,1);
		break;

		default:
			break;
	}
}

void  Battery_Command_Comp(int8_t Bat_stas_Falg)
{
	switch(Bat_stas_Falg)
	{
	case 1:
	{
		Battery_integer = (Bat_stas_Val[0]<<8) | Bat_stas_Val[1];  //将数组里的数据组成16进制数据
		Battery_decimal = (Bat_stas_Val[2]<<8) | Bat_stas_Val[3];

		//16进制转十进制
		Battery_result_sum = (double)Battery_integer + ((double)Battery_decimal)*0.001;
		P_Bat_SF4314->Battery_Cur = Battery_result_sum;
		break;
	}
	case 2:
	{
		Battery_integer = (Bat_stas_Val[0]<<8) | Bat_stas_Val[1];
		Battery_decimal = (Bat_stas_Val[2]<<8) | Bat_stas_Val[3];
		Battery_result_sum = (double)Battery_integer + ((double)Battery_decimal)*0.001;
		P_Bat_SF4314->Battery_Vol = Battery_result_sum;

		break;
	}
	case 3:
	{
		Battery_integer = (Bat_stas_Val_percentage[0]<<8 | Bat_stas_Val_percentage[1]);
		Batter_percentage = Battery_integer;
		P_Bat_SF4314->Battery_SOC_Perc = Batter_percentage;
		break;
	}
	}
}










