#include "stdafx.h"
#include "ECGData.h"
#include "Verbpo.h"

CECGData::CECGData()
{
	int i;
	for (i = 0; i < 7; i++)
	{
		m_i32ShowBuf[i] = new int[SHOW_BUFF_LENGTH];
		m_i32ShowDateW = 0, m_i32ShowDateR = 0, m_i32ShowDateLength = 0;

		m_i32SaveData[i] = new int[SHOW_BUFF_LENGTH];
		m_i32SaveDateW = 0, m_i32SaveDateR = 0, m_i32SaveDateLength = 0;

		
		m_i32PrintData[i] = new int[SHOW_BUFF_LENGTH];

		m_stGET_ECG_DATA.pECGData[i] = new int[ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE];
	}
	m_i32PrintDateW = 0, m_i32PrintDateR = 0, m_i32PrintDateLength = 0;

	m_i32RespShowBuf = new int[SHOW_BUFF_LENGTH];
	m_i32RespShowDateW = 0, m_i32RespShowDateR = 0, m_i32RespShowDateLength = 0;
	
	m_i32PlethShowBuf = new int[SHOW_BUFF_LENGTH];
	m_i32PlethShowDateW = 0, m_i32PlethShowDateR = 0, m_i32PlethShowDateLength = 0;
}


CECGData::~CECGData()
{
	int i;
	for (i = 0; i < 7; i++)
	{
		delete m_i32ShowBuf[i];
		delete m_i32PrintData[i];
		delete m_i32SaveData[i];

		delete m_stGET_ECG_DATA.pECGData[i];
	}
	delete m_i32RespShowBuf;
}

int CECGData::GetSaveData(struct GET_ECG_DATA *st)
{

	return 0;
}

int CECGData::GetShowData(struct GET_ECG_DATA *st)
{
	int i = 0, j = 0,t=0;
	g_CSECGDataShow.Lock();
	//获得每个通道要保存的数据长度
	j = m_i32ShowDateLength;
	if (j >= st->i32Length)
		j = st->i32Length;

	//复制数据
	for (t = 0; t < 7; t++)
	{
		for (i = 0; i < j; i++)
		{
			st->pECGData[t][i] = m_i32SaveData[t][(m_i32ShowDateR + i) % SHOW_BUFF_LENGTH];
		}
	}
	st->i32DataLength = j;
	//调节读和长度
	m_i32ShowDateLength -= j;
	
	st->i32HR = m_i32HR;
	st->i32ST1 = m_i32ST1;
	st->i32ST2 = m_i32ST2;
	st->m_i32LeadOff = m_i32LeadOff, st->m_i32LeadOverLoad = m_i32LeadOverLoad;
	st->m_i32PVCs = m_i32PVCs;


	g_CSECGDataShow.Unlock();
	m_i32ShowDateR = (m_i32ShowDateR + j)%SHOW_BUFF_LENGTH;
	
	return 0;
}


int CECGData::GetPrintData(struct GET_ECG_DATA *st)
{
	//int i = 0, a_i32PrintDateR, t = 0;
	//CString strTime;

	//g_CSECGDataPrint.Lock();
	//a_i32PrintDateR = m_i32PrintDateW;
	//g_CSECGDataPrint.Unlock();

	//a_i32PrintDateR = (a_i32PrintDateR + SHOW_BUFF_LENGTH - ECG_FILE_DATA_LENGTH) % SHOW_BUFF_LENGTH;
	////复制数据
	//for (t = 0; t < 7; t++)
	//{
	//	for (i = 0; i < ECG_FILE_DATA_LENGTH; i++)
	//	{
	//		st->pECGData[t][i] = m_i16PrintData[t][(a_i32PrintDateR + i) % SHOW_BUFF_LENGTH];
	//	}
	//}
	//st->i32DataLength = ECG_FILE_DATA_LENGTH;
	//
	//if (theApp.m_i32Connect == 0)
	//{
	//	wcscpy_s(st->cID, 32, theApp.m_ECGFileOutput->m_stInputECGData.cID);
	//	wcscpy_s(st->cName, 32, theApp.m_ECGFileOutput->m_stInputECGData.cName);
	//	wcscpy_s(st->cSex, 8, theApp.m_ECGFileOutput->m_stInputECGData.cSex);
	//	wcscpy_s(st->cAge, 8, theApp.m_ECGFileOutput->m_stInputECGData.cAge);
	//	wcscpy_s(st->cWeight, 8, theApp.m_ECGFileOutput->m_stInputECGData.cWeight);
	//	wcscpy_s(st->cHeight, 8, theApp.m_ECGFileOutput->m_stInputECGData.cHeight);
	//	wcscpy_s(st->cTime, 32, theApp.m_ECGFileOutput->m_stInputECGData.cTime);
	//}
	//else
	//{
	//	COleDateTime tm = COleDateTime::GetCurrentTime();
	//	strTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	//	wcscpy_s(st->cID, 32, theApp.m_Patient->cID);
	//	wcscpy_s(st->cName, 32, theApp.m_Patient->cName);
	//	wcscpy_s(st->cSex, 8, theApp.m_Patient->cSex);
	//	wcscpy_s(st->cAge, 8, theApp.m_Patient->cAge);
	//	wcscpy_s(st->cWeight, 8, theApp.m_Patient->cWeight);
	//	wcscpy_s(st->cHeight, 8, theApp.m_Patient->cHeight);

	//	wcscpy_s(st->cTime, 32, strTime);
	//}

	return 0;
}

int CECGData::AddData2ShowBuf(struct GET_ECG_DATA *st)
{
	int i = 0,t=0;
	
	g_CSECGDataShow.Lock();
	for (i = 0; i < st->i32DataLength; i++)
	{
		for (t = 0; t < 7;t++)
		{
			m_i32SaveData[t][m_i32ShowDateW] = st->pECGData[t][i];
		}
		

		m_i32ShowDateW = (m_i32ShowDateW + 1)%SHOW_BUFF_LENGTH;
		if (m_i32ShowDateLength<SHOW_BUFF_LENGTH)
		{
			m_i32ShowDateLength++;
		}
	}
	g_CSECGDataShow.Unlock();

	return 0;
}


//心电导联连接和信号信息
int CECGData::Add_Lead_STS(UINT8 leadoff, UINT8 overload)
{
	g_CSECGDataShow.Lock();
	m_i32LeadOff = leadoff;
	m_i32LeadOverLoad = overload;
	g_CSECGDataShow.Unlock();
	
	return 0;
}


int CECGData::Get_Lead_STS(UINT8 *leadoff, UINT8 *overload)
{
	g_CSECGDataShow.Lock();
	*leadoff = m_i32LeadOff;
	*overload = m_i32LeadOverLoad;
	g_CSECGDataShow.Unlock();
	return 0;
}


//心律
int CECGData::Add_HR(int hr)
{
	g_CSECGDataShow.Lock();
	m_i32HR = hr;
	g_CSECGDataShow.Unlock();
	return 0;
}


int CECGData::Get_HR(int *hr)
{
	g_CSECGDataShow.Lock();
	*hr = m_i32HR;
	g_CSECGDataShow.Unlock();
	return 0;
}

//心律
int CECGData::Add_Beat(int b)
{
	g_CSECGDataShow.Lock();
	m_i32Beat = b;
	g_CSECGDataShow.Unlock();

	return 0;
}

int CECGData::Get_Beat(int *b)
{
	g_CSECGDataShow.Lock();
	*b = m_i32Beat;
	m_i32Beat = 0;
	g_CSECGDataShow.Unlock();
	return 0;
}
//ST值
int CECGData::Add_ST(int st1, int st2)
{
	g_CSECGDataShow.Lock();
	m_i32ST1 = st1;
	m_i32ST2 = st2;
	g_CSECGDataShow.Unlock();
	return 0;
}


int CECGData::Get_ST(int *st1, int *st2)
{
	g_CSECGDataShow.Lock();
	*st1 = m_i32ST1;
	*st2 = m_i32ST2;
	g_CSECGDataShow.Unlock();
	return 0;
}


//ST 模板波形
int CECGData::Add_Pattern(struct ECG_PATTERN *st)
{

	return 0;
}

int CECGData::AddData2SaveBuf(struct GET_ECG_DATA *st)
{
	int i = 0, t = 0, a_i32SaveDateR;

	g_CSECGDataSave.Lock();
	for (i = 0; i < st->i32DataLength; i++)
	{
		for (t = 0; t < 7; t++)
		{
			m_i32SaveData[t][m_i32SaveDateW] = st->pECGData[t][i];
		}
		m_i32SaveDateW = (m_i32SaveDateW + 1) % SHOW_BUFF_LENGTH;
		if (m_i32SaveDateLength < SHOW_BUFF_LENGTH)
		{
			m_i32SaveDateLength++;
		}
		
	}
	g_CSECGDataSave.Unlock();
	if (((m_i32SaveDateW + SHOW_BUFF_LENGTH - m_i32SaveDateR) % SHOW_BUFF_LENGTH)>ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE)
	{
		a_i32SaveDateR = m_i32SaveDateR;
		for (i = 0; i < ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE;i++)
		{
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_I][i] = m_i32SaveData[ECG_WAVE_I][a_i32SaveDateR];
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_II][i] = m_i32SaveData[ECG_WAVE_I][a_i32SaveDateR];
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_III][i] = m_i32SaveData[ECG_WAVE_I][a_i32SaveDateR];
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_AVR][i] = m_i32SaveData[ECG_WAVE_AVR][a_i32SaveDateR];
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_AVL][i] = m_i32SaveData[ECG_WAVE_AVL][a_i32SaveDateR];
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_AVF][i] = m_i32SaveData[ECG_WAVE_AVF][a_i32SaveDateR];
			m_stGET_ECG_DATA.pECGData[ECG_WAVE_V1][i] = m_i32SaveData[ECG_WAVE_V1][a_i32SaveDateR];
			
			//m_stGET_ECG_DATA.i32HR =  theApp.m_i32HR;
			//m_stGET_ECG_DATA.i32ST1 = theApp.m_i32ST1;
			//m_stGET_ECG_DATA.i32ST2 = theApp.m_i32ST2;
			a_i32SaveDateR = (a_i32SaveDateR + 1) % SHOW_BUFF_LENGTH;
		}
		m_stGET_ECG_DATA.i32Length = ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE;
		m_i32SaveDateR = a_i32SaveDateR;
		//((DataSaveThread *)theApp.m_pThrdDataSave)->m_pCECGDateSaveFile->make_save_data(*theApp.m_Patient, &m_stGET_ECG_DATA);
	}
	return 0;
}

int CECGData::AddData2PrintBuf(struct GET_ECG_DATA *st)
{
	int i = 0, t = 0;

	g_CSECGDataPrint.Lock();
	for (i = 0; i < st->i32DataLength; i++)
	{
		for (t = 0; t < 7; t++)
		{
			m_i32PrintData[t][m_i32PrintDateW] = st->pECGData[t][i];
		}


		m_i32PrintDateW = (m_i32PrintDateW + 1) % SHOW_BUFF_LENGTH;
		if (m_i32PrintDateLength < SHOW_BUFF_LENGTH)
		{
			m_i32PrintDateLength++;
		}
	}
	g_CSECGDataPrint.Unlock();

	return 0;
}


int CECGData::GetRespShowData(struct GET_RESP_DATA *st)
{
	int i = 0, j = 0, t = 0;
	g_CSRespDataShow.Lock();
	j = m_i32RespShowDateLength;
	if (j >= st->i32Length)
		j = st->i32Length;

	//复制数据

	for (i = 0; i < j; i++)
	{
		st->pRespData[i] = m_i32RespShowBuf[(m_i32RespShowDateR + i) % SHOW_BUFF_LENGTH];
	}
	
	st->i32DataLength = j;
	//调节读和长度
	m_i32RespShowDateLength -= j;
	g_CSRespDataShow.Unlock();
	m_i32RespShowDateR = (m_i32RespShowDateR + j) % SHOW_BUFF_LENGTH;

	return 0;
}


int CECGData::AddData2RespShowBuf(struct GET_RESP_DATA *st)
{
	int i = 0;

	g_CSRespDataShow.Lock();
	for (i = 0; i < st->i32DataLength; i++)
	{
		m_i32RespShowBuf[m_i32RespShowDateW] = st->pRespData[i];
		m_i32RespShowDateW = (m_i32RespShowDateW + 1) % SHOW_BUFF_LENGTH;
		if (m_i32RespShowDateLength<SHOW_BUFF_LENGTH)
		{
			m_i32RespShowDateLength++;
		}
	}
	g_CSRespDataShow.Unlock();

	return 0;
}


int CECGData::GetOXYShowData(struct GET_OXY_DATA *st)
{
	int i = 0, j = 0, t = 0;
	g_CSOxyDataShow.Lock();
	j = m_i32PlethShowDateLength;
	if (j >= st->i32Length)
		j = st->i32Length;

	//复制数据
	for (i = 0; i < j; i++)
	{
		st->pPlethData[i] = m_i32PlethShowBuf[(m_i32PlethShowDateR + i) % SHOW_BUFF_LENGTH];
	}
	st->i32DataLength = j;
	//调节读和长度
	m_i32PlethShowDateLength -= j;

	/*st->u16PR;
	st->u16Finger;
	st->u16Beat;
	st->u16Search;
	st->u16Probe;
	st->u16Bar;
	st->u16Signal;*/
	g_CSOxyDataShow.Unlock();
	m_i32PlethShowDateR = (m_i32PlethShowDateR + j) % SHOW_BUFF_LENGTH;
	return 0;
}

int CECGData::Add_PlethWave(UINT8 data)
{
	int i = 0;

	g_CSOxyDataShow.Lock();
	
	m_i32PlethShowBuf[m_i32PlethShowDateW] = data;
	m_i32PlethShowDateW = (m_i32PlethShowDateW + 1) % SHOW_BUFF_LENGTH;
	if (m_i32PlethShowDateLength<SHOW_BUFF_LENGTH)
	{
		m_i32PlethShowDateLength++;
	}
	
	g_CSOxyDataShow.Unlock();
	return 0;
}

void CECGData::copy_print_data(struct GET_ECG_DATA *st, int length)
{
	/*int i = 0,j=0;
	int	l = length;
	if (l > ECG_WAVE_BUFF_LENGTH)
		l = ECG_WAVE_BUFF_LENGTH;

	for (i = 0; i < 7;i++)
	{
		memcpy(m_i32PrintData[i], st->pECGData[i], l * 2);
	}
	
	m_i32PrintDateW = (m_i32PrintDateW + l) % ECG_WAVE_BUFF_LENGTH;
	m_i32PrintDateLength += l;
	if (m_i32PrintDateLength > ECG_WAVE_BUFF_LENGTH)
	{
		m_i32PrintDateLength = ECG_WAVE_BUFF_LENGTH;
	}*/

}

int CECGData::ResetShowBuff()
{
	g_CSECGDataShow.Lock();
	m_i32ShowDateW = 0, m_i32ShowDateR = 0, m_i32ShowDateLength = 0;
	g_CSECGDataShow.Unlock();
	return 0;
}

int CECGData::ResetSaveBuff()
{
	g_CSECGDataSave.Lock();
	m_i32SaveDateW = 0, m_i32SaveDateR = 0, m_i32SaveDateLength = 0;
	g_CSECGDataSave.Unlock();
	
	return 0;
}

int CECGData::ResetPrintBuff()
{

	g_CSECGDataPrint.Lock();
	m_i32PrintDateW = 0, m_i32PrintDateR = 0, m_i32PrintDateLength = 0;
	g_CSECGDataPrint.Unlock();

	return 0;
}



