// CompDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Counter.h"
#include "CompDlg.h"

//---------------------------------------------------------
// Counter header file
//---------------------------------------------------------
#include "ccapcnt.h"
//---------------------------------------------------------
// Global variable
//---------------------------------------------------------
extern	short	ChannelNum;			// Total number of channels
extern	short	Id;					// Device ID
extern	long	Ret;				// Return value
extern	char	szError[256];		// Error string
extern	TCHAR	szReturnCode[256];	// The string for error
extern	TCHAR	szText[100];		// Temporary string

static	long	CompValue[8];		// Comparison value
static	long	PulseWidth = 0;		// Pulse width


// CCompDlg dialog

IMPLEMENT_DYNAMIC(CCompDlg, CDialog)
CCompDlg::CCompDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompDlg::IDD, pParent)
{
}

CCompDlg::~CCompDlg()
{
}

void CCompDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_COMP0, m_Comp0);
	DDX_Control(pDX, IDC_EDIT_COMP1, m_Comp1);
	DDX_Control(pDX, IDC_EDIT_COMP2, m_Comp2);
	DDX_Control(pDX, IDC_EDIT_COMP3, m_Comp3);
	DDX_Control(pDX, IDC_EDIT_COMP4, m_Comp4);
	DDX_Control(pDX, IDC_EDIT_COMP5, m_Comp5);
	DDX_Control(pDX, IDC_EDIT_COMP6, m_Comp6);
	DDX_Control(pDX, IDC_EDIT_COMP7, m_Comp7);
	DDX_Control(pDX, IDC_EDIT_PULSE, m_Pulse);
	DDX_Control(pDX, IDC_RADIO_COMP0, m_RadioComp);
}


BEGIN_MESSAGE_MAP(CCompDlg, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_BN_CLICKED(IDC_RADIO_COMP0, OnBnClickedRadioComp0)
	ON_BN_CLICKED(IDC_RADIO_COMP1, OnBnClickedRadioComp1)
END_MESSAGE_MAP()


// CCompDlg message handlers

//---------------------------------------------------------
//	Initialize dialog
//---------------------------------------------------------
BOOL CCompDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//-----------------------------------
	// Initialize screen items
	//-----------------------------------
	short	i;
	m_RadioComp.SetCheck(1);
	m_Comp[0] = &m_Comp0;
	m_Comp[1] = &m_Comp1;
	m_Comp[2] = &m_Comp2;
	m_Comp[3] = &m_Comp3;
	m_Comp[4] = &m_Comp4;
	m_Comp[5] = &m_Comp5;
	m_Comp[6] = &m_Comp6;
	m_Comp[7] = &m_Comp7;
	for (i=0; i<8; i++) {
		if (i >= ChannelNum) {
			m_Comp[i]->EnableWindow(FALSE);
		}
	}
	//-----------------------------------
	// Get and display the count-match pulse
	//-----------------------------------
	long	PlsWidth;
	Ret = CntGetPulseWidth(Id, 0, &PlsWidth);
	if (Ret != CNT_ERR_SUCCESS) {
		PlsWidth = 0;
	}
	wsprintf(szText, _T("%lX"), PlsWidth);
	m_Pulse.SetWindowText(szText);
	//-----------------------------------
	// Get and display count comparison value
	//-----------------------------------
	for (i=0; i<ChannelNum; i++) {
		CompValue[i] = 0;
		Ret = CntGetCountMatchReg(Id, i, 0, &CompValue[i]);
		wsprintf(szText, _T("%ld"), CompValue[i]);
		m_Comp[i]->SetWindowText(szText);
	}

	return TRUE;
}

//---------------------------------------------------------
//	Process OK button
//---------------------------------------------------------
void CCompDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	short	i;
	//-----------------------------------
	// Get setting value from screen
	//-----------------------------------
	for (i=0; i<8; i++) {
		m_Comp[i]->GetWindowText(szText, sizeof(szText));
		if (m_RadioComp.GetCheck() == 1) {
			swscanf_s(szText, _T("%ld"), &CompValue[i]);
		} else {
			swscanf_s(szText, _T("%lX"), &CompValue[i]);
		}
	}
	m_Pulse.GetWindowText(szText, sizeof(szText));
	swscanf_s(szText, _T("%lX"), &PulseWidth);
	//-----------------------------------
	// Each channel
	//-----------------------------------
	for (i=0; i<ChannelNum; i++) {
		//-----------------------------------
		// Set comparison value
		//-----------------------------------
		Ret = CntSetCountMatchEvent(Id, i, 0, CompValue[i], 1);
		//-----------------------------
		// Process error
		//-----------------------------
		if (Ret != CNT_ERR_SUCCESS) {
			CntGetErrorString(Ret, szError);
			wsprintf(szReturnCode, _T("CntSetCountMatchEvent : %S"), szError);
			MessageBox(szReturnCode, _T("Error"), MB_OK);
			break;
		}
		//-----------------------------------
		// Set pulse width
		//-----------------------------------
		Ret = CntSetPulseWidth(Id, i, PulseWidth);
		//-----------------------------
		// Process error
		//-----------------------------
		if (Ret != CNT_ERR_SUCCESS) {
			CntGetErrorString(Ret, szError);
			wsprintf(szReturnCode, _T("CntSetPulseWidth : %S"), szError);
			MessageBox(szReturnCode, _T("Error"), MB_OK);
			break;
		}
	}
	//-----------------------------------
	// Set pulse width
	//-----------------------------------
	OnOK();
}

//---------------------------------------------------------
//	Process Cancel button
//---------------------------------------------------------
void CCompDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

//---------------------------------------------------------
//	Change the display of the comparison value
//---------------------------------------------------------
//	Dec
void CCompDlg::OnBnClickedRadioComp0()
{
	// TODO: Add your control notification handler code here
	//-----------------------------------
	// Refresh screen information
	//-----------------------------------
	short	i;
	for (i=0; i<8; i++) {
		wsprintf(szText, _T("%d"), CompValue[i]);
		m_Comp[i]->SetWindowText(szText);
	}
}

//	Hex
void CCompDlg::OnBnClickedRadioComp1()
{
	// TODO: Add your control notification handler code here
	//-----------------------------------
	// Refresh screen information
	//-----------------------------------
	short	i;
	for (i=0; i<8; i++) {
		wsprintf(szText, _T("%08X"), CompValue[i]);
		m_Comp[i]->SetWindowText(szText);
	}
}
