#include "IIC_Base.h"
#include "sys.h" 
#include "delay.h"
#include "PCA9548.h"
#include "PCA9536.h"
#include "Probe_MSO7000X_2d5g.h"
 
//////////////////////////////////////////////////////////////////////////////////
// 			2203/4/X GHz_ljw 合并版
// 			2203/4/23 GHz_ljw GPIO初始化 Res_On 对齐到 CH_On 
//////////////////////////////////////////////////////////////////////////////
u8 probeOn1_Last = 1;
u8 probeOn2_Last = 1;
u8 probeOn3_Last = 1;
u8 probeOn4_Last = 1;
u8 probeOnRes_Last = 1;
 
u8 probeBtn1_Exit = 1;
u8 probeBtn2_Exit = 1;
u8 probeBtn3_Exit = 1;
u8 probeBtn4_Exit = 1;
u8 probeBtnRes_Exit = 1;
/*void SwitchIICRoadByPCA9548(unsigned char channelID)
{
	IIC_Start();
	IIC_Send_Byte(PCA9548_ADDR << 1);
	
	if (IIC_Wait_Ack() != 0)
	{
		goto cmd_fail;	
	}
		
	IIC_Send_Byte(channelID);
	
	if (IIC_Wait_Ack() != 0)
	{
		goto cmd_fail;	
	}
	
	IIC_Stop();
 
cmd_fail:
	IIC_Stop();	
}
unsigned char GetCurrentIICRoadPCA9548(void)
{
		u8 result=0;
	 
    IIC_Start();
    //IIC_Wait_Ack();  
    IIC_Send_Byte(PCA9548_ADDR);
    IIC_Wait_Ack();
    result=IIC_Read_Byte(0);
		IIC_NAck();
    IIC_Stop();//产生一个停止条件
    return result;	
}*/

void Probe_ReadFactoryInfo(unsigned char channelID,unsigned char *pbuffer,unsigned short len)
{
}
void Probe_SetLed(u8 led,u8 enable)
{
	if(led > 5 || led ==0)
	{
		return;
	}
	/*if(led==3||led==4)
	{
		u8 tst = 1 << (led-1);
		PCA9548_Switch(tst);
		delay_ms(1);
		u8 tst2 = PCA9548_Read();
		delay_ms(1);
		PCA9536_LedCtrl(enable);
		led=led;//test point
		tst=tst;
		tst2=tst2;
		
	}*/
	led-=1;
	//ledState&= enable << led;
	
	PCA9548_Switch(1 << led);
	delay_ms(1);
	PCA9536_LedCtrl(enable);
}
/*LedIsOn ：位控，bit0表示通道1，bit1:表示通道2.对应的bit为1表示亮灯，0表示不关闭*/
void Probes_LedCtrl(u8 LedIsOn)
{
		u8 CH1_LED_EN=(LedIsOn&0X01);
		u8 CH2_LED_EN=(LedIsOn&0X02)>>1;
		u8 CH3_LED_EN=(LedIsOn&0X04)>>2;
		u8 CH4_LED_EN=(LedIsOn&0X08)>>3;
		u8 CH_Res_LED_EN=(LedIsOn&0X10)>>4;
	
		Probe_SetLed(1,CH1_LED_EN);
		delay_ms(1);
		Probe_SetLed(2,CH2_LED_EN);
		delay_ms(1);
		Probe_SetLed(3,CH3_LED_EN);
		delay_ms(1);
		Probe_SetLed(4,CH4_LED_EN);
		delay_ms(1);
		Probe_SetLed(5,CH_Res_LED_EN); 
		delay_ms(1);
}

/*SingleOrDifference ：位控，bit0表示通道1，bit1:表示通道2.对应的bit为1表示Difference，0表示Single*/
void Probe_DifferenceOrSingleCtrl(unsigned short SingleOrDifference_DifferenceIs0)
{
}


/*读取探头的连接状态，是状态值[1表示按下状态，0表示松开状态]*/
/* 按位表示每个通道，bit0表示通道1，bit1表示通道2， bit2表示通道3*/
/*要转换为事件，请在应用层进行处理*/
unsigned short Probe_GetConnectState(void)
{
		//通道连接
    u8 probeOn1 = Probe1_On;
    u8 probeOn2 = Probe2_On;
    u8 probeOn3 = Probe3_On;
    u8 probeOn4 = Probe4_On;
		u8 probeOnRes = Probe_Res_On;
    u8 result= ~(0xE0 |probeOn1 | (probeOn2<<1)| (probeOn3<<2)
		| (probeOn4<<3) | (probeOnRes<<4));

		if(probeOn1_Last != Probe1_On && Probe1_On == 0)
		{
				delay_ms(30);
				if(Probe1_On == 0){
					PCA9548_Switch(1);
					delay_ms(1);
					PCA9536_Init();
				}
		}
		if(probeOn2_Last != Probe2_On && Probe2_On == 0)
		{
				delay_ms(30);
				if(Probe2_On == 0){	
					PCA9548_Switch(2);
					delay_ms(1);
					PCA9536_Init();
				}
		}
		if(probeOn3_Last != Probe3_On && Probe3_On == 0)
		{
				delay_ms(30);
				if(Probe3_On == 0)
				{	
					PCA9548_Switch(4);
					delay_ms(1);
					PCA9536_Init();
				}
		}
		if(probeOn4_Last != Probe4_On && Probe4_On == 0)
		{
				delay_ms(30);
				if(Probe4_On == 0)
				{	
					PCA9548_Switch(8);
					delay_ms(1);
					PCA9536_Init();
				}
		}
		if(probeOnRes_Last != Probe_Res_On && Probe_Res_On == 0)
		{
				delay_ms(30);
				if(Probe_Res_On == 0)
				{	
					PCA9548_Switch(16);
					delay_ms(1);
					PCA9536_Init();
				}
		}
		probeOn1_Last = Probe1_On;
		probeOn2_Last = Probe2_On;
		probeOn3_Last = Probe3_On;
		probeOn4_Last = Probe4_On;
		probeOnRes_Last = Probe_Res_On;	
		return result;
}
/*读取探头的按钮状态，是状态值[1表示按下状态，0表示松开状态]*/
/* 按位表示每个通道，bit0表示通道1，bit1表示通道2， bit2表示通道3*/
/*要转换为按下的动作，请在应用层进行处理*/
unsigned short Probe_GetButtonState(void)
{
		u8  probeBtn1 = Probe1_Btn;
		u8  probeBtn2 = Probe2_Btn;
		u8  probeBtn3 = Probe3_Btn;
		u8  probeBtn4 = Probe4_Btn;
		u8  probeBtnRes = Probe_Res_Btn;
		if(probeBtn1_Exit == 0)
		{
				probeBtn1 = 0;
				probeBtn1_Exit =1;
		}
		if(probeBtn2_Exit == 0)
		{
				probeBtn2 = 0;
				probeBtn2_Exit =1;
		}
		if(probeBtn3_Exit == 0)
		{
				probeBtn3 = 0;
				probeBtn3_Exit =1;
		}
		if(probeBtn4_Exit == 0)
		{
				probeBtn4 = 0;
				probeBtn4_Exit =1;
		}
		if(probeBtnRes_Exit == 0)
		{
				probeBtnRes = 0;
				probeBtnRes_Exit =1;
		}
	 
    //通道按钮	
    u8 result= ~(0xE0 |probeBtn1 | (probeBtn2<<1)
		| (probeBtn3<<2)| (probeBtn4<<3) | (probeBtnRes<<4));	
	
		return result;
}

void Probe_GPIO_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD, ENABLE);//使能GPIO 时钟

	//初始化设置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_6 ; //| GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//开漏
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
	GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化

	//====================================================================
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
	GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化
}
















