
// WaveformJudgement2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "WaveformJudgement2.h"
#include "WaveformJudgement2Dlg.h"
#include "afxdialogex.h"
//-------------------------------------
// Include API-DIG(WDM) header file
//-------------------------------------
#include "cdig.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CWaveformJudgement2Dlg dialog



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

void CWaveformJudgement2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATUS, m_Status);
	DDX_Control(pDX, IDC_RICHEDIT_DATA, m_EditData);
	DDX_Control(pDX, IDC_EDIT_INFO, m_Info);
	DDX_Control(pDX, IDC_EDIT_DEVICE, m_DeviceName);
	DDX_Control(pDX, IDC_COUNT_JUDGE, m_JudgeCount);
}

BEGIN_MESSAGE_MAP(CWaveformJudgement2Dlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INIT, &CWaveformJudgement2Dlg::OnBnClickedButtonInit)
	ON_BN_CLICKED(IDC_BUTTON_SETTING, &CWaveformJudgement2Dlg::OnBnClickedButtonSetting)
	ON_BN_CLICKED(IDC_BUTTON_START, &CWaveformJudgement2Dlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_INFO, &CWaveformJudgement2Dlg::OnBnClickedButtonInfo)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CWaveformJudgement2Dlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CWaveformJudgement2Dlg::OnBnClickedButtonExit)
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// CWaveformJudgement2Dlg message handlers

BOOL CWaveformJudgement2Dlg::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

	//---------------------------------
	// Initialize device name
	//---------------------------------
	m_DeviceName.SetWindowText(_T("DIG000"));

	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 CWaveformJudgement2Dlg::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 CWaveformJudgement2Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//===============================================
// Init handling
//===============================================
void CWaveformJudgement2Dlg::OnBnClickedButtonInit()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	char	device_name[256];	// Device name
	TCHAR	szDevice_name[256];
	long	ret;				// Return value

	//---------------------------------
	// Init handling
	//---------------------------------
	m_DeviceName.GetWindowText(szDevice_name, sizeof(szDevice_name));
	sprintf_s(device_name, "%S", szDevice_name);
	ret = DigInit(device_name, &m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigInit = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}

	//---------------------------------
	// Reset device
	//---------------------------------
	ret = DigResetDevice(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigResetDevice = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}

	m_Info.SetWindowText(_T("Init : Normality completion"));
}

//===============================================
// Set device
//===============================================
void CWaveformJudgement2Dlg::OnBnClickedButtonSetting()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	//---------------------------------
	// Initialize variable
	//---------------------------------
	m_Records = 0;

	//---------------------------------
	// Set the operation mode : Waveform judgment instrument mode
	//---------------------------------
	ret = DigSetDigitizerMode(m_Id, DIG_MODE_JUDGE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetDigitizerMode = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling clock source : Crystal oscillator of board
	//---------------------------------
	ret = DigSetClockSource(m_Id, DIG_ECU_SRC_CLOCK);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetClockSource = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling rate : 1MHz
	//---------------------------------
	ret = DigSetSampleRate(m_Id, 1000000);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetSampleRate = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the length of record : 512
	//---------------------------------
	ret = DigSetRecordLength(m_Id, MAX_SAMPLES);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRecordLength = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of records : 1
	//---------------------------------
	ret = DigSetRecordNumber(m_Id, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRecordNumber = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set duty ratio : 50%
	//---------------------------------
	ret = DigSetDutyCycle(m_Id, 50.0);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetDutyCycle = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set channel : Enable channel0 only
	//---------------------------------
	// Enable channel0
	ret = DigSetChannel(m_Id, DIG_CHANNEL0, DIG_ENABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	// Disable channel1
	ret = DigSetChannel(m_Id, DIG_CHANNEL1, DIG_DISABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set range : +/-20V
	//---------------------------------
	ret = DigSetRange(m_Id, DIG_CHANNEL0, DIG_RANGE_40VPP);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRange = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the type of pre-trigger : Auto
	//---------------------------------
	ret = DigSetPreTriggerType(m_Id, DIG_TRIGGERTYPE_AUTO);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreTriggerType = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the type of post-trigger : Analog edge trigger
	//---------------------------------
	ret = DigSetPostTriggerType(m_Id, DIG_TRIGGERTYPE_AIEDGE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPostTriggerType = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Detailed setting for the post-trigger : TriggerSource : channel0, Trigger level : 0V, Rising edge, 1
	//---------------------------------
	ret = DigSetPostTriggerAiEdge(m_Id, DIG_CHANNEL0, 0.0, DIG_TRIGGER_SLOPE_RISE, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPostTriggerAiEdge = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Specifies the channel to perform waveform judgment : Channel0
	//---------------------------------
	ret = DigSetJudgeChannel(m_Id, DIG_CHANNEL0, DIG_ENABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetJudgeChannel = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the upper limit value for counting the waveform judgment result : Count the FAIL records, The upper limit value of count times is 1
	//---------------------------------
	ret = DigSetJudgeCountLimit(m_Id, DIG_CHANNEL0, DIG_JUDGE_NG, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetJudgeCountLimit = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the data for waveform judgment : UpperData : +5V, LowerData : -5V
	//---------------------------------
	for (long i = 0; i < MAX_SAMPLES; i++){
		m_UpperData[i] = 5.0;
		m_LowerData[i] = -5.0;
	}
	ret = DigSetJudgeData(m_Id, DIG_CHANNEL0, MAX_SAMPLES, &m_UpperData[0], &m_LowerData[0]);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetJudgeData = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}

	ret = DigGetJudgeData(m_Id, DIG_CHANNEL0, MAX_SAMPLES, &m_UpperData[0], &m_LowerData[0]);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetJudgeData = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Set device : Normality completion"));
}

//===============================================
// Start Judgment
//===============================================
void CWaveformJudgement2Dlg::OnBnClickedButtonStart()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	//---------------------------------
	// Initialize variable
	//---------------------------------
	m_Records = 0;

	//---------------------------------
	// Operation start
	//---------------------------------
	ret = DigStart(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStart = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Start Judgment : Normality completion"));

	//---------------------------------
	// Polling until the operation status is changed to be OFF
	//---------------------------------
	long	status;				// Status

	do{
		//---------------------------------
		// In order to enable the dialog operation
		//---------------------------------
		MSG msg;
		while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)){
			if (!GetMessage(&msg, NULL, NULL, NULL)){
				PostQuitMessage(0);
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		//---------------------------------
		// Get the opration status
		//---------------------------------
		ret = DigGetStatus(m_Id, &status);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
			m_Info.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%xH"), status);
		m_Status.SetWindowText(cstr);
	} while ((status & DIG_STATUS_BUSY) == DIG_STATUS_BUSY);
	//---------------------------------
	// After operation stop, get the number of samples which are being stored in driver memory
	//---------------------------------
	ret = DigGetDriverMemoryRecords(m_Id, &m_Records);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetDriverMemoryRecords = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Retrieve the acquisition data : Retrieve the data of 1 record from the new data
	//---------------------------------
	if (m_Records <= 0){
		m_Info.SetWindowText(_T("There are not acquistion data."));
	}
	else{
		ret = DigReadLatestRecords(m_Id, DIG_RECORD_ALL, 1, &m_Data[0], &m_Judge[0], &m_JudgeDetail[0]);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigReadLatestRecords = %d : %s"), ret, szError_string);
			m_Info.SetWindowText(cstr);
			return;
		}
		//---------------------------------
		// Display the judge result
		//---------------------------------
		cstr.Format(_T("%d"), m_Judge[0]);
		m_JudgeCount.SetWindowText(cstr);
		CString temp;
		for (int i = 0; i < MAX_SAMPLES; i++){
			cstr.Format(_T("%d\t%.2f\t%.2f\t%.2f\t%d\r\n"), i + 1, m_Data[i], m_UpperData[i], m_LowerData[i], m_JudgeDetail[i]);
			temp += cstr;
		}
		m_EditData.SetWindowText(temp);
	}
}

//===============================================
// Get status
//===============================================
void CWaveformJudgement2Dlg::OnBnClickedButtonInfo()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	long	ret;				// Return value
	long	status;				// Status

	//---------------------------------
	// Get the operation status
	//---------------------------------
	ret = DigGetStatus(m_Id, &status);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%xH"), status);
	m_Status.SetWindowText(cstr);
	m_Info.SetWindowText(_T("Get status : Normality completion"));
}

//===============================================
// Stop Judgment
//===============================================
void CWaveformJudgement2Dlg::OnBnClickedButtonStop()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	//---------------------------------
	// Stop Judgment
	//---------------------------------
	ret = DigStop(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStop = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Stop Judgment : Normality completion"));
}

//===============================================
// Exit handling
//===============================================
void CWaveformJudgement2Dlg::OnBnClickedButtonExit()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	ret = DigExit(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigExit = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Exit handling : Normality completion"));
}



void CWaveformJudgement2Dlg::OnDestroy()
{
	DigExit(m_Id);
	CDialogEx::OnDestroy();
}
