
// pulse_measurementDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pulse_measurement.h"
#include "pulse_measurementDlg.h"
#include "afxdialogex.h"
#include "CDio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Cpulse_measurementDlg dialog
//==========================================
// Global variable
//==========================================
short	dev_id;					// Device ID


Cpulse_measurementDlg::Cpulse_measurementDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PULSE_MEASUREMENT_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void Cpulse_measurementDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_DeviceName);
	DDX_Control(pDX, IDC_COMBO_CHANNEL_NUM, m_ChannelNum);
	DDX_Control(pDX, IDC_COMBO_LOGIC0, m_Logic0);
	DDX_Control(pDX, IDC_COMBO_LOGIC1, m_Logic1);
	DDX_Control(pDX, IDC_COMBO_LOGIC2, m_Logic2);
	DDX_Control(pDX, IDC_COMBO_LOGIC3, m_Logic3);
	DDX_Control(pDX, IDC_EDIT_PERIOD0, m_Period0);
	DDX_Control(pDX, IDC_EDIT_PERIOD1, m_Period1);
	DDX_Control(pDX, IDC_EDIT_PERIOD2, m_Period2);
	DDX_Control(pDX, IDC_EDIT_PERIOD3, m_Period3);
	DDX_Control(pDX, IDC_EDIT_TIME_VALUE0, m_TimeValue0);
	DDX_Control(pDX, IDC_EDIT_TIME_VALUE1, m_TimeValue1);
	DDX_Control(pDX, IDC_EDIT_TIME_VALUE2, m_TimeValue2);
	DDX_Control(pDX, IDC_EDIT_TIME_VALUE3, m_TimeValue3);
	DDX_Control(pDX, IDC_COMBO_TIME_UNIT0, m_TimeUnit0);
	DDX_Control(pDX, IDC_COMBO_TIME_UNIT1, m_TimeUnit1);
	DDX_Control(pDX, IDC_COMBO_TIME_UNIT2, m_TimeUnit2);
	DDX_Control(pDX, IDC_COMBO_TIME_UNIT3, m_TimeUnit3);
	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_ERROR0, m_Error0);
	DDX_Control(pDX, IDC_EDIT_ERROR1, m_Error1);
	DDX_Control(pDX, IDC_EDIT_ERROR2, m_Error2);
	DDX_Control(pDX, IDC_EDIT_ERROR3, m_Error3);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_ReturnCode);
}

BEGIN_MESSAGE_MAP(Cpulse_measurementDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DIO_INIT, &Cpulse_measurementDlg::OnBnClickedButtonDioInit)
	ON_BN_CLICKED(IDC_BUTTON_DIO_SET_PULSE_MEASUREMENT_SETTING, &Cpulse_measurementDlg::OnBnClickedButtonDioSetPulseMeasurementSetting)
	ON_BN_CLICKED(IDC_BUTTON_DIO_START_PULSE_MEASUREMENT, &Cpulse_measurementDlg::OnBnClickedButtonDioStartPulseMeasurement)
	ON_BN_CLICKED(IDC_BUTTON_DIO_READ_PULSE_MEASUREMENT_DATA, &Cpulse_measurementDlg::OnBnClickedButtonDioReadPulseMeasurementData)
	ON_BN_CLICKED(IDC_BUTTON_DIO_STOP_PULSE_MEASUREMENT, &Cpulse_measurementDlg::OnBnClickedButtonDioStopPulseMeasurement)
	ON_BN_CLICKED(IDC_BUTTON_DIO_EXIT, &Cpulse_measurementDlg::OnBnClickedButtonDioExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &Cpulse_measurementDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// Cpulse_measurementDlg message handlers
//=========================================================
// Dialog initialization
//=========================================================
BOOL Cpulse_measurementDlg::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	ch_no;
	//----------------------------------------
	// Set control array
	//----------------------------------------
	//--------------------
	// Pulse measurement logic control
	//--------------------
	m_Logic[0]	= &m_Logic0;
	m_Logic[1]	= &m_Logic1;
	m_Logic[2]	= &m_Logic2;
	m_Logic[3]	= &m_Logic3;
	//--------------------
	// Pulse measurement period control
	//--------------------
	m_Period[0]	= &m_Period0;
	m_Period[1]	= &m_Period1;
	m_Period[2]	= &m_Period2;
	m_Period[3]	= &m_Period3;
	//--------------------
	// Pulse measurement time control
	//--------------------
	m_TimeValue[0]	= &m_TimeValue0;
	m_TimeValue[1]	= &m_TimeValue1;
	m_TimeValue[2]	= &m_TimeValue2;
	m_TimeValue[3]	= &m_TimeValue3;
	//--------------------
	// Pulse measurement time unit control
	//--------------------
	m_TimeUnit[0]	= &m_TimeUnit0;
	m_TimeUnit[1]	= &m_TimeUnit1;
	m_TimeUnit[2]	= &m_TimeUnit2;
	m_TimeUnit[3]	= &m_TimeUnit3;
	//--------------------
	// Pulse measurement status control
	//--------------------
	m_Status[0]	= &m_Status0;
	m_Status[1]	= &m_Status1;
	m_Status[2]	= &m_Status2;
	m_Status[3]	= &m_Status3;
	//--------------------
	// Pulse measurement error control
	//--------------------
	m_Error[0]	= &m_Error0;
	m_Error[1]	= &m_Error1;
	m_Error[2]	= &m_Error2;
	m_Error[3]	= &m_Error3;
	//----------------------------------------
	// Initialize the display
	//----------------------------------------
	//--------------------
	// Device name
	//--------------------
	m_DeviceName.SetWindowText(_T("DIO000"));
	//--------------------
	// Number of channels
	//--------------------
	m_ChannelNum.AddString(_T("1"));
	m_ChannelNum.AddString(_T("2"));
	m_ChannelNum.AddString(_T("3"));
	m_ChannelNum.AddString(_T("4"));
	m_ChannelNum.SelectString(-1, _T("4"));
	//--------------------
	// For each channel
	//--------------------
	for(ch_no = 0; ch_no < MAX_PULSE_MEASUREMENT_CH; ch_no++){
		//--------------------
		// Pulse measurement logic control
		//--------------------
		m_Logic[ch_no]->AddString(_T("OFF Time"));
		m_Logic[ch_no]->AddString(_T("ON Time"));
		m_Logic[ch_no]->SelectString(-1, _T("OFF Time"));
		//--------------------
		// Pulse measurement period control
		//--------------------
		m_Period[ch_no]->SetWindowText(_T("0"));
		//--------------------
		// Pulse measurement time unit control
		//--------------------
		m_TimeUnit[ch_no]->AddString(_T("sec"));
		m_TimeUnit[ch_no]->AddString(_T("msec"));
		m_TimeUnit[ch_no]->AddString(_T("usec"));
		m_TimeUnit[ch_no]->AddString(_T("nsec"));
		m_TimeUnit[ch_no]->SelectString(-1, _T("nsec"));
		//--------------------
		// Pulse measurement time control
		//--------------------
		m_TimeValue[ch_no]->SetWindowText(_T(""));
		//--------------------
		// Pulse measurement status control
		//--------------------
		m_Status[ch_no]->SetWindowText(_T(""));
		//--------------------
		// Pulse measurement error control
		//--------------------
		m_Error[ch_no]->SetWindowText(_T(""));
	}

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

//=========================================================
// It is the process when the DioInit button is clicked
// Initialize the device
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonDioInit()
{
	TCHAR	sz_device_name[256];
	TCHAR	return_string[256];
	char	device_name[256];
	char	error_string[256];
	long	ret;

	//----------------------------------------
	// Get device name
	//----------------------------------------
	m_DeviceName.GetWindowText(sz_device_name, _countof(sz_device_name));
	sprintf_s(device_name, "%S", sz_device_name);
	//----------------------------------------
	// Initialization and get device ID
	//----------------------------------------
	ret	= DioInit(device_name, &dev_id);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioInit = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioSetPulseMeasurementSetting button is clicked
// Set the pulse measurement of the device
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonDioSetPulseMeasurementSetting()
{
	TCHAR			cstr[256];
	TCHAR			return_string[256];
	char			error_string[256];
	unsigned short	pulse_measurement_target[MAX_PULSE_MEASUREMENT_CH];
	unsigned short	pulse_measurement_period[MAX_PULSE_MEASUREMENT_CH];
	short			ch_no_array[MAX_PULSE_MEASUREMENT_CH];
	long			ret;
	short			ch_no;
	short			pulse_measurement_ch_num;
	
	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pulse_measurement_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
		//----------------------------------------
		// Get specified value of pulse measurement logic
		//----------------------------------------
		m_Logic[ch_no]->GetLBText(m_Logic[ch_no]->GetCurSel(), cstr);
		if(wcscmp(cstr, _T("OFF Time")) == 0){
			pulse_measurement_target[ch_no]	= DIO_PULSE_MEASUREMENT_TARGET_OFF_TIME;
		}else{
			pulse_measurement_target[ch_no]	= DIO_PULSE_MEASUREMENT_TARGET_ON_TIME;
		}
		//----------------------------------------
		// Get specified value of pulse measurement period
		//----------------------------------------
		m_Period[ch_no]->GetWindowText(cstr, _countof(cstr));
		pulse_measurement_period[ch_no]	= (unsigned short)wcstoul(cstr, NULL, 10);
	}
	//----------------------------------------
	// Set pulse measurement
	//----------------------------------------
	ret	= DioSetPulseMeasurementSetting(dev_id, ch_no_array, pulse_measurement_ch_num, pulse_measurement_target, pulse_measurement_period);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioSetPulseMeasurementSetting = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioStartPulseMeasurement button is clicked
// Start the pulse measurement
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonDioStartPulseMeasurement()
{
	TCHAR	cstr[256];
	TCHAR	return_string[256];
	char	error_string[256];
	short	ch_no_array[MAX_PULSE_MEASUREMENT_CH];
	long	ret;
	short	ch_no;
	short	pulse_measurement_ch_num;
	
	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pulse_measurement_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
	}
	//----------------------------------------
	// Start pulse measurement
	//----------------------------------------
	ret	= DioStartPulseMeasurement(dev_id, ch_no_array, pulse_measurement_ch_num);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioStartPulseMeasurement = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioReadPulseMeasurementData button is clicked
// Get the pulse measurement data
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonDioReadPulseMeasurementData()
{
	TCHAR				cstr[256];
	TCHAR				return_string[256];
	char				error_string[256];
	unsigned long long	pulse_measurement_time_value[MAX_PULSE_MEASUREMENT_CH];
	unsigned short		pulse_measurement_status[MAX_PULSE_MEASUREMENT_CH];
	unsigned short		pulse_measurement_error[MAX_PULSE_MEASUREMENT_CH];
	unsigned short		pulse_measurement_time_unit[MAX_PULSE_MEASUREMENT_CH];
	short				ch_no_array[MAX_PULSE_MEASUREMENT_CH];
	long				ret;
	short				ch_no;
	short				pulse_measurement_ch_num;
	
	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pulse_measurement_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
		//----------------------------------------
		// Initialize pulse measurement time
		//----------------------------------------
		pulse_measurement_time_value[ch_no]	= 0;
		//----------------------------------------
		// Get specified value of pulse measurement time unit
		//----------------------------------------
		m_TimeUnit[ch_no]->GetLBText(m_TimeUnit[ch_no]->GetCurSel(), cstr);
		if(wcscmp(cstr, _T("sec")) == 0){
			pulse_measurement_time_unit[ch_no]	= DIO_PULSE_MEASUREMENT_TIME_UNIT_S;
		}else if(wcscmp(cstr, _T("msec")) == 0){
			pulse_measurement_time_unit[ch_no]	= DIO_PULSE_MEASUREMENT_TIME_UNIT_MS;
		}else if(wcscmp(cstr, _T("usec")) == 0){
			pulse_measurement_time_unit[ch_no]	= DIO_PULSE_MEASUREMENT_TIME_UNIT_US;
		}else{
			pulse_measurement_time_unit[ch_no]	= DIO_PULSE_MEASUREMENT_TIME_UNIT_NS;
		}
	}
	//----------------------------------------
	// Get pulse measurement data
	//----------------------------------------
	ret	= DioReadPulseMeasurementData(dev_id, ch_no_array, pulse_measurement_ch_num, pulse_measurement_status, pulse_measurement_error, pulse_measurement_time_value, pulse_measurement_time_unit);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioReadPulseMeasurementData = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
	//----------------------------------------
	// Terminate process in case of error
	//----------------------------------------
	if(ret != DIO_ERR_SUCCESS){
		return;
	}
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++){
		//----------------------------------------
		// Update the display of pulse measurement status
		//----------------------------------------
		_itow_s(pulse_measurement_status[ch_no], cstr, 100, 16);
		m_Status[ch_no]->SetWindowText(cstr);
		//----------------------------------------
		// Update the display of pulse measurement error
		//----------------------------------------
		_itow_s(pulse_measurement_error[ch_no], cstr, 100,16);
		m_Error[ch_no]->SetWindowText(cstr);
		//----------------------------------------
		// Update the display of pulse measurement time
		//----------------------------------------
		_ui64tow_s(pulse_measurement_time_value[ch_no], cstr, 100, 10);
		m_TimeValue[ch_no]->SetWindowText(cstr);
	}
}

//=========================================================
// It is the process when the DioStopPulseMeasurement button is clicked
// Stop the pulse measurement
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonDioStopPulseMeasurement()
{
	TCHAR	cstr[256];
	TCHAR	return_string[256];
	char	error_string[256];
	short	ch_no_array[MAX_PULSE_MEASUREMENT_CH];
	long	ret;
	short	ch_no;
	short	pulse_measurement_ch_num;

	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pulse_measurement_ch_num = (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for (ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++) {
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no] = ch_no;
	}
	//----------------------------------------
	// Stop pulse measurement
	//----------------------------------------
	ret = DioStopPulseMeasurement(dev_id, ch_no_array, pulse_measurement_ch_num);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioStopPulseMeasurement = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioExit button is clicked
// Perform the exit process of device
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonDioExit()
{
	TCHAR	return_string[256];
	char	error_string[256];
	long	ret;

	//----------------------------------------
	// Exit process
	//----------------------------------------
	ret	= DioExit(dev_id);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioExit = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the Close button is clicked
// Perform the exit process of device and terminate the application
//=========================================================
void Cpulse_measurementDlg::OnBnClickedButtonExit()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	DioExit(dev_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
