#include "StdAfx.h"
#include "MeterParam.h"


CMeterParam::CMeterParam(void)
{
	m_nDevIndex = 0;
	m_bRun = FALSE;
	m_nInterval = 1;
	m_nPreTime = 0;
	m_nCurDataIndex = 0;
	m_nCommType = COMM_USB;
	m_nCOMPort = 0;
	m_DMMData.nType = DMM_VOLTAGE;	
	m_DMMData.bOverflow = TRUE;
	m_DMMData.flValue = 0.0f;
	m_DMMData.nDotPos = 0;
	m_DMMData.nUnit = 3;
	m_DMMData.nUnitType = 0;
	m_DMMData.nDCAC = DMM_DC;
	m_DMMData.nMode = DMM_AUTO;
	m_DMMData.bRel = FALSE;
	m_DMMData.nRange = 0;
	m_DMMData.bAmA = FALSE;
	m_DMMData.nBar = 0;
	m_DMMData.nBarSign = 0;
	m_DMMRelData = m_DMMData;
	m_nCurDIV = 0;
	m_bSaveToBmp = FALSE;
	m_strSaveFileName = _T("");
	m_dbMaxScale = g_dbVDCDIV[0][0];
	m_dbMinScale = g_dbVDCDIV[0][1];
	ClearDataDeque();
	m_recordid=0;
	m_hDequeEvent = CreateEvent(NULL,FALSE,TRUE,NULL);
}


CMeterParam::~CMeterParam(void)
{
	CloseHandle(m_hDequeEvent);
}

void CMeterParam::ClearDataDeque()
{
	//m_DequeData.clear();
	//m_DequeTime.clear();
	m_DequeFullData.clear();
	//m_DequeStrData.clear();
	//m_DequeID.clear();
	m_nDequeDataLen = 0;
}

void CMeterParam::RecordData()
{
	if(m_DMMData.nType != DMM_CONTI && m_DMMData.bOverflow != 1)
	{
		DWORD nCurTime = GetTickCount();
		// this will glitch every 49 days of system uptime
		if(nCurTime > m_nPreTime + g_dbInterval[m_nInterval])
		{
			m_nPreTime = nCurTime;
			TCHAR chData[20];
			FullData fulldata;
			double dbValue = FormatRecordData(m_DMMData.nUnit,m_DMMData.nUnitType,m_DMMData.flValue);
			int nLen = dmmFormatDataToStr(chData,m_DMMData.nUnit,m_DMMData.nUnitType,m_DMMData.flValue);
			CString strData;
			strData.Format(_T("%s"),chData);
			m_nCurDIV = GetCurDIV(dbValue);
			CString strTime;
			SYSTEMTIME st;
			GetLocalTime(&st);
			strTime.Format(_T("%02d/%02d/%d  %02d:%02d:%02d.%d"),st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond,st.wMilliseconds/100);
			fulldata.year=st.wYear;
			fulldata.month=st.wMonth;
			fulldata.day=st.wDay;
			fulldata.hour=st.wHour;
			fulldata.minute=st.wMinute;
			fulldata.second=st.wSecond;
			fulldata.millisecond=st.wMilliseconds;
			fulldata.value=dbValue;
			fulldata.nUnit=m_DMMData.nUnit;
			fulldata.nUnitType=m_DMMData.nUnitType;
			fulldata.DT=strTime;
			fulldata.humanValue=strData;
			if(m_DMMData.nType==DMM_CURRENT){
				fulldata.unit="I";
			}
			else if(m_DMMData.nType==DMM_OHM){
				fulldata.unit="R";
			}
			else if(m_DMMData.nType==DMM_CAP){
				fulldata.unit="C";
			}
			else if(m_DMMData.nType==DMM_CONTI){
				fulldata.unit="T";
			}
			else if(m_DMMData.nType==DMM_DIODE){
				fulldata.unit="D";
			}
			else {
				fulldata.unit="V";
			}
			/* units
				V: 7 - v,  3- mv
				R: 7
				A: 7 - , 4 - 

			*/

		
			fulldata.id=m_recordid;

			WaitForSingleObject(m_hDequeEvent,INFINITE);
			ResetEvent(m_hDequeEvent);
			m_recordid++;
			if(m_nDequeDataLen >= MAX_DATA_LEN)
			{
				m_DequeFullData.pop_front();
				m_DequeFullData.push_back(fulldata);

				//m_DequeID.pop_front();
				//m_DequeID.push_back(m_recordid);

				//m_DequeData.front();
				//m_DequeData.pop_front();
				//m_DequeData.push_back(dbValue);

				//m_DequeTime.front();
				//m_DequeTime.pop_front();
				//m_DequeTime.push_back(strTime);
			
				//m_DequeStrData.front();
				//m_DequeStrData.pop_front();
				//m_DequeStrData.push_back(strData);

				m_nDequeDataLen = MAX_DATA_LEN;
			}
			else
			{
				m_DequeFullData.push_back(fulldata);
				//m_DequeID.push_back(m_recordid);
				//m_DequeData.push_back(dbValue);
				//m_DequeTime.push_back(strTime);
				//m_DequeStrData.push_back(strData);
				
				m_nDequeDataLen++;
			}
			
			SetEvent(m_hDequeEvent);
		}
	}
}



int CMeterParam::GetCurDIV(double dbValue)
{
	int i = 0;
	if(m_DMMData.nType == DMM_VOLTAGE)
	{
		if(m_DMMData.nDCAC == DMM_DC)
		{
			for(i = 0; i < V_DC_DIV; i++)
			{
				if(dbValue <= g_dbVDCDIV[i][0] && dbValue >= g_dbVDCDIV[i][1])
				{
					m_dbMaxScale = g_dbVDCDIV[i][0];
					m_dbMinScale = g_dbVDCDIV[i][1];
					return i;
				}
			}
			return 0;
		}
		else if(m_DMMData.nDCAC == DMM_AC)
		{
			for(i = 0; i < V_AC_DIV; i++)
			{
				if(dbValue <= g_dbVACDIV[i][0] && dbValue >= g_dbVACDIV[i][1])
				{
					m_dbMaxScale = g_dbVACDIV[i][0];
					m_dbMinScale = g_dbVACDIV[i][1];
					return i;
				}
			}
			return 0;
		}
		else
		{
			return 0;
		}
	}
	else if(m_DMMData.nType == DMM_CURRENT)
	{
		if(m_DMMData.nDCAC == DMM_DC)
		{
			for(i = 0; i < A_DC_DIV; i++)
			{
				if(dbValue <= g_dbADCDIV[i][0] && dbValue >= g_dbADCDIV[i][1])
				{
					m_dbMaxScale = g_dbADCDIV[i][0];
					m_dbMinScale = g_dbADCDIV[i][1];
					return i;
				}
			}
			return 0;
		}
		else if(m_DMMData.nDCAC == DMM_AC)
		{
			for(i = 0; i < A_AC_DIV; i++)
			{
				if(dbValue <= g_dbAACDIV[i][0] && dbValue >= g_dbAACDIV[i][1])
				{
					m_dbMaxScale = g_dbAACDIV[i][0];
					m_dbMinScale = g_dbAACDIV[i][1];
					return i;
				}
			}
			return 0;
		}
		else
		{
			return 0;
		}
	}
	else if(m_DMMData.nType == DMM_OHM)
	{
		for(i = 0; i < R_DIV; i++)
		{
			if(dbValue <= g_dbRDIV[i][0] && dbValue >= g_dbRDIV[i][1])
			{
				m_dbMaxScale = g_dbRDIV[i][0];
				m_dbMinScale = g_dbRDIV[i][1];
				return i;
			}
		}
		return 0;
	}
	else if(m_DMMData.nType == DMM_CAP)
	{
		for(i = 0; i < C_DIV; i++)
		{
			if(dbValue <= g_dbCDIV[i][0] && dbValue >= g_dbCDIV[i][1])
			{
				m_dbMaxScale = g_dbCDIV[i][0];
				m_dbMinScale = g_dbCDIV[i][1];
				return i;
			}
		}
		return 0;
	}
	else if(m_DMMData.nType == DMM_DIODE)
	{
		return 0;
	}
	else
	{
		return 0;
	}
}


ULONG CMeterParam::GetDequeFullData(FullData* pValue, int startid)
{
	deque <FullData>::iterator iterDoc;
	ULONG i = 0;
	FullData t;

	WaitForSingleObject(m_hDequeEvent,INFINITE);
	ResetEvent(m_hDequeEvent);
	for(iterDoc = m_DequeFullData.begin(); iterDoc != m_DequeFullData.end(); iterDoc++)
	{
		t=*iterDoc;
		if(t.id>=startid){
			pValue[i] = t;
			i++;
		}
	}
	SetEvent(m_hDequeEvent);
	return i;
}

ULONG CMeterParam::GetDequeDataValues(double* pValue, int startid)
{
	deque <FullData>::iterator iterDoc;
	ULONG i = 0;
	FullData t;

	WaitForSingleObject(m_hDequeEvent,INFINITE);
	ResetEvent(m_hDequeEvent);
	for(iterDoc = m_DequeFullData.begin(); iterDoc != m_DequeFullData.end(); iterDoc++)
	{
		t=*iterDoc;
		if(t.id>=startid){
			pValue[i] = t.value;
			i++;
		}
	}
	SetEvent(m_hDequeEvent);
	return i;
}


int CMeterParam::SearchDevice()
{
//Search device...
	return dmmSearch();
}

USHORT CMeterParam::HDGetDMMData()
{
	USHORT nRe = 0;
	DMM_DATA data;
	if(m_bRun)
	{
		nRe = dmmGetData(m_nDevIndex,&data);
	}
	else
	{
		nRe = 0;
	}
	// ???????????????

	if(1 == nRe)
	{
		m_DMMData = data;
		RecordData();
	}
	return nRe;
}

void CMeterParam::HDSetDMMType(short nType)
{
	dmmSetType(m_nDevIndex,nType);
}

void CMeterParam::HDSetVDCAUTOV()
{
	dmmSetVDCAUTOV(m_nDevIndex);
}

void CMeterParam::HDSetVACAUTOV()
{
	dmmSetVACAUTOV(m_nDevIndex);
}

void CMeterParam::HDSetADCAUTOMA()
{
	dmmSetADCAUTOMA(m_nDevIndex);
}

void CMeterParam::HDSetAACAUTOMA()
{
	dmmSetAACAUTOMA(m_nDevIndex);
}

void CMeterParam::HDSetVDCManual60mV()
{
	dmmSetVDCMANUAL60MV(m_nDevIndex);
}

void CMeterParam::HDSetVACManual60mV()
{
	dmmSetVACMANUAL60MV(m_nDevIndex);
}

void CMeterParam::HDSetADCMAManual60mA()
{
	dmmSetADCMANUAL60MA(m_nDevIndex);
}

void CMeterParam::HDSetAACMAManual60mA()
{
	dmmSetAACMANUAL60MA(m_nDevIndex);
}

void CMeterParam::HDSetOHMAUTO()
{
	dmmSetOHMAUTO(m_nDevIndex);
}

void CMeterParam::HDSetOHMManual()
{
	dmmSetOHMMANUAL600(m_nDevIndex);
}

void CMeterParam::HDSetRel()
{
	dmmSetRel(m_nDevIndex);
	m_DMMRelData = m_DMMData;
}

void CMeterParam::HDExitRel()
{
	dmmExitRel(m_nDevIndex);
}

void CMeterParam::HDSetVDCManualDIV(short nDIV)
{
	dmmSetVDCManualDIV(m_nDevIndex,nDIV);
}

void CMeterParam::HDSetVACManualDIV(short nDIV)
{
	dmmSetVACManualDIV(m_nDevIndex,nDIV);
}

void CMeterParam::HDSetADCManualDIV(short nDIV)
{
	dmmSetADCManualDIV(m_nDevIndex,nDIV);
}

void CMeterParam::HDSetAACManualDIV(short nDIV)
{
	dmmSetAACManualDIV(m_nDevIndex,nDIV);
}

void CMeterParam::HDSetOHMDIV(short nDIV)
{
	dmmSetOHMDIV(m_nDevIndex,nDIV);
}

void CMeterParam::HDSetADCA()
{
	dmmSetADC10A(m_nDevIndex);
}

void CMeterParam::HDSetAACA()
{
	dmmSetAAC10A(m_nDevIndex);
}

BOOL CMeterParam::HDOpenCOM()
{
	return OpenCOM(m_nDevIndex,m_nCOMPort);
}

void CMeterParam::HDCloseCOM()
{
	return CloseCOM(m_nDevIndex);
}

BOOL CMeterParam::HDDevConnect()
{
	return DmmIsDevConnect(m_nDevIndex,m_nCOMPort);
}