#include "delay.h"
#include "CommandsDefine.h"
#include "AnalogChannelCtrl_Dictionary.h"
#include "ProjectConfig.h"
#include "Probe_MSO7000X_2d5g.h"
#include "AnalogChannelCtrl_2d5g_base.h"
#include "Helper.h"

#define PACKAGE_EXTERNALSET_DATA_BYTES 9

//////////////////////////////////////////////////////////////////////////////
// 			2203/5/X GHz_ljw 合并版,业务逻辑下沉
//////////////////////////////////////////////////////////////////////////////
u8 Get4094ExternalCtrlWord(struct Paras595 params, u32* ctrlWordCH,u8* ctrlWordExit)
{
	u8 _ctrlWord = 2;// 0b000000010; TRIG_SOURCE_SELECT_2=1 EXT , TRIG_HF_RJ_CTRL常低
	
	if((params.exterHFRejection & 1) == 1)
	{
		_ctrlWord|= 128; //0b10000000;
	}
	
	// 耦合 
	if(params.coupling == 0)//DC
	{
		_ctrlWord|= 0;// 0b00000000;
	}
	else if(params.coupling == 1) //AC
	{
		_ctrlWord|= 8;// 0b00001000;
	}
	else if(params.coupling == 2) //LFR
	{
		ctrlWordCH[0] |=  (u32)1<<31;// 1;
	}
	else if(params.coupling == 3) //HFR
	{
		ctrlWordCH[0] ^= (u32)1<<31;// 0;
	}
	else
	{
		return 0; //error
	}
	
	// 阻抗
	if(params.high_low == 1)//50
	{
		_ctrlWord|= 64;// 0b0100_0000;
	}
	else if(params.high_low == 0)//1M
	{
		_ctrlWord|= 0;// 0b000000000;
	}
	else
	{
		return 0; //error
	}
	
	//衰减
	if(params.att==1) //Ext
	{
		_ctrlWord|= 0;
	}
	else if(params.att==ATTLV1) //Ext 5
	{
		_ctrlWord|= 32;// 0b0010_0000;
	}
	else
	{
		return 0; //error
	}
	
	ctrlWordExit[0] = _ctrlWord;
	return 1;
}
u8 ExternalChannelSet_Check_Len(u8 *pData,u16 dataLen,u8 bIsContentBitInverted_Recv)
{
	if(dataLen < PACKAGE_EXTERNALSET_DATA_BYTES + PACKAGE_CHANNELSET_INDEX_BYTES)
	{
		return 0;
	}
	
	u8 channelCount=0;
	for (u8 i=0; i<MAX_CHANNEL_NUM; i++)
	{
		if (((1<<i) & pData[0])==1)
			channelCount++;
	}

	if((dataLen - PACKAGE_CHANNELSET_INDEX_BYTES) != (channelCount*PACKAGE_EXTERNALSET_DATA_BYTES))
	{
		return 0;
	}
	if (bIsContentBitInverted_Recv)
	{
		u8 checkDataLen=PACKAGE_CHANNELSET_INDEX_BYTES+channelCount*PACKAGE_EXTERNALSET_DATA_BYTES;
		return Helper_CheckInvertBytesIsRegist(pData,checkDataLen);
	}
	return 1;
}
//TRIG_LEVEL_2
void ExterDACPostion(u16 dac)
{
	u32 dacindex = 8-6;
	u32 dacindexEx = 0xC;
	u32 Port = 1;
	 
	u32 ConfigWord_dac =  0x10300000;
	ConfigWord_dac |= dacindexEx << 24;
	ConfigWord_dac |= Port << 16;
	ConfigWord_dac |= dac;
	DAC_AD5628(ConfigWord_dac,dacindex);
}
//TRIG_LEVEL_1
void ExterDACSensit(u16 dac)
{
	u32 dacindex = 8-6;
	u32 dacindexEx = 0xC;
	u32 Port = 3;
	 
	u32 ConfigWord_dac =  0x10300000;
	ConfigWord_dac |= dacindexEx << 24;
	ConfigWord_dac |= Port << 16;
	ConfigWord_dac |= dac;
	DAC_AD5628(ConfigWord_dac,dacindex);
}
void ExternalChannelSet(u8 *pData,u8 channelId)
{
	u8 att = pData[1];		
	u8 coupling = pData[2];
	u8 high_low = pData[3];
	u8 exterHFRejection = pData[8];
	u16 dacSensit = pData[4] + (pData[5]<<8); 
  u16 dacPostion = pData[6] + (pData[7]<<8);
	High8HC595 = 0;
	//控制字
	struct Paras595 params595;
	//dataType: 0-ref,1-offset_lz,2-offset_hz,3-move_lz,4-move_hz
	 
	//ConfigWord |= dacindexEx << 24;
	//ConfigWord |= Port << 16;
	//ConfigWord |= Data;
	
	//控制HC595/CD4094
	u32 ctrlWordCH595=Low32HC595;
	u8 ctrlWordExit595=0;
	params595.att = att;
	params595.coupling = coupling;
	params595.high_low = high_low;
	params595.exterHFRejection = exterHFRejection;
	u8 result = Get4094ExternalCtrlWord(params595,&ctrlWordCH595,&ctrlWordExit595);
	if(result==0){
		//参数错误
		return;
	}
	//High8HC595_new |= (ctrlWord595 >> 1);
	//Low32HC595_new |= (ctrlWord595 & 0x1) << 31;
	High8HC595 = ctrlWordExit595;
	Low32HC595 = ctrlWordCH595;
	//if(High8HC595_new!=High8HC595||Low32HC595_new!=Low32HC595)
	//{
		//CD_4094(High8HC595_new,1);
		//CD_4094(Low32HC595_new,4);
		CD_4094(High8HC595,1);
		CD_4094(Low32HC595,4);
		//delay_ms(1); 
		//Low32HC595 = Low32HC595_new;
		//High8HC595 = High8HC595_new;
	//}

	//dac
	//SendDataTo5675(8, 3, 0xc, (uint)dacSendValue); //0x8600??0v????
	ExterDACSensit(dacSensit);
	ExterDACPostion(dacPostion);
	delay_ms(2);
	
}

void CMD0x23_Request_ExternalChannelSet_process(u8 *pData,u16 dataLen,u8 bIsContentBitInverted_Recv,u8 bIsContentBitInverted_SendBack)
{
#ifdef RUNNING_WHERE_AT_BOOT	
	Command_SendbackErrorMessage(Error0x02_CannotRunAtBoot,1);
#else
	
	if(ExternalChannelSet_Check_Len(pData,dataLen,bIsContentBitInverted_Recv)==0)
	{ 
		Command_SendbackErrorMessage(Error0x05_CMD_ParaLentghError,1);
		return;
	}
	u8 channelId = pData[0];
	u8 channelCount=0;	
	for(int i=0; i<MAX_CHANNEL_NUM; i++)
	{
			if(((channelId>>i) & 0x01) == 1)
			{
				ExternalChannelSet(pData + 1 + channelCount * PACKAGE_EXTERNALSET_DATA_BYTES,i+1);
				channelCount++;
			}
	}		
	
#endif	
}
