/*----------------------------------------------------------------*
 *																  *
 *						电池电压读取模块				 		  *
 *																  *
 *						  作者：钱向红						  *
 *																  *
 *						  时间：2017-02-10					      *
 *																  *
 *----------------------------------------------------------------*/
 
#include "includes.h"

// 充放电改变标志
uint8_t bat_CurrentEvent = 0;
uint32_t Poweroff_time = 0;

// 初始化引脚
void Battery_Init(void)
{
	// ADC初始化和采样初始化一起
}

void Battery_Open()
{
	// ADC初始化和采样初始化一起
}

uint32_t DS2788_ReadBattery(data_dtu_t *sample)
{
	uint32_t adc = 0;
	static uint8_t last_PowerStatus = 0;
	uint8_t powerStatus;
	int16_t voltMulti = 1;	// 电压倍数
	
	// 为了避免ADC模块的使用冲突，测量电压和传感器采样在一起执行
	// 因称重传感器未用，故将电池电压的adc值存放在称重传感器的位置
	// 此处直接取出使用
	adc = dcBuff.sampleData.adWeight;
	
	// 获取采样电压：单位mV (3000mv * 610K / 100K / 4095 = 4.468864，参考电压3.0v，100K/610K分压)
	sample->batVoltage = adc * 4.468864;
	// 电池电压补偿（测量误差）
	sample->batVoltage += 80;

	// 电池串联节数
	if(sample->batVoltage >= 5800)
		voltMulti = sample->batVoltage / 2900;

	if(!dcBuff.configDisplay.op_SEND_GPS_DATA)
	{
		if(sample->batVoltage >= 3580 * voltMulti)
			sample->batPct = 100;
		else if(sample->batVoltage >= 3510 * voltMulti)
			sample->batPct = 80;
		else if(sample->batVoltage >= 3410 * voltMulti)
			sample->batPct = 60;
		else if(sample->batVoltage >= 3310 * voltMulti)
			sample->batPct = 40;
		else if(sample->batVoltage >= 3210 * voltMulti)
			sample->batPct = 20;
		else if(sample->batVoltage >= 3110 * voltMulti)
			sample->batPct = 10;
		else if(sample->batVoltage >= 3010 * voltMulti)
			sample->batPct = 5;
		else
			sample->batPct = 0;
		
		sample->batMaxCapa = 65535;
		adc = 76000ul / 100 * sample->batPct;
		if(adc > 65535)
			sample->batCapa = 65535;
		else
			sample->batCapa = adc;
	}
	else // 槽车-充电电池
	{
		if(sample->batVoltage >= 4180)
			sample->batPct = 100;
		else if(sample->batVoltage >= 4060)
			sample->batPct = 90;
		else if(sample->batVoltage >= 3980)
			sample->batPct = 80;
		else if(sample->batVoltage >= 3920)
			sample->batPct = 70;
		else if(sample->batVoltage >= 3870)
			sample->batPct = 60;
		else if(sample->batVoltage >= 3820)
			sample->batPct = 50;
		else if(sample->batVoltage >= 3790)
			sample->batPct = 40;
		else if(sample->batVoltage >= 3770)
			sample->batPct = 30;
		else if(sample->batVoltage >= 3740)
			sample->batPct = 20;
		else if(sample->batVoltage >= 3680)
			sample->batPct = 10;
		else if(sample->batVoltage >= 3450)
			sample->batPct = 5;
		else
			sample->batPct = 0;
		
		sample->batMaxCapa = 3000;
		sample->batCapa = sample->batMaxCapa / 100 * sample->batPct;
	}
	
	sample->batTempr = 0;
	
	// 检查充放电改变标志
	powerStatus = VCC_POWER_STATUS();
	if(powerStatus)
		sample->batCurrent = 1;
	else
		sample->batCurrent = -1;
	
	if(last_PowerStatus != powerStatus)
		bat_CurrentEvent = 1;
	else
		bat_CurrentEvent = 0;
	last_PowerStatus = powerStatus;
	
	if(!dcBuff.configDisplay.op_SEND_GPS_DATA)
	{	
		bat_veryLow = 0;
		sample->batLow = 0;
	}
	else	// 槽车-充电电池
	{
		// 电压低于3.77v(30%)，为极低状态
		// 电压高于3.82v(50%)，解除极低状态
		if(sample->batVoltage <= 3770)
			bat_veryLow = 1;
		if(sample->batVoltage >= 3820)
			bat_veryLow = 0;

		// 电压低于3.82v(50%)，为低状态
		// 电压高于3.92v(70%)，解除低状态
		sample->batLow = dcBuff.dtuData.batLow;
		if(sample->batVoltage <= 3820)
			sample->batLow = 1;
		if(sample->batVoltage >= 3920)
			sample->batLow = 0;
	}
	
	// 打印信息
	printf("batVoltage:\t%.2f v\n", (sample->batVoltage / 10) / 100.0);
	if(dcBuff.configDisplay.op_SEND_GPS_DATA)
	{
		printf("\nbat_veryLow:\t%d\n", bat_veryLow);
		printf("batLow:\t\t%d\n", sample->batLow);
	}
	printf("batPct:\t\t%d %%\n", sample->batPct);
	printf("batCapa:\t%d mAhr\n", sample->batCapa);
	printf("batMaxCapa:\t%d mAhr\n", sample->batMaxCapa);
	printf("batCurrent:\t%d mA\n", sample->batCurrent);
//	printf("batTempr:\t%d ℃\n", sample->batTempr);
	
	return TRUE;
}
