﻿
// DigitalIODlg.cpp : implementation file
//

#include "stdafx.h"
#include "DigitalIO.h"
#include "DigitalIODlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDigitalIODlg dialog



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

void CDigitalIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_EDIT_IO_DIRECTION, m_io_direction);
	DDX_Control(pDX, IDC_EDIT_INPUT_BYTE_NO, m_input_byte_no);
	DDX_Control(pDX, IDC_EDIT_INPUT_BIT_NO, m_input_bit_no);
	DDX_Control(pDX, IDC_EDIT_OUTPUT_BYTE_NO, m_output_byte_no);
	DDX_Control(pDX, IDC_EDIT_OUTPUT_BIT_NO, m_output_bit_no);
	DDX_Control(pDX, IDC_EDIT_INPUT_BYTE_DATA, m_input_byte_data);
	DDX_Control(pDX, IDC_EDIT_INPUT_BIT_DATA, m_input_bit_data);
	DDX_Control(pDX, IDC_EDIT_OUTPUT_BYTE_DATA, m_output_byte_data);
	DDX_Control(pDX, IDC_EDIT_OUTPUT_BIT_DATA, m_output_bit_data);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CDigitalIODlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CDigitalIODlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_AIO_RESET_DEVICE, &CDigitalIODlg::OnBnClickedButtonAioResetDevice)
	ON_BN_CLICKED(IDC_BUTTON_SET_DIO_DIRECTION, &CDigitalIODlg::OnBnClickedButtonSetDioDirection)
	ON_BN_CLICKED(IDC_BUTTON_INPUT_DI_BYTE, &CDigitalIODlg::OnBnClickedButtonInputDiByte)
	ON_BN_CLICKED(IDC_BUTTON_INPUT_DI_BIT, &CDigitalIODlg::OnBnClickedButtonInputDiBit)
	ON_BN_CLICKED(IDC_BUTTON_OUTPUT_DO_BYTE, &CDigitalIODlg::OnBnClickedButtonOutputDoByte)
	ON_BN_CLICKED(IDC_BUTTON_OUTPUT_DO_BIT, &CDigitalIODlg::OnBnClickedButtonOutputDoBit)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CDigitalIODlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CDigitalIODlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;						// ID for device access

// CDigitalIODlg message handlers

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

	//----------------------------------------
	// Update the initial display of device name
	//----------------------------------------
	m_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Update the initial display of I/O direction of digital I/O ports
	//----------------------------------------
	m_io_direction.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of the specified digital input port No.
	//----------------------------------------
	m_input_byte_no.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of the specified digital input port data
	//----------------------------------------
	m_input_byte_data.SetWindowTextW(_T("00"));
	//----------------------------------------
	// Update the initial display of the specified digital input bit No.
	//----------------------------------------
	m_input_bit_no.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of the specified digital input bit data
	//----------------------------------------
	m_input_bit_data.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of the specified digital output port No.
	//----------------------------------------
	m_output_byte_no.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of the specified digital output port data
	//----------------------------------------
	m_output_byte_data.SetWindowTextW(_T("00"));
	//----------------------------------------
	// Update the initial display of the specified digital output bit No.
	//----------------------------------------
	m_output_bit_no.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of the specified digital output bit data
	//----------------------------------------
	m_output_bit_data.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_id	= 0;

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


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CDigitalIODlg::OnBnClickedButtonAioInit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	device_name[256];	// Device name
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Device initialization
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioInit(device_name, &g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioResetDevice button is clicked
// Reset the device
//================================================================================
void CDigitalIODlg::OnBnClickedButtonAioResetDevice()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Reset device
	//----------------------------------------
	ret	= AioResetDevice(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioResetDevice = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSetDioDirection button is clicked
// Set I/O direction of digital I/O ports
//================================================================================
void CDigitalIODlg::OnBnClickedButtonSetDioDirection()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	long	io_direction;		// I/O direction

	//----------------------------------------
	// Get I/O direction from textbox
	//----------------------------------------
	m_io_direction.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%d"), &io_direction);
	//----------------------------------------
	// Set I/O direction of digital I/O ports
	//----------------------------------------
	ret	= AioSetDioDirection(g_id, io_direction);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetDioDirection = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioInputDiByte button is clicked
// Input data in byte from the specified digital input port
//================================================================================
void CDigitalIODlg::OnBnClickedButtonInputDiByte()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	short	di_port;			// Digital input port No.
	short	di_data;			// Input data

	//----------------------------------------
	// Get port No. from textbox
	//----------------------------------------
	m_input_byte_no.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &di_port);
	//----------------------------------------
	// Get specified port data
	//----------------------------------------
	ret	= AioInputDiByte(g_id, di_port, &di_data);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Get error string
	//--------------------
	AioGetErrorString(ret, error_string);
	//--------------------
	// Display message
	//--------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInputDiByte = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		return;
	}
	//----------------------------------------
	// Display specified port data
	//----------------------------------------
	text_string.Format(_T("%02hx"), di_data);
	//--------------------
	// Update the display of conversion data
	//--------------------
	m_input_byte_data.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioInputDiBit button is clicked
// Input data from the specified digital input bit
//================================================================================
void CDigitalIODlg::OnBnClickedButtonInputDiBit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	short	di_bit;				// Digital input bit No.
	short	di_data;			// Input data

	//----------------------------------------
	// Get bit No. from textbox
	//----------------------------------------
	m_input_bit_no.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &di_bit);
	//----------------------------------------
	// Get specified bit data
	//----------------------------------------
	ret	= AioInputDiBit(g_id, di_bit, &di_data);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Get error string
	//--------------------
	AioGetErrorString(ret, error_string);
	//--------------------
	// Display message
	//--------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInputDiBit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		return;
	}
	//----------------------------------------
	// Display specified bit data
	//----------------------------------------
	text_string.Format(_T("%d"), di_data);
	//--------------------
	// Update the display of conversion data
	//--------------------
	m_input_bit_data.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioOutputDoByte button is clicked
// Output data to specified digital output byte
//================================================================================
void CDigitalIODlg::OnBnClickedButtonOutputDoByte()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	short	do_port;			// Digital output port No.
	short	do_data;			// Output data

	//----------------------------------------
	// Get port No. from textbox
	//----------------------------------------
	m_output_byte_no.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &do_port);
	//----------------------------------------
	// Get output data from textbox
	//----------------------------------------
	m_output_byte_data.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hx"), &do_data);
	//----------------------------------------
	// Output data to specified digital output byte
	//----------------------------------------
	ret	= AioOutputDoByte(g_id, do_port, do_data);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioOutputDoByte = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioOutputDoBit button is clicked
// Output data to specified digital output bit
//================================================================================
void CDigitalIODlg::OnBnClickedButtonOutputDoBit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	short	do_bit;				// Digital output bit No.
	short	do_data;			// Output data

	//----------------------------------------
	// Get bit No. from textbox
	//----------------------------------------
	m_output_bit_no.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &do_bit);
	//----------------------------------------
	// Get output data from textbox
	//----------------------------------------
	m_output_bit_data.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &do_data);
	//----------------------------------------
	// Output data to specified digital output bit
	//----------------------------------------
	ret	= AioOutputDoBit(g_id, do_bit, do_data);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioOutputDoBit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioExit button is clicked
// Perform the exit process of device
//================================================================================
void CDigitalIODlg::OnBnClickedButtonAioExit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Exit process
	//----------------------------------------
	ret	= AioExit(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioExit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Close button is clicked
// Perform the exit process of device and terminate the application
//================================================================================
void CDigitalIODlg::OnBnClickedClose()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
