
// pwm_outputDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pwm_output.h"
#include "pwm_outputDlg.h"
#include "afxdialogex.h"
#include "CDio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Cpwm_outputDlg dialog

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

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

void Cpwm_outputDlg::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_EDIT_RETURN_CODE, m_ReturnCode);
	DDX_Control(pDX, IDC_EDIT_PULSE_PERIOD0, m_PulsePeriodValue0);
	DDX_Control(pDX, IDC_EDIT_PULSE_PERIOD1, m_PulsePeriodValue1);
	DDX_Control(pDX, IDC_EDIT_PULSE_PERIOD2, m_PulsePeriodValue2);
	DDX_Control(pDX, IDC_EDIT_PULSE_PERIOD3, m_PulsePeriodValue3);
	DDX_Control(pDX, IDC_COMBO_PERIOD_UNIT0, m_PeriodUnit0);
	DDX_Control(pDX, IDC_COMBO_PERIOD_UNIT1, m_PeriodUnit1);
	DDX_Control(pDX, IDC_COMBO_PERIOD_UNIT2, m_PeriodUnit2);
	DDX_Control(pDX, IDC_COMBO_PERIOD_UNIT3, m_PeriodUnit3);
	DDX_Control(pDX, IDC_EDIT_PULSE_DUTY0, m_PulseDutyValue0);
	DDX_Control(pDX, IDC_EDIT_PULSE_DUTY1, m_PulseDutyValue1);
	DDX_Control(pDX, IDC_EDIT_PULSE_DUTY2, m_PulseDutyValue2);
	DDX_Control(pDX, IDC_EDIT_PULSE_DUTY3, m_PulseDutyValue3);
	DDX_Control(pDX, IDC_COMBO_PULSE_PHASE0, m_PulsePhase0);
	DDX_Control(pDX, IDC_COMBO_PULSE_PHASE1, m_PulsePhase1);
	DDX_Control(pDX, IDC_COMBO_PULSE_PHASE2, m_PulsePhase2);
	DDX_Control(pDX, IDC_COMBO_PULSE_PHASE3, m_PulsePhase3);
	DDX_Control(pDX, IDC_EDIT_PWM_STATUS0, m_PwmStatus0);
	DDX_Control(pDX, IDC_EDIT_PWM_STATUS1, m_PwmStatus1);
	DDX_Control(pDX, IDC_EDIT_PWM_STATUS2, m_PwmStatus2);
	DDX_Control(pDX, IDC_EDIT_PWM_STATUS3, m_PwmStatus3);
	DDX_Control(pDX, IDC_EDIT_PWM_ERROR0, m_PwmError0);
	DDX_Control(pDX, IDC_EDIT_PWM_ERROR1, m_PwmError1);
	DDX_Control(pDX, IDC_EDIT_PWM_ERROR2, m_PwmError2);
	DDX_Control(pDX, IDC_EDIT_PWM_ERROR3, m_PwmError3);
}

BEGIN_MESSAGE_MAP(Cpwm_outputDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DIO_INIT, &Cpwm_outputDlg::OnBnClickedButtonDioInit)
	ON_BN_CLICKED(IDC_BUTTON_DIO_SET_PWM_PULSE_PERIOD, &Cpwm_outputDlg::OnBnClickedButtonDioSetPwmPulsePeriod)
	ON_BN_CLICKED(IDC_BUTTON_DIO_SET_PWM_PULSE_PHASE, &Cpwm_outputDlg::OnBnClickedButtonDioSetPwmPulsePhase)
	ON_BN_CLICKED(IDC_BUTTON_DIO_SET_PWM_PULSE_DUTY, &Cpwm_outputDlg::OnBnClickedButtonDioSetPwmPulseDuty)
	ON_BN_CLICKED(IDC_BUTTON_DIO_START_PWM, &Cpwm_outputDlg::OnBnClickedButtonDioStartPwm)
	ON_BN_CLICKED(IDC_BUTTON_DIO_GET_PWM_STATUS, &Cpwm_outputDlg::OnBnClickedButtonDioGetPwmStatus)
	ON_BN_CLICKED(IDC_BUTTON_DIO_STOP_PWM, &Cpwm_outputDlg::OnBnClickedButtonDioStopPwm)
	ON_BN_CLICKED(IDC_BUTTON_DIO_EXIT, &Cpwm_outputDlg::OnBnClickedButtonDioExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &Cpwm_outputDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// Cpwm_outputDlg message handlers
//=========================================================
// Dialog initialization
//=========================================================
BOOL Cpwm_outputDlg::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;
	TCHAR	cstr[256];
	//----------------------------------------
	// Set control array
	//----------------------------------------
	//--------------------
	// Pulse period control
	//--------------------
	m_PulsePeriodValue[0]	= &m_PulsePeriodValue0;
	m_PulsePeriodValue[1]	= &m_PulsePeriodValue1;
	m_PulsePeriodValue[2]	= &m_PulsePeriodValue2;
	m_PulsePeriodValue[3]	= &m_PulsePeriodValue3;
	//--------------------
	// Pulse period unit control
	//--------------------
	m_PeriodUnit[0]			= &m_PeriodUnit0;
	m_PeriodUnit[1]			= &m_PeriodUnit1;
	m_PeriodUnit[2]			= &m_PeriodUnit2;
	m_PeriodUnit[3]			= &m_PeriodUnit3;
	//--------------------
	// Pulse duty ratio control
	//--------------------
	m_PulseDutyValue[0]		= &m_PulseDutyValue0;
	m_PulseDutyValue[1]		= &m_PulseDutyValue1;
	m_PulseDutyValue[2]		= &m_PulseDutyValue2;
	m_PulseDutyValue[3]		= &m_PulseDutyValue3;
	//--------------------
	// Pulse phase shift control
	//--------------------
	m_PulsePhase[0]			= &m_PulsePhase0;
	m_PulsePhase[1]			= &m_PulsePhase1;
	m_PulsePhase[2]			= &m_PulsePhase2;
	m_PulsePhase[3]			= &m_PulsePhase3;
	//--------------------
	// PWM status control
	//--------------------
	m_PwmStatus[0]			= &m_PwmStatus0;
	m_PwmStatus[1]			= &m_PwmStatus1;
	m_PwmStatus[2]			= &m_PwmStatus2;
	m_PwmStatus[3]			= &m_PwmStatus3;
	//--------------------
	// PWM error control
	//--------------------
	m_PwmError[0]			= &m_PwmError0;
	m_PwmError[1]			= &m_PwmError1;
	m_PwmError[2]			= &m_PwmError2;
	m_PwmError[3]			= &m_PwmError3;
	//----------------------------------------
	// 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_PWM_CH; ch_no++){
		//--------------------
		// Pulse period control
		//--------------------
		wsprintf(cstr, _T("%u"), (ch_no + 1) * 100);
		m_PulsePeriodValue[ch_no]->SetWindowText(cstr);
		//--------------------
		// Pulse period unit control
		//--------------------
		m_PeriodUnit[ch_no]->AddString(_T("sec"));
		m_PeriodUnit[ch_no]->AddString(_T("msec"));
		m_PeriodUnit[ch_no]->AddString(_T("usec"));
		m_PeriodUnit[ch_no]->AddString(_T("nsec"));
		m_PeriodUnit[ch_no]->SelectString(-1, _T("msec"));
		//--------------------
		// Pulse duty ratio control
		//--------------------
		wsprintf(cstr, _T("%u"), 32767);
		m_PulseDutyValue[ch_no]->SetWindowText(cstr);
		//--------------------
		// Pulse phase shift control
		//--------------------
		m_PulsePhase[ch_no]->AddString(_T("0"));
		m_PulsePhase[ch_no]->AddString(_T("90"));
		m_PulsePhase[ch_no]->AddString(_T("180"));
		m_PulsePhase[ch_no]->SelectString(-1, _T("0"));
		//--------------------
		// PWM status control
		//--------------------
		m_PwmStatus[ch_no]->SetWindowText(_T(""));
		//--------------------
		// PWM error control
		//--------------------
		m_PwmError[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 Cpwm_outputDlg::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 Cpwm_outputDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//=========================================================
// It is the process when the DioInit button is clicked
// Initialize the device
//=========================================================
void Cpwm_outputDlg::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 DioSetPwmPulsePeriod button is clicked
// Set the PWM pulse period of the device
//=========================================================
void Cpwm_outputDlg::OnBnClickedButtonDioSetPwmPulsePeriod()
{
	TCHAR				cstr[256];
	TCHAR				return_string[256];
	char				error_string[256];
	unsigned long long	pulse_period_value[MAX_PWM_CH];
	unsigned short		pulse_period_unit[MAX_PWM_CH];
	short				ch_no_array[MAX_PWM_CH];
	long				ret;
	short				ch_no;
	short				pwm_ch_num;

	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pwm_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pwm_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
		//----------------------------------------
		// Get specified value of PWM pulse period
		//----------------------------------------
		m_PulsePeriodValue[ch_no]->GetWindowText(cstr, _countof(cstr));
		pulse_period_value[ch_no]	= wcstoull(cstr, NULL, 10);
		//----------------------------------------
		// Get specified value of pulse period unit
		//----------------------------------------
		m_PeriodUnit[ch_no]->GetLBText(m_PeriodUnit[ch_no]->GetCurSel(), cstr);
		if(wcscmp(cstr, _T("sec")) == 0){
			pulse_period_unit[ch_no]	= DIO_PWM_PULSE_UNIT_S;
		}else if(wcscmp(cstr, _T("msec")) == 0){
			pulse_period_unit[ch_no]	= DIO_PWM_PULSE_UNIT_MS;
		}else if(wcscmp(cstr, _T("usec")) == 0){
			pulse_period_unit[ch_no]	= DIO_PWM_PULSE_UNIT_US;
		}else{
			pulse_period_unit[ch_no]	= DIO_PWM_PULSE_UNIT_NS;
		}
	}
	//----------------------------------------
	// PWM pulse period setting
	//----------------------------------------
	ret	= DioSetPwmPulsePeriod(dev_id, ch_no_array, pwm_ch_num, pulse_period_value, pulse_period_unit);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioSetPwmPulsePeriod = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioSetPwmPulsePhase button is clicked
// Set the PWM pulse phase shift of the device
//=========================================================
void Cpwm_outputDlg::OnBnClickedButtonDioSetPwmPulsePhase()
{
	TCHAR	cstr[256];
	TCHAR	return_string[256];
	char	error_string[256];
	short	pulse_phase[MAX_PWM_CH];
	short	ch_no_array[MAX_PWM_CH];
	long	ret;
	short	ch_no;
	short	pwm_ch_num;

	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pwm_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pwm_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
		//----------------------------------------
		// Get specified value of PWM pulse phase shift
		//----------------------------------------
		m_PulsePhase[ch_no]->GetLBText(m_PulsePhase[ch_no]->GetCurSel(), cstr);
		if(wcscmp(cstr, _T("0")) == 0){
			pulse_phase[ch_no]	= DIO_PWM_PULSE_PHASE_0_DEGREES;
		}else if(wcscmp(cstr, _T("90")) == 0){
			pulse_phase[ch_no]	= DIO_PWM_PULSE_PHASE_90_DEGREES;
		}else{
			pulse_phase[ch_no]	= DIO_PWM_PULSE_PHASE_180_DEGREES;
		}
	}
	//----------------------------------------
	// PWM pulse phase shift setting
	//----------------------------------------
	ret	= DioSetPwmPulsePhase(dev_id, ch_no_array, pwm_ch_num, pulse_phase);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioSetPwmPulsePhase = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioSetPwmPulseDuty button is clicked
// Set the PWM pulse duty ratio of the device
//=========================================================
void Cpwm_outputDlg::OnBnClickedButtonDioSetPwmPulseDuty()
{
	TCHAR			cstr[256];
	TCHAR			return_string[256];
	char			error_string[256];
	unsigned short	pulse_duty[MAX_PWM_CH];
	short			ch_no_array[MAX_PWM_CH];
	long			ret;
	short			ch_no;
	short			pwm_ch_num;

	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pwm_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pwm_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
		//----------------------------------------
		// Get specified value of PWM pulse duty ratio
		//----------------------------------------
		m_PulseDutyValue[ch_no]->GetWindowText(cstr, _countof(cstr));
		pulse_duty[ch_no]	= (unsigned short)wcstoul(cstr, NULL, 10);
	}
	//----------------------------------------
	// PWM pulse duty ratio setting
	//----------------------------------------
	ret	= DioSetPwmPulseDuty(dev_id, ch_no_array, pwm_ch_num, pulse_duty);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioSetPwmPulseDuty = %d : %S"), ret, error_string);
	m_ReturnCode.SetWindowText(return_string);
}

//=========================================================
// It is the process when the DioStartPwm button is clicked
// Start the PWM pulse output
//=========================================================
void Cpwm_outputDlg::OnBnClickedButtonDioStartPwm()
{
	TCHAR	cstr[256];
	TCHAR	return_string[256];
	char	error_string[256];
	short	ch_no_array[MAX_PWM_CH];
	long	ret;
	short	ch_no;
	short	pwm_ch_num;

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

//=========================================================
// It is the process when the DioGetPwmStatus button is clicked
// Get the PWM status
//=========================================================
void Cpwm_outputDlg::OnBnClickedButtonDioGetPwmStatus()
{
	TCHAR			cstr[256];
	TCHAR			return_string[256];
	char			error_string[256];
	unsigned short	pwm_status[MAX_PWM_CH];
	unsigned short	pwm_error[MAX_PWM_CH];
	short			ch_no_array[MAX_PWM_CH];
	long			ret;
	short			ch_no;
	short			pwm_ch_num;

	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pwm_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pwm_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
	}
	//----------------------------------------
	// Get PWM status
	//----------------------------------------
	ret	= DioGetPwmStatus(dev_id, ch_no_array, pwm_ch_num, pwm_status, pwm_error);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioGetPwmStatus = %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 < pwm_ch_num; ch_no++){
		//----------------------------------------
		// Update the display of PWM status
		//----------------------------------------
		_itow_s(pwm_status[ch_no], cstr, 16);
		m_PwmStatus[ch_no]->SetWindowText(cstr);
		//----------------------------------------
		// Update the display of PWM error
		//----------------------------------------
		_itow_s(pwm_error[ch_no], cstr, 16);
		m_PwmError[ch_no]->SetWindowText(cstr);
	}
}

//=========================================================
// It is the process when the DioStopPwm button is clicked
// Stop the PWM pulse output
//=========================================================
void Cpwm_outputDlg::OnBnClickedButtonDioStopPwm()
{
	TCHAR	cstr[256];
	TCHAR	return_string[256];
	char	error_string[256];
	short	ch_no_array[MAX_PWM_CH];
	long	ret;
	short	ch_no;
	short	pwm_ch_num;

	//----------------------------------------
	// Get number of channels
	//----------------------------------------
	m_ChannelNum.GetLBText(m_ChannelNum.GetCurSel(), cstr);
	pwm_ch_num	= (short)wcstoul(cstr, NULL, 10);
	//----------------------------------------
	// For each channel
	//----------------------------------------
	for(ch_no = 0; ch_no < pwm_ch_num; ch_no++){
		//----------------------------------------
		// Store channel number
		//----------------------------------------
		ch_no_array[ch_no]	= ch_no;
	}
	//----------------------------------------
	// Stop PWM pulse output
	//----------------------------------------
	ret	= DioStopPwm(dev_id, ch_no_array, pwm_ch_num);
	//----------------------------------------
	// Error process
	//----------------------------------------
	DioGetErrorString(ret, error_string);
	wsprintf(return_string, _T("DioStopPwm = %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 Cpwm_outputDlg::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 Cpwm_outputDlg::OnBnClickedButtonExit()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	DioExit(dev_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
