// MultiIODlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiIO.h"
#include "MultiIODlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

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

#define	DIO_MAX_ACCS_PORTS		8		// Maxinum number of access ports in this sample
#define	DIO_MAX_ACCS_BITS		8		// Maxinum number of access bits in this sample


// CMultiIODlg dialog



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

void CMultiIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_DEVICE_NAME, m_DeviceName);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA0, m_InpPortData0);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA1, m_InpPortData1);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA2, m_InpPortData2);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA3, m_InpPortData3);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA4, m_InpPortData4);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA5, m_InpPortData5);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA6, m_InpPortData6);
	DDX_Control(pDX, IDC_EDIT_INP_PORT_DATA7, m_InpPortData7);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA0, m_InpBitData0);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA1, m_InpBitData1);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA2, m_InpBitData2);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA3, m_InpBitData3);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA4, m_InpBitData4);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA5, m_InpBitData5);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA6, m_InpBitData6);
	DDX_Control(pDX, IDC_EDIT_INP_BIT_DATA7, m_InpBitData7);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA0, m_OutPortData0);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA1, m_OutPortData1);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA2, m_OutPortData2);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA3, m_OutPortData3);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA4, m_OutPortData4);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA5, m_OutPortData5);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA6, m_OutPortData6);
	DDX_Control(pDX, IDC_EDIT_OUT_PORT_DATA7, m_OutPortData7);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA0, m_OutBitData0);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA1, m_OutBitData1);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA2, m_OutBitData2);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA3, m_OutBitData3);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA4, m_OutBitData4);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA5, m_OutBitData5);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA6, m_OutBitData6);
	DDX_Control(pDX, IDC_EDIT_OUT_BIT_DATA7, m_OutBitData7);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_ReturnCode);
}

BEGIN_MESSAGE_MAP(CMultiIODlg, 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_DIO_INP_MULTI_BYTE, OnBnClickedButtonDioInpMultiByte)
	ON_BN_CLICKED(IDC_BUTTON_DIO_INP_MULTI_BIT, OnBnClickedButtonDioInpMultiBit)
	ON_BN_CLICKED(IDC_BUTTON_DIO_OUT_MULTI_BYTE, OnBnClickedButtonDioOutMultiByte)
	ON_BN_CLICKED(IDC_BUTTON_DIO_OUT_MULTI_BIT, OnBnClickedButtonDioOutMultiBit)
END_MESSAGE_MAP()


// CMultiIODlg message handlers

//---------------------------------------------------------
//	Initialize dialog
//---------------------------------------------------------
BOOL CMultiIODlg::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
	//-----------------------------------
	// Port input
	m_InpPortData0.SetWindowText(_T(""));
	m_InpPortData1.SetWindowText(_T(""));
	m_InpPortData2.SetWindowText(_T(""));
	m_InpPortData3.SetWindowText(_T(""));
	m_InpPortData4.SetWindowText(_T(""));
	m_InpPortData5.SetWindowText(_T(""));
	m_InpPortData6.SetWindowText(_T(""));
	m_InpPortData7.SetWindowText(_T(""));
	m_InpPort[0] = &m_InpPortData0;
	m_InpPort[1] = &m_InpPortData1;
	m_InpPort[2] = &m_InpPortData2;
	m_InpPort[3] = &m_InpPortData3;
	m_InpPort[4] = &m_InpPortData4;
	m_InpPort[5] = &m_InpPortData5;
	m_InpPort[6] = &m_InpPortData6;
	m_InpPort[7] = &m_InpPortData7;
	// Bit input
	m_InpBitData0.SetWindowText(_T(""));
	m_InpBitData1.SetWindowText(_T(""));
	m_InpBitData2.SetWindowText(_T(""));
	m_InpBitData3.SetWindowText(_T(""));
	m_InpBitData4.SetWindowText(_T(""));
	m_InpBitData5.SetWindowText(_T(""));
	m_InpBitData6.SetWindowText(_T(""));
	m_InpBitData7.SetWindowText(_T(""));
	m_InpBit[0] = &m_InpBitData0;
	m_InpBit[1] = &m_InpBitData1;
	m_InpBit[2] = &m_InpBitData2;
	m_InpBit[3] = &m_InpBitData3;
	m_InpBit[4] = &m_InpBitData4;
	m_InpBit[5] = &m_InpBitData5;
	m_InpBit[6] = &m_InpBitData6;
	m_InpBit[7] = &m_InpBitData7;
	// Port output
	m_OutPortData0.SetWindowText(_T("11"));
	m_OutPortData1.SetWindowText(_T("22"));
	m_OutPortData2.SetWindowText(_T("33"));
	m_OutPortData3.SetWindowText(_T("44"));
	m_OutPortData4.SetWindowText(_T("55"));
	m_OutPortData5.SetWindowText(_T("66"));
	m_OutPortData6.SetWindowText(_T("77"));
	m_OutPortData7.SetWindowText(_T("88"));
	m_OutPort[0] = &m_OutPortData0;
	m_OutPort[1] = &m_OutPortData1;
	m_OutPort[2] = &m_OutPortData2;
	m_OutPort[3] = &m_OutPortData3;
	m_OutPort[4] = &m_OutPortData4;
	m_OutPort[5] = &m_OutPortData5;
	m_OutPort[6] = &m_OutPortData6;
	m_OutPort[7] = &m_OutPortData7;
	// Bit output
	m_OutBitData0.SetWindowText(_T("1"));
	m_OutBitData1.SetWindowText(_T("0"));
	m_OutBitData2.SetWindowText(_T("1"));
	m_OutBitData3.SetWindowText(_T("0"));
	m_OutBitData4.SetWindowText(_T("1"));
	m_OutBitData5.SetWindowText(_T("0"));
	m_OutBitData6.SetWindowText(_T("1"));
	m_OutBitData7.SetWindowText(_T("0"));
	m_OutBit[0] = &m_OutBitData0;
	m_OutBit[1] = &m_OutBitData1;
	m_OutBit[2] = &m_OutBitData2;
	m_OutBit[3] = &m_OutBitData3;
	m_OutBit[4] = &m_OutBitData4;
	m_OutBit[5] = &m_OutBitData5;
	m_OutBit[6] = &m_OutBitData6;
	m_OutBit[7] = &m_OutBitData7;
	// Return value
	m_ReturnCode.SetWindowText(_T(""));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//---------------------------------------------------------
//	Initialization
//---------------------------------------------------------
void CMultiIODlg::OnBnClickedButtonDioInit()
{
	// TODO: Add your control notification handler code here
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	short	i;
	//-----------------------------
	// 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);
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Get maximum number of ports
	//-----------------------------
	Ret = DioGetMaxPorts(Id, &InpPortNum, &OutPortNum);
	if (Ret != DIO_ERR_SUCCESS) {
		DioGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("DioGetMaxPorts : %S"), szError);
		m_ReturnCode.SetWindowText(szReturnCode);
		return;
	}
	// Up to 8 ports in this sample
	if (InpPortNum > DIO_MAX_ACCS_PORTS) {
		InpPortNum = DIO_MAX_ACCS_PORTS;
	}
	if (OutPortNum > DIO_MAX_ACCS_PORTS) {
		OutPortNum = DIO_MAX_ACCS_PORTS;
	}
	//-----------------------------
	// Get maximum number of bits
	//-----------------------------
	Ret = DioGetMaxBits(Id, &InpBitNum, &OutBitNum);
	if (Ret != DIO_ERR_SUCCESS) {
		DioGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("DioGetMaxBits : %S"), szError);
		m_ReturnCode.SetWindowText(szReturnCode);
		return;
	}
	// Up to 8 bits in this sample
	if (InpBitNum > DIO_MAX_ACCS_BITS) {
		InpBitNum = DIO_MAX_ACCS_BITS;
	}
	if (OutBitNum > DIO_MAX_ACCS_BITS) {
		OutBitNum = DIO_MAX_ACCS_BITS;
	}
	//-----------------------------
	// Mask screen
	//-----------------------------
	for (i=0; i<DIO_MAX_ACCS_PORTS; i++) {
		if (i < InpPortNum) {
			m_InpPort[i]->EnableWindow(TRUE);
		} else {
			m_InpPort[i]->EnableWindow(FALSE);
		}
	}
	for (i=0; i<DIO_MAX_ACCS_BITS; i++) {
		if (i < InpBitNum) {
			m_InpBit[i]->EnableWindow(TRUE);
		} else {
			m_InpBit[i]->EnableWindow(FALSE);
		}
	}
	for (i=0; i<DIO_MAX_ACCS_PORTS; i++) {
		if (i < OutPortNum) {
			m_OutPort[i]->EnableWindow(TRUE);
		} else {
			m_OutPort[i]->EnableWindow(FALSE);
		}
	}
	for (i=0; i<DIO_MAX_ACCS_BITS; i++) {
		if (i < OutBitNum) {
			m_OutBit[i]->EnableWindow(TRUE);
		} else {
			m_OutBit[i]->EnableWindow(FALSE);
		}
	}
}

//---------------------------------------------------------
//	Termination
//---------------------------------------------------------
void CMultiIODlg::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 CMultiIODlg::OnBnClickedButtonExit()
{
	// TODO: Add your control notification handler code here
	//-----------------------------
	// Termination
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Exit dialog
	//-----------------------------
	CDialog::OnOK();
}

//---------------------------------------------------------
//	MultiPort input
//---------------------------------------------------------
void CMultiIODlg::OnBnClickedButtonDioInpMultiByte()
{
	// TODO: Add your control notification handler code here
	short	InpPortNo[DIO_MAX_ACCS_PORTS];
	BYTE	InpPortData[DIO_MAX_ACCS_PORTS];
	short	i;
	//-----------------------------
	// Set port No.
	//-----------------------------
	for (i=0; i<InpPortNum; i++) {
		InpPortNo[i] = i;
	}
	//-----------------------------
	// Port input
	//-----------------------------
	Ret = DioInpMultiByte(Id, &InpPortNo[0], InpPortNum, &InpPortData[0]);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioInpMultiByte : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Display data
	//-----------------------------
	for (i=0; i<InpPortNum; i++) {
		wsprintf(szText, _T("%02X"), InpPortData[i]);
		m_InpPort[i]->SetWindowText(szText);
	}
}

//---------------------------------------------------------
//	MultiBit input
//---------------------------------------------------------
void CMultiIODlg::OnBnClickedButtonDioInpMultiBit()
{
	// TODO: Add your control notification handler code here
	short	InpBitNo[DIO_MAX_ACCS_BITS];
	BYTE	InpBitData[DIO_MAX_ACCS_BITS];
	short	i;
	//-----------------------------
	// Set bit No.
	//-----------------------------
	for (i=0; i<InpBitNum; i++) {
		InpBitNo[i] = i;
	}
	//-----------------------------
	// Bit input
	//-----------------------------
	Ret = DioInpMultiBit(Id, &InpBitNo[0], InpBitNum, &InpBitData[0]);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioInpMultiBit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------
	// Display data
	//-----------------------------
	for (i=0; i<InpBitNum; i++) {
		wsprintf(szText, _T("%X"), InpBitData[i]);
		m_InpBit[i]->SetWindowText(szText);
	}
}

//---------------------------------------------------------
//	MultiPort output
//---------------------------------------------------------
void CMultiIODlg::OnBnClickedButtonDioOutMultiByte()
{
	// TODO: Add your control notification handler code here
	short	OutPortNo[DIO_MAX_ACCS_PORTS];
	BYTE	OutPortData[DIO_MAX_ACCS_PORTS];
	short	i;
	DWORD	dwTmp;
	//-----------------------------
	// Set port No. and port data
	//-----------------------------
	for (i=0; i<OutPortNum; i++) {
		OutPortNo[i] = i;
		m_OutPort[i]->GetWindowText(szText, sizeof(szText));
		swscanf_s(szText, _T("%X"), &dwTmp);
		OutPortData[i] = (BYTE)dwTmp;
	}
	//-----------------------------
	// Port output
	//-----------------------------
	Ret = DioOutMultiByte(Id, &OutPortNo[0], OutPortNum, &OutPortData[0]);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioOutMultiByte : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}

//---------------------------------------------------------
//	MultiBit output
//---------------------------------------------------------
void CMultiIODlg::OnBnClickedButtonDioOutMultiBit()
{
	// TODO: Add your control notification handler code here
	short	OutBitNo[DIO_MAX_ACCS_BITS];
	BYTE	OutBitData[DIO_MAX_ACCS_BITS];
	short	i;
	DWORD	dwTmp;
	//-----------------------------
	// Set bit No. and bit data
	//-----------------------------
	for (i=0; i<OutBitNum; i++) {
		OutBitNo[i] = i;
		m_OutBit[i]->GetWindowText(szText, sizeof(szText));
		swscanf_s(szText, _T("%X"), &dwTmp);
		OutBitData[i] = (BYTE)dwTmp;
	}
	//-----------------------------
	// Bit output
	//-----------------------------
	Ret = DioOutMultiBit(Id, &OutBitNo[0], OutBitNum, &OutBitData[0]);
	//-----------------------------
	// Process error
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("DioOutMultiBit : %S"), szError);
	m_ReturnCode.SetWindowText(szReturnCode);	
	if (Ret != DIO_ERR_SUCCESS) {
		return;
	}
}