// DataSaveThread.cpp : 实现文件
//

#include "stdafx.h"
#include "vECG.h"
#include "DataSaveThread.h"
#define ECG_I_LEAD		0x00
#define ECG_II_LEAD		0x01
#define ECG_III_LEAD	0x02
#define ECG_AVF_LEAD	0x03
#define ECG_AVL_LEAD	0x04
#define ECG_AVR_LEAD	0x05
#define ECG_V1_LEAD		0x06
#define ECG_V2_LEAD		0x07
#define ECG_V3_LEAD		0x08
#define ECG_V4_LEAD		0x09
#define ECG_V5_LEAD		0x0a
#define ECG_V6_LEAD		0x0b

// DataSaveThread

IMPLEMENT_DYNCREATE(DataSaveThread, CWinThread)

DataSaveThread::DataSaveThread()
{
	int i = 0;
	CString a_str;
	for (i = 0; i < 13; i++)
	{
		m_i16ECGData[i] = new INT16[SAVE_ECGDATA_LENGTH];
		m_i16WaveDateW[i] = 0, m_i16WaveDateR[i] = 0, m_i16WaveDateLength[i] = 0;
	}

	
	
	m_pCECGDateSaveFile = new CECGDateSaveFile;
	m_i32Exit = 0;
}

DataSaveThread::~DataSaveThread()
{
	
}

BOOL DataSaveThread::InitInstance()
{
	// TODO:    在此执行任意逐线程初始化
	return TRUE;
}

int DataSaveThread::ExitInstance()
{
	// TODO:    在此执行任意逐线程清理
	int i = 0;
	
	for (i = 0; i < 13; i++)
	{
		delete m_i16ECGData[i];
	}
	
	delete m_pCECGDateSaveFile;
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(DataSaveThread, CWinThread)
END_MESSAGE_MAP()

int DataSaveThread::Add_ECGData(unsigned short *buff, int length, int type)
{
	int i = 0;
	g_CSECGData.Lock();
	for (i = 0; i < length; i++)
	{
		m_i16ECGData[type][(m_i16WaveDateW[type] + i) % SAVE_ECGDATA_LENGTH] = *(buff + i);
	}
	m_i16WaveDateW[type] = (m_i16WaveDateW[type] + length)&(SAVE_ECGDATA_LENGTH - 1);
	m_i16WaveDateLength[type] += length;
	g_CSECGData.Unlock();
	return 0;
}

int DataSaveThread::Add_ECGData2(int data,  int type)
{
	
	g_CSECGData.Lock();
	m_i16ECGData[type][(m_i16WaveDateW[type] + 1) % SAVE_ECGDATA_LENGTH] = data;

	m_i16WaveDateW[type] = (m_i16WaveDateW[type] + 1)&(SAVE_ECGDATA_LENGTH - 1);
	m_i16WaveDateLength[type] += 1;
	g_CSECGData.Unlock();
	return 0;
}




int DataSaveThread::Run()
{
	while (1)
	{
		//Save_RawData();

		m_pCECGDateSaveFile->do_save_data();
		Sleep(2);
		if (m_i32Exit)
		{
			return ExitInstance();
		}
		
	}

	return 0;
}
// DataSaveThread 消息处理程序
