#include "delay.h"
#include "ProbeEEPROM.h"
#include "24CXX.h"
#include "PCA9548.h"
//////////////////////////////////////////////////////////////////////////////
// 			2203/4/X GHz_ljw 合并版
// 			2203/5/9 GHz_ljw 加入写产品信息
//////////////////////////////////////////////////////////////////////////////

u16 ProbeEEPROM_Read(u8* cmd,u8* readDatas)
{
		u8 CH1_CTRL_CMD=(cmd[0]&0X01);
		u8 CH2_CTRL_CMD=(cmd[0]&0X02);
		u8 CH3_CTRL_CMD=(cmd[0]&0X04);
		u8 CH4_CTRL_CMD=(cmd[0]&0X08);
		u8 CH_Res_CTRL_CMD = (cmd[0]&0X10);
		u8 SendChannelId=0;

		//u8 POWER_EN=(cmd[4]&0X01);
		if(CH1_CTRL_CMD != 0) {
				SendChannelId = 1;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);			
		}
		if(CH2_CTRL_CMD != 0) {
				SendChannelId = 2;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
			
		}
		if(CH3_CTRL_CMD != 0) {
				SendChannelId = 4;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}
		if(CH4_CTRL_CMD != 0) {
				SendChannelId = 8;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}
		if(CH_Res_CTRL_CMD!=0)
		{
				SendChannelId = 16;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}			
		if(SendChannelId!= PCA9548_Read())
		{
			return 0;
		}
		//if(SendChannelId!=0 && AT24CXX_Check()==1)
		if(SendChannelId!=0 )
		{ 		
				AT24CXX_Read(0,readDatas,EEPROM_DATA_LEN);
				return EEPROM_DATA_LEN;
		}
		return 0;
}

u8 ProbeEEPROM_Write(u8* cmd,u8* writeDatas)
{
	
		u8 CH1_CTRL_CMD=(cmd[0]&0X01);
		u8 CH2_CTRL_CMD=(cmd[0]&0X02);
		u8 CH3_CTRL_CMD=(cmd[0]&0X04);
		u8 CH4_CTRL_CMD=(cmd[0]&0X08);
		u8 CH_Res_CTRL_CMD = (cmd[0]&0X10);
		u8 SendChannelId=0;
		 
		//u8 POWER_EN=(cmd[4]&0X01);
		if(CH1_CTRL_CMD != 0) {
				SendChannelId = 1;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);	
		
		}
		if(CH2_CTRL_CMD != 0) {
				SendChannelId = 2;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}
		if(CH3_CTRL_CMD != 0) {
				SendChannelId = 4;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}
		if(CH4_CTRL_CMD != 0) {
				SendChannelId = 8;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}
		if(CH_Res_CTRL_CMD!=0)
		{
				SendChannelId = 16;
				PCA9548_Switch(SendChannelId);
				delay_ms(1);
		}			
		if(SendChannelId!= PCA9548_Read())
		{
			return 0;
		}
		if(SendChannelId!=0)
		{ 		
				return AT24CXX_Write(0,writeDatas,EEPROM_DATA_LEN); 
		}
		return 0;
}
