// ModeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Counter.h"
#include "ModeDlg.h"

//---------------------------------------------------------
// Counter header file
//---------------------------------------------------------
#include "ccapcnt.h"
//---------------------------------------------------------
// External variable
//---------------------------------------------------------
extern	short	ChannelNum;			// Total number of channels
extern	HWND	g_hWnd;				// Window handle of the main dialog
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
//---------------------------------------------------------
// Define mode
//---------------------------------------------------------

typedef struct {
	TCHAR	Str[50];
	short	Num;
} StrNum;

static StrNum Combo_ZMode[] = {
	{	_T("Not Use"),		CNT_ZPHASE_NOT_USE		},
	{	_T("Next Time"),	CNT_ZPHASE_NEXT_ONE		},
	{	_T("Everytime"),	CNT_ZPHASE_EVERY_TIME	},
	{	_T("End"),			0						}
};

static StrNum Combo_ZLogic[] = {
	{	_T("Positive Logic"),	CNT_ZLOGIC_POSITIVE		},
	{	_T("Negative Logic"),	CNT_ZLOGIC_NEGATIVE		},
	{	_T("End"),				0						}
};

static StrNum Combo_ChannelSignal[] = {
	{	_T("Photoisolator"),	CNT_SIGTYPE_ISOLATE		},
	{	_T("TTL"),				CNT_SIGTYPE_TTL			},
	{	_T("Differential"),		CNT_SIGTYPE_LINERECEIVER},
	{	_T("End"),				0						}
};

static StrNum Combo_CntDir[] = {
	{	_T("Down-count"),	CNT_DIR_DOWN	},
	{	_T("Up-count"),		CNT_DIR_UP		},
	{	_T("End"),			0				}
};

static StrNum Combo_Phase[] = {
	{	_T("1 Phase"),		CNT_MODE_1PHASE		},
	{	_T("2 Phase"),		CNT_MODE_2PHASE		},
	{	_T("Gate Control"),	CNT_MODE_GATECONTROL},
	{	_T("End"),			0					}
};

static StrNum Combo_Mul[]={
	{	_T("x1"),			CNT_MUL_X1		},
	{	_T("x2"),			CNT_MUL_X2		},
	{	_T("x4"),			CNT_MUL_X4		},
	{	_T("End"),			0				}
};

static StrNum Combo_SyncClr[]={
	{	_T("Asynchronous Clear"),	CNT_CLR_ASYNC	},
	{	_T("Synchronous Clear"),	CNT_CLR_SYNC	},
	{	_T("End"),					0				}
};


// CModeDlg dialog

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

CModeDlg::~CModeDlg()
{
}

void CModeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_CH_NO, m_ChNo);
	DDX_Control(pDX, IDC_COMBO_ZMODE, m_ZMode);
	DDX_Control(pDX, IDC_COMBO_ZLOGIC, m_ZLogic);
	DDX_Control(pDX, IDC_COMBO_SOURCE, m_Source);
	DDX_Control(pDX, IDC_COMBO_DIR, m_Dir);
	DDX_Control(pDX, IDC_EDIT_FILTER, m_Filter);
	DDX_Control(pDX, IDC_COMBO_PHASE, m_Phase);
	DDX_Control(pDX, IDC_COMBO_MUL, m_Mul);
	DDX_Control(pDX, IDC_COMBO_CLR, m_Clr);
}


BEGIN_MESSAGE_MAP(CModeDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_SET, OnBnClickedButtonSet)
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_CBN_SELCHANGE(IDC_COMBO_CH_NO, OnCbnSelchangeComboChNo)
END_MESSAGE_MAP()


// CModeDlg message handlers

//---------------------------------------------------------
//	Convert a number to a string and convert a string to a number
//---------------------------------------------------------
TCHAR *NumToStr(StrNum *p, short Num)
{
	short	i;
	static	TCHAR None[2] = _T("");
	for (i=0; lstrcmp(p[i].Str, _T("End")) != 0; i++) {
		if (p[i].Num == Num) {
			return p[i].Str;
		}
	}
	return None;
}

short StrToNum(StrNum *p, const TCHAR *Str)
{
	short	i;
	for (i=0; lstrcmp(p[i].Str, _T("End")) != 0; i++) {
		if (lstrcmp(p[i].Str, Str) == 0) {
			return p[i].Num;
		}
	}
	return 0;
}
//---------------------------------------------------------
//	Initialize dialog
//---------------------------------------------------------
BOOL CModeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//-----------------------------------
	// Initialize combobox
	//-----------------------------------
	short	i;
	for (i=0; i<ChannelNum; i++) {
		wsprintf(szText, _T("%d"), (int)i);
		m_ChNo.AddString(szText);
	}
	m_ChNo.SetCurSel(0);
	for (i=0; lstrcmp(Combo_ZMode[i].Str, _T("End")) != 0; i++) {
		m_ZMode.AddString(Combo_ZMode[i].Str);
	}
	for (i=0; lstrcmp(Combo_ZLogic[i].Str, _T("End")) != 0; i++) {
		m_ZLogic.AddString(Combo_ZLogic[i].Str);
	}    
	for (i = 0; lstrcmp(Combo_ChannelSignal[i].Str, _T("End")) != 0; i++) {
		m_Source.AddString(Combo_ChannelSignal[i].Str);
	}    
	for (i = 0; lstrcmp(Combo_CntDir[i].Str, _T("End")) != 0; i++) {
		m_Dir.AddString(Combo_CntDir[i].Str);
	}    
	for (i = 0; lstrcmp(Combo_Phase[i].Str, _T("End")) != 0; i++) {
		m_Phase.AddString(Combo_Phase[i].Str);
	}    
	for (i = 0; lstrcmp(Combo_Mul[i].Str, _T("End")) != 0; i++) {
		m_Mul.AddString(Combo_Mul[i].Str);
	}    
	for (i = 0; lstrcmp(Combo_SyncClr[i].Str, _T("End")) != 0; i++) {
		m_Clr.AddString(Combo_SyncClr[i].Str);
	} 
	//-----------------------------------
	// Select channel 0
	//-----------------------------------
	OnCbnSelchangeComboChNo();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//---------------------------------------------------------
//	Process Set button
//---------------------------------------------------------
void CModeDlg::OnBnClickedButtonSet()
{
	// TODO: Add your control notification handler code here
	//-----------------------------------
	// Get mode from screen
	//-----------------------------------
	m_ZMode.GetWindowText(szText, sizeof(szText));
	ZMode = StrToNum(Combo_ZMode, szText);
	m_ZLogic.GetWindowText(szText, sizeof(szText));
	ZLogic = StrToNum(Combo_ZLogic,szText);
	m_Source.GetWindowText(szText, sizeof(szText));
	SigType = StrToNum(Combo_ChannelSignal, szText);
	m_Dir.GetWindowText(szText, sizeof(szText));
	Dir = StrToNum(Combo_CntDir, szText);
	m_Phase.GetWindowText(szText, sizeof(szText));
	Phase = StrToNum(Combo_Phase, szText);
	m_Mul.GetWindowText(szText, sizeof(szText));
	Mul = StrToNum(Combo_Mul, szText);
	m_Clr.GetWindowText(szText, sizeof(szText));
	SyncClr = StrToNum(Combo_SyncClr, szText);
	m_Filter.GetWindowText(szText, sizeof(szText));
	swscanf_s(szText, _T("%hX"), &FilterValue);
	//-----------------------------------
	// Set mode
	//-----------------------------------
	Ret = CntSetZMode(Id, ChNo, ZMode);
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntSetZMode : %S"), szError);
		MessageBox(szReturnCode, _T("Error"), MB_OK);
	}
	Ret = CntSetZLogic(Id, ChNo, ZLogic);
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntSetZLogic : %S"), szError);
		MessageBox(szReturnCode, _T("Error"), MB_OK);
	}
	Ret = CntSelectChannelSignal(Id, ChNo, SigType);
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntSelectChannelSignal : %S"), szError);
		MessageBox(szReturnCode, _T("Error"), MB_OK);
	}
	Ret = CntSetCountDirection(Id, ChNo, Dir);
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntSetCountDirection : %S"), szError);
		MessageBox(szReturnCode, _T("Error"), MB_OK);
	}
	Ret = CntSetOperationMode(Id, ChNo, Phase, Mul, SyncClr);
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntSetOperationMode : %S"), szError);
		MessageBox(szReturnCode, _T("Error"), MB_OK);
	}
	Ret = CntSetDigitalFilter(Id, ChNo, FilterValue);
	if (Ret != CNT_ERR_SUCCESS) {
		CntGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("CntSetDigitalFilter : %S"), szError);
		MessageBox(szReturnCode, _T("Error"), MB_OK);
	}
	
	OnOK();
}

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

//---------------------------------------------------------
//	Select channel combobox
//---------------------------------------------------------
void CModeDlg::OnCbnSelchangeComboChNo()
{
	// TODO: Add your control notification handler code here
	//-----------------------------------
	// Get mode
	//-----------------------------------
	m_ChNo.GetWindowText(szText, sizeof(szText));
	ChNo = (short)_ttoi(szText);

	CntGetZMode(Id, ChNo, &ZMode);
	CntGetZLogic(Id, ChNo, &ZLogic);
	CntGetChannelSignal(Id, ChNo, &SigType);
	CntGetCountDirection(Id, ChNo, &Dir);
	CntGetOperationMode(Id, ChNo, &Phase, &Mul, &SyncClr);
	CntGetDigitalFilter(Id, ChNo, &FilterValue);
	//-----------------------------------
	// Set mode
	//-----------------------------------
	m_ZMode.SelectString(-1, NumToStr(Combo_ZMode, ZMode));
	m_ZLogic.SelectString(-1, NumToStr(Combo_ZLogic, ZLogic));
	m_Source.SelectString(-1, NumToStr(Combo_ChannelSignal, SigType));
	m_Dir.SelectString(-1, NumToStr(Combo_CntDir, Dir));
	m_Phase.SelectString(-1, NumToStr(Combo_Phase, Phase));
	m_Mul.SelectString(-1, NumToStr(Combo_Mul, Mul));
	m_Clr.SelectString(-1, NumToStr(Combo_SyncClr, SyncClr));
	wsprintf(szText,_T("%X"), FilterValue);
	m_Filter.SetWindowText(szText);
}
