// CompDlg.cpp : implementation file
//

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

//---------------------------------------------------------
// Counter header file
//---------------------------------------------------------
#include "ccnt.h"
//---------------------------------------------------------
// Global parameter
//---------------------------------------------------------
extern	short	ChannelNum;			// All channel number
extern	HWND	g_hWnd;				// Main dialog window handle
extern	short	Id;					// Device ID
extern	long	Ret;				// Return code
extern	char	szError[256];		// Error string
extern	TCHAR	szReturnCode[256];	// Error string express
extern	TCHAR	szText[100];		// Temporary string

static	DWORD	CompValue[8] = {	// Comparison value
	100, 200, 300, 400, 500, 600, 700, 800
};
static	short	PulseWidth = 0;		// Pulse width

// CCompDlg dialog

IMPLEMENT_DYNAMIC(CCompDlg, CDialogEx)

CCompDlg::CCompDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CCompDlg::IDD, pParent)
{

}

CCompDlg::~CCompDlg()
{
}

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


BEGIN_MESSAGE_MAP(CCompDlg, CDialogEx)
	ON_BN_CLICKED(IDC_RADIO_COMP0, &CCompDlg::OnBnClickedRadioComp0)
	ON_BN_CLICKED(IDC_RADIO_COMP1, &CCompDlg::OnBnClickedRadioComp1)
END_MESSAGE_MAP()


// CCompDlg message handlers

//---------------------------------------------------------
// Dialog initialization
//---------------------------------------------------------
BOOL CCompDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//-----------------------------------
	// Screen item initialization
	//-----------------------------------
	short	i;
	short	ChNo;
	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++) {
		wsprintf(szText, _T("%lu"), CompValue[i]);
		m_Comp[i]->SetWindowText(szText);
		if (i >= ChannelNum) {
			m_Comp[i]->EnableWindow(FALSE);
		}
	}
	for (i = 0; i <= 0x0f; i++) {
		wsprintf(szText, _T("%X"), i);
		m_Pulse.AddString(szText);
	}
	//-----------------------------------
	// Get setting value
	//-----------------------------------
	ChNo = 0;
	CntGetPulseWidth(Id, ChNo, &PulseWidth);
	wsprintf(szText, _T("%X"), PulseWidth);
	m_Pulse.SelectString(-1, szText);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

//---------------------------------------------------------
// OK button
//---------------------------------------------------------
void CCompDlg::OnOK()
{
	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) {
			_stscanf_s(szText, _T("%lu"), &CompValue[i]);
		}
		else {
			_stscanf_s(szText, _T("%X"), &CompValue[i]);
		}
	}
	m_Pulse.GetWindowText(szText, sizeof(szText));
	_stscanf_s(szText, _T("%hX"), &PulseWidth);
	//-----------------------------------
	// Each channel
	//-----------------------------------
	for (i = 0; i<ChannelNum; i++) {
		//-----------------------------------
		// Set comparison value
		//-----------------------------------
		Ret = CntNotifyCountUp(Id, i, 0, CompValue[i], g_hWnd);
		//-----------------------------
		// Error process
		//-----------------------------
		if (Ret != CNT_ERR_SUCCESS) {
			CntGetErrorString(Ret, szError);
			wsprintf(szReturnCode, _T("CntNotifyCountUp : Ret = %d : %S"), Ret, szError);
			MessageBox(szReturnCode, _T("Error"), MB_OK);
			break;
		}
		//-----------------------------------
		// Set pulse width
		//-----------------------------------
		Ret = CntSetPulseWidth(Id, i, PulseWidth);
		//-----------------------------
		// Error process
		//-----------------------------
		if (Ret != CNT_ERR_SUCCESS) {
			CntGetErrorString(Ret, szError);
			wsprintf(szReturnCode, _T("CntSetPulseWidth : Ret = %d : %S"), Ret, szError);
			MessageBox(szReturnCode, _T("Error"), MB_OK);
			break;
		}
	}

	CDialogEx::OnOK();
}

//---------------------------------------------------------
// Cancel button
//---------------------------------------------------------
void CCompDlg::OnCancel()
{
	CDialogEx::OnCancel();
}

//---------------------------------------------------------
// Comparison value expression change
//---------------------------------------------------------
// Dec
void CCompDlg::OnBnClickedRadioComp0()
{
	//-----------------------------------
	// Screen infomation renew
	//-----------------------------------
	short	i;
	for (i = 0; i<8; i++) {
		wsprintf(szText, _T("%lu"), CompValue[i]);
		m_Comp[i]->SetWindowText(szText);
	}
}

// Hex
void CCompDlg::OnBnClickedRadioComp1()
{
	//-----------------------------------
	// Screen infomation renew
	//-----------------------------------
	short	i;
	for (i = 0; i<8; i++) {
		wsprintf(szText, _T("%08X"), CompValue[i]);
		m_Comp[i]->SetWindowText(szText);
	}
}
