#include "usr_config.h"
#include "bsp_time.h"
#include "system.h"
#include "hal_led.h"
#include "nrf_gpio.h"
#include "hal_ble_client.h"
#include "hal_ble_host.h"
#include "app_charge.h"
#include "app_flash.h"
#include "hal_battery.h"
#include "hal_ble_uart0.h"
#include "ble_comm.h"

/************************ 函数声明 ***********************************/
void app_charge_Process(void);
void cb_BLE_Client_T_CHARGE(void* handle);

/************************ 变量 ***********************************/
static uint8_t app_charge_state = BLE_CHARGE_PULLOUT;

/********************************************************/
uint8_t app_charge_Getstate(void)
{
	return app_charge_state;
}

void send_charge(void)
{
	static uint8_t sbuf[2];
	uint8_t L = 0;
	if(mFlash.isHost) sbuf[L++] = 0;
	else sbuf[L++] = 1;
	sbuf[L++] = app_charge_state;
	BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
}

void cb_BLE_Client_R_CHARGE(void* handle)
{
	BLE_Client_Rx_t* target = handle;
	send_charge();
	BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
}

void cb_BLE_Host_R_CHARGE(void* handle)
{
	BLE_Host_Rx_t* target = handle;
	BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
	
//	SEGGER_RTT_printf(0,"cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
}

void cb_BLE_Client_T_CHARGE(void* handle) 
{
	SEGGER_RTT_printf(0,"cb_BLE_Client_T_CHARGE\n");
}

void app_charge_Vol_Send(BLE_CMD_n cmd)
{
	uint8_t buf[32];
	uint8_t L = 0;
	
	buf[L++] = GetBatteryPersent();
	if(mFlash.isHost)BLE_Host_Tx_Send(0,cmd,buf,L);
	else BLE_Client_Tx_Send(0,cmd,buf,L);
}

static uint8_t OtherShoesVol = 0;
void cb_BLE_CLient_R_CHAR_VOL(void* handle)
{	
	UART0_Rx_t* target = handle;
	OtherShoesVol = target->pDat[0];
//	SEGGER_RTT_printf(0,"OtherShoesVol=%d\n",OtherShoesVol);
}

static void app_charge_Process(void)
{
	uint8_t client_connect =0;
	uint8_t host_connect =0;
	static uint8_t ResponseFlag =0;
	
	app_charge_Vol_Send(BLE_CHAR_VOL); //共享充电电量
	
	uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
	
	//进入充电状态
	client_connect = slave_isconnect();
	if(mFlash.isHost){
		 host_connect = host_isconnect();
	}
	else{
		 host_connect = slave_isconnect();
	}
	
//		SEGGER_RTT_printf(0,"charge state:%d\n",ch);
	if(!ch){	//没充电
		if(app_charge_state!=BLE_CHARGE_PULLOUT){ SEGGER_RTT_printf(0,"charge out...\n");
			app_charge_state = BLE_CHARGE_PULLOUT;
			Process_SetHoldOn(app_charge_Process,0);
			LED_Stop(LED_CHARGE);
			if(client_connect){
				ResponseFlag = 0;
				send_charge();
			}
			else ResponseFlag =1;
		}
		return;
	}
	
	if(host_connect && GetBatteryPersent()>=100 && OtherShoesVol>=100){	//充满
		if(app_charge_state!=BLE_CHARGE_DONE){ SEGGER_RTT_printf(0,"charge done:%d...\n",OtherShoesVol);
			app_charge_state = BLE_CHARGE_DONE;
			Process_SetHoldOn(app_charge_Process,1);
			LED_Start(LED_CHARGE,COLOR_GREEN);
			send_charge();
		}
	}else{	//正在充电
		if(app_charge_state!=BLE_CHARGE_INSERT){ SEGGER_RTT_printf(0,"charge in...\n");
			app_charge_state = BLE_CHARGE_INSERT;
			Process_SetHoldOn(app_charge_Process,1);
			LED_Start(LED_CHARGE,COLOR_ORANGE);
			if(client_connect){
				send_charge();
				ResponseFlag = 0;
			}
			else ResponseFlag =1;
		}
	}
	if(client_connect && ResponseFlag){
		ResponseFlag =0;
		send_charge();
	}
}

static void app_charge_monitor_battery_charge_data_process(void)
{
	#define BATTERY_VOL_THRESHOLD_MIN											2500		//2.5V
	#define BATTERY_VOL_THRESHOLD_MAX											4000		//4V
	#define CHARGE_VOL_THRESHOLD													80			//充电前和充电期间的充电电压变化值,单位mv
	
	//监测电池和充电数据
	int16_t						bat_vol;
	int16_t						charge_vol;
	int16_t						charge_threshold;
	uint32_t 					ch = nrf_gpio_pin_read(PIN_CHARGING);
	static uint32_t 	charge_cycle = 50;
	static int16_t		before_charge_vol;		//充电前的电压值
	static int16_t		charge_vol_max;				//充电期间最大的电压值
	
	if(!ch)//没充电
	{		
		charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
		before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
		charge_vol_max = 0;
		charge_cycle = 50;
	}else	//充电
	{			
		/* 过筛50轮 */
		if(charge_cycle != 0){
			charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
			charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
			charge_cycle--;
			return;
		}
		charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
		charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;		
		bat_vol    = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
		
		//当电池电量没满，充电（经测试，电量没满的充电电压跟电池电压有关，最小充电电压100+mv）
		charge_threshold = 	charge_vol_max - before_charge_vol;	//充电前和充电期间的充电电压变化值
//		DEBUG_LOG("charge_threshold:%d,%d,%d,bat:%d\n",charge_threshold,ob_monitor.charge_vol_max,ob_monitor.before_charge_vol,bat_vol);
		if(charge_threshold < CHARGE_VOL_THRESHOLD)
		{		
			Except_SetExceptype(EXCEPT_DATA_CHARGE);
			Except_TxError(EXCEPT_DATA_CHARGE,"except_charging_chip");
		}
		else
		{
			Except_ClearExceptype(EXCEPT_DATA_CHARGE);
		}
		
		//电池小于2.5V或充电电压变化小于阈值且电池电压大于4V
		if(bat_vol <= BATTERY_VOL_THRESHOLD_MIN || (charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol > BATTERY_VOL_THRESHOLD_MAX))
		{
			Except_SetExceptype(EXCEPT_DATA_BATTERY);
			Except_TxError(EXCEPT_DATA_BATTERY,"except_battery");
		}
		else
		{
			Except_ClearExceptype(EXCEPT_DATA_BATTERY);
		}
	}
}

void app_charge_Init(void)
{
	nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL); 

	if(mFlash.isHost) 
		BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_CLient_R_CHAR_VOL);
	else 
		BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_CLient_R_CHAR_VOL);
	
	BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
	BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
	Process_Start(1000,"app_charge",app_charge_Process);
	app_charge_Process();
	
	Process_Start(10,"app_charge_monitor_battery_charge_data_process",app_charge_monitor_battery_charge_data_process);
}


