
// SimpleIODlg.cpp : implementation file
//

#include "stdafx.h"
#include "SimpleIO.h"
#include "SimpleIODlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// DIO Header File
//---------------------------------------------------------
#include "cdio.h"

// CSimpleIODlg dialog



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

void CSimpleIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DIR_DATA, m_DirData);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_ReturnCode);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_NO, m_OutPortNo);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA, m_OutPortData);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_NO, m_OutBitNo);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA, m_OutBitData);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_NO, m_InpPortNo);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA, m_InpPortData);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_NO, m_InpBitNo);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA, m_InpBitData);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_DeviceName);
}

BEGIN_MESSAGE_MAP(CSimpleIODlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DIO_INIT, &CSimpleIODlg::OnBnClickedButtonDioInit)
	ON_BN_CLICKED(IDC_BUTTON_DIO_SET_IO_DIRECTION, &CSimpleIODlg::OnBnClickedButtonDioSetIoDirection)
	ON_BN_CLICKED(IDC_BUTTON_INP_BYTE, &CSimpleIODlg::OnBnClickedButtonInpByte)
	ON_BN_CLICKED(IDC_BUTTON_INP_BIT, &CSimpleIODlg::OnBnClickedButtonInpBit)
	ON_BN_CLICKED(IDC_BUTTON_OUT_BYTE, &CSimpleIODlg::OnBnClickedButtonOutByte)
	ON_BN_CLICKED(IDC_BUTTON_OUT_BIT, &CSimpleIODlg::OnBnClickedButtonOutBit)
	ON_BN_CLICKED(IDC_BUTTON_ECHO_BACK_BYTE, &CSimpleIODlg::OnBnClickedButtonEchoBackByte)
	ON_BN_CLICKED(IDC_BUTTON_ECHO_BACK_BIT, &CSimpleIODlg::OnBnClickedButtonEchoBackBit)
	ON_BN_CLICKED(IDC_BUTTON_DIO_EXIT, &CSimpleIODlg::OnBnClickedButtonDioExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CSimpleIODlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CSimpleIODlg message handlers

//---------------------------------------------------------
// Dialog Initialization
//--------------------------------------------------------
BOOL CSimpleIODlg::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

	//-----------------------------------
	// Screen Item Initialization
	//-----------------------------------
	m_DeviceName.SetWindowText(_T("DIO000"));
	m_InpPortNo.SetWindowText(_T("0"));
	m_InpBitNo.SetWindowText(_T("0"));
	m_OutPortNo.SetWindowText(_T("0"));
	m_OutBitNo.SetWindowText(_T("0"));
	m_OutPortData.SetWindowText(_T("55"));
	m_OutBitData.SetWindowText(_T("1"));
	m_ReturnCode.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 CSimpleIODlg::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 CSimpleIODlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//---------------------------------------------------------
// Initialization Process
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonDioInit()
{
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	//-----------------------------
	// Get device name from screen
	//-----------------------------
	m_DeviceName.GetWindowText(szDeviceName, sizeof(szDeviceName));
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------
	// Execute initialization function and get ID
	//-----------------------------
	Ret = DioInit(DeviceName, &Id);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Set IoDirection
// It can be used only by the devices support the function.
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonDioSetIoDirection()
{
	DWORD	dwDir;

	//-----------------------------
	// Get IoDirection
	//-----------------------------
	m_DirData.GetWindowText(szText, sizeof(szText));
	_stscanf_s(szText, _T("%X"), &dwDir);
	//-----------------------------
	// Set IoDirection
	//-----------------------------
	Ret = DioSetIoDirection(Id, dwDir);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
// 1 port input
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonInpByte()
{
	short	InpPortNo;
	BYTE	InpPortData;
	//-----------------------------
	// Get port No. from screen
	//-----------------------------
	m_InpPortNo.GetWindowText(szText, sizeof(szText));
	InpPortNo = (short)_ttoi(szText);
	//-----------------------------
	// Port input
	//-----------------------------
	Ret = DioInpByte(Id, InpPortNo, &InpPortData);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Data express
	//-----------------------------
	wsprintf(szText, _T("%02X"), InpPortData);
	m_InpPortData.SetWindowText(szText);
}

//---------------------------------------------------------
// 1 bit input
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonInpBit()
{
	short	InpBitNo;
	BYTE	InpBitData;
	//-----------------------------
	// Get bit No. from screen
	//-----------------------------
	m_InpBitNo.GetWindowText(szText, sizeof(szText));
	InpBitNo = (short)_ttoi(szText);
	//-----------------------------
	// Bit input
	//-----------------------------
	Ret = DioInpBit(Id, InpBitNo, &InpBitData);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Data express
	//-----------------------------
	wsprintf(szText, _T("%02X"), InpBitData);
	m_InpBitData.SetWindowText(szText);
}

//---------------------------------------------------------
// 1 port output
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonOutByte()
{
	short	OutPortNo;
	BYTE	OutPortData;
	DWORD	dwTmp;
	//-----------------------------
	// Get port No. and output data from screen
	//-----------------------------
	m_OutPortNo.GetWindowText(szText, sizeof(szText));
	OutPortNo = (short)_ttoi(szText);
	m_OutPortData.GetWindowText(szText, sizeof(szText));
	_stscanf_s(szText, _T("%X"), &dwTmp);
	OutPortData = (BYTE)dwTmp;
	//-----------------------------
	// Port output
	//-----------------------------
	Ret = DioOutByte(Id, OutPortNo, OutPortData);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
// 1 bit output
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonOutBit()
{
	short	OutBitNo;
	BYTE	OutBitData;
	DWORD	dwTmp;
	//-----------------------------
	// Get bit No. and output data from screen
	//-----------------------------
	m_OutBitNo.GetWindowText(szText, sizeof(szText));
	OutBitNo = (short)_ttoi(szText);
	m_OutBitData.GetWindowText(szText, sizeof(szText));
	_stscanf_s(szText, _T("%X"), &dwTmp);
	OutBitData = (BYTE)dwTmp;
	//-----------------------------
	// Bit output
	//-----------------------------
	Ret = DioOutBit(Id, OutBitNo, OutBitData);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
// 1 port EchoBack
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonEchoBackByte()
{
	short	OutPortNo;
	BYTE	OutPortData;
	//-----------------------------
	// Get port No. from screen
	//-----------------------------
	m_OutPortNo.GetWindowText(szText, sizeof(szText));
	OutPortNo = (short)_ttoi(szText);
	//-----------------------------
	// Port EchoBack
	//-----------------------------
	Ret = DioEchoBackByte(Id, OutPortNo, &OutPortData);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Data express
	//-----------------------------
	wsprintf(szText, _T("%02X"), OutPortData);
	m_OutPortData.SetWindowText(szText);
}

//---------------------------------------------------------
// 1 Bit EchoBack
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonEchoBackBit()
{
	short	OutBitNo;
	BYTE	OutBitData;
	//-----------------------------
	// Get bit No. from screen
	//-----------------------------
	m_OutBitNo.GetWindowText(szText, sizeof(szText));
	OutBitNo = (short)_ttoi(szText);
	//-----------------------------
	// Bit EchoBack
	//-----------------------------
	Ret = DioEchoBackBit(Id, OutBitNo, &OutBitData);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Data Express
	//-----------------------------
	wsprintf(szText, _T("%02X"), OutBitData);
	m_OutBitData.SetWindowText(szText);
}

//---------------------------------------------------------
// Exit process
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonDioExit()
{
	//-----------------------------
	// Exit process execute
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Dialog exit
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonExit()
{
	//-----------------------------
	// Exit function execute
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Dialog exit
	//-----------------------------
	CDialogEx::OnOK();
}
