// NotifyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CounterCallback.h"
#include "CounterDlg.h"
#include "NotifyDlg.h"
#include "afxdialogex.h"

//---------------------------------------------------------
// Counter header
//---------------------------------------------------------
#include "CCnt.h"
//---------------------------------------------------------
// External variable
//---------------------------------------------------------
extern	short	ChannelNum;			// Total number of channels
//---------------------------------------------------------
// Data initialization
//---------------------------------------------------------
static	BOOL	bNotifyError = TRUE;
static	BOOL	bNotifyCarry = TRUE;
static	DWORD	CompData[CHANNEL_NUM][2] = {
	{ 100, 150 },
	{ 200, 250 },
	{ 300, 350 },
	{ 400, 450 },
};

// CNotifyDlg dialog

IMPLEMENT_DYNAMIC(CNotifyDlg, CDialogEx)

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

}

CNotifyDlg::~CNotifyDlg()
{
}

void CNotifyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO_ERROR0, m_Error);
	DDX_Control(pDX, IDC_RADIO_CARRY0, m_Carry);
	DDX_Control(pDX, IDC_EDIT_CH3_REG1, m_Ch3Reg1);
	DDX_Control(pDX, IDC_EDIT_CH3_REG0, m_Ch3Reg0);
	DDX_Control(pDX, IDC_EDIT_CH2_REG1, m_Ch2Reg1);
	DDX_Control(pDX, IDC_EDIT_CH2_REG0, m_Ch2Reg0);
	DDX_Control(pDX, IDC_EDIT_CH1_REG1, m_Ch1Reg1);
	DDX_Control(pDX, IDC_EDIT_CH1_REG0, m_Ch1Reg0);
	DDX_Control(pDX, IDC_EDIT_CH0_REG1, m_Ch0Reg1);
	DDX_Control(pDX, IDC_EDIT_CH0_REG0, m_Ch0Reg0);
}


BEGIN_MESSAGE_MAP(CNotifyDlg, CDialogEx)
END_MESSAGE_MAP()


// CNotifyDlg message handlers

//===================================================
// Dialog initialization
//===================================================
BOOL CNotifyDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	TCHAR	szBuf[100];
	short	i;

	//-----------------------------------
	// Control array settings
	//-----------------------------------
	//------------------
	// Compare register 0
	//------------------
	m_Reg0[0]	= &m_Ch0Reg0;
	m_Reg0[1]	= &m_Ch1Reg0;
	m_Reg0[2]	= &m_Ch2Reg0;
	m_Reg0[3]	= &m_Ch3Reg0;
	//------------------
	// Compare register 1
	//------------------
	m_Reg1[0]	= &m_Ch0Reg1;
	m_Reg1[1]	= &m_Ch1Reg1;
	m_Reg1[2]	= &m_Ch2Reg1;
	m_Reg1[3]	= &m_Ch3Reg1;
	//-------------------------------
	// Set initial data
	//-------------------------------
	for(i = 0; i < CHANNEL_NUM; i++){
		wsprintf(szBuf, _T("%lu"), CompData[i][0]);
		m_Reg0[i]->SetWindowText(szBuf);
		wsprintf(szBuf, _T("%lu"), CompData[i][1]);
		m_Reg1[i]->SetWindowText(szBuf);
		//------------------
		// Disables the display when the number of loops exceeds the total number of channels.
		//------------------
		if(i >= ChannelNum){
			m_Reg0[i]->EnableWindow(FALSE);
			m_Reg1[i]->EnableWindow(FALSE);
		//------------------
		// Enables the display when the number of loops is less than or equal to the total number of channels.
		//------------------
		}else{
			m_Reg0[i]->EnableWindow(TRUE);
			m_Reg1[i]->EnableWindow(TRUE);
		}
	}

	if (bNotifyError == TRUE) {
		m_Error.SetCheck(TRUE);
	}
	else {
		SendDlgItemMessage(IDC_RADIO_ERROR1, BM_SETCHECK, 1, 0);
	}
	if (bNotifyCarry == TRUE) {
		m_Carry.SetCheck(TRUE);
	}
	else {
		SendDlgItemMessage(IDC_RADIO_CARRY1, BM_SETCHECK, 1, 0);
	}

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

//---------------------------------------------
// OK button processing
//---------------------------------------------
void CNotifyDlg::OnOK()
{
	TCHAR		szBuf[100];
	long		ret;
	short		ChNo, RegNo;
	CString		str;
	char		error_string[256];
	TCHAR		szError_string[256];
	//-------------------------------
	// Get setting information
	//-------------------------------
	m_Ch0Reg0.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[0][0]);
	m_Ch0Reg1.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[0][1]);
	m_Ch1Reg0.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[1][0]);
	m_Ch1Reg1.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[1][1]);
	m_Ch2Reg0.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[2][0]);
	m_Ch2Reg1.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[2][1]);
	m_Ch3Reg0.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[3][0]);
	m_Ch3Reg1.GetWindowText(szBuf, sizeof(szBuf));	_stscanf_s(szBuf, _T("%lu"), &CompData[3][1]);
	bNotifyError = m_Error.GetCheck();
	bNotifyCarry = m_Carry.GetCheck();

	//-------------------------------
	// Set setting information to device
	//-------------------------------
	for (ChNo = 0; ChNo<ChannelNum; ChNo++) {
		for (RegNo = 0; RegNo<2; RegNo++) {
			//---------------------------------------------
			// Set count match notification & compare register
			//---------------------------------------------
			ret = CntNotifyCountUp(Id, ChNo, RegNo, CompData[ChNo][RegNo], hMainWnd);
			if (ret != 0) {
				CntGetErrorString(ret, error_string);
				wsprintf(szError_string, _T("%S"), error_string);
				str.Format(_T("CntNotifyCountUp : ret = %d \n %s"), ret, szError_string);
				MessageBox(str, _T("Error"), MB_OK);
				return;
			}
		}
	}
	if (bNotifyError == TRUE) {
		//---------------------------------------------
		// Set counter error notification
		//---------------------------------------------
		ret = CntNotifyCounterError(Id, hMainWnd);
		if (ret != 0) {
			CntGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			str.Format(_T("CntNotifyCounterError : ret = %d \n %s"), ret, szError_string);
			MessageBox(str, _T("Error"), MB_OK);
			return;
		}
	}
	if (bNotifyCarry == TRUE) {
		//---------------------------------------------
		// Set carry/borrow notification
		//---------------------------------------------
		ret = CntNotifyCarryBorrow(Id, hMainWnd);
		if (ret != 0) {
			CntGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			str.Format(_T("CntNotifyCarryBorrow : ret = %d \n %s"), ret, szError_string);
			MessageBox(str, _T("Error"), MB_OK);
		}
	}

	CDialogEx::OnOK();
}

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