#include "app_chargerpin_conn_detect.h"
#include "system.h"
#include "bsp_time.h"
#include "bsp_adc.h"
#include "ble_comm.h"
#include "hal_led.h"
#include "hal_mt.h"
#include "app_flash.h"
#include "app_pair_chargerpin.h"
#include "app_one_wire.h"
#include "fml_adc.h"

#define SHELF_CHECK_ADC_VALUE 1800

//=================debug=============
#include "ringframe.h"
extern char logbuftemp[50];
extern int logbuftemp_len ;
extern ringframe_t battlog;
#define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}}

//void cb_BLE_Client_PRADC(void* handle)
//{
////    BLE_Client_Rx_t* target = handle;
//    DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1);
//	BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR);
//}

//=================debug=============
extern void app_pair_client_process_case1(void);
extern void app_pair_client_process(void);
struct client_ram_type{
	char init;
	char statu;
	short adc_value;
	pair_line_t pair_line;
	unsigned int delay_pair_count;
	Event into_event;
	Event over_event;
}client_ram={0,0,DISCONNECT_CHARGE,0,NULL,NULL};


struct{
	char init;
	char statu;
	unsigned int pulluppcs_timer;
	short adc_value;
	pair_line_t pair_line;
	unsigned int delay_pair_count;
	Event into_event;
	Event over_event;
}host_ram={0,0,0,DISCONNECT_CHARGE,0,NULL,NULL};
extern void app_pair_host_process(void);
extern void app_pair_host_process_case1(void);
extern void app_pair_host_process_case2(void);
void fml_adc_callback(uint32_t sample_point, Fml_Adc_All_Channel_Adc_Value_t all_adc_value)
{
	#if 1
	switch(sample_point)
	{
		case FML_ADC_SAMPLE_POINT_0:
		case FML_ADC_SAMPLE_POINT_2:
			//PAIR_CHARGERPIN_PRINT("FML_ADC_SAMPLE_POINT_2 NULL %d\n",all_adc_value.channel_4_adc_value);
			if(host_ram.init)
			{
				if(host_ram.statu == 1)
				{
					host_ram.adc_value=all_adc_value.channel_4_adc_value;
					app_pair_host_process_case1();
				}
			}
			break;
		case FML_ADC_SAMPLE_POINT_1:
			//PAIR_CHARGERPIN_PRINT("FML_ADC_SAMPLE_POINT_1 NULL %d\n",all_adc_value.channel_4_adc_value);
			if(host_ram.init)
			{
				if(host_ram.statu == 2)
				{
					host_ram.adc_value=all_adc_value.channel_4_adc_value;
					app_pair_host_process_case2();
				}
			}
			break;
	}
	
	if(client_ram.init)
	{
		if(client_ram.statu == 1)
		{
			client_ram.adc_value=all_adc_value.channel_4_adc_value;
			app_pair_client_process_case1();
		}
	}
	
	#else
	static char asjdhaskjhdki=0;
	PAIR_CHARGERPIN_PRINT("ADC_Read fml_adc_callback %d\n", all_adc_value.channel_4_adc_value);
	if(asjdhaskjhdki%2==0)fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_PULLUP);
	else fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
	asjdhaskjhdki++;
	#endif
}



//====================================================================================
#define ADC_CHECKMS 1000
#define PAIROVERTIME 3000
#define DELAY_PAIR_COUNT 100


void app_pair_host_init(Event into,Event over)
{
	host_ram.init=1;
	host_ram.adc_value=0;
	host_ram.delay_pair_count=0;
	host_ram.pulluppcs_timer=0;
	host_ram.statu=0;
	host_ram.pair_line=DISCONNECT_CHARGE;
	host_ram.into_event=into;
	host_ram.over_event=over;
	fml_adc_sample_update_notify_register(fml_adc_callback);
	Process_Start(ADC_CHECKMS, "app_pair_host_process", app_pair_host_process);
}
void app_pair_host_uninit(void)
{
	host_ram.init=0;
	Process_Stop(app_pair_host_process);
}

#define DISCON_HOST_MAX_ADC 1200
#define DISCON_HOST_MIN_ADC 600

#define CON_HOST_MAX_ADC DISCON_HOST_MIN_ADC
#define CON_HOST_MIN_ADC 300

static void null_pcs(void)
{
}
#define UPPULL_MS 3
//-------------------------------------------
unsigned char shefcheck_buff[5];
char shefcheck_event_fag=0;
check_callbackt check_callbackt_ram;
Event check_callback_find_hi_ram;
void shefcheck_event(unsigned char *buff, int length)
{
	shefcheck_event_fag=1;
}
void check_callback_regist(check_callbackt evt)
{
check_callbackt_ram=evt;
}
void check_callback_find_hi_regist(Event evt)
{
check_callback_find_hi_ram=evt;
}
//-------------------------------------------

void app_pair_host_process_case1(void)
{
				if(mFlash.isHost!=HOST_SHOSE)
				{
					app_pair_host_uninit();
					app_pair_chargerpin_Init();
					PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost );
				}
//==============================s
				if(host_ram.adc_value>DISCON_HOST_MAX_ADC)
				{
					PAIR_CHARGERPIN_PRINT("CHARGE\n");
					if(host_ram.pair_line!=CHARGE)
					{
						PAIR_CHARGERPIN_INFO("INTO CHARGE %d\n", mFlash.isHost);
						host_ram.pair_line=CHARGE;
					}
					host_ram.statu=1;
				}
				
				PAIR_CHARGERPIN_PRINT("ADC f(  %d  ):isHost %d Ticks %d\n", host_ram.adc_value, mFlash.isHost,TIME_GetTicks());
				if(host_ram.adc_value>SHELF_CHECK_ADC_VALUE)
				{
					PAIR_CHARGERPIN_PRINT("------------check_callback_find_hi_ram-----check_callbackt_ram---------%d %d\n", mFlash.isHost, TIME_GetTicks());
					if(check_callbackt_ram)
					{
						host_ram.statu=11;
						host_ram.delay_pair_count=TIME_GetTicks();
						Process_UpdatePeroid(app_pair_host_process,100);
						Process_SetHoldOn(app_pair_host_process,1);
						one_byte_receive_init(shefcheck_buff,2,shefcheck_event);
						shefcheck_event_fag=0;
						fml_adc_remove_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
						if(check_callback_find_hi_ram)check_callback_find_hi_ram();
						PAIR_CHARGERPIN_INFO("INTO SHELF_CHECK_ADC_VALUE %d\n", mFlash.isHost);
						PAIR_CHARGERPIN_PRINT("------------check_callback_find_hi_ram--------------%d %d\n",  mFlash.isHost, TIME_GetTicks());
						return;
					}
					else
					{
						PAIR_CHARGERPIN_PRINT("check_callbackt_ram NULL\n");
					}
				}
//==============================e
				fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_PULLUP);
				PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP); %d \n",TIME_GetTicks() );
				host_ram.pulluppcs_timer = TIME_GetTicks();
				Process_Start(0, "null_pcs", null_pcs);
				Process_SetHoldOn(null_pcs, 1);
				host_ram.statu = 2;
}

void app_pair_host_process_case2(void)
{
				
				fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
				Process_Stop(null_pcs);
				log("ad:%d,%d\n",host_ram.adc_value, host_ram.pair_line);
				PAIR_CHARGERPIN_PRINT("ADC (  %d  ): pair_line %d\n", host_ram.adc_value, host_ram.pair_line);
						
				if(host_ram.adc_value>DISCON_HOST_MIN_ADC)
				{
					PAIR_CHARGERPIN_PRINT("DISCONNECT_CHARGE\n");
					if(host_ram.pair_line!=DISCONNECT_CHARGE)
					{
						PAIR_CHARGERPIN_INFO("INTO DISCONNECT_CHARGE %d\n", mFlash.isHost);
						host_ram.pair_line=DISCONNECT_CHARGE;
					}
					host_ram.statu=1;
				}
				else if(host_ram.adc_value>CON_HOST_MIN_ADC)
				{
					PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n");
					if(host_ram.pair_line!=CONNECT_NO_CHARGE)
					{
						host_ram.pair_line=CONNECT_NO_CHARGE;
						fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_PULLUP);
						host_ram.delay_pair_count=TIME_GetTicks();
						
						Process_UpdatePeroid(app_pair_host_process,1);
						Process_SetHoldOn(app_pair_host_process,1);
						
						fml_adc_remove_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
						
						host_ram.statu=3;		
						PAIR_CHARGERPIN_INFO("INTO CONNECT_NO_CHARGE %d\n", mFlash.isHost);		
					}
					else
					{
						host_ram.statu=1;
					}
				}
				else
				{
					PAIR_CHARGERPIN_PRINT("ADCVALUE ======================================================ERROR %d\n", host_ram.adc_value);
					host_ram.statu=1;
				}
}

static void app_pair_host_process(void)
{
	for(int i=1;i>0;i--)
	{
		switch(host_ram.statu)
		{
			case 0:
				
				fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
				
				host_ram.statu=1;
				break;
			case 1:

				break;
			case 2:
			
				break;
			case 3://¨¦?¨¤-¨¨?¡ä¨®?¨²¡¤¡é??????D?o?
				if ((TIME_GetTicks() - host_ram.delay_pair_count > ADC_CHECKMS+ADC_CHECKMS))
				{
					PAIR_CHARGERPIN_PRINT(" UP pull over time  %d \n", TIME_GetTicks());
					log("pull over time\n");
					nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
					Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_host_process,0);
					host_ram.statu=1;
				}
				else if(nrf_gpio_pin_read(PIN_CHARGING))//?¨¢¦Ì?¨¦?¨¤-D?o?¨°?o¨®¡ê??¦Ì?¡Âclient¨°??-¨¢??¨®¨¦?
				{
					PAIR_CHARGERPIN_PRINT(" nrf_gpio_pin_read(PIN_CHARGING SUCCESS \n", TIME_GetTicks());
					host_ram.statu=4;
					host_ram.delay_pair_count=TIME_GetTicks();
					nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
				}
				break;
			case 4:
				if(nrf_gpio_pin_read(PIN_CHARGING)==0)
				{
					PAIR_CHARGERPIN_PRINT(" into_event  %d \n", TIME_GetTicks());
					host_ram.statu=5;
					host_ram.delay_pair_count=TIME_GetTicks();
					
				
					fml_adc_remove_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
					
					
					log("into_event-H\n");
					host_ram.into_event();
					
				}
				else if(TIME_GetTicks() - host_ram.delay_pair_count > 500)
				{
					PAIR_CHARGERPIN_PRINT("  wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
					host_ram.statu=0;
					Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_host_process,0);	
					i=3;					
				}
				break;
			case 5:
				if (TIME_GetTicks() - host_ram.delay_pair_count > PAIROVERTIME)
				{
					PAIR_CHARGERPIN_PRINT("  over_event %d\n", TIME_GetTicks());
					host_ram.statu=0;
					host_ram.over_event();
					Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_host_process,0);	
					i=3;
				}
				break;
//==============================s				
			case 11:
				if((TIME_GetTicks() - host_ram.delay_pair_count > 5000)||(shefcheck_event_fag))
				{
					if(shefcheck_event_fag)
					{
						if(check_callbackt_ram)check_callbackt_ram(shefcheck_buff[0]);
					}
					else 
					{
						PAIR_CHARGERPIN_PRINT("  =====================wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
					}
					host_ram.statu=0;
					Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_host_process,0);
					one_byte_receive_uninit();
					PAIR_CHARGERPIN_INFO("EXTI SHELF_CHECK_ADC_VALUE %d\n", mFlash.isHost);
					i=3;					
				}
				break;
//==============================s
		}
	}
}
//============================================================================================================


#define CONN_MAX_ADC 1000
#define CONN_MIN_ADC 600
void app_pair_client_init(Event into,Event over)
{
	client_ram.init=1;
	client_ram.adc_value=0;
	client_ram.delay_pair_count=0;
	client_ram.statu=0;
	client_ram.pair_line=DISCONNECT_CHARGE;
	client_ram.into_event=into;
	client_ram.over_event=over;
	fml_adc_sample_update_notify_register(fml_adc_callback);
	Process_Start(ADC_CHECKMS, "app_pair_client_process", app_pair_client_process);
}
void app_pair_client_uninit(void)
{
	client_ram.init=0;
	Process_Stop(app_pair_client_process);
}

void app_pair_client_process_case1(void) 
{
				if(mFlash.isHost==HOST_SHOSE)
				{
					app_pair_client_uninit();
					app_pair_chargerpin_Init();
					PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost );
				}

				log("ad:%d,%d\n",client_ram.adc_value, client_ram.pair_line);
				PAIR_CHARGERPIN_PRINT("ADC (  %d  ):isHost %d Ticks %d\n", client_ram.adc_value, mFlash.isHost,TIME_GetTicks());
			
				if(client_ram.adc_value>CONN_MAX_ADC)
				{
					PAIR_CHARGERPIN_PRINT("CHARGE\n");
					if(client_ram.pair_line!=CHARGE)
					{
						PAIR_CHARGERPIN_INFO("INTO CHARGE %d\n", mFlash.isHost);
						client_ram.pair_line=CHARGE;
					}
				}
				else if(client_ram.adc_value>CONN_MIN_ADC)
				{
					PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n");
					if(client_ram.pair_line!=CONNECT_NO_CHARGE)
					{
						PAIR_CHARGERPIN_INFO("INTO CONNECT_NO_CHARGE %d\n", mFlash.isHost);
						client_ram.pair_line=CONNECT_NO_CHARGE;	

						
						fml_adc_remove_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
						
						
						nrf_gpio_cfg(
							PIN_CHARGING,
							NRF_GPIO_PIN_DIR_OUTPUT,
							NRF_GPIO_PIN_INPUT_CONNECT,
							NRF_GPIO_PIN_NOPULL,
							NRF_GPIO_PIN_D0H1,
							NRF_GPIO_PIN_NOSENSE);
						nrf_gpio_cfg_watcher(PIN_CHARGING);
						nrf_gpio_pin_set(PIN_CHARGING);//¨¤-??100ms
						
						client_ram.delay_pair_count=TIME_GetTicks();
						Process_UpdatePeroid(app_pair_client_process,1);
						Process_SetHoldOn(app_pair_client_process,1);
						client_ram.statu=2;							
										
					}
				}
				else
				{
					if(client_ram.pair_line!=DISCONNECT_CHARGE)
					{
						PAIR_CHARGERPIN_INFO("INTO DISCONNECT_CHARGE %d\n", mFlash.isHost);
						client_ram.pair_line=DISCONNECT_CHARGE;
					}
				}
				
//==============================s				
				if(client_ram.adc_value>SHELF_CHECK_ADC_VALUE)
				{
					if(check_callbackt_ram)
					{
						client_ram.statu=11;
						client_ram.delay_pair_count=TIME_GetTicks();
						Process_UpdatePeroid(app_pair_client_process,100);
						Process_SetHoldOn(app_pair_client_process,1);
						one_byte_receive_init(shefcheck_buff,2,shefcheck_event);
						shefcheck_event_fag=0;
						fml_adc_remove_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
						PAIR_CHARGERPIN_INFO("INTO SHELF_CHECK_ADC_VALUE %d\n", 1);
						if(check_callback_find_hi_ram)
						{
							check_callback_find_hi_ram();
						}
						return;
					}
					else
					{
						PAIR_CHARGERPIN_PRINT("check_callbackt_ram NULL\n");
					}
				}
//==============================e					
}
static void app_pair_client_process(void)
{
	for(int i=1;i>0;i--)
	{
		switch(client_ram.statu)
		{
			case 0:
				
				fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
				
				client_ram.statu=1;
				break;
			case 1:
	
				break;
			case 2:
				if(TIME_GetTicks() - client_ram.delay_pair_count > 100)//¦Ì¨¨100ms
				{
					nrf_gpio_pin_clear(PIN_CHARGING);
					client_ram.statu=3;
					client_ram.delay_pair_count=TIME_GetTicks();
				}
				break;
			case 3:
				if(nrf_gpio_pin_read(PIN_CHARGING)==0)
				{
					log("into_event-C\n");
					client_ram.into_event();	
					client_ram.delay_pair_count=TIME_GetTicks();
					PAIR_CHARGERPIN_PRINT("  into_event-C %d\n", TIME_GetTicks());
					PAIR_CHARGERPIN_PRINT("  wait pair %d\n", nrf_gpio_pin_read(PIN_CHARGING));	
					client_ram.statu=4;
				}
				else if(TIME_GetTicks() - client_ram.delay_pair_count > 100)
				{
					PAIR_CHARGERPIN_PRINT("  wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
					client_ram.statu=0;
					Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_client_process,0);	
					i=3;					
				}
				break;
			case 4:
				if (TIME_GetTicks() - client_ram.delay_pair_count > PAIROVERTIME)
				{
					client_ram.statu=0;
					client_ram.over_event();
					Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_client_process,0);					
					PAIR_CHARGERPIN_PRINT("  over_event %d\n", TIME_GetTicks());
					i=3;
				}
				break;
//==============================s				
			case 11:
				if((TIME_GetTicks() - client_ram.delay_pair_count > 5000)||(shefcheck_event_fag))
				{
					if(shefcheck_event_fag)
					{
						if(check_callbackt_ram)check_callbackt_ram(shefcheck_buff[0]);
					}
					else 
					{
						PAIR_CHARGERPIN_PRINT("  =====================wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
					}
					client_ram.statu=0;
					Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
					Process_SetHoldOn(app_pair_client_process,0);
					one_byte_receive_uninit();
					PAIR_CHARGERPIN_INFO("EXTI SHELF_CHECK_ADC_VALUE %d\n", 1);
					i=3;					
				}
				break;
//==============================s
		}
	}
}


pair_line_t app_chargepin_pairline(void)
{
	if (host_ram.init)
	{
		return host_ram.pair_line;
	}
	if (client_ram.init)
	{
		return client_ram.pair_line;
	}
	return 0xff;
}




