// SimpleIODlg.cpp : implementation file
//

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

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// Digital I/O header file
//---------------------------------------------------------
#include "ccapdio.h"

// CSimpleIODlg dialog



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

void CSimpleIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_DEVICE_NAME, m_DeviceName);
	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_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_RETURN_CODE, m_ReturnCode);
}

BEGIN_MESSAGE_MAP(CSimpleIODlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_DIO_INIT, OnBnClickedButtonDioInit)
	ON_BN_CLICKED(IDC_BUTTON_DIO_EXIT, OnBnClickedButtonDioExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnBnClickedButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_INP_BYTE, OnBnClickedButtonInpByte)
	ON_BN_CLICKED(IDC_BUTTON_INP_BIT, OnBnClickedButtonInpBit)
	ON_BN_CLICKED(IDC_BUTTON_OUT_BYTE, OnBnClickedButtonOutByte)
	ON_BN_CLICKED(IDC_BUTTON_OUT_BIT, OnBnClickedButtonOutBit)
	ON_BN_CLICKED(IDC_BUTTON_ECHO_BACK_BYTE, OnBnClickedButtonEchoBackByte)
	ON_BN_CLICKED(IDC_BUTTON_ECHO_BACK_BIT, OnBnClickedButtonEchoBackBit)
END_MESSAGE_MAP()


// CSimpleIODlg message handlers

//---------------------------------------------------------
//	Initialize dialog
//---------------------------------------------------------	
BOOL CSimpleIODlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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

	// TODO: Add extra initialization here
	//-----------------------------------
	// Detect device
	//-----------------------------------
	short	Index;
	short	GroupID, UnitID, DeviceID;
	char	DeviceName[256], Device[256];
	TCHAR	szDeviceName[256];

	for (Index=0; ; Index++) {
		Ret = DioQueryDeviceName(Index, &GroupID, &UnitID, &DeviceID, DeviceName, Device);
		if (Ret != DIO_ERR_SUCCESS) {
			break;
		}
		wsprintf(szDeviceName, _T("%S"), DeviceName);
		m_DeviceName.AddString(szDeviceName);
	}
	m_DeviceName.SetCurSel(0);
	//-----------------------------------
	// Initialize screen items
	//-----------------------------------
	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
}

void CSimpleIODlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	CDialog::OnSysCommand(nID, lParam);
	
}

// 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
	{
		CDialog::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
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonDioInit()
{
	// TODO: Add your control notification handler code here
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	//-----------------------------
	// Get device name from screen
	//-----------------------------
	m_DeviceName.GetWindowText(szDeviceName, sizeof(DeviceName));
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------
	// Initialize and get ID
	//-----------------------------
	Ret = DioInit(DeviceName, &Id);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioInit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
//	Termination
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonDioExit()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Termination
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioExit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
//	Exit dialog
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonExit()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Termination
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Exit dialog
	//-----------------------------
	CDialog::OnOK();
}

//---------------------------------------------------------
//	1 port input
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonInpByte()
{
	// TODO: Add your control notification handler code here
	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);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioInpByte : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Display data
	//-----------------------------
	wsprintf(szText, _T("%02X"), InpPortData);
	m_InpPortData.SetWindowText(szText);
}

//---------------------------------------------------------
//	1 bit input
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonInpBit()
{
	// TODO: Add your control notification handler code here
	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);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioInpBit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Display data
	//-----------------------------
	wsprintf(szText, _T("%X"), InpBitData);
	m_InpBitData.SetWindowText(szText);
}

//---------------------------------------------------------
//	1 port output
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonOutByte()
{
	// TODO: Add your control notification handler code here
	short	OutPortNo;
	BYTE	OutPortData;
	DWORD	dwTmp;
	//-----------------------------
	// Get the port No. and the output data from screen
	//-----------------------------
	m_OutPortNo.GetWindowText(szText, sizeof(szText));
	OutPortNo = (short)_ttoi(szText);
	m_OutPortData.GetWindowText(szText, sizeof(szText));
	swscanf_s(szText, _T("%X"), &dwTmp);
	OutPortData = (BYTE)dwTmp;
	//-----------------------------
	// Port output
	//-----------------------------
	Ret = DioOutByte(Id, OutPortNo, OutPortData);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioOutByte : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
//	1 bit output
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonOutBit()
{
	// TODO: Add your control notification handler code here
	short	OutBitNo;
	BYTE	OutBitData;
	DWORD	dwTmp;
	//-----------------------------
	// Get the bit No. and the output data from screen
	//-----------------------------
	m_OutBitNo.GetWindowText(szText, sizeof(szText));
	OutBitNo = (short)_ttoi(szText);
	m_OutBitData.GetWindowText(szText, sizeof(szText));
	swscanf_s(szText, _T("%X"), &dwTmp);
	OutBitData = (BYTE)dwTmp;
	//-----------------------------
	// Bit output
	//-----------------------------
	Ret = DioOutBit(Id, OutBitNo, OutBitData);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioOutBit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
//	1 port EchoBack
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonEchoBackByte()
{
	// TODO: Add your control notification handler code here
	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);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioEchoBackByte : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Display data
	//-----------------------------
	wsprintf(szText, _T("%02X"), OutPortData);
	m_OutPortData.SetWindowText(szText);
}

//---------------------------------------------------------
//	1 Bit EchoBack
//---------------------------------------------------------
void CSimpleIODlg::OnBnClickedButtonEchoBackBit()
{
	// TODO: Add your control notification handler code here
	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);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioEchoBackBit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Display data
	//-----------------------------
	wsprintf(szText, _T("%X"), OutBitData);
	m_OutBitData.SetWindowText(szText);
}
