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

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



// CPlethWave

INT CPlethWave::m_i32NormalWidth = 930, CPlethWave::m_i32NormalHeight = 120;
INT CPlethWave::m_i32BigWidth = 863, CPlethWave::m_i32BigHeight = 120;
INT CPlethWave::m_i32ECGWidth = 930, CPlethWave::m_i32ECGHeight = 95;
INT CPlethWave::m_i32TrendWidth = 770, CPlethWave::m_i32TrendHeight = 120;

IMPLEMENT_DYNAMIC(CPlethWave, CStatic)

CPlethWave::CPlethWave(int Width, int Height, int LeadType)
{
	m_i32Width = CPlethWave::m_i32NormalWidth;
	m_i32Height = CPlethWave::m_i32NormalHeight;
	m_i32Style = 0;
	//构造心电波形画笔对象
	penRespLine = new CPen(PS_SOLID, 1, RGB(0x80, 0xCD, 0xFF));
	penErase = new CPen(PS_SOLID, 1, RGB(0, 0, 0));

	m_i16WaveDate = new INT16[PLETH_WAVE_BUFF_LENGTH];
	m_i32WaveDateW = 0, m_i32WaveDateR = 0, m_i32WaveDateLength = 0;

	m_i32Freeze = 0;
	m_i32Speed = 1;
	m_i32EraseLast = 0;
	m_u16TempX = 0;
	m_i32EraseFlag = 0;
}

CPlethWave::~CPlethWave()
{
	delete m_i16WaveDate;
	delete penErase;
	delete penRespLine;
	dcMem.DeleteDC(); //用于缓冲作图的内存DC 
	dcMemGrid.DeleteDC(); //用于缓冲作图的内存DC 
	bmp.DeleteObject(); //内存中承载临时图象的位图
	bmpGrid.DeleteObject(); //内存中承载临时图象的位图
}

//设置波形的速度
void CPlethWave::Set_Speed(int speed)
{
	m_i32Speed = speed+1;
}

void CPlethWave::Set_Gain(int gain)
{
	m_i32Gain = gain + 1;
}
/**
*  @brief 清除心电波形缓冲区的数据
*  @author Venus
*  @param   date 心电波形数据
*  @return  0
*  @note
*  @remarks
*  @since  1.0
*/
void CPlethWave::Add_WaveDate(int date)
{
	m_CSWaveData.Lock();
	m_i16WaveDate[m_i32WaveDateW] = date;
	if (++m_i32WaveDateW == PLETH_WAVE_BUFF_LENGTH)
		m_i32WaveDateW = 0;

	if (m_i32WaveDateLength < PLETH_WAVE_BUFF_LENGTH)
	{
		m_i32WaveDateLength++;
	}
	m_CSWaveData.Unlock();
}

//在屏幕上单位长度的像素点个数
void CPlethWave::Set_UnitLength_Pixel(int px)
{
	UnitLength_Pixel = px;
}

//波形的单位长度的数字量
void CPlethWave::Set_UnitLength_Value(int Value)
{
	UnitLength_Value = Value;

}



BEGIN_MESSAGE_MAP(CPlethWave, CStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CREATE()
//	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

int CPlethWave::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, CPlethWave::m_i32NormalWidth, CPlethWave::m_i32NormalHeight);
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, CPlethWave::m_i32NormalWidth, CPlethWave::m_i32NormalHeight, RGB(0, 0, 0));

	dcMemGrid.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmpGrid.CreateCompatibleBitmap(&pDC, CPlethWave::m_i32NormalWidth, CPlethWave::m_i32NormalHeight);
	dcMemGrid.SelectObject(&bmpGrid); //将位图选择进内存DC
	dcMemGrid.FillSolidRect(0, 0, CPlethWave::m_i32NormalWidth, CPlethWave::m_i32NormalHeight, RGB(0, 0, 0));

	int a_i32x = 0, j = 0, i = 0;

	dcMemGrid.SelectObject(penRespLine);
	dcMemGrid.MoveTo(0, 0);
	dcMemGrid.LineTo(0, m_i32Height);
	dcMemGrid.MoveTo(1, 0);
	dcMemGrid.LineTo(1, m_i32Height);
	dcMemGrid.MoveTo(2, 0);
	dcMemGrid.LineTo(2, m_i32Height);

	dcMemGrid.MoveTo(0, 0);
	dcMemGrid.LineTo(20, 0);
	dcMemGrid.MoveTo(0, 1);
	dcMemGrid.LineTo(20, 1);
	dcMemGrid.MoveTo(20, 3);
	dcMemGrid.LineTo(20, 3);

	dcMemGrid.MoveTo(0, m_i32Height - 3);
	dcMemGrid.LineTo(20, m_i32Height - 3);
	dcMemGrid.MoveTo(0, m_i32Height - 2);
	dcMemGrid.LineTo(20, m_i32Height - 2);
	dcMemGrid.MoveTo(0, m_i32Height - 1);
	dcMemGrid.LineTo(20, m_i32Height - 1);

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

//控件鼠标左键双击
void  CPlethWave::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	//	this->GetParent()->PostMessage(WM_ECGWAVE_DBLCLK, 0, 0);
}


//绘制心电波形
void CPlethWave::Draw_Wave()
{
	int a_i32WaveDateLength = 0;

	m_CSWaveData.Lock();
	a_i32WaveDateLength = m_i32WaveDateLength;
	m_CSWaveData.Unlock();

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

//波形显示区域 完全擦除
void CPlethWave::Erase()
{
	m_i32LastX = 0;
	m_u16TempX = 0;
	m_i32EraseLast = 0;
	m_i32EraseFlag = 1;
	dcMem.BitBlt(m_rectCtrl.left, m_rectCtrl.top, m_i32Width, m_i32Height, &dcMemGrid, 0, 0, SRCCOPY);
}


void CPlethWave::Erase_Wave(int x)
{
	CPen *pOldPen;
	int  i = 0;


	for (i = m_i32EraseLast; i < x; i++)
	{
		pOldPen = dcMem.SelectObject(penErase);
		//通过画 黑色竖线的方法 擦除 旧的波形
		dcMem.MoveTo((41 + i) % m_i32Width, 0);
		dcMem.LineTo((41 + i) % m_i32Width, m_i32Height);
		dcMem.MoveTo((42 + i) % m_i32Width, 0);
		dcMem.LineTo((42 + i) % m_i32Width, m_i32Height);
		dcMem.MoveTo((43 + i) % m_i32Width, 0);
		dcMem.LineTo((43 + i) % m_i32Width, m_i32Height);
		dcMem.MoveTo((44 + i) % m_i32Width, 0);
		dcMem.LineTo((44 + i) % m_i32Width, m_i32Height);
		dcMem.MoveTo((45 + i) % m_i32Width, 0);
		dcMem.LineTo((45 + i) % m_i32Width, m_i32Height);
		dcMem.MoveTo((46 + i) % m_i32Width, 0);
		dcMem.LineTo((46 + i) % m_i32Width, m_i32Height);
		dcMem.MoveTo((47 + i) % m_i32Width, 0);
		dcMem.LineTo((47 + i) % m_i32Width, m_i32Height);
	}
	m_i32EraseLast = x;
}

//复位 波形缓冲区的数据
int CPlethWave::Reset_WaveDataBuff()
{
	m_CSWaveData.Lock();
	m_i32WaveDateW = 0, m_i32WaveDateR = 0, m_i32WaveDateLength = 0;
	m_CSWaveData.Unlock();

	return 0;
}

//波形数据 拷贝 到 缓冲区中
void CPlethWave::copy_data(INT16 *buf, int length)
{
	int l = 0;

	l = length;
	if (l > PLETH_WAVE_BUFF_LENGTH)
		l = PLETH_WAVE_BUFF_LENGTH;

	memcpy(m_i16WaveDate, buf, l * 2);

	m_i32WaveDateW = (m_i32WaveDateW + l) % PLETH_WAVE_BUFF_LENGTH;
	m_i32WaveDateLength += l;
	if (m_i32WaveDateLength > PLETH_WAVE_BUFF_LENGTH)
	{
		m_i32WaveDateLength = PLETH_WAVE_BUFF_LENGTH;
	}
}


void CPlethWave::copy_data(CPlethWave & PlethWave)
{
	m_CSWaveData.Lock();
	m_i32WaveDateW = PlethWave.m_i32WaveDateW, m_i32WaveDateR = PlethWave.m_i32WaveDateR, m_i32WaveDateLength = PlethWave.m_i32WaveDateLength;
	memcpy(m_i16WaveDate, PlethWave.m_i16WaveDate, PLETH_WAVE_BUFF_LENGTH * 2);
	m_CSWaveData.Unlock();
}



BOOL CPlethWave::OnEraseBkgnd(CDC* pDC)
{

	return TRUE;
}


void CPlethWave::OnPaint()
{
	CPen *pOldPen;
	int a_i32WaveDateLength = 0, a_i32WaveDateR, i = 0, y = 0, a_i32Temp = 0, j = 0;
	CPaintDC pDC(this);

	m_CSWaveData.Lock();
	a_i32WaveDateLength = m_i32WaveDateLength;
	m_CSWaveData.Unlock();
	a_i32WaveDateR = m_i32WaveDateR;

	float a_fTemp = 0.0f, a_fSpeed = 0.0f;

	//判断接收的缓冲区中有无数据
	if (a_i32WaveDateLength)
	{

		for (i = 0; i < a_i32WaveDateLength; i++)
		{
			y = m_i32Height - (m_i16WaveDate[a_i32WaveDateR] * (1 << (m_i32Gain))*UnitLength_Pixel / UnitLength_Value) / 4;
			if (y >= m_i32Height)
				y = m_i32Height - 1;
			else if (y < 0)
				y = 0;

			a_fSpeed = (m_i32Speed);
			a_fTemp = (float)m_u16TempX;
			a_i32Temp = (int)(a_fTemp*1.0f *a_fSpeed/ 4.0f);

			if (a_i32Temp + 40 < m_i32Width)
			{
				//	if (m_i32XLast != a_i32Temp)
				{
					//是否冻结波形
					if (!m_i32Freeze)
					{
						Erase_Wave(a_i32Temp);
					}
				}
				//是否冻结波形
				if (!m_i32Freeze)
				{
					//冻结，就不画波形
					pOldPen = dcMem.SelectObject(penRespLine);
					if (m_i32EraseFlag)
					{
						m_i32EraseFlag = 0;
						dcMem.MoveTo(40 + m_i32LastX, y);
						dcMem.LineTo(40 + a_i32Temp, y);
					}
					else
					{
						dcMem.MoveTo(40 + m_i32LastX, m_i32LastY);
						dcMem.LineTo(40 + a_i32Temp, y);
					}
					//恢复先前的画笔
					dcMem.SelectObject(pOldPen);
				}
				m_i32LastX = a_i32Temp;
				m_i32LastY = y;
				m_u16TempX++;
			}
			else
			{
				//是否冻结波形
				if (!m_i32Freeze)
				{
					//冻结，就不画波形
					//Erase_Wave(a_i32Temp);
					pOldPen = dcMem.SelectObject(penErase);
					dcMem.MoveTo(40, 0);
					dcMem.LineTo(40, m_i32Height);
					dcMem.MoveTo(41, 0);
					dcMem.LineTo(41, m_i32Height);
					pOldPen = dcMem.SelectObject(penRespLine);
					dcMem.MoveTo(40, m_i32LastY);
					dcMem.LineTo(40, y);
					//恢复先前的画笔
					dcMem.SelectObject(pOldPen);
				}
				m_i32LastX = 0;
				m_i32LastY = y;
				m_u16TempX = 1;
			}


			a_i32WaveDateR = (a_i32WaveDateR + 1) % (PLETH_WAVE_BUFF_LENGTH);
		}
		m_i32WaveDateR = a_i32WaveDateR;
		m_CSWaveData.Lock();
		m_i32WaveDateLength -= a_i32WaveDateLength;
		m_CSWaveData.Unlock();
	}
	pDC.BitBlt(m_rectCtrl.left, m_rectCtrl.top, m_i32Width, m_i32Height, &dcMem, 0, 0, SRCCOPY);
}


//设置 波形显示的模式 ，即波形显示区域的大小
void CPlethWave::Set_Style(INT style)
{
	m_i32Style = style;

	if (m_i32Style == 0)
	{
		m_i32Width = CPlethWave::m_i32NormalWidth;
		m_i32Height = CPlethWave::m_i32NormalHeight;
	}
	else if (m_i32Style == 1)
	{
		m_i32Width = CPlethWave::m_i32BigWidth;
		m_i32Height = CPlethWave::m_i32BigHeight;
	}
	else if (m_i32Style == 2)
	{
		m_i32Width = CPlethWave::m_i32ECGWidth;
		m_i32Height = CPlethWave::m_i32ECGHeight;
	}
	else
	{
		m_i32Width = CPlethWave::m_i32TrendWidth;
		m_i32Height = CPlethWave::m_i32TrendHeight;
	}
	m_u16TempX = 0;
	m_i32LastX = 0;
	m_i32EraseLast = 0;
	dcMem.FillSolidRect(0, 0, m_i32Width, m_i32Height, RGB(0, 0, 0));
	Reset_WaveDataBuff();
	Erase();
	Invalidate();
}

//设置 是否 冻结 波形
void CPlethWave::Set_Freeze(INT f)
{
	m_i32Freeze = f;
}