#include "stdafx.h"
#include "../Verbpo.h"
#include "DataRecWave.h"


static TCHAR LeadType[12][12] = { _T("I"), _T("II"), _T("III"), _T("avF"), _T("avL"), _T("avR"),
_T("V1"), _T("V2"), _T("V3"), _T("V4"), _T("V5"), _T("V6") };

// CECGWave



INT CDataRecWave::m_i32Height=465;
INT  CDataRecWave::m_i32Width = 600;
//在屏幕上单位长度的像素点个数
INT  CDataRecWave::m_i32ECGPixel=75;
//波形的单位长度的数字量
INT  CDataRecWave::m_i32ECGValue=2048;
//在屏幕上单位长度的像素点个数
INT  CDataRecWave::m_i32RespPixel=70;
//波形的单位长度的数字量
INT  CDataRecWave::m_i32RespValue=256;
//在屏幕上单位长度的像素点个数
INT  CDataRecWave::m_i32PlethPixel=70;
//波形的单位长度的数字量
INT  CDataRecWave::m_i32PlethValue=100;

IMPLEMENT_DYNAMIC(CDataRecWave, CStatic)

CDataRecWave::CDataRecWave()
{
	//构造心电波形画笔对象
	//penECGLine = new CPen(PS_SOLID, 1, RGB(0, 255, 0));
	//构造擦除画笔对象
	//penErase = new CPen(PS_SOLID, 1, RGB(0, 0, 0));
	
	m_stSAVE_DATA.pECGData[0] = new UINT16[1000];
	m_stSAVE_DATA.pECGData[1] = new UINT16[1000];
	m_stSAVE_DATA.pECGData[2] = new UINT16[1000];
	m_stSAVE_DATA.pRespData = new UINT8[250];
	m_stSAVE_DATA.pPlethData = new UINT8[250];

	penECGLine = new CPen(PS_SOLID, 1, CData::m_rgbECGWave);
	penRespLine = new CPen(PS_SOLID, 1, CData::m_rgbRespWave);
	penPlethLine = new CPen(PS_SOLID, 1, CData::m_rgbPlethWave);

	m_pCPonit = new CPoint[1000];

	m_i32LastX = 0;
	m_u16TempX = 0;
	m_i32EraseLast = 0;

	m_u16TempX = 0;
	m_i32EraseLast = 0;

	m_i32EraseFlag = 0;

}

CDataRecWave::~CDataRecWave()
{
	delete m_stSAVE_DATA.pECGData[0];
	delete m_stSAVE_DATA.pECGData[1];
	delete m_stSAVE_DATA.pECGData[2];
	delete m_stSAVE_DATA.pRespData;
	delete m_stSAVE_DATA.pPlethData;

	delete penECGLine;
	delete penRespLine;
	delete penPlethLine;

	delete m_pCPonit;
	font.DeleteObject();
}


BEGIN_MESSAGE_MAP(CDataRecWave, CStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CREATE()
END_MESSAGE_MAP()

int CDataRecWave::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	//	this->MoveWindow(0, 0, m_i32Width, m_i32Height);
	// 获得控件区域
	//GetClientRect(&m_rectCtrl);
	//客户区DC，可以绘画及显示
	CClientDC pDC(this);
	//依附窗口DC创建兼容内存DC
	dcMem.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmp.CreateCompatibleBitmap(&pDC, CDataRecWave::m_i32Width, CDataRecWave::m_i32Height);
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, CDataRecWave::m_i32Width, CDataRecWave::m_i32Height, RGB(0, 0, 0));

	font.CreateFont(18,                                          //   nHeight     
		0,                                                   //   nWidth     
		0,                                                   //   nEscapement   
		0,                                                   //   nOrientation     
		FW_NORMAL,                                   //   nWeight     
		FALSE,                                           //   bItalic     
		FALSE,                                           //   bUnderline     
		0,                                                   //   cStrikeOut     
		ANSI_CHARSET,                             //   nCharSet     
		OUT_DEFAULT_PRECIS,                 //   nOutPrecision     
		CLIP_DEFAULT_PRECIS,               //   nClipPrecision     
		DEFAULT_QUALITY,                       //   nQuality     
		DEFAULT_PITCH | FF_SWISS,     //   nPitchAndFamily       
		_T("宋体"));


	m_i32Show = 0;
	

	Erase();

	//	return CStatic::OnCreate(lpCreateStruct);
	return TRUE;
}




//绘制心电波形
void CDataRecWave::Draw_Wave()
{
	Erase();
	if (m_i32Show==0)
	{
		Draw_ECG_Wave(0, 0, ECG_I_LEAD);
		Draw_ECG_Wave(0, 155, ECG_II_LEAD);
		Draw_Resp_Wave(0,310);
		Draw_Pleth_Wave(0, 390);
	} 
	else if (m_i32Show == 1)
	{
		Draw_ECG_Wave(0, 0, ECG_III_LEAD);
		Draw_ECG_Wave(0, 155, ECG_AVF_LEAD);
		Draw_ECG_Wave(0, 315, ECG_AVL_LEAD);
	} 
	else
	{
		Draw_ECG_Wave(0, 0, ECG_AVR_LEAD);
		Draw_ECG_Wave(0, 155, ECG_V1_LEAD);
	}
	

	/*m_CSWaveData.Lock();

	m_CSWaveData.Unlock();*/
	
	CRect rect;
	this->GetWindowRect(rect);     // 获取要刷新的STATIC控件的刷新区域
	ScreenToClient(rect);   // 屏幕上指定点的屏幕坐标转换成用户坐标
	InvalidateRect(rect);
}



void CDataRecWave::Erase()
{

	m_i32LastX = 0;
	m_u16TempX = 0;
	m_i32EraseLast = 0;
	m_i32EraseFlag = 1;
	dcMem.FillSolidRect(0, 0, m_i32Width, m_i32Height, RGB(0, 0, 0));
}





void CDataRecWave::copy_data(struct SAVE_DATA *st)
{
	memcpy(m_stSAVE_DATA.pECGData[0], st->pECGData[0], CData::m_i32SaveDataECGWaveL * 2);
	memcpy(m_stSAVE_DATA.pECGData[1], st->pECGData[1], CData::m_i32SaveDataECGWaveL * 2);
	memcpy(m_stSAVE_DATA.pECGData[2], st->pECGData[2], CData::m_i32SaveDataECGWaveL * 2);
	memcpy(m_stSAVE_DATA.pRespData, st->pRespData, CData::m_i32SaveDataRespWaveL);
	memcpy(m_stSAVE_DATA.pPlethData, st->pPlethData, CData::m_i32SaveDataPlethWaveL);
}


void CDataRecWave::show_change()
{
	m_i32Show = (m_i32Show + 1) % 3;
}



void CDataRecWave::Draw_ECG_Wave(INT x, INT y, INT type)
{
	INT i = 0, j = 0, t = 0, k = 0, a_i32PointNum = 0;

	dcMem.SelectObject(&font);
	dcMem.SetTextColor(CData::m_rgbECGWave);
	
	switch (type)
	{
	case ECG_I_LEAD:
		dcMem.TextOut(5+x, y+5, _T("I"));
		break;

	case ECG_II_LEAD:
		dcMem.TextOut(5 + x, y + 5, _T("II"));
		break;

	case ECG_III_LEAD:
		dcMem.TextOut(5 + x, y + 5, _T("III"));
		break;

	case ECG_AVF_LEAD:
		dcMem.TextOut(5 + x, y + 5, _T("AVF"));
		break;

	case ECG_AVL_LEAD:
		dcMem.TextOut(5 + x, y + 5, _T("AVL"));
		break;

	case ECG_AVR_LEAD:
		dcMem.TextOut(5 + x, y + 5, _T("AVR"));
		break;

	case ECG_V1_LEAD:
		dcMem.TextOut(5 + x, y + 5, _T("V1"));
		break;
	}
	dcMem.TextOut(5 + x, y + 55, _T("ECG"));

	dcMem.SelectObject(penECGLine);
	dcMem.MoveTo(80 + x, y + 75 - CDataRecWave::m_i32ECGPixel/2);
	dcMem.LineTo(80 + x, y + 75 + CDataRecWave::m_i32ECGPixel / 2);
	dcMem.MoveTo(81 + x, y + 75 - CDataRecWave::m_i32ECGPixel / 2);
	dcMem.LineTo(81 + x, y + 75 + CDataRecWave::m_i32ECGPixel / 2);
	dcMem.MoveTo(82 + x, y + 75 - CDataRecWave::m_i32ECGPixel / 2);
	dcMem.LineTo(82 + x, y + 75 + CDataRecWave::m_i32ECGPixel / 2);

	for (i = 0; i < 1000; i++)
	{
		switch (type)
		{
		case ECG_I_LEAD:
			t = 150 - m_stSAVE_DATA.pECGData[0][i] * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			break;

		case ECG_II_LEAD:
			t = 150 - m_stSAVE_DATA.pECGData[1][i] * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			break;

		case ECG_III_LEAD:
			t = 150 - (m_stSAVE_DATA.pECGData[1][i] - m_stSAVE_DATA.pECGData[0][i]) * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			break;

		case ECG_AVF_LEAD:
			t = 150 - ( 2048 + ((m_stSAVE_DATA.pECGData[1][i]-2048) - (m_stSAVE_DATA.pECGData[0][i]-2048)/2)) * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			
			break;

		case ECG_AVL_LEAD:
			t = 150 - (2048 + ((m_stSAVE_DATA.pECGData[0][i] - 2048) - (m_stSAVE_DATA.pECGData[1][i] - 2048) / 2)) * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			break;

		case ECG_AVR_LEAD:
			t =150 - (2048 - ((m_stSAVE_DATA.pECGData[0][i] - 2048) + (m_stSAVE_DATA.pECGData[1][i] - 2048)) / 2) * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			break;

		case ECG_V1_LEAD:
			t = 150 - m_stSAVE_DATA.pECGData[2][i] * CDataRecWave::m_i32ECGPixel / CDataRecWave::m_i32ECGValue;
			break;
		}

		if (t < 0)
			t = 0;
		else if (t>150)
			t = 150;


		m_pCPonit[a_i32PointNum].x = a_i32PointNum/2 + 100;
		m_pCPonit[a_i32PointNum].y = t + y;
	
		a_i32PointNum++;
	}
	dcMem.Polyline(m_pCPonit, 1000);
}



void CDataRecWave::Draw_Resp_Wave(INT x, INT y)
{
	INT i = 0, j = 0, t = 0, k = 0, a_i32PointNum=0;
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(CData::m_rgbRespWave);
	dcMem.TextOut(5 + x, y + 28, _T("Resp"));

	dcMem.SelectObject(penRespLine);
	dcMem.MoveTo(80 + x, y );
	dcMem.LineTo(80 + x, y + CDataRecWave::m_i32RespPixel );
	dcMem.MoveTo(81 + x, y );
	dcMem.LineTo(81 + x, y +  CDataRecWave::m_i32RespPixel );
	dcMem.MoveTo(82 + x, y );
	dcMem.LineTo(82 + x, y +  CDataRecWave::m_i32RespPixel);

	for (i = 0; i < 250;i++)
	{
		t = 75 - m_stSAVE_DATA.pRespData[i] * CDataRecWave::m_i32RespPixel / CDataRecWave::m_i32RespValue;

		if (t < 0)
			t = 0;
		else if (t>75)
			t = 75;


		m_pCPonit[a_i32PointNum * 2].x = a_i32PointNum * 2+100 ;
		m_pCPonit[a_i32PointNum * 2].y = t + y;
		m_pCPonit[a_i32PointNum * 2 + 1].x = a_i32PointNum * 2 + 1 + 100;
		m_pCPonit[a_i32PointNum * 2+1].y = t + y;
		a_i32PointNum++;
	}



	dcMem.Polyline(m_pCPonit, 500);
}



void CDataRecWave::Draw_Pleth_Wave(INT x, INT y)
{
	INT i = 0, j = 0, t = 0, k = 0, a_i32PointNum = 0;

	dcMem.SelectObject(&font);
	dcMem.SetTextColor(CData::m_rgbPlethWave);
	dcMem.TextOut(5 + x, y + 28, _T("Pleth"));

	dcMem.SelectObject(penPlethLine);
	dcMem.MoveTo(80 + x, y);
	dcMem.LineTo(80 + x, y + CDataRecWave::m_i32PlethPixel);
	dcMem.MoveTo(81 + x, y);
	dcMem.LineTo(81 + x, y + CDataRecWave::m_i32PlethPixel);
	dcMem.MoveTo(82 + x, y);
	dcMem.LineTo(82 + x, y + CDataRecWave::m_i32PlethPixel);

	for (i = 0; i < 250; i++)
	{
		t = 75 - m_stSAVE_DATA.pPlethData[i] * CDataRecWave::m_i32PlethPixel / CDataRecWave::m_i32PlethValue;

		if (t < 0)
			t = 0;
		else if (t>75)
			t = 75;

		m_pCPonit[a_i32PointNum * 2].x = a_i32PointNum * 2 + 100;
		m_pCPonit[a_i32PointNum * 2].y = t + y;
		m_pCPonit[a_i32PointNum * 2 + 1].x = a_i32PointNum * 2 + 1 + 100;
		m_pCPonit[a_i32PointNum * 2 + 1].y = t + y;
		a_i32PointNum++;
	}
	dcMem.Polyline(m_pCPonit, 500);
}




BOOL CDataRecWave::OnEraseBkgnd(CDC* pDC)
{

	return TRUE;
}


void CDataRecWave::OnPaint()
{
	CPaintDC pDC(this);
	
	pDC.BitBlt(m_rectCtrl.left, m_rectCtrl.top, CDataRecWave::m_i32Width, CDataRecWave::m_i32Height, &dcMem, 0, 0, SRCCOPY);

	//	CStatic::OnPaint();
}

