// SSampMode.cpp : implementation file
//

#include "stdafx.h"
#include "Sync.h"
#include "SyncDlg.h"
#include "SSampMode.h"
#include "afxdialogex.h"
// Counter header
#include "ccnt.h"

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

// Sampling Start
static StrNum Start[] = {
	{ _T("ExtSig1"), CNTS_START_EXTSIG_1 },
	{ _T("ExtSig2"), CNTS_START_EXTSIG_2 },
	{ _T("ExtSig3"), CNTS_START_EXTSIG_3 },
	{ _T("End"), 0 }
};
// Counter Start
static StrNum StartWith[] = {
	{ _T("Start counter regardless of sampling"), CNTS_START_CNT_NOT_WITH_SAMP },
	{ _T("Start counter when sampling starts"), CNTS_START_CNT_WITH_SAMP },
	{ _T("End"), 0 }
};
// Sampling Clock
static StrNum Clock[] = {
	{ _T("ExtSig1"), CNTS_CLOCK_EXTSIG_1 },
	{ _T("ExtSig2"), CNTS_CLOCK_EXTSIG_2 },
	{ _T("ExtSig3"), CNTS_CLOCK_EXTSIG_3 },
	{ _T("End"), 0 }
};
// Sampling Stop
static StrNum Stop[] = {
	{ _T("ExtSig1"), CNTS_STOP_EXTSIG_1 },
	{ _T("ExtSig2"), CNTS_STOP_EXTSIG_2 },
	{ _T("ExtSig3"), CNTS_STOP_EXTSIG_3 },
	{ _T("Stop transfer by specified times"), CNTS_STOP_NUM },
	{ _T("End"), 0 }
};

static StrNum StopWith[] = {
	{ _T("Stop counter regardless of sampling"), CNTS_STOP_CNT_NOT_WITH_SAMP },
	{ _T("Stop counter when sampling stops"), CNTS_STOP_CNT_WITH_SAMP },
	{ _T("End"), 0 }
};

static TCHAR szStart[50] = _T("ExtSig2");
static TCHAR szStartWith[50] = _T("Start counter regardless of sampling");
static TCHAR szClock[50] = _T("ExtSig1");
static TCHAR szStop[50] = _T("ExtSig3");
static TCHAR szStopWith[50] = _T("Stop counter regardless of sampling");

unsigned long			S_NotifyCount = 500;
extern short			S_IsRing;
static unsigned long	SStopNum = 1000;


static short str_to_num(StrNum *strnum, TCHAR *str)
{
	WORD	i;
	for (i = 0; lstrcmp(strnum[i].Str, _T("End")) != 0; i++) {
		if (lstrcmp(strnum[i].Str, str) == 0) {
			return strnum[i].Num;
		}
	}
	return 0;
}

static TCHAR * num_to_str(StrNum *strnum, short num)
{
	WORD	i;
	for (i = 0; lstrcmp(strnum[i].Str, _T("End")) != 0; i++) {
		if (strnum[i].Num == num) {
			return strnum[i].Str;
		}
	}
	return strnum[0].Str;
}

// CSSampMode dialog

IMPLEMENT_DYNAMIC(CSSampMode, CDialogEx)

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

}

CSSampMode::~CSSampMode()
{
}

void CSSampMode::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_STOP_NUMBER, m_StopNumber);
	DDX_Control(pDX, IDC_COMBO_STOP_WITH_SAMP, m_StopWith);
	DDX_Control(pDX, IDC_COMBO_START_WITH_SAMP, m_StartWith);
	DDX_Control(pDX, IDC_RADIO_ONCE, m_Radio_Once);
	DDX_Control(pDX, IDC_EDIT_NUMBER_DATA, m_NumberData);
	DDX_Control(pDX, IDC_COMBO_STOP, m_Stop);
	DDX_Control(pDX, IDC_COMBO_CLOCK, m_Clock);
	DDX_Control(pDX, IDC_COMBO_START, m_Start);
}


BEGIN_MESSAGE_MAP(CSSampMode, CDialogEx)
	ON_CBN_SELENDOK(IDC_COMBO_STOP, &CSSampMode::OnCbnSelendokComboStop)
END_MESSAGE_MAP()


// CSSampMode message handlers

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

	WORD	i;
	TCHAR	szBuf[100];

	//--------------------------------
	// Initialize display
	//--------------------------------
	// Start
	for (i = 0; lstrcmp(Start[i].Str, _T("End")) != 0; i++) {
		m_Start.AddString(Start[i].Str);
	}
	// Start with
	for (i = 0; lstrcmp(StartWith[i].Str, _T("End")) != 0; i++) {
		m_StartWith.AddString(StartWith[i].Str);
	}
	// Clock
	for (i = 0; lstrcmp(Clock[i].Str, _T("End")) != 0; i++) {
		m_Clock.AddString(Clock[i].Str);
	}
	// Stop
	for (i = 0; lstrcmp(Stop[i].Str, _T("End")) != 0; i++) {
		m_Stop.AddString(Stop[i].Str);
	}
	// Stop with
	for (i = 0; lstrcmp(StopWith[i].Str, _T("End")) != 0; i++) {
		m_StopWith.AddString(StopWith[i].Str);
	}

	m_Start.SelectString(-1, szStart);
	m_StartWith.SelectString(-1, szStartWith);
	m_Clock.SelectString(-1, szClock);
	m_Stop.SelectString(-1, szStop);
	m_StopWith.SelectString(-1, szStopWith);

	wsprintf(szBuf, _T("%lu"), S_NotifyCount);
	m_NumberData.SetWindowText(szBuf);
	wsprintf(szBuf, _T("%lu"), SStopNum);
	m_StopNumber.SetWindowText(szBuf);

	if (S_IsRing == CNTS_WRITE_ONCE) {
		m_Radio_Once.SetCheck(TRUE);
		SendDlgItemMessage(IDC_RADIO_RING, BM_SETCHECK, 0, 0);
	}
	else {
		m_Radio_Once.SetCheck(FALSE);
		SendDlgItemMessage(IDC_RADIO_RING, BM_SETCHECK, 1, 0);
	}
	OnCbnSelendokComboStop();

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

//===================================================
// OK button processing
//===================================================
void CSSampMode::OnOK()
{
	TCHAR	szBuf[100];
	int		Checked;
	short	dwStart;
	short	ExtSig1, ExtSig2, ExtSig3;
	WORD	wStartWith;
	short	dwClock;
	short	dwStop;
	WORD	wStopWith;
	long	ret;
	CString	strMsg;
	char	error_string[256];
	TCHAR	szError_string[256];

	//--------------------------------
	// Get displayed data
	//--------------------------------
	m_Start.GetWindowText(szStart, sizeof(szStart));
	m_StartWith.GetWindowText(szStartWith, sizeof(szStartWith));
	m_Clock.GetWindowText(szClock, sizeof(szClock));
	m_Stop.GetWindowText(szStop, sizeof(szStop));
	m_StopWith.GetWindowText(szStopWith, sizeof(szStopWith));

	m_NumberData.GetWindowText(szBuf, sizeof(szBuf));
	_stscanf_s(szBuf, _T("%lu"), &S_NotifyCount);
	Checked = m_Radio_Once.GetCheck();
	m_StopNumber.GetWindowText(szBuf, sizeof(szBuf));
	_stscanf_s(szBuf, _T("%lu"), &SStopNum);

	//--------------------------------
	// Convert setting value to a number
	//--------------------------------
	dwStart = str_to_num(Start, szStart);
	wStartWith = (WORD)str_to_num(StartWith, szStartWith);

	dwClock = str_to_num(Clock, szClock);

	dwStop = str_to_num(Stop, szStop);
	wStopWith = (WORD)str_to_num(StopWith, szStopWith);

	if (Checked == 1) {
		S_IsRing = CNTS_WRITE_ONCE;
	}
	else {
		S_IsRing = CNTS_WRITE_RING;
	}

	//--------------------------------
	// Set sampling condition
	//--------------------------------
	ExtSig1 = ExtSig2 = ExtSig3 = 1;
	ret = CntSetSlave(S_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		strMsg.Format(_T("CntSetSlave ret = %d: %s"), ret, szError_string);
		MessageBox(strMsg, _T("Error"), MB_OK);
		return;
	}

	ret = CntSetSamplingStartTrigger(S_Id, dwStart, wStartWith, CHANNEL_NUM);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		strMsg.Format(_T("CntSetSamplingStartTrigger ret = %d: %s"), ret, szError_string);
		MessageBox(strMsg, _T("Error"), MB_OK);
		return;
	}
	ret = CntSetSamplingClockTrigger(S_Id, dwClock);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		strMsg.Format(_T("CntSetSamplingClockTrigger ret = %d: %s"), ret, szError_string);
		MessageBox(strMsg, _T("Error"), MB_OK);
		return;
	}
	ret = CntSetSamplingStopTrigger(S_Id, dwStop, wStopWith, CHANNEL_NUM);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		strMsg.Format(_T("CntSetSamplingStopTrigger ret = %d: %s"), ret, szError_string);
		MessageBox(strMsg, _T("Error"), MB_OK);
		return;
	}

	ret = CntSetSamplingStopNumber(S_Id, SStopNum);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		strMsg.Format(_T("CntSetSamplingStopNumber ret = %d: %s"), ret, szError_string);
		MessageBox(strMsg, NULL, MB_OK);
	}

	CDialogEx::OnOK();
}

//===================================================
// Cancel button processing
//===================================================
void CSSampMode::OnCancel()
{
	CDialogEx::OnCancel();
}

//===================================================
// Stop condition setting is changed
//===================================================
void CSSampMode::OnCbnSelendokComboStop()
{
	WORD	wCurSel;
	TCHAR	szBuf[100];

	wCurSel = m_Stop.GetCurSel();
	m_Stop.GetLBText(wCurSel, szBuf);
	if (lstrcmp(szBuf, _T("Stop transfer by specified times")) == 0) {
		m_StopNumber.EnableWindow(TRUE);
	}
	else {
		m_StopNumber.EnableWindow(FALSE);
	}
}
