// CounterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Counter.h"
#include "CounterDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// Counter header file
//---------------------------------------------------------
#include "ccapcnt.h"

//---------------------------------------------------------
// Global variable
//---------------------------------------------------------
short	ChannelNum;			// Total number of channels
short	Id;					// Device ID
long	Ret;				// Return value
char	szError[256];		// Error string
TCHAR	szReturnCode[256];	// The string for error
TCHAR	szText[100];		// Temporary string

//---------------------------------------------------------
// Dialog header file
//---------------------------------------------------------
#include "ModeDlg.h"
#include "CompDlg.h"


// CCounterDlg dialog



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

void CCounterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_DEVICE_NAME, m_DeviceName);
	DDX_Control(pDX, IDC_EDIT_START, m_Start);
	DDX_Control(pDX, IDC_EDIT_ERR, m_Err);
	DDX_Control(pDX, IDC_EDIT_CH0, m_Ch0);
	DDX_Control(pDX, IDC_EDIT_CH1, m_Ch1);
	DDX_Control(pDX, IDC_EDIT_CH2, m_Ch2);
	DDX_Control(pDX, IDC_EDIT_CH3, m_Ch3);
	DDX_Control(pDX, IDC_EDIT_CH4, m_Ch4);
	DDX_Control(pDX, IDC_EDIT_CH5, m_Ch5);
	DDX_Control(pDX, IDC_EDIT_CH6, m_Ch6);
	DDX_Control(pDX, IDC_EDIT_CH7, m_Ch7);
	DDX_Control(pDX, IDC_EDIT_STATUS0, m_Status0);
	DDX_Control(pDX, IDC_EDIT_STATUS1, m_Status1);
	DDX_Control(pDX, IDC_EDIT_STATUS2, m_Status2);
	DDX_Control(pDX, IDC_EDIT_STATUS3, m_Status3);
	DDX_Control(pDX, IDC_EDIT_STATUS4, m_Status4);
	DDX_Control(pDX, IDC_EDIT_STATUS5, m_Status5);
	DDX_Control(pDX, IDC_EDIT_STATUS6, m_Status6);
	DDX_Control(pDX, IDC_EDIT_STATUS7, m_Status7);
	DDX_Control(pDX, IDC_EDIT_PRESET0, m_Preset0);
	DDX_Control(pDX, IDC_EDIT_PRESET1, m_Preset1);
	DDX_Control(pDX, IDC_EDIT_PRESET2, m_Preset2);
	DDX_Control(pDX, IDC_EDIT_PRESET3, m_Preset3);
	DDX_Control(pDX, IDC_EDIT_PRESET4, m_Preset4);
	DDX_Control(pDX, IDC_EDIT_PRESET5, m_Preset5);
	DDX_Control(pDX, IDC_EDIT_PRESET6, m_Preset6);
	DDX_Control(pDX, IDC_EDIT_PRESET7, m_Preset7);
	DDX_Control(pDX, IDC_RADIO_CH0, m_RadioCh);
	DDX_Control(pDX, IDC_RADIO_PRESET0, m_RadioPreset);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_ReturnCode);
}

BEGIN_MESSAGE_MAP(CCounterDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CNT_INIT, OnBnClickedButtonCntInit)
	ON_BN_CLICKED(IDC_BUTTON_CNT_EXIT, OnBnClickedButtonCntExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnBnClickedButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_CNT_MODE, OnBnClickedButtonCntMode)
	ON_BN_CLICKED(IDC_BUTTON_CNT_COMPARE, OnBnClickedButtonCntCompare)
	ON_BN_CLICKED(IDC_BUTTON_CNT_START, OnBnClickedButtonCntStart)
	ON_BN_CLICKED(IDC_BUTTON_CNT_STOP, OnBnClickedButtonCntStop)
	ON_BN_CLICKED(IDC_BUTTON_CNT_PRESET, OnBnClickedButtonCntPreset)
	ON_BN_CLICKED(IDC_RADIO_CH0, OnBnClickedRadioCh0)
	ON_BN_CLICKED(IDC_RADIO_CH1, OnBnClickedRadioCh1)
	ON_BN_CLICKED(IDC_RADIO_PRESET0, OnBnClickedRadioPreset0)
	ON_BN_CLICKED(IDC_RADIO_PRESET1, OnBnClickedRadioPreset1)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CCounterDlg message handlers

//---------------------------------------------------------
//	Initialize dialog
//---------------------------------------------------------
BOOL CCounterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	short	i;
	DWORD	PresetData;
	//-----------------------------------
	// Detect device
	//-----------------------------------
	short	Index;
	short	GroupID, UnitID, DeviceID;
	char	DeviceName[256], Device[256];
	TCHAR	szDeviceName[256];

	for (Index=0; ; Index++) {
		Ret = CntQueryDeviceName(Index, &GroupID, &UnitID, &DeviceID, DeviceName, Device);
		if (Ret != CNT_ERR_SUCCESS) {
			break;
		}
		wsprintf(szDeviceName, _T("%S"), DeviceName);
		m_DeviceName.AddString(szDeviceName);
	}
	m_DeviceName.SetCurSel(0);
	//-----------------------------------
	// Initialize screen items
	//-----------------------------------
	m_ReturnCode.SetWindowText(_T(""));
	m_RadioCh.SetCheck(1);
	m_RadioPreset.SetCheck(1);
	// Count value
	m_Ch[0] = &m_Ch0;
	m_Ch[1] = &m_Ch1;
	m_Ch[2] = &m_Ch2;
	m_Ch[3] = &m_Ch3;
	m_Ch[4] = &m_Ch4;
	m_Ch[5] = &m_Ch5;
	m_Ch[6] = &m_Ch6;
	m_Ch[7] = &m_Ch7;
	for (i=0; i<8; i++) {
		m_Ch[i]->SetWindowText(_T(""));
	}
	// Status
	m_Status[0] = &m_Status0;
	m_Status[1] = &m_Status1;
	m_Status[2] = &m_Status2;
	m_Status[3] = &m_Status3;
	m_Status[4] = &m_Status4;
	m_Status[5] = &m_Status5;
	m_Status[6] = &m_Status6;
	m_Status[7] = &m_Status7;
	for (i=0; i<8; i++) {
		m_Status[i]->SetWindowText(_T(""));
	}
	// Preset value
	m_Preset[0] = &m_Preset0;
	m_Preset[1] = &m_Preset1;
	m_Preset[2] = &m_Preset2;
	m_Preset[3] = &m_Preset3;
	m_Preset[4] = &m_Preset4;
	m_Preset[5] = &m_Preset5;
	m_Preset[6] = &m_Preset6;
	m_Preset[7] = &m_Preset7;
	for (i=0; i<8; i++) {
		PresetData = i * 100;
		wsprintf(szText, _T("%d"), PresetData);
		m_Preset[i]->SetWindowText(szText);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCounterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	CDialog::OnSysCommand(nID, lParam);
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCounterDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCounterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//---------------------------------------------------------
//	Initialization
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntInit()
{
	// TODO: Add your control notification handler code here
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	short	i;
	//-----------------------------
	// Get device name from screen
	//-----------------------------
	m_DeviceName.GetWindowText(szDeviceName, sizeof(DeviceName));
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------
	// Initialize and get ID
	//-----------------------------
	Ret = CntInit(DeviceName, &Id);
	//-----------------------------
	// Process error
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("CntInit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != CNT_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Get total number of channels
	//-----------------------------
	CntGetMaxChannels(Id, &ChannelNum);
	//-----------------------------
	// Disable the display of counter, status and preset
	//-----------------------------
	for (i=0; i<8; i++) {
		if (i < ChannelNum) {
			m_Ch[i]->EnableWindow(TRUE);
			m_Status[i]->EnableWindow(TRUE);
			m_Preset[i]->EnableWindow(TRUE);
		} else {
			m_Ch[i]->EnableWindow(FALSE);
			m_Status[i]->EnableWindow(FALSE);
			m_Preset[i]->EnableWindow(FALSE);
		}
	}
	//-----------------------------
	// Start timer
	//-----------------------------
	nIDEvent = 1;
	nIDEvent = SetTimer(nIDEvent, 100, NULL);
}

//---------------------------------------------------------
//	Termination
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntExit()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Stop timer
	//-----------------------------
	KillTimer(nIDEvent);
	//-----------------------------
	// Termination
	//-----------------------------
	Ret = CntExit(Id);
	//-----------------------------
	// Process error
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("CntExit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
}

//---------------------------------------------------------
//	Exit dialog
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonExit()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Stop timer
	//-----------------------------
	KillTimer(nIDEvent);
	//-----------------------------
	// Termination
	//-----------------------------
	Ret = CntExit(Id);
	//-----------------------------
	// Exit dialog
	//-----------------------------
	CDialog::OnOK();
}

//---------------------------------------------------------
//	Display mode dialog
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntMode()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Display dialog in modal
	//-----------------------------
	CModeDlg	dlg;
	dlg.DoModal();
}

//---------------------------------------------------------
//	Display comparison dialog
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntCompare()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Display dialog in modal
	//-----------------------------
	CCompDlg	dlg;
	dlg.DoModal();
}

//---------------------------------------------------------
//	Counter start
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntStart()
{
	// TODO: Add your control notification handler code here
	short	ChNo[8];
	short	i;
	//-----------------------------
	// Set parameter
	//-----------------------------
	for (i=0; i<ChannelNum; i++) {
		ChNo[i] = i;
	}
	//-----------------------------
	// Counter start
	//-----------------------------
	Ret = CntStartCount(Id, &ChNo[0], ChannelNum);
	//-----------------------------
	// Process error
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("CntStartCount : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != CNT_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
//	Counter stop
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntStop()
{
	// TODO: Add your control notification handler code here
	short	ChNo[8];
	short	i;
	//-----------------------------
	// Set parameter
	//-----------------------------
	for (i=0; i<ChannelNum; i++) {
		ChNo[i] = i;
	}
	//-----------------------------
	// Counter stop
	//-----------------------------
	Ret = CntStopCount(Id, &ChNo[0], ChannelNum);
	//-----------------------------
	// Process error
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("CntStopCount : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
//	Preset
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntPreset()
{
	// TODO: Add your control notification handler code here
	short	ChNo[8];
	long	PresetData[8];
	short	i;
	//-----------------------------
	// Get data from screen and set parameter
	//-----------------------------
	for (i=0; i<ChannelNum; i++) {
		ChNo[i] = i;
		m_Preset[i]->GetWindowText(szText, sizeof(szText));
		if (m_RadioPreset.GetCheck() == 1) {
			swscanf_s(szText, _T("%d"), &PresetData[i]);
		} else {
			swscanf_s(szText, _T("%X"), &PresetData[i]);
		}
	}
	//-----------------------------
	// Preset
	//-----------------------------
	Ret = CntPreset(Id, &ChNo[0], ChannelNum, &PresetData[0]);
	//-----------------------------
	// Process error
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("CntPreset : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
//	Process timer
//---------------------------------------------------------
void CCounterDlg::OnTimer(UINT_PTR nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	short	ChNo[8];
	long	CountData[8];
	short	Sts[8];
	short	i;
	BOOL	bIs10;
	BYTE	Start, Err;
	//-----------------------------
	// Get start register value
	//-----------------------------
	Ret = CntGetRemoteStatus(Id, &Start, &Err);
	//-----------------------------
	// Process error
	//-----------------------------
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntGetRemoteStatus : %S"), szError);
		m_ReturnCode.SetWindowText(szReturnCode);
		CDialog::OnTimer(nIDEvent);
		return;
	}
	//-----------------------------
	// Set parameter
	//-----------------------------
	for (i=0; i<ChannelNum; i++) {
		ChNo[i] = i;
		CountData[i] = 0;
	}
	//-----------------------------
	// Get count value
	//-----------------------------
	Ret = CntReadCount(Id, &ChNo[0], ChannelNum, &CountData[0]);
	//-----------------------------
	// Process error
	//-----------------------------
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntReadCount : %S"), szError);
		m_ReturnCode.SetWindowText(szReturnCode);
		CDialog::OnTimer(nIDEvent);
		return;
	}
	//-----------------------------
	// Get status
	//-----------------------------
	for (i=0; i<ChannelNum; i++) {
		Ret = CntReadStatus(Id, i, &Sts[i]);
		//-----------------------------
		// Process error
		//-----------------------------
		if (Ret != CNT_ERR_SUCCESS) {
			CntGetErrorString(Ret, szError);
			wsprintf(szReturnCode, _T("CntReadStatus : %S"), szError);
			m_ReturnCode.SetWindowText(szReturnCode);
			CDialog::OnTimer(nIDEvent);
			return;
		}
	}
	//-----------------------------
	// Display count data and status data
	//-----------------------------
	wsprintf(szText, _T("%02X"), (int)Start);
	m_Start.SetWindowText(szText);
	wsprintf(szText, _T("%02X"), (int)Err);
	m_Err.SetWindowText(szText);
	bIs10 = m_RadioCh.GetCheck();
	for (i=0; i<ChannelNum; i++) {
		if (bIs10) {
			wsprintf(szText, _T("%d"), CountData[i]);
		} else {
			wsprintf(szText, _T("%08X"), CountData[i]);
		}
		m_Ch[i]->SetWindowText(szText);
		wsprintf(szText, _T("%02X"), Sts[i]);
		m_Status[i]->SetWindowText(szText);
	}
	CDialog::OnTimer(nIDEvent);
}

//---------------------------------------------------------
//	Shift Dec/Hex
//---------------------------------------------------------
//	Channel data : Dec
void CCounterDlg::OnBnClickedRadioCh0()
{
	// TODO: Add your control notification handler code here
	OnTimer(nIDEvent);
}

//	Channel data : Hex
void CCounterDlg::OnBnClickedRadioCh1()
{
	// TODO: Add your control notification handler code here
	OnTimer(nIDEvent);
}

//	Preset data : Dec
void CCounterDlg::OnBnClickedRadioPreset0()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Get data from screen and change the display
	//-----------------------------
	short	i;
	DWORD	PresetData;
	for (i=0; i<8; i++) {
		m_Preset[i]->GetWindowText(szText, sizeof(szText));
		swscanf_s(szText, _T("%X"), &PresetData);
		wsprintf(szText, _T("%d"), PresetData);
		m_Preset[i]->SetWindowText(szText);
	}
}

//	Preset data : Hex
void CCounterDlg::OnBnClickedRadioPreset1()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Get data from screen and change the display
	//-----------------------------
	short	i;
	DWORD	PresetData;
	for (i=0; i<8; i++) {
		m_Preset[i]->GetWindowText(szText, sizeof(szText));
		swscanf_s(szText, _T("%d"), &PresetData);
		wsprintf(szText, _T("%08X"), PresetData);
		m_Preset[i]->SetWindowText(szText);
	}
}
