// DTMsrDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DTMsrDemo.h"
#include "DTMsrDemoDlg.h"
#include "../../../SDK/Include/DTMsrAPI.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#pragma comment(lib,"../../../SDK/lib/DTMsrAPI.lib")

CDTMsrDemoDlg *g_pThisWnd=NULL;


void ReceiveMsrData(TRACKDATA *pTrackData, DWORD dwTrackCount);
void ReceiveMsrData(TRACKDATA *pTrackData, DWORD dwTrackCount)
{
	CString str;
	static TCHAR wzDataBuf[1024];
	if (g_pThisWnd)
	{
		for (int i=0;i<dwTrackCount;i++)
		{
			if (pTrackData[i].startSentinel==0 && pTrackData[i].endSentinel==0)
			{
				str.Format(_T("%s"),pTrackData[i].lpszDataBuf);
			}
			else
			{
				str.Format(_T("[SS:%X]%s[ES:%X]"),pTrackData[i].startSentinel,pTrackData[i].lpszDataBuf,pTrackData[i].endSentinel);
			}
			g_pThisWnd->AddOutputInfo(str);
		}

	}
}

// CDTMsrDemoDlg dialog

CDTMsrDemoDlg::CDTMsrDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDTMsrDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDTMsrDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_MSR_PORT, m_port);
	DDX_Control(pDX, IDC_COMBO_BAUD, m_baud);
	DDX_Control(pDX, IDC_COMBO_DATABITS, m_databits);
	DDX_Control(pDX, IDC_COMBO_PARITY, m_parity);
	DDX_Control(pDX, IDC_COMBO_STOPBITS, m_stopbits);
	DDX_Control(pDX, IDC_EDIT_MSR_DATA, m_msrData);
	DDX_Control(pDX, IDC_LIST_FORMAT, m_listFormat);
	DDX_Control(pDX, IDC_COMBO_SS, m_ss);
	DDX_Control(pDX, IDC_COMBO_ES, m_es);
	DDX_Control(pDX, IDC_CHECK_PARSE, m_parseEnabel);
}

BEGIN_MESSAGE_MAP(CDTMsrDemoDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT_MSR, OnBnClickedButtonConnectMsr)
	ON_BN_CLICKED(IDC_BUTTON_READ, OnBnClickedButtonRead)
	ON_BN_CLICKED(IDC_BUTTON_WRITE, OnBnClickedButtonWrite)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnBnClickedButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnBnClickedButtonDel)
	ON_BN_CLICKED(IDC_BUTTON_WRITE_PARSE_FORMAT, OnBnClickedButtonWriteParseFormat)
END_MESSAGE_MAP()



// CDTMsrDemoDlg message handlers

BOOL CDTMsrDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	m_port.SetCurSel(2);

	UINT baud[5] = {2400,4800,9600,14400,19200};
	int i;

	CString str;
	for(i=0;i<5;i++)
	{
		str.Format(_T("%d"),baud[i]);
		m_baud.InsertString(i,str);
	}
	m_databits.InsertString(0,_T("7"));
	m_databits.InsertString(1,_T("8"));

	m_stopbits.InsertString(0,_T("1"));


	CString szParity[3]={_T("None"),_T("Odd"),_T("Even")};
	for (i=0;i<3;i++)
	{
		m_parity.InsertString(i,szParity[i]);
	}

	int j=0;
	for (i=0;i<64;i++)
	{
		m_szTrackFormat[i][0]=0;
		m_szTrackFormat[i][1]=0;
	}

	j=0;
	for (i =0;i<15;i++)
	{
		m_szTrackFormat[j++][0] = 33+i;

	}
	for (i =0;i<7;i++)
	{
		m_szTrackFormat[j++][0] = 58+i;

	}
	for (i =0;i<6;i++)
	{
		m_szTrackFormat[j++][0] = 91+i;

	}
	for (i =0;i<4;i++)
	{
		m_szTrackFormat[j++][0] = 123+i;

	}
	m_nTrackFormat = j;
	m_szTrackFormat[j++][0]=0;


	for (i = 0;i<m_nTrackFormat;i++)
	{
		m_ss.InsertString(i,(TCHAR*)m_szTrackFormat[i]);
		m_es.InsertString(i,(TCHAR*)m_szTrackFormat[i]);

	}
	m_ss.SetCurSel(16);
	m_es.SetCurSel(20);

	Msr_SetDataReceiverCallback(&ReceiveMsrData);
	EnableDlgItems(FALSE);
	//Msr_SetDataReceiverCallback(&(CDTMsrDemoDlg::TestRev));
	g_pThisWnd = this;

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CDTMsrDemoDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_DTMSRDEMO_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_DTMSRDEMO_DIALOG));
	}
}
#endif

void CDTMsrDemoDlg::EnableDlgItems(BOOL bEnable)
{

	m_port.EnableWindow(!bEnable);
	m_baud.EnableWindow(bEnable);
	m_databits.EnableWindow(bEnable);
	m_parity.EnableWindow(bEnable);
	m_stopbits.EnableWindow(bEnable);
	CWnd* pWnd = GetDlgItem(IDC_BUTTON_READ);
	pWnd->EnableWindow(bEnable);
	pWnd = GetDlgItem(IDC_BUTTON_WRITE);
	pWnd->EnableWindow(bEnable);

}

// CDTMsrDemoDlg message handlers
void CDTMsrDemoDlg::ShowErrMsg(int error)
{
	CString str;
	switch(error)
	{
	case 0:
		break;
	case 1:
		str = _T("Interface is invalid!");
		break;
	case 2:
		str = _T("No device found!");
		break;
	case 3:
		str = _T("Time out");
		break;
	case 4:
		str = _T("Not connected");
		break;
	case 5:
		str = _T("Dll inner error");
		break;
	case 6:
		str = _T("Not support");
		break;
	case 7:
		str = _T("Connection lost");
		break;
	default:
		str = _T("UnKnown");
		break;
	}
	if (str.IsEmpty()==FALSE)
	{
		AfxMessageBox(str);
	}
}

void CDTMsrDemoDlg::OnBnClickedButtonConnectMsr()
{
	CString str;
	unsigned short result = 0;
	GetDlgItemText(IDC_BUTTON_CONNECT_MSR,str);
	if (str.Compare(_T("Connect"))==0)
	{
		int index = m_port.GetCurSel();
		if (index>=0)
		{
			index+=1;
		}

		result= Msr_Connect(index);
		if (result==0)
		{
			SetDlgItemText(IDC_BUTTON_CONNECT_MSR,_T("Disconnect"));
			TCHAR name[256]={0},version[256]={0};
			Msr_GetFirmwareInfo(name,256,version,256);

			SetDlgItemText(IDC_EDIT_PRODUCTNAME,name);
			SetDlgItemText(IDC_EDIT_VERSION,version);

			UpdataPortParameters();
			EnableDlgItems(TRUE);

		}
		else
		{
			ShowErrMsg(result);
		}

	}
	else
	{
		result = Msr_Disconnect();
		if (result==0)
		{
			SetDlgItemText(IDC_BUTTON_CONNECT_MSR,_T("Connect"));
			SetDlgItemText(IDC_EDIT_PRODUCTNAME,_T(""));
			SetDlgItemText(IDC_EDIT_VERSION,_T(""));
			EnableDlgItems(FALSE);
		}
		ShowErrMsg(result);


	}

}
void CDTMsrDemoDlg::UpdataPortParameters()
{
	int baud,parity,databits,stopbits;
	int i=0;
	int count;
	CString str,strParam;
	USHORT result = Msr_GetPortParameters(&baud,&databits,&parity,&stopbits);
	//TRACE("Port Parameters:%d,%d,%d,%d",baud,databits,parity,stopbits);
	if (result==0)
	{

		count = m_baud.GetCount();

		strParam.Format(_T("%d"),baud);
		for (i=0;i<count;i++)
		{
			m_baud.GetLBText(i,str);
			if (str.Compare(strParam)==0)
			{
				m_baud.SetCurSel(i);
				break;
			}
		}

		//databits;
		count = m_databits.GetCount();
		strParam.Format(_T("%d"),databits);
		for (i=0;i<count;i++)
		{
			m_databits.GetLBText(i,str);
			if (str.Compare(strParam)==0)
			{
				m_databits.SetCurSel(i);
				break;
			}
		}


		//parity

		if (parity<3)
		{
			m_parity.SetCurSel(parity);
		}

		if (stopbits>=0&&stopbits<1)
		{
			m_stopbits.SetCurSel(stopbits);
		}

	}


}


void CDTMsrDemoDlg::TestRev(void *pBuf,long len)
{

}

void CDTMsrDemoDlg::AddOutputInfo(CString info)
{

	if (info.IsEmpty())
	{
		return;
	}

	CString text;
	m_msrData.GetWindowText( text );

	text = text + info+_T("\r\n") ;

	if( m_msrData.GetLineCount() >=50 )
	{
		int pos = text.Find( _T("\r\n") , 1 );
		text = text.Mid( pos+1 );
	}
	m_msrData.SetWindowText( text );

	m_msrData.LineScroll(m_msrData.GetLineCount());	
}
void CDTMsrDemoDlg::OnBnClickedButtonRead()
{
	UpdataPortParameters();
}

void CDTMsrDemoDlg::OnBnClickedButtonWrite()
{
	int baud,databits,parity,stopbits;
	CString str;

	int index = 0;
	index = m_baud.GetCurSel();
	if (index<0)
	{
		AfxMessageBox(_T("请选择一个波特率！"));
		return;
	}

	m_baud.GetLBText(index,str);
	baud = _ttoi(str.GetBuffer());

	index = m_databits.GetCurSel();
	if (index<0)
	{
		AfxMessageBox(_T("请选择一个数据位！"));
		return;
	}
	m_databits.GetLBText(index,str);
	databits  =_ttoi(str.GetBuffer());

	parity  =m_parity.GetCurSel();
	if (parity<0)
	{
		AfxMessageBox(_T("请选择一个校验位！"));
		return;
	}

	stopbits  =m_stopbits.GetCurSel();
	if (stopbits<0)
	{
		AfxMessageBox(_T("请选择一个停止位！"));
		return;
	}

	//TRACE("In CDTMsrDemoDlg Write: %d,%d,%d,%d",baud,databits,parity,stopbits);

	USHORT result = Msr_SetPortParameters(baud,databits,parity,stopbits);
	ShowErrMsg(result);

}

void CDTMsrDemoDlg::OnBnClickedButtonAdd()
{
	CString str;
	str.Format(_T("%s%s"),m_szTrackFormat[m_ss.GetCurSel()],m_szTrackFormat[m_es.GetCurSel()]);
	m_listFormat.AddString(str);
}

void CDTMsrDemoDlg::OnBnClickedButtonDel()
{
	int cur=m_listFormat.GetCurSel();
	if (cur>=0)
	{
		m_listFormat.DeleteString(cur);
	}
}

void CDTMsrDemoDlg::OnBnClickedButtonWriteParseFormat()
{
	int count=m_listFormat.GetCount();
	BOOL bParse = FALSE;
	if (m_parseEnabel.GetCheck()==BST_CHECKED)
	{
		bParse = TRUE;
	}
	PARSEDATAFORMAT *pFormatData = NULL;
	TCHAR szFormat[10];
	if (count>0)
	{
		pFormatData = new PARSEDATAFORMAT[count];
		int n=0;
		for(n=0;n<count;n++)
		{
			m_listFormat.GetText(n, szFormat);
			//TRACE("In UI,Parse Format,Index=%d,SS=%c,ES=%c",n,szFormat[0],szFormat[1]);
			pFormatData[n].startSentinel=szFormat[0];
			pFormatData[n].endSentinel=szFormat[1];
		}
	}

	Msr_ParseEnable(bParse,pFormatData,count);

	if (pFormatData)
	{
		delete[] pFormatData;
	}

}


void CDTMsrDemoDlg::OnDestroy()
{
	Msr_Disconnect();
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
}

