#include "stdafx.h"
#include "../Verbpo.h"
#include "AlmRecWave.h"
#include "../ThreadDataAlmRec.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") };

// CAlmRecWave



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

IMPLEMENT_DYNAMIC(CAlmRecWave, CStatic)


CAlmRecWave::CAlmRecWave()
{
	m_stSAVE_ALM.pECGData[0] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
	m_stSAVE_ALM.pECGData[1] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];

	m_stSAVE_ALM.pRespData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmRespWaveLength];
	m_stSAVE_ALM.pPlethData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength];

	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[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
}


CAlmRecWave::~CAlmRecWave()
{
	delete m_stSAVE_ALM.pECGData[0];
	delete m_stSAVE_ALM.pECGData[1];
	
	delete m_stSAVE_ALM.pRespData;
	delete m_stSAVE_ALM.pPlethData;

	delete penECGLine;
	delete penRespLine;
	delete penPlethLine;

	delete m_pCPonit;
	font.DeleteObject();
	fontBig.DeleteObject();
}


BEGIN_MESSAGE_MAP(CAlmRecWave, CStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


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

	fontBig.CreateFont(32,                                          //   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;
	m_i32Index = 0;

	Erase();

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


void CAlmRecWave::OnDestroy()
{
	CStatic::OnDestroy();

	// TODO:  在此处添加消息处理程序代码

	dcMem.DeleteDC();
	bmp.DeleteObject();


}

//绘制心电波形
void CAlmRecWave::Draw_Wave()
{
	Erase();
	if (m_stSAVE_ALM.stInfo.u8Flag == 0xAA)
	{
		dcMem.SelectObject(&fontBig);
		dcMem.SetTextColor(CData::m_rgbECGWave);
		dcMem.TextOut(170, 100, _T("无相关报警信息"));
	}
	else
	{
		Draw_X_Time();
		if (m_i32Show == 0)
		{
			Draw_ECG_Wave(0, 0, ECG_I_LEAD);
			Draw_ECG_Wave(0, 125, ECG_II_LEAD);

		}
		else if (m_i32Show == 1)
		{
			Draw_Resp_Wave(0, 0);
			Draw_Pleth_Wave(0, 125);
		}
	}
	
	
	Invalidate();
	//CRect rect;
	//this->GetWindowRect(rect);     // 获取要刷新的STATIC控件的刷新区域
	//ScreenToClient(rect);   // 屏幕上指定点的屏幕坐标转换成用户坐标
	//InvalidateRect(rect);
}


void CAlmRecWave::Erase()
{
	dcMem.FillSolidRect(0, 0, m_i32Width, m_i32Height, RGB(0, 0, 0));
}


void CAlmRecWave::copy_data(struct SAVE_ALM *st)
{
	memcpy(&(m_stSAVE_ALM.stInfo), &(st->stInfo), sizeof(struct SAVE_ALM_FILE_INFO));
	memcpy(&(m_stSAVE_ALM.stPara), &(st->stPara), sizeof(struct SAVE_DATA_PARA));
	memcpy(m_stSAVE_ALM.pECGData[0], st->pECGData[0], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
	memcpy(m_stSAVE_ALM.pECGData[1], st->pECGData[1], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
	memcpy(m_stSAVE_ALM.pRespData, st->pRespData, CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength);
	memcpy(m_stSAVE_ALM.pPlethData, st->pPlethData, CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength);
}


void CAlmRecWave::show_change()
{
	m_i32Show = (m_i32Show + 1) % 2;
}

void CAlmRecWave::Draw_X_Time()
{
	INT i = 0,j=0;
	dcMem.SelectObject(penECGLine);


	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 250;j+=2)
		{
			dcMem.SetPixel(200 + 100 * i, j, RGB(15, 100, 200));
		}
		
	}
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(230, 180, 240));
	if (m_i32Index==0)
	{
		dcMem.TextOut(100 + 0 - 30, 253, _T("-15"));
		dcMem.TextOut(100 + 100 - 30, 253, _T("-14"));
		dcMem.TextOut(100 + 200 - 30, 253, _T("-13"));
		dcMem.TextOut(100 + 300 - 30, 253, _T("-12"));
		dcMem.TextOut(100 + 400 - 30, 253, _T("-11"));
		dcMem.TextOut(100 + 500 - 30, 253, _T("-10"));
	}
	else if (m_i32Index == 1)
	{
		dcMem.TextOut(100 + 0 - 30, 253, _T("-10"));
		dcMem.TextOut(100 + 100 - 30, 253, _T("-9"));
		dcMem.TextOut(100 + 200 - 30, 253, _T("-8"));
		dcMem.TextOut(100 + 300 - 30, 253, _T("-7"));
		dcMem.TextOut(100 + 400 - 30, 253, _T("-6"));
		dcMem.TextOut(100 + 500 - 30, 253, _T("-5"));
	}
	else
	{
		dcMem.TextOut(100 + 0 - 30, 253, _T("-5"));
		dcMem.TextOut(100 + 100 - 30, 253, _T("-4"));
		dcMem.TextOut(100 + 200 - 30, 253, _T("-3"));
		dcMem.TextOut(100 + 300 - 30, 253, _T("-2"));
		dcMem.TextOut(100 + 400 - 30, 253, _T("-1"));
		dcMem.TextOut(100 + 500 - 30, 253, _T("0"));
	}
	
	dcMem.TextOut(5 , 233, _T("时间偏移"));
	dcMem.TextOut(25, 257, _T("(秒)"));
}

void CAlmRecWave::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 + 40, _T("ECG"));

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

	for (i = 2000 * m_i32Index; i < 2000 * m_i32Index+2000; i++)
	{
		switch (type)
		{
		case ECG_I_LEAD:
			t = 120 - m_stSAVE_ALM.pECGData[0][i] * CAlmRecWave::m_i32ECGPixel / CAlmRecWave::m_i32ECGValue;
			break;

		case ECG_II_LEAD:
			t = 120 - m_stSAVE_ALM.pECGData[1][i] * CAlmRecWave::m_i32ECGPixel / CAlmRecWave::m_i32ECGValue;
			break;
		}

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

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


void CAlmRecWave::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 + 40, _T("Resp"));

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

	for (i = 500 * m_i32Index; i < 500 + 500 * m_i32Index; i++)
	{
		t = 120 - m_stSAVE_ALM.pRespData[i] * CAlmRecWave::m_i32RespPixel / CAlmRecWave::m_i32RespValue;

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

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

	dcMem.Polyline(m_pCPonit, 500);
}


void CAlmRecWave::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 + 40, _T("Pleth"));

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

	for (i = 500 * m_i32Index; i < 500 + 500 * m_i32Index; i++)
	{
		t = 120 - m_stSAVE_ALM.pPlethData[i] * CAlmRecWave::m_i32PlethPixel / CAlmRecWave::m_i32PlethValue;

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

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


void CAlmRecWave::Move_Draw_Section(INT dir)
{
	if (dir==-1)
	{
		if (m_i32Index >= 1)
		{
			m_i32Index--;
		}
	}
	else if (dir==1)
	{
		if (m_i32Index <=1)
		{
			m_i32Index++;
		}
	}
}


BOOL CAlmRecWave::OnEraseBkgnd(CDC* pDC)
{

	return TRUE;
}


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

	//	CStatic::OnPaint();
}

