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

#include "stdafx.h"
#include "Verbpo.h"
#include "ThreadDraw.h"
#include "DialogAlmRec.h"
#include "CStaticWave\ECGRespSpo2Trend.h"
#include "StaticValueECGNormal.h"
#include "StaticValueRespNormal.h"
#include "StaticValueSPO2Normal.h"
#include "Data.h"

IMPLEMENT_DYNCREATE(CThreadDraw, CWinThread)

CThreadDraw::CThreadDraw()
{
	m_i32Init = 0;
	m_i32DrawFlag = 0;
	m_i32Redraw = 0;
	m_i32Show = 0;
	m_i32Time = 0;
	m_pi16Spo2 = new INT16[150];
	m_pi16HRBuf = new INT16[150];
	m_pCPonit = new CPoint[150 * 25];
	m_pRespWaveBuff1 = new INT16[150 * 25];
	m_pRespWaveBuff2 = new INT16[150 * 25];
	m_pi16RR = new INT16[150];
	m_i32RRBufW = 0, m_i32RRBufR = 0, m_i32RRBufL = 0;
	m_i32HRBufW = 0, m_i32HRBufR = 0, m_i32HRBufL = 0;
	m_i32Spo2BufW = 0, m_i32Spo2BufR = 0, m_i32Spo2BufL = 0;
	m_u16HRNum = 0;
	m_i32RespWaveBuffW1 = 0, m_i32RespWaveBuffR1 = 0, m_i32RespWaveBuffL1 = 0;
	m_i32RespWaveBuffW2 = 0, m_i32RespWaveBuffR2 = 0, m_i32RespWaveBuffL2 = 0;
	m_u16RespWaveSum = 0, m_u16RespWaveNum = 0;

	m_pi16Spo2AddBuf = new INT16[150];
	m_pi16HRAddBuf = new INT16[150];
	m_pi16RRAddBuf = new INT16[150];
	m_pRespWaveAddBuff1 = new INT16[150 * 25];
	m_pRespWaveAddBuff2 = new INT16[150 * 25];

	m_i32RRAddBufW = 0, m_i32RRAddBufR = 0, m_i32RRAddBufL = 0;
	m_i32HRAddBufW = 0, m_i32HRAddBufR = 0, m_i32HRAddBufL = 0;
	m_i32Spo2AddBufW = 0, m_i32Spo2AddBufR = 0, m_i32Spo2AddBufL = 0;
	m_i32RespWaveBuffW1 = 0, m_i32RespWaveBuffR1 = 0, m_i32RespWaveBuffL1 = 0;
	m_i32RespWaveBuffW2 = 0, m_i32RespWaveBuffR2 = 0, m_i32RespWaveBuffL2 = 0;
}


CThreadDraw::~CThreadDraw()
{
	delete m_pi16Spo2;
	delete m_pi16HRBuf;
	delete m_pi16RR;
	delete m_pCPonit;
	delete m_pRespWaveBuff1;
	delete m_pRespWaveBuff2;

	delete m_pi16Spo2AddBuf ;
	delete m_pi16HRAddBuf ;
	delete m_pi16RRAddBuf;
	delete m_pRespWaveAddBuff1;
	delete m_pRespWaveAddBuff2 ;
}

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

int CThreadDraw::ExitInstance()
{
	dcMem.DeleteDC();
	bmp.DeleteObject();

	dcMemBK.DeleteDC();
	bmpBK.DeleteObject();

	dcMemBK2.DeleteDC();
	bmpBK2.DeleteObject();

	font.DeleteObject();
	fontSet.DeleteObject();
	delete penBK;
	delete penGrid;
	delete penECGLine;
	delete penRespLine;
	delete penSpo2Line;
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CThreadDraw, CWinThread)
END_MESSAGE_MAP()


// CThreadDraw 消息处理程序

void CThreadDraw::init_dc(CDC* pDC)
{
	dcMem.CreateCompatibleDC(pDC);
	//创建兼容位图
	bmp.CreateCompatibleBitmap(pDC, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height);
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height, RGB(0, 0, 0));

	dcMemBK.CreateCompatibleDC(pDC);
	//创建兼容位图
	bmpBK.CreateCompatibleBitmap(pDC, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height);
	dcMemBK.SelectObject(&bmpBK); //将位图选择进内存DC
	dcMemBK.FillSolidRect(0, 0, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height, RGB(0, 0, 0));

	dcMemBK2.CreateCompatibleDC(pDC);
	//创建兼容位图
	bmpBK2.CreateCompatibleBitmap(pDC, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height);
	dcMemBK2.SelectObject(&bmpBK2); //将位图选择进内存DC
	dcMemBK2.FillSolidRect(0, 0, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::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("宋体"));

	fontSet.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("宋体"));

	penBK = new CPen(PS_SOLID, 1, RGB(210, 210, 210));
	penGrid = new CPen(PS_DOT, 1, RGB(210, 210, 210));
	penECGLine = new CPen(PS_SOLID, 2, CStaticValueECGNormal::m_rgbText);
	penRespLine = new CPen(PS_SOLID, 2, CStaticValueRespNormal::m_rgbText);
	penSpo2Line = new CPen(PS_SOLID, 2, CStaticValueSPO2Normal::m_rgbText);

	//画出框子

	dcMemBK.SelectObject(penBK);

	//上横线
	dcMemBK.MoveTo(0, 0);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 0);
	//下横线
	dcMemBK.MoveTo(0, CECGRespSpo2Trend::m_i32Height - 1);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height - 1);
	//左竖线
	dcMemBK.MoveTo(0, 0);
	dcMemBK.LineTo(0, CECGRespSpo2Trend::m_i32Height);
	//右竖线
	dcMemBK.MoveTo(CECGRespSpo2Trend::m_i32Width - 1, 0);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width - 1, CECGRespSpo2Trend::m_i32Height - 1);

	//HR 横向 虚线
	dcMemBK.SelectObject(penGrid);
	dcMemBK.MoveTo(35, 2);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 2);

	dcMemBK.MoveTo(35, 16);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 16);

	dcMemBK.MoveTo(35, 30);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 30);

	dcMemBK.MoveTo(35, 44);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 44);

	dcMemBK.MoveTo(35, 58);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 58);

	dcMemBK.MoveTo(35, 72);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 72);

	//HR 竖向 虚线
	dcMemBK.MoveTo(35, 2);
	dcMemBK.LineTo(35, 72);

	dcMemBK.MoveTo(35 + 180, 2);
	dcMemBK.LineTo(35 + 180, 72);

	dcMemBK.MoveTo(35 + 180 * 2, 2);
	dcMemBK.LineTo(35 + 180 * 2, 72);

	dcMemBK.MoveTo(35 + 180 * 3, 2);
	dcMemBK.LineTo(35 + 180 * 3, 72);

	dcMemBK.MoveTo(35 + 180 * 4, 2);
	dcMemBK.LineTo(35 + 180 * 4, 72);

	dcMemBK.MoveTo(35 + 180 * 5, 2);
	dcMemBK.LineTo(35 + 180 * 5, 72);

	//HR SPO2 分割线
	dcMemBK.SelectObject(penBK);
	dcMemBK.MoveTo(35, 74);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 74);

	//Spo2  横向 虚线
	dcMemBK.SelectObject(penGrid);
	dcMemBK.MoveTo(35, 76);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 76);

	dcMemBK.MoveTo(35, 90);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 90);

	dcMemBK.MoveTo(35, 104);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 104);

	dcMemBK.MoveTo(35, 118);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 118);

	dcMemBK.MoveTo(35, 132);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 132);

	dcMemBK.MoveTo(35, 147);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 147);

	//Spo2  竖向 虚线
	dcMemBK.MoveTo(35, 76);
	dcMemBK.LineTo(35, 147);

	dcMemBK.MoveTo(35 + 180, 76);
	dcMemBK.LineTo(35 + 180, 147);

	dcMemBK.MoveTo(35 + 180 * 2, 76);
	dcMemBK.LineTo(35 + 180 * 2, 147);

	dcMemBK.MoveTo(35 + 180 * 3, 76);
	dcMemBK.LineTo(35 + 180 * 3, 147);

	dcMemBK.MoveTo(35 + 180 * 4, 76);
	dcMemBK.LineTo(35 + 180 * 4, 147);

	dcMemBK.MoveTo(35 + 180 * 5, 76);
	dcMemBK.LineTo(35 + 180 * 5, 147);

	//SPO2 RR分割线
	dcMemBK.SelectObject(penBK);
	dcMemBK.MoveTo(35, 149);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 149);

	//RR 横向 虚线
	dcMemBK.SelectObject(penGrid);
	dcMemBK.MoveTo(35, 151);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 151);

	dcMemBK.SelectObject(penGrid);
	dcMemBK.MoveTo(35, 168);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 168);

	dcMemBK.MoveTo(35, 185);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 185);

	dcMemBK.MoveTo(35, 202);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 202);

	dcMemBK.MoveTo(35, 219);
	dcMemBK.LineTo(CECGRespSpo2Trend::m_i32Width, 219);

	//RR 竖向 虚线
	dcMemBK.MoveTo(35, 149);
	dcMemBK.LineTo(35, 219);

	dcMemBK.MoveTo(35 + 180, 149);
	dcMemBK.LineTo(35 + 180, 219);

	dcMemBK.MoveTo(35 + 180 * 2, 149);
	dcMemBK.LineTo(35 + 180 * 2, 219);

	dcMemBK.MoveTo(35 + 180 * 3, 149);
	dcMemBK.LineTo(35 + 180 * 3, 219);

	dcMemBK.MoveTo(35 + 180 * 4, 149);
	dcMemBK.LineTo(35 + 180 * 4, 219);

	dcMemBK.MoveTo(35 + 180 * 5, 149);
	dcMemBK.LineTo(35 + 180 * 5, 219);

	dcMemBK.SelectObject(&font);
	dcMemBK.SetTextColor(CStaticValueECGNormal::m_rgbText);

	dcMemBK.TextOut(0, 2, _T("350"));
	dcMemBK.TextOut(0, 30, _T("HR"));
	dcMemBK.TextOut(0, 58, _T("0"));
	dcMemBK.SetTextColor(CStaticValueSPO2Normal::m_rgbText);
	dcMemBK.TextOut(0, 76, _T("100"));
	dcMemBK.TextOut(0, 104, _T("So2"));
	dcMemBK.TextOut(0, 130, _T("0"));
	dcMemBK.SetTextColor(CStaticValueRespNormal::m_rgbText);
	dcMemBK.TextOut(0, 149, _T("120"));
	dcMemBK.TextOut(0, 178, _T("RR"));
	dcMemBK.TextOut(0, 206, _T("0"));




	dcMemBK2.SelectObject(penBK);

	//上横线
	dcMemBK2.MoveTo(0, 0);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 0);
	//下横线
	dcMemBK2.MoveTo(0, CECGRespSpo2Trend::m_i32Height - 1);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height - 1);
	//左竖线
	dcMemBK2.MoveTo(0, 0);
	dcMemBK2.LineTo(0, CECGRespSpo2Trend::m_i32Height);
	//右竖线
	dcMemBK2.MoveTo(CECGRespSpo2Trend::m_i32Width - 1, 0);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width - 1, CECGRespSpo2Trend::m_i32Height - 1);

	//HR 横向 虚线
	dcMemBK2.SelectObject(penGrid);
	dcMemBK2.MoveTo(35, 2);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 2);

	dcMemBK2.MoveTo(35, 16);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 16);

	dcMemBK2.MoveTo(35, 30);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 30);

	dcMemBK2.MoveTo(35, 44);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 44);

	dcMemBK2.MoveTo(35, 58);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 58);

	dcMemBK2.MoveTo(35, 72);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 72);

	//HR 竖向 虚线
	dcMemBK2.MoveTo(35, 2);
	dcMemBK2.LineTo(35, 72);

	dcMemBK2.MoveTo(35 + 180, 2);
	dcMemBK2.LineTo(35 + 180, 72);

	dcMemBK2.MoveTo(35 + 180 * 2, 2);
	dcMemBK2.LineTo(35 + 180 * 2, 72);

	dcMemBK2.MoveTo(35 + 180 * 3, 2);
	dcMemBK2.LineTo(35 + 180 * 3, 72);

	dcMemBK2.MoveTo(35 + 180 * 4, 2);
	dcMemBK2.LineTo(35 + 180 * 4, 72);

	dcMemBK2.MoveTo(35 + 180 * 5, 2);
	dcMemBK2.LineTo(35 + 180 * 5, 72);

	//HR SPO2 分割线
	dcMemBK2.SelectObject(penBK);
	dcMemBK2.MoveTo(35, 74);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 74);

	//Spo2  横向 虚线
	dcMemBK2.SelectObject(penGrid);
	dcMemBK2.MoveTo(35, 76);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 76);

	dcMemBK2.MoveTo(35, 90);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 90);

	dcMemBK2.MoveTo(35, 104);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 104);

	dcMemBK2.MoveTo(35, 118);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 118);

	dcMemBK2.MoveTo(35, 132);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 132);

	dcMemBK2.MoveTo(35, 147);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 147);

	//Spo2  竖向 虚线
	dcMemBK2.MoveTo(35, 76);
	dcMemBK2.LineTo(35, 147);

	dcMemBK2.MoveTo(35 + 180, 76);
	dcMemBK2.LineTo(35 + 180, 147);

	dcMemBK2.MoveTo(35 + 180 * 2, 76);
	dcMemBK2.LineTo(35 + 180 * 2, 147);

	dcMemBK2.MoveTo(35 + 180 * 3, 76);
	dcMemBK2.LineTo(35 + 180 * 3, 147);

	dcMemBK2.MoveTo(35 + 180 * 4, 76);
	dcMemBK2.LineTo(35 + 180 * 4, 147);

	dcMemBK2.MoveTo(35 + 180 * 5, 76);
	dcMemBK2.LineTo(35 + 180 * 5, 147);

	//SPO2 RR分割线
	dcMemBK2.SelectObject(penBK);
	dcMemBK2.MoveTo(35, 149);
	dcMemBK2.LineTo(CECGRespSpo2Trend::m_i32Width, 149);


	dcMemBK2.SelectObject(&font);
	dcMemBK2.SetTextColor(CStaticValueECGNormal::m_rgbText);

	dcMemBK2.TextOut(0, 2, _T("350"));
	dcMemBK2.TextOut(0, 30, _T("HR"));
	dcMemBK2.TextOut(0, 58, _T("0"));
	dcMemBK2.SetTextColor(CStaticValueSPO2Normal::m_rgbText);
	dcMemBK2.TextOut(0, 76, _T("100"));
	dcMemBK2.TextOut(0, 104, _T("So2"));
	dcMemBK2.TextOut(0, 130, _T("0"));
	dcMemBK2.SetTextColor(CStaticValueRespNormal::m_rgbText);
	dcMemBK2.TextOut(0, 155, _T("呼吸"));
	dcMemBK2.TextOut(0, 188, _T("波"));

	Erase();
	Draw_Time();
}


void CThreadDraw::get_data()
{
	INT i = 0, j = 0;

	m_csShow.Lock();
	m_i32RespWaveAddBuffR1 = (m_i32RespWaveAddBuffW1 + 25 * 75 - m_i32RespWaveAddBuffL1) % (25 * 75);
	for (i = 0; i < m_i32RespWaveAddBuffL1; i++)
	{
		m_pRespWaveBuff1[m_i32RespWaveBuffW1] = m_pRespWaveAddBuff1[m_i32RespWaveAddBuffR1];
		m_i32RespWaveBuffW1 = (m_i32RespWaveBuffW1 + 1) % (25 * 75);
		m_i32RespWaveAddBuffR1 = (m_i32RespWaveAddBuffR1 + 1) % (25 * 75);
		if (m_i32RespWaveBuffL1<25 * 75)
		{
			m_i32RespWaveBuffL1++;
		}
	}
	m_i32RespWaveAddBuffL1 = 0;

	m_i32RespWaveAddBuffR2 = (m_i32RespWaveAddBuffW2 + 25 * 150 - m_i32RespWaveAddBuffL2) % (25 * 150);
	for (i = 0; i < m_i32RespWaveAddBuffL2; i++)
	{
		m_pRespWaveBuff2[m_i32RespWaveBuffW2] = m_pRespWaveAddBuff2[m_i32RespWaveAddBuffR2];
		m_i32RespWaveBuffW2 = (m_i32RespWaveBuffW2 + 1) % (25 * 150);
		m_i32RespWaveAddBuffR2 = (m_i32RespWaveAddBuffR2 + 1) % (25 * 150);
		if (m_i32RespWaveBuffL2<25 * 150)
		{
			m_i32RespWaveBuffL2++;
		}
	}
	m_i32RespWaveAddBuffL2 = 0;

	m_i32HRAddBufR = (m_i32HRAddBufW + 150 - m_i32HRAddBufL) % (150);
	for (i = 0; i < m_i32HRAddBufL; i++)
	{
		m_pi16HRBuf[m_i32HRBufW] = m_pi16HRAddBuf[m_i32HRAddBufR];
		m_i32HRBufW = (m_i32HRBufW + 1) % 150;
		m_i32HRAddBufR = (m_i32HRAddBufR + 1) % 150;
		if (m_i32HRBufL < 150)
		{
			m_i32HRBufL++;
		}
	}
	m_i32HRAddBufL = 0;

	m_i32Spo2AddBufR = (m_i32Spo2AddBufW + 150 - m_i32Spo2AddBufL) % (150);
	for (i = 0; i < m_i32Spo2AddBufL; i++)
	{
		m_pi16Spo2[m_i32Spo2BufW] = m_pi16Spo2AddBuf[m_i32Spo2AddBufR];
		m_i32Spo2BufW = (m_i32Spo2BufW + 1) % 150;
		m_i32Spo2AddBufR = (m_i32Spo2AddBufR + 1) % 150;
		if (m_i32Spo2BufL < 150)
		{
			m_i32Spo2BufL++;
		}
	}
	m_i32Spo2AddBufL = 0;

	if (m_i32RRAddBufL)
	{
		m_csDC.Lock();
		m_i32Redraw = 1;
		m_csDC.Unlock();
	}

	m_i32RRAddBufR = (m_i32RRAddBufW + 150 - m_i32RRAddBufL) % (150);
	for (i = 0; i < m_i32RRAddBufL; i++)
	{
		m_pi16RR[m_i32RRBufW] = m_pi16RRAddBuf[m_i32RRAddBufR];
		m_i32RRBufW = (m_i32RRBufW + 1) % 150;
		m_i32RRAddBufR = (m_i32RRAddBufR + 1) % 150;
		if (m_i32RRBufL < 150)
		{
			m_i32RRBufL++;
		}
	}
	m_i32RRAddBufL = 0;

	m_csShow.Unlock();


/*
	m_csDC.Lock();
	a_i32DrawFlag = m_i32DrawFlag;
	if (a_i32DrawFlag == 0)
	{
		m_i32DrawFlag = 3;
	}
	m_csDC.Unlock();

	if (a_i32DrawFlag == 0)
	{
		for (i = 0; i < l; i++)
		{
			m_u16RespWaveSum += wave[i];
			if (++m_u16RespWaveNum >= 5)
			{
				m_u16RespWaveNum = 0;
				m_pRespWaveBuff1[m_i32RespWaveBuffW1] = 219 - m_u16RespWaveSum  * CECGRespSpo2Trend::m_i32RespPixel / CECGRespSpo2Trend::m_i32RespValue / 5;
				m_pRespWaveBuff2[m_i32RespWaveBuffW2] = 219 - m_u16RespWaveSum  * CECGRespSpo2Trend::m_i32RespPixel / CECGRespSpo2Trend::m_i32RespValue / 5;
				m_u16RespWaveSum = 0;


				m_i32RespWaveBuffW1 = (m_i32RespWaveBuffW1 + 1) % (25 * 75);
				m_i32RespWaveBuffW2 = (m_i32RespWaveBuffW2 + 1) % (25 * 150);
				if (m_i32RespWaveBuffL1<25 * 75)
				{
					m_i32RespWaveBuffL1++;
				}
				if (m_i32RespWaveBuffL2 < 25 * 150)
				{
					m_i32RespWaveBuffL2++;
				}

				if (++m_u16HRNum >= 100)
				{
					m_u16HRNum = 0;
					m_pi16HRBuf[m_i32HRBufW] = hr;
					m_i32HRBufW = (m_i32HRBufW + 1) % 150;

					m_pi16Spo2[m_i32Spo2BufW] = spo2;
					m_i32Spo2BufW = (m_i32Spo2BufW + 1) % 150;

					m_pi16RR[m_i32RRBufW] = rr;
					m_i32RRBufW = (m_i32RRBufW + 1) % 150;

					if (m_i32HRBufL < 150)
					{
						m_i32HRBufL++;
					}
					if (m_i32Spo2BufL < 150)
					{
						m_i32Spo2BufL++;
					}
					if (m_i32RRBufL < 150)
					{
						m_i32RRBufL++;
					}
					m_csDC.Lock();
					m_i32Redraw = 1;
					m_csDC.Unlock();
				}
			}
		}
		m_csDC.Lock();
		m_i32DrawFlag = 0;
		m_csDC.Unlock();
	}*/

}

//绘制呼吸波形
void CThreadDraw::Draw_Wave()
{
	INT m_i32SaveDC = 0;
	INT i = 0, j = 0, t = 0, k = 0;

	m_i32SaveDC = dcMem.SaveDC();
	dcMem.SelectObject(penRespLine);
	if (m_i32Time == 0)
	{
		m_i32RespWaveBuffR1 = (m_i32RespWaveBuffW1 + 25 * 75 - 1) % (25 * 75);
		for (i = 0; (i < m_i32RespWaveBuffL1); i++)
		{
			t = 35 + 180 * 5 - i * 12 / 25;
			k =m_pRespWaveBuff1[m_i32RespWaveBuffR1];

			if (k < 151)
				k = 151;
			else if (k>219)
				k = 219;

			m_pCPonit[i].x = t;
			m_pCPonit[i].y = k;
			m_i32RespWaveBuffR1 = (m_i32RespWaveBuffR1 + 25 * 75 - 1) % (25 * 75);
		}
		dcMem.Polyline(m_pCPonit, m_i32RespWaveBuffL1);
	}
	else
	{
		m_i32RespWaveBuffR2 = (m_i32RespWaveBuffW2 + 25 * 150 - 1) % (25 * 150);
		for (i = 0; (i < m_i32RespWaveBuffL2); i++)
		{
			t = 35 + 180 * 5 - i * 6 / 25;
			k=m_pRespWaveBuff2[m_i32RespWaveBuffR2];
			if (k < 151)
				k = 151;
			else if (k>219)
				k = 219;

			m_pCPonit[i].x = t;
			m_pCPonit[i].y = k;
			m_i32RespWaveBuffR2 = (m_i32RespWaveBuffR2 + 25 * 150 - 1) % (25 * 150);
		}
		dcMem.Polyline(m_pCPonit, m_i32RespWaveBuffL2);
	}
	dcMem.RestoreDC(m_i32SaveDC);
}

//趋势共存图 绘制 心律
void CThreadDraw::Draw_HR()
{
	INT m_i32SaveDC = 0;
	INT i = 0, j = 0, l = 0, t = 0, a = 0;
	if (m_i32HRBufL)
	{
		if (m_i32Time == 0)
		{
			l = 75;
			t = 12;
		}
		else
		{
			l = 150;
			t = 6;
		}
		m_i32HRBufR = (m_i32HRBufW + 150 - 1) % 150;
		//如果心律 数值无效 则不绘制响应的点
		if (m_pi16HRBuf[m_i32HRBufR] == CData::m_i16HRInvalidValue)
		{
			a = 1000;
		} 
		else
		{
			a = (m_pi16HRBuf[m_i32HRBufR] - 0) * 14 / 70;
			if (a > 70)
			{
				a = 70;
			}
			else if (a < 0)
			{
				a = 0;
			}
		}
		
		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(penECGLine);
		dcMem.MoveTo(35 + 180 * 5, 72 - a);

		for (i = 0; (i < m_i32HRBufL) && (i < l); i++)
		{
			dcMem.LineTo(35 + 180 * 5 - i*t, 72 - a);
			m_i32HRBufR = (m_i32HRBufR + 150 - 1) % 150;
			//如果心律 数值无效 则不绘制响应的点
			if (m_pi16HRBuf[m_i32HRBufR] == CData::m_i16HRInvalidValue)
			{
				a = 1000;
			}
			else
			{
				a = (m_pi16HRBuf[m_i32HRBufR] - 0) * 14 / 70;
				if (a > 70)
				{
					a = 70;
				}
				else if (a < 0)
				{
					a = 0;
				}
			}

			
			dcMem.MoveTo(35 + 180 * 5 - i*t, 72 - a);
		}
		dcMem.RestoreDC(m_i32SaveDC);
	}
}

//趋势共存图 绘制 血氧饱和度
void CThreadDraw::Draw_Spo2()
{
	INT m_i32SaveDC = 0;
	INT i = 0, j = 0, l = 0, t = 0, a = 0;
	if (m_i32Spo2BufL)
	{
		if (m_i32Time == 0)
		{
			l = 75;
			t = 12;
		}
		else
		{
			l = 150;
			t = 6;
		}
		m_i32Spo2BufR = (m_i32Spo2BufW + 150 - 1) % 150;

		//如果血氧饱和度 数值无效 则不绘制响应的点
		if (m_pi16Spo2[m_i32Spo2BufR] == CData::m_i16Spo2InvalidValue)
		{
			a = 1000;
		} 
		else
		{
			a = (m_pi16Spo2[m_i32Spo2BufR] - 0) * 14 / 20;
			if (a > 70)
			{
				a = 70;
			}
			else if (a < 0)
			{
				a = 0;
			}
		}

		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(penSpo2Line);
		dcMem.MoveTo(35 + 180 * 5, 147 - a);

		for (i = 0; (i < m_i32Spo2BufL) && (i < l); i++)
		{
			dcMem.LineTo(35 + 180 * 5 - i*t, 147 - a);
			m_i32Spo2BufR = (m_i32Spo2BufR + 150 - 1) % 150;
			//如果血氧饱和度 数值无效 则不绘制响应的点
			if (m_pi16Spo2[m_i32Spo2BufR] == CData::m_i16Spo2InvalidValue)
			{
				a = 1000;
			}
			else
			{
				a = (m_pi16Spo2[m_i32Spo2BufR] - 0) * 14 / 20;
				if (a > 70)
				{
					a = 70;
				}
				else if (a < 0)
				{
					a = 0;
				}
			}
			dcMem.MoveTo(35 + 180 * 5 - i*t, 147 - a);
		}
		dcMem.RestoreDC(m_i32SaveDC);
	}
}

//趋势共存图 绘制 呼吸率
void CThreadDraw::Draw_RR()
{
	INT m_i32SaveDC = 0;
	INT i = 0, j = 0, l = 0, t = 0, a = 0;
	if (m_i32RRBufL)
	{
		if (m_i32Time == 0)
		{
			l = 75;
			t = 12;
		}
		else
		{
			l = 150;
			t = 6;
		}
		m_i32RRBufR = (m_i32RRBufW + 150 - 1) % 150;
		//如果呼吸率 数值无效 则不绘制响应的点
		if (m_pi16RR[m_i32RRBufR] == CData::m_i16RRInvalidValue)
		{
			a = 1000;
		} 
		else
		{
			a = (m_pi16RR[m_i32RRBufR] - 0) * 17 / 30;
			if (a > 68)
			{
				a = 68;
			}
			else if (a < 0)
			{
				a = 0;
			}
		}
	
		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(penRespLine);
		dcMem.MoveTo(35 + 180 * 5, 219 - a);

		for (i = 0; (i < m_i32RRBufL) && (i < l); i++)
		{
			dcMem.LineTo(35 + 180 * 5 - i*t, 219 - a);
			m_i32RRBufR = (m_i32RRBufR + 150 - 1) % 150;
			//如果呼吸率 数值无效 则不绘制响应的点
			if (m_pi16RR[m_i32RRBufR] == CData::m_i16RRInvalidValue)
			{
				a = 1000;
			}
			else
			{
				a = (m_pi16RR[m_i32RRBufR] - 0) * 17 / 30;
				if (a > 68)
				{
					a = 68;
				}
				else if (a < 0)
				{
					a = 0;
				}
			}
			
			dcMem.MoveTo(35 + 180 * 5 - i*t, 219 - a);
		}
		dcMem.RestoreDC(m_i32SaveDC);
	}
}

//绘制时间轴
void CThreadDraw::Draw_Time()
{
	INT m_i32SaveDC = 0;
	INT a_i32Show = 0, a_i32Time=0;
	CFont *a_oldCFont;

	m_csShow.Lock();
	a_i32Show = m_i32Show;
	a_i32Time = m_i32Time;
	m_csShow.Unlock();

	if (a_i32Show == 0)
	{
		m_i32SaveDC=dcMem.SaveDC();
		
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(210, 210, 210));
		dcMem.TextOut(10, 222, _T("-75s"));
		dcMem.TextOut(10 + 180, 222, _T("-60s"));
		dcMem.TextOut(10 + 180 * 2, 222, _T("-45s"));
		dcMem.TextOut(10 + 180 * 3, 222, _T("-30s"));
		dcMem.TextOut(10 + 180 * 4, 222, _T("-15s"));

		a_oldCFont = dcMem.SelectObject(&fontSet);
		dcMem.SetTextColor(CStaticValueECGNormal::m_rgbText);
		dcMem.TextOut(10 + 180 * 3 + 70, 222, _T("呼吸率"));
		
		dcMem.RestoreDC(m_i32SaveDC);
	}
	else
	{
		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(210, 210, 210));
		dcMem.TextOut(10, 222, _T("-150s"));
		dcMem.TextOut(10 + 180, 222, _T("-120s"));
		dcMem.TextOut(10 + 180 * 2, 222, _T("-90s"));
		dcMem.TextOut(10 + 180 * 3, 222, _T("-60s"));
		dcMem.TextOut(10 + 180 * 4, 222, _T("-30s"));

		a_oldCFont = dcMem.SelectObject(&fontSet);
		dcMem.SetTextColor(CStaticValueECGNormal::m_rgbText);
		dcMem.TextOut(10 + 180 * 3 + 70, 222, _T("呼吸波"));
		
		dcMem.RestoreDC(m_i32SaveDC);
		
	}
	if (a_i32Time==0)
	{
		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(&fontSet);
		dcMem.SetTextColor(CStaticValueECGNormal::m_rgbText);
		dcMem.TextOut(10 + 180 + 70, 222, _T("75秒"));
		dcMem.RestoreDC(m_i32SaveDC);
	}
	else
	{
		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(&fontSet);
		dcMem.SetTextColor(CStaticValueECGNormal::m_rgbText);
		dcMem.TextOut(10 + 180 + 70, 222, _T("150秒"));
		dcMem.RestoreDC(m_i32SaveDC);
	}
}


void CThreadDraw::Erase()
{
	INT m_i32SaveDC = 0;
	m_i32SaveDC = dcMem.SaveDC();
	if (m_i32Show == 0)
		dcMem.BitBlt(0, 0, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height, &dcMemBK, 0, 0, SRCCOPY);
	else
		dcMem.BitBlt(0, 0, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height, &dcMemBK2, 0, 0, SRCCOPY);
	dcMem.RestoreDC(m_i32SaveDC);
}


void CThreadDraw::add_data(UINT8 *wave, INT l, INT16 rr, INT16 hr, INT16 spo2)
{
	INT i = 0, j = 0;

	m_csShow.Lock();
	for (i = 0; i < l; i++)
	{
		m_u16RespWaveSum += wave[i];
		if (++m_u16RespWaveNum >= 5)
		{
			m_u16RespWaveNum = 0;
			m_pRespWaveAddBuff1[m_i32RespWaveAddBuffW1] = 219 - m_u16RespWaveSum  * CECGRespSpo2Trend::m_i32RespPixel / CECGRespSpo2Trend::m_i32RespValue / 5;
			m_pRespWaveAddBuff2[m_i32RespWaveAddBuffW2] = 219 - m_u16RespWaveSum  * CECGRespSpo2Trend::m_i32RespPixel / CECGRespSpo2Trend::m_i32RespValue / 5;
			m_u16RespWaveSum = 0;

			m_i32RespWaveAddBuffW1 = (m_i32RespWaveAddBuffW1 + 1) % (25 * 75);
			m_i32RespWaveAddBuffW2 = (m_i32RespWaveAddBuffW2 + 1) % (25 * 150);
			if (m_i32RespWaveAddBuffL1<25 * 75)
			{
				m_i32RespWaveAddBuffL1++;
			}
			if (m_i32RespWaveAddBuffL2 < 25 * 150)
			{
				m_i32RespWaveAddBuffL2++;
			}

			if (++m_u16HRNum >= 25)
			{
				m_u16HRNum = 0;
				m_pi16HRAddBuf[m_i32HRAddBufW] = hr;
				m_i32HRAddBufW = (m_i32HRAddBufW + 1) % 150;

				m_pi16Spo2AddBuf[m_i32Spo2AddBufW] = spo2;
				m_i32Spo2AddBufW = (m_i32Spo2AddBufW + 1) % 150;

				m_pi16RRAddBuf[m_i32RRAddBufW] = rr;
				m_i32RRAddBufW = (m_i32RRAddBufW + 1) % 150;

				if (m_i32HRAddBufL < 150)
				{
					m_i32HRAddBufL++;
				}
				if (m_i32Spo2AddBufL < 150)
				{
					m_i32Spo2AddBufL++;
				}
				if (m_i32RRAddBufL < 150)
				{
					m_i32RRAddBufL++;
				}
			}
		}
	}
	m_csShow.Unlock();
}


void CThreadDraw::show_change(INT a)
{
	m_csShow.Lock();
	m_i32Show = a;
	m_csShow.Unlock();
}


void CThreadDraw::time_change(INT a)
{
	m_csShow.Lock();
	m_i32Time = a;
	m_csShow.Unlock();
}


void CThreadDraw::draw()
{
	INT a_i32DrawFlag = 0, a_i32Redraw = 0, a_i32Show=0;

	m_csShow.Lock();
	a_i32Show=m_i32Show;
	m_csShow.Unlock();

	//绘制的时候，上锁
	m_csDC.Lock();
	a_i32DrawFlag = m_i32DrawFlag;
	a_i32Redraw = m_i32Redraw;
	if (a_i32DrawFlag==0)
	{
		m_i32DrawFlag = 1;
	}
	m_csDC.Unlock();

	if ((a_i32DrawFlag == 0))
	{
		if (a_i32Redraw == 1)
		{
			Erase();

			if (a_i32Show == 0)
			{
				Draw_RR();
			}
			else
			{
				Draw_Wave();
			}

			Draw_HR();
			Draw_Spo2();
			Draw_Time();
			m_csDC.Lock();
			m_i32Redraw = 0;
			m_csDC.Unlock();
		}
		
		m_csDC.Lock();
		m_i32DrawFlag = 0;
		m_csDC.Unlock();
	}
}


INT CThreadDraw::try_copy_dc(CDC* pDC)
{
	INT a_i32DrawFlag;

	//如果没有在绘制 趋势共存图 时候，才可以拷贝图形
	m_csDC.Lock();
	a_i32DrawFlag = m_i32DrawFlag;
	if (a_i32DrawFlag == 0)
	{
		m_i32DrawFlag = 2;
	}
	m_csDC.Unlock();
	if (a_i32DrawFlag == 0)
	{
		pDC->BitBlt(0, 0, CECGRespSpo2Trend::m_i32Width, CECGRespSpo2Trend::m_i32Height, &dcMem, 0, 0, SRCCOPY);
		m_csDC.Lock();
		m_i32DrawFlag = 0;
		m_csDC.Unlock();

		return 0;
	}
	return 1;
}


int CThreadDraw::Run()
{

	while (1)
	{
		if (m_i32Init)
		{
			get_data();
			draw();
		}
		
		if (m_i32Exit)
		{
			return ExitInstance();
		}
		Sleep(40);
	}
	return 0;
}