// Hard.cpp: implementation of the CHard class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "VCDSO.h"
#include "Hard.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHard::CHard()
{
	ULONG i = 0;
	m_nDeviceIndex = 0;
	m_nDeviceNum = 0;
	for(i=0;i<MAX_CH_NUM;i++)
	{
		m_pSrcData[i] = new short[BUF_10K_LEN];
	}
	m_clrRGB[CH1] = RGB(255,255,0);
	m_clrRGB[CH2] = RGB(0,255,255);
	m_clrRGB[CH3] = RGB(255,0,255);
	m_clrRGB[CH4] = RGB(0,255,0);
	memset(m_nCalLevel,0,sizeof(m_nCalLevel));
	m_nTimeDIV = 2;
	m_nYTFormat = 0;
	m_stControl.nCHSet = 0x0F;////Factory Setup
	m_stControl.nTimeDIV = m_nTimeDIV;//Factory Setup
	m_stControl.nTriggerSource = CH1;//Factory Setup
	m_stControl.nHTriggerPos = 2;//Factory Setup
	m_stControl.nVTriggerPos = 64;//Factory Setup
	m_stControl.nTriggerSlope = RISE;//Factory Setup
	m_stControl.nBufferLen = BUF_10K_LEN;//Factory Setup
	m_stControl.nReadDataLen = BUF_10K_LEN;//Factory Setup
	m_stControl.nAlreadyReadLen = BUF_10K_LEN;//Factory Setup
	m_stControl.nALT = 0;//Factory Setup
	for(i=0;i<MAX_CH_NUM;i++)
	{
		RelayControl.bCHEnable[i] = 1;
		RelayControl.nCHVoltDIV[i] = 6;
		RelayControl.nCHCoupling[i] = AC;
		RelayControl.bCHBWLimit[i] = 0;
	}	
	RelayControl.nTrigSource = CH1;
	RelayControl.bTrigFilt = 0;
	RelayControl.nALT = 0;
	m_nTriggerMode = EDGE;
	m_nTriggerSlope = RISE;
	m_nTriggerSweep = AUTO;
	m_nLeverPos[CH1] = 64;
	m_nLeverPos[CH2] = 96;
	m_nLeverPos[CH3] = 160;
	m_nLeverPos[CH4] = 192;
	//
	m_nReadOK = 0;
}

CHard::~CHard()
{

}

void CHard::Init()
{
	m_nDeviceIndex = 0;
	if(m_nComType == 0)//USB
	{
		dsoSetUSBBus(m_nDeviceIndex);
		dsoHTReadCalibrationData(m_nDeviceIndex,m_nCalLevel,CAL_LEVEL_LEN);
		dsoHTSetSampleRate(m_nDeviceIndex,m_nTimeDIV,m_nYTFormat);
		dsoHTSetCHAndTrigger(m_nDeviceIndex,RelayControl);
		dsoHTSetTriggerAndSyncOutput(m_nDeviceIndex,m_nTriggerMode,m_nTriggerSlope,0,2,0,0,1,0);
		for(int i=0;i<MAX_CH_NUM;i++)
		{
			dsoHTSetCHPos(m_nDeviceIndex,m_nCalLevel,RelayControl.nCHVoltDIV[i],MAX_DATA-m_nLeverPos[i],i);
		}
		dsoHTSetVTriggerLevel(m_nDeviceIndex,m_nCalLevel,MAX_DATA-m_nLeverPos[CH1]);
		dsoHTSetHTriggerLength(m_nDeviceIndex,m_stControl.nBufferLen,m_stControl.nHTriggerPos,m_nTimeDIV,m_nYTFormat);
	}
	else//LAN/WIFI
	{
		dsoLANReadCalibrationData(m_nDeviceIndex,m_nCalLevel,CAL_LEVEL_LEN);
		dsoLANSetSampleRate(m_nDeviceIndex,m_nTimeDIV,m_nYTFormat);
		dsoLANSetCHAndTrigger(m_nDeviceIndex,RelayControl);
		dsoLANSetTriggerAndSyncOutput(m_nDeviceIndex,m_nTriggerMode,m_nTriggerSlope,0,2,0,0,1,0);
		for(int i=0;i<MAX_CH_NUM;i++)
		{
			dsoLANSetCHPos(m_nDeviceIndex,m_nCalLevel,RelayControl.nCHVoltDIV[i],MAX_DATA-m_nLeverPos[i],i);
		}
		dsoLANSetVTriggerLevel(m_nDeviceIndex,m_nCalLevel,MAX_DATA-m_nLeverPos[CH1]);
		dsoLANSetHTriggerLength(m_nDeviceIndex,m_stControl.nBufferLen,m_stControl.nHTriggerPos,m_nTimeDIV,m_nYTFormat);
	}
}

int CHard::InitLAN()
{
	USHORT abcd[4] = {192,168,1,2};//192.168.1.2
	if(dsoLANInitSocket(m_nDeviceIndex,abcd,50000) != 1)
	{
		AfxMessageBox(_T("InitLAN Fail!"));
		return 0;
	}
	return 1;
}

void CHard::ReadData()
{
	int i=0;
	USHORT* pReadData[MAX_CH_NUM];
	for(i=0;i<MAX_CH_NUM;i++)
	{
		pReadData[i] = new USHORT[m_stControl.nReadDataLen];
	}
	if(m_nComType == 0)
	{
		m_nReadOK = dsoSDGetData(m_nDeviceIndex,pReadData[CH1],pReadData[CH2],pReadData[CH3],pReadData[CH4],&m_stControl,2);
	}
	else
	{
		m_nReadOK = dsoSDLANGetData(m_nDeviceIndex,pReadData[CH1],pReadData[CH2],pReadData[CH3],pReadData[CH4],&m_stControl,2);
	}
	if(m_nReadOK == 1)
	{
		for(i=0;i<MAX_CH_NUM;i++)
		{
			SourceToDisplay(pReadData[i],m_stControl.nReadDataLen,i);
		}
	}

	for(i=0;i<MAX_CH_NUM;i++)
	{
		delete pReadData[i];
	}
}

void CHard::SourceToDisplay(USHORT* pData,ULONG nDataLen,USHORT nCH)
{
	for(ULONG i=0;i<nDataLen;i++)
	{
		*(m_pSrcData[nCH]+i) = *(pData+i) - (MAX_DATA - m_nLeverPos[nCH]);
	}
}