#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"
#include "app_one_wire.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);
}

extern	void selfcheck_trigger_callback(char order);
unsigned char recbuf[4];
static char selfcheck_trigger_ok=0;
static unsigned int ms_cnt=0;
__IO char tsfd=0;
void wait_times(void)
{
		ms_cnt++;
		if(tsfd)
		{
			tsfd=0;
			selfcheck_trigger_callback((char)recbuf[0]);	
		}
		if(ms_cnt<1000)return;
//		SEGGER_RTT_printf(0,"wait_times out...\n");
		Process_Stop(wait_times);
		one_byte_receive_uninit();
		ms_cnt=0;selfcheck_trigger_ok=0;
		fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
}
void Event_self_check(unsigned char *buf,int length)
{
	tsfd=1;
//	SEGGER_RTT_printf(0,"Event_self_check out... %x\n",buf[0]);
}

void open_one_shel(void)
{
	if(selfcheck_trigger_ok)return;
	selfcheck_trigger_ok=1;
	one_byte_receive_init(recbuf,2,Event_self_check);
//	SEGGER_RTT_printf(0,"open_one_shel in...\n");
	Process_Start(1, "wait_times", wait_times);
	Process_SetHoldOn(wait_times, 1);
}


static void hal_charge_Process(void)
{
	#if ONE_WIRE_ENABLE
	pair_line_t CHARGE_state = app_chargepin_pairline();
	if(CHARGE != CHARGE_state){	//没充电
	if(1 == pair_state)return;//配对不检测充电
	#else
	
	uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
	if(!ch){	//没充电	
	#endif
		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();
		}
		return;
	}
	
	open_one_shel();
	if(GetBatteryPersent() >=100){
			if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:...\n");
				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();
				
		}
	}
}

void hal_charge_init(void)
{
	nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL); 
	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);
}


