#include "app_chargerpin_conn_detect.h"
#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_flash.h"
#include "hal_battery.h"
#include "ble_comm.h"
#include "app_pair_chargerpin.h"
#include "hal_charge.h"
#include "fml_adc.h"

/************************ 函数声明 ***********************************/

/************************ 变量 ***********************************/
static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
static uint8_t pair_state =0;


static void pair_start_cb(){
	hal_charge_state = BLE_CHARGE_PULLOUT;
	pair_state =1;
}

static void pair_done_cb(){
	pair_state =0;
}

PAIR_START_REGISTER(pair_start_cb);
PAIR_DONE_REGISTER(pair_done_cb);


/********************************************************/
uint8_t hal_charge_Getstate(void)
{
	return hal_charge_state;
}

static void hal_send_charge(void)
{
	uint8_t sbuf[2]={0};
	uint8_t L = 0;
	if(mFlash.isHost) sbuf[L++] = 0;
	else sbuf[L++] = 1;
	sbuf[L++] = hal_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;
	DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
	hal_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);
//	DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
}

static void hal_charge_Vol_Charge_Send(void)
{
	uint8_t buf[10]={0};
	uint8_t L = 0;
	buf[L++] = GetBatteryPersent();
	buf[L++] = hal_charge_state;
  if(mFlash.isHost)BLE_Host_Tx_Send(0,BLE_CHAR_VOL,buf,L);
	else BLE_Client_Tx_Send(0,BLE_CHAR_VOL,buf,L);
}

static uint8_t OtherShoesVol = 0;
static uint8_t otherShoes_charge = BLE_CHARGE_PULLOUT;
static void cb_BLE_R_CHAR_VOL(void* handle)
{	
	BLE_Client_Rx_t* target = handle;
	OtherShoesVol = target->pDat[0];
	otherShoes_charge = target->pDat[1];
	if(!mFlash.isHost)hal_charge_Vol_Charge_Send();
	DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
}

static void cb_BLE_Host_R_CHAR_VOL(void* handle)
{	
	BLE_Host_Rx_t* target = handle;
	OtherShoesVol = target->pDat[0];
	otherShoes_charge = target->pDat[1];
//	DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
}

short ChargeADC_value =0;

static void hal_charge_Process(void)
{
	static uint8_t client_connect =0;
	
	if(slave_isconnect() && !client_connect){
		hal_send_charge();
		uint8_t data =0;
		BLE_Host_Tx_Send(0,BLE_CHARGE,&data,1);
	}
	else if(!slave_isconnect() && client_connect && (!mFlash.isHost)){
		OtherShoesVol = 0;
    otherShoes_charge =0;
	}
	
	if(client_connect != slave_isconnect())client_connect = slave_isconnect();
	
	pair_line_t CHARGE_state = app_chargepin_pairline();
	
	if(1 == pair_state)return;//配对不检测充电
	
	if(CHARGE != CHARGE_state){	//没充电
		if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
			hal_charge_state = BLE_CHARGE_PULLOUT;
			Process_SetHoldOn(hal_charge_Process,0);
			hal_send_charge();
			OtherShoesVol = 0;
      otherShoes_charge =0;
			if(mFlash.isHost)hal_charge_Vol_Charge_Send(); //共享充电电量和充电状态
		}
		return;
	}
	
	if(mFlash.isHost)hal_charge_Vol_Charge_Send(); //共享充电电量和充电状态
	if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE) && 
		GetBatteryPersent() >=100 && 
	  OtherShoesVol >=100 ){	//左右鞋都充满电的情况下
			if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol);
				hal_charge_state = BLE_CHARGE_DONE;
				Process_SetHoldOn(hal_charge_Process,1);
				hal_send_charge();
		}
	}else{	//正在充电
		if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
			hal_charge_state = BLE_CHARGE_INSERT;
			Process_SetHoldOn(hal_charge_Process,1);
			hal_send_charge();
		}
	}
}

static void hal_charge_monitor_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						charge_vol = 0;
//	int16_t						charge_threshold = 0;
	static uint32_t 	charge_cycle = 50;
	static int16_t		before_charge_vol = 0;		//充电前的电压值
	static int16_t		charge_vol_max = 0;				//充电期间最大的电压值
	int16_t    adcval =0;
	if(BLE_CHARGE_PULLOUT == hal_charge_state)//没充电
	{		
		if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
		charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
		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){
			if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
			charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
			charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
			charge_cycle--;
			return;
		}
		if(-1 == fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
		charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
		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;	//充电前和充电期间的充电电压变化值
	}
}

void hal_charge_init(void)
{
	BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL);
	BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_Host_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,"charge",hal_charge_Process);
	hal_charge_Process();
	
	Process_Start(10,"charge_monitor",hal_charge_monitor_process);
}


