// Usual Counter Sample
// CounterDlg.cpp : implementation file
//

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

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// Counter hander file
//---------------------------------------------------------
#include "ccnt.h"

//---------------------------------------------------------
// Global parameter
//---------------------------------------------------------
short	ChannelNum;			// all channel number
HWND	g_hWnd;				// main dialog window handle
short	Id;					// device ID
long	Ret;				// return code
char	szError[256];		// error string
TCHAR	szReturnCode[256];	// error string express
TCHAR	szText[100];		// temporary string
short	NumDispFlag;		// 0=Dec; 16=Hex

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

// CCounterDlg dialog



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

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

BEGIN_MESSAGE_MAP(CCounterDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_MESSAGE(CNTM_COUNTUP_CH0, Ch0Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH1, Ch1Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH2, Ch2Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH3, Ch3Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH4, Ch4Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH5, Ch5Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH6, Ch6Handler)
	ON_MESSAGE(CNTM_COUNTUP_CH7, Ch7Handler)
	ON_BN_CLICKED(IDC_BUTTON_CNT_INIT, &CCounterDlg::OnBnClickedButtonCntInit)
	ON_BN_CLICKED(IDC_BUTTON_CNT_MODE, &CCounterDlg::OnBnClickedButtonCntMode)
	ON_BN_CLICKED(IDC_BUTTON_CNT_COMPARE, &CCounterDlg::OnBnClickedButtonCntCompare)
	ON_BN_CLICKED(IDC_BUTTON_CNT_START, &CCounterDlg::OnBnClickedButtonCntStart)
	ON_BN_CLICKED(IDC_BUTTON_CNT_PRESET, &CCounterDlg::OnBnClickedButtonCntPreset)
	ON_BN_CLICKED(IDC_BUTTON_CNT_STOP, &CCounterDlg::OnBnClickedButtonCntStop)
	ON_BN_CLICKED(IDC_BUTTON_CNT_EXIT, &CCounterDlg::OnBnClickedButtonCntExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CCounterDlg::OnBnClickedButtonExit)
	ON_BN_CLICKED(IDC_RADIO_CH0, &CCounterDlg::OnBnClickedRadioCh0)
	ON_BN_CLICKED(IDC_RADIO_CH1, &CCounterDlg::OnBnClickedRadioCh1)
	ON_BN_CLICKED(IDC_RADIO_PRESET0, &CCounterDlg::OnBnClickedRadioPreset0)
	ON_BN_CLICKED(IDC_RADIO_PRESET1, &CCounterDlg::OnBnClickedRadioPreset1)
END_MESSAGE_MAP()


// CCounterDlg message handlers

//---------------------------------------------------------
// Dialog initialization
//---------------------------------------------------------
BOOL CCounterDlg::OnInitDialog()
{
	CDialogEx::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

	short	i;
	DWORD	PresetData;
	//-----------------------------------
	// Global parameter initialization
	//-----------------------------------
	g_hWnd = m_hWnd;				// main dialog window handle
	//-----------------------------------
	// Screen item initialization
	//-----------------------------------
	m_DeviceName.SetWindowText(_T("CNT000"));
	m_ReturnCode.SetWindowText(_T(""));
	m_CompResult.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("%lu"), PresetData);
		m_Preset[i]->SetWindowText(szText);
	}

	NumDispFlag = 0;

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

// 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
	{
		CDialogEx::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);
}


//---------------------------------------------------------
// Timer process
//---------------------------------------------------------
void CCounterDlg::OnTimer(UINT_PTR nIDEvent)
{
	short	ChNo[8];
	DWORD	CountData[8];
	short	Sts[8];
	short	i;
	BOOL	bIs10;
	//-----------------------------
	// 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]);
	//-----------------------------
	// Error process
	//-----------------------------
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntReadCount Ret = %d : %S"), Ret, szError);
		m_ReturnCode.SetWindowText(szReturnCode);
		CDialog::OnTimer(nIDEvent);
		return;
	}
	//-----------------------------
	// Get status
	//-----------------------------
	for (i = 0; i<ChannelNum; i++) {
		Ret = CntReadStatus(Id, i, &Sts[i]);
		//-----------------------------
		// Error process
		//-----------------------------
		if (Ret != CNT_ERR_SUCCESS) {
			CntGetErrorString(Ret, szError);
			wsprintf(szReturnCode, _T("CntReadStatus Ret = %d : %S"), Ret, szError);
			m_ReturnCode.SetWindowText(szReturnCode);
			CDialog::OnTimer(nIDEvent);
			return;
		}
	}
	//-----------------------------
	// Count data, status data express
	//-----------------------------
	bIs10 = m_RadioCh.GetCheck();
	for (i = 0; i<ChannelNum; i++) {
		if (bIs10) {
			wsprintf(szText, _T("%lu"), 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);
	}

	CDialogEx::OnTimer(nIDEvent);
}

//---------------------------------------------------------
// Initialization Process
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntInit()
{
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	short	i;
	//-----------------------------
	// Get device name from screen 
	//-----------------------------
	m_DeviceName.GetWindowText(szDeviceName, sizeof(szDeviceName));
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------
	// Initializtion function execute, get ID
	//-----------------------------
	Ret = CntInit(DeviceName, &Id);
	//-----------------------------
	// Error process
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != CNT_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Get all channel numbe
	//-----------------------------
	CntGetMaxChannels(Id, &ChannelNum);
	//-----------------------------
	// Counter, Status, Preset
	// Exprocess invalidate
	//-----------------------------
	for (i = ChannelNum; i<8; i++) {
		m_Ch[i]->EnableWindow(FALSE);
		m_Status[i]->EnableWindow(FALSE);
		m_Preset[i]->EnableWindow(FALSE);
	}
}

//---------------------------------------------------------
// Mode dialog express
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntMode()
{
	//-----------------------------
	// Dialog express in modal
	//-----------------------------
	CModeDlg	dlg;
	dlg.DoModal();
}

//---------------------------------------------------------
// Comparison dialog express
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntCompare()
{
	//-----------------------------
	// Dialog express in modal
	//-----------------------------
	CCompDlg	dlg;
	dlg.DoModal();
}

//---------------------------------------------------------
// Counter start
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntStart()
{
	short	ChNo[8];
	short	i;
	//-----------------------------
	// Set parameter
	//-----------------------------
	for (i = 0; i<ChannelNum; i++) {
		ChNo[i] = i;
	}
	//-----------------------------
	// Counter start
	//-----------------------------
	Ret = CntStartCount(Id, &ChNo[0], ChannelNum);
	//-----------------------------
	// Error process
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != CNT_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Timer start
	//-----------------------------
	nIDEvent = 1;
	nIDEvent = SetTimer(nIDEvent, 100, NULL);
}

//---------------------------------------------------------
// Preset
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntPreset()
{
	short	ChNo[8];
	DWORD	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) {
			_stscanf_s(szText, _T("%lu"), &PresetData[i]);
		}
		else {
			_stscanf_s(szText, _T("%X"), &PresetData[i]);
		}
	}
	//-----------------------------
	// Preset
	//-----------------------------
	Ret = CntPreset(Id, &ChNo[0], ChannelNum, &PresetData[0]);
	//-----------------------------
	// Error process
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Counter stop
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntStop()
{
	short	ChNo[8];
	short	i;
	//-----------------------------
	// Set parameter
	//-----------------------------
	for (i = 0; i<ChannelNum; i++) {
		ChNo[i] = i;
	}
	//-----------------------------
	// Counter stop
	//-----------------------------
	Ret = CntStopCount(Id, &ChNo[0], ChannelNum);
	//-----------------------------
	// Error process
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	//-----------------------------
	// Timer stop
	//-----------------------------
	KillTimer(nIDEvent);
}

//---------------------------------------------------------
// Exit process
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonCntExit()
{
	//-----------------------------
	// Timer stop
	//-----------------------------
	KillTimer(nIDEvent);
	//-----------------------------
	// Exit function execute
	//-----------------------------
	Ret = CntExit(Id);
	//-----------------------------
	// Error process
	//-----------------------------
	CntGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Dialog exit
//---------------------------------------------------------
void CCounterDlg::OnBnClickedButtonExit()
{
	//-----------------------------
	// Timer stop
	//-----------------------------
	KillTimer(nIDEvent);
	//-----------------------------
	// Exit function execute
	//-----------------------------
	Ret = CntExit(Id);
	//-----------------------------
	// Dialog exit
	//-----------------------------
	CDialogEx::OnOK();
}

//---------------------------------------------------------
// Dec/Hex shift process
//---------------------------------------------------------
// Channel data: Dec
void CCounterDlg::OnBnClickedRadioCh0()
{
	OnTimer(nIDEvent);
}

// Channel data: Hex
void CCounterDlg::OnBnClickedRadioCh1()
{
	OnTimer(nIDEvent);
}

// Preset data: Dec
void CCounterDlg::OnBnClickedRadioPreset0()
{
	//-----------------------------
	// Get data from screen and exprossion change
	//-----------------------------
	if (NumDispFlag != 0){
		short	i;
		DWORD	PresetData;
		for (i = 0; i<8; i++) {
			m_Preset[i]->GetWindowText(szText, sizeof(szText));
			_stscanf_s(szText, _T("%X"), &PresetData);
			wsprintf(szText, _T("%lu"), PresetData);
			m_Preset[i]->SetWindowText(szText);
		}
		NumDispFlag = 0;
	}
}

// Channel data: Hex
void CCounterDlg::OnBnClickedRadioPreset1()
{
	//-----------------------------
	// Get data from screen and exprossion change
	//-----------------------------
	if (NumDispFlag != 1){
		short	i;
		DWORD	PresetData;
		for (i = 0; i<8; i++) {
			m_Preset[i]->GetWindowText(szText, sizeof(szText));
			_stscanf_s(szText, _T("%lu"), &PresetData);
			wsprintf(szText, _T("%08X"), PresetData);
			m_Preset[i]->SetWindowText(szText);
		}
		NumDispFlag = 1;
	}
}

//---------------------------------------------------------
// Message process(consistent with countj
//---------------------------------------------------------
// Handler subroutine consistent with count
void CCounterDlg::CountMatch(short ChNo, short UpDown, DWORD Count)
{
	TCHAR	szUpDown[10];
	//-----------------------------
	// Create screem data 
	//-----------------------------
	lstrcpy(szUpDown, _T(""));
	if (UpDown == CNTM_DIR_UP) {
		lstrcpy(szUpDown, _T("Up"));
	}
	if (UpDown == CNTM_DIR_DOWN) {
		lstrcpy(szUpDown, _T("Down"));
	}
	wsprintf(szText, _T("Ch%d : Count=%lu : %s"), ChNo, Count, szUpDown);
	//-----------------------------
	// Express
	//-----------------------------
	m_CompResult.SetWindowText(szText);
}

// Channel 0: Handler consistent with count
LRESULT CCounterDlg::Ch0Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 0;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 1: Handler consistent with count
LRESULT CCounterDlg::Ch1Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 1;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 2: Handler consistent with count
LRESULT CCounterDlg::Ch2Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 2;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 3: Handler consistent with count
LRESULT CCounterDlg::Ch3Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 3;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 4: Handler consistent with count
LRESULT CCounterDlg::Ch4Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 4;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 5: Handler consistent with count
LRESULT CCounterDlg::Ch5Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 5;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 6: Handler consistent with count
LRESULT CCounterDlg::Ch6Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 6;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}
// Channel 7: Handler consistent with count
LRESULT CCounterDlg::Ch7Handler(WPARAM wParam, LPARAM lParam)
{
	//LOWORD(wParam)=ID Getted from CntInit function
	//HIWORD(wParam)=Direction(UP=1, DOWN=2)
	//lParam=Compare Data
	short	Id = LOWORD(wParam);
	short	ChNo = 7;
	short	UpDown = HIWORD(wParam);
	DWORD	Count = (DWORD)lParam;
	CountMatch(ChNo, UpDown, Count);
	return 0;
}