// 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()
{
	int i = 0;
	m_nBufferLen = BUF_2_5K_LEN;
	m_nReadLen = BUF_2_5K_LEN;
	i = 0;
	m_fVoltDiv[i++] = 5e-4;//500uV
	m_fVoltDiv[i++] = 1e-3;//1mV
	m_fVoltDiv[i++] = 2e-3;
	m_fVoltDiv[i++] = 5e-3;
	m_fVoltDiv[i++] = 1e-2;
	m_fVoltDiv[i++] = 2e-2;
	m_fVoltDiv[i++] = 5e-2;
	m_fVoltDiv[i++] = 0.1;
	m_fVoltDiv[i++] = 0.2;
	m_fVoltDiv[i++] = 0.5;
	m_fVoltDiv[i++] = 0.1;
	m_fVoltDiv[i++] = 0.2;
	m_fVoltDiv[i++] = 0.5;
	m_fVoltDiv[i++] = 1;
	m_fVoltDiv[i++] = 2;
	m_fVoltDiv[i++] = 5;
	m_fVoltDiv[i++] = 10;//10V
	for ( i = 0; i < MAX_CH_NUM; i++)
	{
		m_ch[i].bCHEnable = true;
		m_ch[i].bCHBWLimit = true;
		m_ch[i].nCHCoupling = DC;
		m_ch[i].nCHVoltDIV = 8;
		m_ch[i].m_pDisData= (short *)malloc(m_nReadLen * sizeof(short));
		m_ch[i].m_pSrcData= (WORD *)malloc(m_nReadLen * sizeof(WORD));
		//m_ch[i].m_nLeverPos = 192-32*i;
	}
	m_ch[CH1].m_nLeverPos = 192;
	m_ch[CH2].m_nLeverPos = 160;
	m_ch[CH3].m_nLeverPos = 96;
	m_ch[CH4].m_nLeverPos = 64;
	m_ch[CH1].clrRGB= RGB(255, 255, 0);
	m_ch[CH2].clrRGB = RGB(0, 255, 255);
	m_ch[CH3].clrRGB = RGB(255, 0, 255);
	m_ch[CH4].clrRGB = RGB(0, 255, 0);
	m_nTriggerHorPos=50;
	m_nTriggerVePos= m_ch[0].m_nLeverPos;
	m_nDeviceIndex = 0;
	m_nTimeDIV = 12;

	m_nTriggerMode = EDGE;
	m_nTriggerSlope = RISE;
	m_nTriggerSweep = AUTO;
	m_nTriggerSource = CH1;
	m_bCollect=TRUE;
	m_nReadOK = 0;
	m_bValidDevice = false;
	m_pDeviceStatus = (PUCHAR)malloc(128);
	Init();

}

CHard::~CHard()
{

}

void CHard::Init()
{
	int i = 0;
	char nChar;
	WORD nWORD;
	m_nDeviceIndex = 0;
	bool bFindDdevice = false;
	float fFloat;
	for (i = 0; i < MAX_CH_NUM; i++)
	{
		if (dsoHTDeviceConnect(i))
		{
			bFindDdevice = true;
			m_nDeviceIndex = i;
			break;
		}
	}
	if (!bFindDdevice)
	{
		MessageBoxA(NULL, "Error", "未发现设备请确定是否连接设备以及安装驱动", 0);
		return;
	}
	m_bValidDevice = true;
	dsoSetUSBBus(m_nDeviceIndex);//上电后总线设置
	dsoInitHard(m_nDeviceIndex);//硬件初始化
	for (int i = 0; i < MAX_CH_NUM; i++)
	{
		nChar = m_ch[i].bCHEnable ? 1 : 0;
		dsoHTAttribsCHEnable(m_nDeviceIndex, i, &nChar, 0);
		nChar = m_ch[i].bCHBWLimit ? 1 : 0;
		dsoHTAttribsBW(m_nDeviceIndex, i, &nChar, 0);
		nChar = m_ch[i].nCHCoupling;
		dsoHTAttribsCHCouple(m_nDeviceIndex, i, &nChar, 0);
		nWORD = m_ch[i].m_nLeverPos;
		dsoHTAttribsCHPos(m_nDeviceIndex, i, &nWORD, 0);
		fFloat = m_fVoltDiv[m_ch[i].nCHVoltDIV]*1000;
		dsoHTAttribsCHVolt(m_nDeviceIndex, i, &fFloat, 0);
		
	
	}
	nChar = m_nTriggerHorPos;
	dsoHTAttribsHTriggerPos(m_nDeviceIndex, &nChar, 0);
	nChar =  m_nTriggerSource;
	dsoHTAttribsTriggerSource(m_nDeviceIndex, &nChar, 0);
	nWORD = m_nTriggerVePos;
	dsoHTAttribsVTriggerPos(m_nDeviceIndex, m_nTriggerSource, 0, &nWORD);
	dsoHTAttribsBufferLength(m_nDeviceIndex, &m_nBufferLen, 0);
	dsoHTAttribsTriggerMode(m_nDeviceIndex, &m_nTriggerMode, 0);
	dsoHTAttribsTriggerSlop(m_nDeviceIndex, &m_nTriggerSlope, 0);
	dsoHTAttribsTriggerSweep(m_nDeviceIndex, &m_nTriggerSweep, 0);
	dsoHTAttribsTriggerSource(m_nDeviceIndex, &m_nTriggerSource, 0);
	nWORD = 8;

	dsoHTSetSampleRateDiv(m_nDeviceIndex, nWORD);
	nChar = 5;
	dsoHTAttribsAcqOption(m_nDeviceIndex, &nChar, 0);
	WORD nStartControl = 0;
	nStartControl |= ((m_nTriggerSweep == AUTO) ? 0x01 : 0);//add by zhang
	nStartControl |= 4;// m_bCollect ? 0 : 0x04;//add by zhang
	dsoHTStartCollectData(m_nDeviceIndex, nStartControl);//modified by zhaang
	m_bRestartCollect = false;

}
bool CHard::ReadData()
{
	if (m_bRestartCollect)
	{
		WORD nStartControl = 0;
		nStartControl |= ((m_nTriggerSweep == AUTO) ? 0x01 : 0);//add by zhang
		nStartControl |= 4;// m_bCollect ? 0 : 0x04;//add by zhang
		dsoHTStartCollectData(m_nDeviceIndex, nStartControl);//modified by zhaang
		m_bRestartCollect = false;
	}
	memset(m_pDeviceStatus, 0, 128);
	dsoHTGetState(m_nDeviceIndex, m_pDeviceStatus);
	if (!(m_pDeviceStatus[0] & 2))
		return false;
	m_nReadOK=dsoHTCollectDataWave(m_nDeviceIndex, m_ch[0].m_pSrcData, m_ch[1].m_pSrcData, m_ch[2].m_pSrcData, m_ch[3].m_pSrcData, m_nReadLen);
	if (m_nReadOK)
	{
		SourceToDisplay();
		//DisplayToRealVolt();//转成实际电压值
	}
	return m_nReadOK;
}

void CHard::SourceToDisplay()
{
	WORD nTemp;
	for (int i = 0; i < MAX_CH_NUM; i++)
	{
		if (!m_ch[i].bCHEnable)
			continue;
		nTemp = range255ToRange200(m_ch[i].m_nLeverPos);
		for (int j = 0; j < m_nReadLen; j++)
		{
			m_ch[i].m_pDisData[j] = m_ch[i].m_pSrcData[j] - (MAX_DATA - nTemp);
		}
		
	}
}
void CHard::DisplayToRealVolt()
{
	WORD nTemp;
	float *CHxRealVolt=(float *)malloc(sizeof(float)*m_nReadLen);

	for (int i = 0; i < MAX_CH_NUM; i++)
	{
		if (!m_ch[i].bCHEnable)
			continue;
		WORD nVolt = m_ch[i].nCHVoltDIV;
		for (int j = 0; j < m_nReadLen; j++)
		{
			CHxRealVolt[j]=m_ch[i].m_pDisData[j] *m_fVoltDiv[nVolt]*8.0f/ DISPLAY_RANGE;//单位伏特V
		}

	}
	free(CHxRealVolt);
}
int CHard::range255ToRange200(int nData)
{
	float p = (nData)*1.0f / (MAX_DATA);
	return (1 - p)*MIN_DISPLAY_DATA + p*MAX_DISPLAY_DATA + 0.5f;
}
void CHard::setCHVpos(int nCH, WORD nValue)
{
	m_ch[nCH].m_nLeverPos = nValue;
	dsoHTAttribsCHPos(m_nDeviceIndex, nCH, &nValue, 0);
	m_bRestartCollect = true;
}
void CHard::setCHCouple(int nCH, WORD nValue)
{
	m_ch[nCH].nCHCoupling = nValue;
	char nChar = nValue;
	dsoHTAttribsCHCouple(m_nDeviceIndex, nCH, &nChar, 0);
	m_bRestartCollect = true;
}
void CHard::setCHVoltDiv(int nCH, WORD nValue)
{
	m_ch[nCH].nCHVoltDIV = nValue;
	float fFloat = m_fVoltDiv[nValue] * 1000;
	dsoHTAttribsCHVolt(m_nDeviceIndex, nCH, &fFloat, 0);
	m_bRestartCollect = true;
}

void CHard::setTriggerHPos(WORD nValue)
{
	m_nTriggerHorPos = nValue;
	char nChar = m_nTriggerHorPos;
	dsoHTAttribsHTriggerPos(m_nDeviceIndex, &nChar, 0);
	m_bRestartCollect = true;
}
void CHard::setTriggerVPos(WORD nValue)
{
	m_nTriggerVePos=nValue;
	dsoHTAttribsVTriggerPos(m_nDeviceIndex, m_nTriggerSource, 0, &nValue);
	m_bRestartCollect = true;
}
void CHard::SetBufferLen(ULONG nValue)
{
	m_nBufferLen = nValue;
	dsoHTAttribsBufferLength(m_nDeviceIndex, &m_nBufferLen, 0);
	m_bRestartCollect = true;
}
void CHard::SetReadLen(ULONG nValue)
{
	if (nValue > m_nBufferLen)
		return;
	if (nValue > m_nReadLen)
	{
		for (int i = 0; i < MAX_CH_NUM; i++)
		{
			free(m_ch[i].m_pDisData);
			free(m_ch[i].m_pSrcData);
			m_ch[i].m_pDisData = (short *)malloc(nValue * sizeof(short));
			m_ch[i].m_pSrcData = (WORD *)malloc(nValue * sizeof(WORD));
		}
	}
	m_nReadLen = nValue;

}
double		CHard::fRound(double f, unsigned nLen)
{
	if (f == 0.0)
		return 0;
	char bNegtive = 1;
	if (f < 0.0)
	{
		bNegtive = -1;
		f = -f;
	}
	int d = int(nLen - floor(log10(f)) - 1);
	return bNegtive*floor(f * pow(10.0, d*1.0) + .5) / pow(10.0, d*1.0);
}
void CHard::SetSampleRate(double fSa)
{
	double fSampleRate = fRound(fSa, 5);
	dsoHTSetSampleRate(m_nDeviceIndex, &fSampleRate);
	m_fSampleRate = fSampleRate;
}
void CHard::SetCHOnOff(int nCH, bool b)
{
	m_ch[nCH].bCHEnable = b;
	char nChar = b ? 1 : 0;
	dsoHTAttribsCHEnable(m_nDeviceIndex, nCH, &nChar, 0);
	m_bRestartCollect = true;

}