// DO_API_TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DO_API_Test.h"
#include "DO_API_TestDlg.h"
#include "..\..\..\Includes\ICPDAS_USBIO.h"
#include <SSTREAM>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

ICPDAS_USBIO USBIO;
BYTE m_byDITotal;
BYTE m_byDOTotal;
BYTE m_byAITotal;
BYTE m_byAOTotal;
BYTE m_byPITotal;
BYTE m_byPOTotal;

/////////////////////////////////////////////////////////////////////////////
// CDO_API_TestDlg dialog

CDO_API_TestDlg::CDO_API_TestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDO_API_TestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDO_API_TestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDO_API_TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDO_API_TestDlg)
	DDX_Control(pDX, IDC_TXTSETSAFETYVAL, m_txtSafetyVal);
	DDX_Control(pDX, IDC_TXTSETSAFETYENABLE, m_txtSafetyEnable);
	DDX_Control(pDX, IDC_TXTSETPWRONENABLECH, m_txtSetPwrOnEnableCh);
	DDX_Control(pDX, IDC_TXTSETPWRONENABLE, m_txtSetPwrOnEnable);
	DDX_Control(pDX, IDC_TXTBOARDID, m_txtUserDefinedBID);
	DDX_Control(pDX, IDC_TXTTIMEOUT, m_txtWDTTimeout);
	DDX_Control(pDX, IDC_TXTNICKNAME, m_txtNickname);
	DDX_Control(pDX, IDC_TXTBID, m_txtBID);
	DDX_Control(pDX, IDC_LBMODULEINFO, m_lbModuleInfo);
	DDX_Control(pDX, IDC_CBXDID, m_cbxDID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDO_API_TestDlg, CDialog)
	//{{AFX_MSG_MAP(CDO_API_TestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOPEN, OnOpenDevice)
	ON_BN_CLICKED(IDOPEN2, OnCloseDevice)
	ON_BN_CLICKED(IDOPEN3, OnLoadDefault)
	ON_BN_CLICKED(IDOPEN4, OnRefreshInfo)
	ON_BN_CLICKED(IDSETNICKNAME, OnSetNickname)
	ON_BN_CLICKED(IDSETWDTTIMEOUT, OnSetWDTTimeout)
	ON_BN_CLICKED(IDSETBOARDID, OnSetBoardID)
	ON_BN_CLICKED(IDGETPWRONENABLE, OnGetPWRONEnable)
	ON_BN_CLICKED(IDGETSAFETYENABLE, OnGetSafetyEnable)
	ON_BN_CLICKED(IDGETSAFETYVAL, OnGetSafetyVal)
	ON_BN_CLICKED(IDSETPWRONENABLE, OnSetPWRONEnable)
	ON_BN_CLICKED(IDSETSAFETYENABLE, OnSetSafetyEnable)
	ON_BN_CLICKED(IDSETSAFETYVAL, OnSetSafetyVal)
	ON_BN_CLICKED(IDCLOSE, OnCloseDevice)
	ON_BN_CLICKED(IDLOADDEFAULT, OnLoadDefault)
	ON_BN_CLICKED(IDREFRESHINFO, OnRefreshInfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDO_API_TestDlg message handlers

BOOL CDO_API_TestDlg::OnInitDialog()
{
	CDialog::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

  m_txtBID.SetWindowText("1");
  m_cbxDID.SetCurSel(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 CDO_API_TestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDO_API_TestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDO_API_TestDlg::OnOpenDevice() 
{
  CString szBID;
  DWORD dwBID;
  int iSelIdx;
  int iModuleID;
  int iErrCode;
  
  m_txtBID.GetWindowText(szBID);
  dwBID = _tcstoul(szBID, NULL, 10);
  
  iSelIdx = m_cbxDID.GetCurSel();
  
  switch (iSelIdx)
  {
  case 0:
	  iModuleID = USB2026;
	  break;
  case 1:
      iModuleID = USB2045;
	  break;
  case 2:
      iModuleID = USB2055;
	  break;
  case 3:
      iModuleID = USB2060;
	  break;
  case 4:
	  iModuleID = USB2064;
	  break;
  case 5:
	  iModuleID = USB2045_32;
	  break;
  case 6:
	  iModuleID = USB2055_32;
	  break;
  case 7:
	  iModuleID = USB2064_16;
	  break;
  case 8:
	  iModuleID = USB2068_18;
	  break;
  case 9:
	  iModuleID = USB2255_32;
    break;
  case 10:
	  iModuleID = USB2255_64;
	  break;
  case 11:
	  iModuleID = USB2261_16;
	  break;
  case 12:
	  iModuleID = USB2261_32;
	  break;
  case 13:
	  iModuleID = USB2260_48;
	  break;
  case 14:
	  iModuleID = USB2260B_48;
	  break;
  case 15:
	  iModuleID = USB2260C_48;
	  break;
  case 16:
	  iModuleID = USB2260_24;
	  break;
  case 17:
	  iModuleID = USB2260B_24;
	  break;
  case 18:
	  iModuleID = USB2260C_24;
	  break;
  }

  USBIO.SetCommTimeout(500);
  if(ERR_NO_ERR != (iErrCode = USBIO.OpenDevice(iModuleID, (BYTE)dwBID)))
  {
    char szMessage[10];
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    char szItemString[200];
    DWORD dwSoftWDTTimeout;
    WORD wFWVer;
    BYTE byDeviceSN[USBIO_SN_LENGTH];
    BYTE byDeviceNickName[USBIO_NICKNAME_LENGTH];
    BYTE bySupIOMask;
    int iIdx;
    
    USBIO.GetSoftWDTTimeout(&dwSoftWDTTimeout);
    sprintf(szItemString, "SoftWDT: %d", dwSoftWDTTimeout);
    m_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetFwVer(&wFWVer);
    sprintf(szItemString, "Version: %d", wFWVer);
    m_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetDeviceNickName(byDeviceNickName);
    sprintf(szItemString, "Description: ");
    for(iIdx = 0; iIdx < USBIO_NICKNAME_LENGTH; iIdx++)
      sprintf(szItemString + strlen(szItemString), "%c", byDeviceNickName[iIdx]);
    m_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetDeviceSN(byDeviceSN);
    sprintf(szItemString, "SN: ");
    for(iIdx = 0; iIdx < USBIO_SN_LENGTH; iIdx++)
      sprintf(szItemString + strlen(szItemString), "%C", byDeviceSN[iIdx]);
    m_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetSupportIOMask(&bySupIOMask);
    sprintf(szItemString, "Support IO Mask: 0x%02X", bySupIOMask);
    m_lbModuleInfo.AddString(szItemString);
    
    if(SUPPORT_DI(bySupIOMask))
    {
      USBIO.GetDITotal(&m_byDITotal);
      sprintf(szItemString, "DI channels: %d", m_byDITotal);
      m_lbModuleInfo.AddString(szItemString);
    }
    
    if(SUPPORT_DO(bySupIOMask))
    {
      USBIO.GetDOTotal(&m_byDOTotal);
      sprintf(szItemString, "DO channels: %d", m_byDOTotal);
      m_lbModuleInfo.AddString(szItemString);
    }
    
    if(SUPPORT_AI(bySupIOMask))
    {
      USBIO.GetAITotal(&m_byAITotal);
      sprintf(szItemString, "AI channels: %d", m_byAITotal);
      m_lbModuleInfo.AddString(szItemString);
    }
    
    if(SUPPORT_AO(bySupIOMask))
    {
      USBIO.GetAOTotal(&m_byAOTotal);
      sprintf(szItemString, "AO channels: %d", m_byAOTotal);
      m_lbModuleInfo.AddString(szItemString);
    }
    
    if(SUPPORT_PI(bySupIOMask))
    {
      USBIO.GetPITotal(&m_byPITotal);
      sprintf(szItemString, "PI channels: %d", m_byPITotal);
      m_lbModuleInfo.AddString(szItemString);
    }
    
    if(SUPPORT_PO(bySupIOMask))
    {
      USBIO.GetPOTotal(&m_byPOTotal);
      sprintf(szItemString, "PO channels: %d", m_byPOTotal);
      m_lbModuleInfo.AddString(szItemString);
    }
  }
}

void CDO_API_TestDlg::OnCloseDevice() 
{
  int iErrCode;
  
  m_lbModuleInfo.ResetContent();
  
  if(ERR_NO_ERR != (iErrCode = USBIO.CloseDevice()))
  {
    char szMessage[10];
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CDO_API_TestDlg::OnLoadDefault() 
{
  int iErrCode;
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.LoadDefault()))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CDO_API_TestDlg::OnRefreshInfo() 
{
  int iErrCode;
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.RefreshDeviceInfo()))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CDO_API_TestDlg::OnSetNickname() 
{
  int iErrCode;
  char szMessage[300];
  BYTE byNickname[USBIO_NICKNAME_LENGTH];
  CString szNickname;
  
  m_txtNickname.GetWindowText(szNickname);
  memset(byNickname, 32, USBIO_NICKNAME_LENGTH);
  sscanf(szNickname, "%s", byNickname);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.SetDeviceNickName(byNickname)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CDO_API_TestDlg::OnSetWDTTimeout() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwWDTTimeout;
  CString szWDTTimeout;
  
  m_txtWDTTimeout.GetWindowText(szWDTTimeout);
  dwWDTTimeout = _tcstoul(szWDTTimeout, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.SetSoftWDTTimeout(dwWDTTimeout)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CDO_API_TestDlg::OnSetBoardID() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwBoardID;
  CString szBoardID;
  
  m_txtUserDefinedBID.GetWindowText(szBoardID);
  dwBoardID = _tcstoul(szBoardID, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.SetUserDefinedBoardID(dwBoardID)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CDO_API_TestDlg::OnGetPWRONEnable() 
{
  int iErrCode;
  BYTE byPwrOnEnable[USBIO_DO_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DO_GetPowerOnEnable(byPwrOnEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module power-on enable:\n");
    for(int iIdx = 0; iIdx < m_byDOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: 0x%02X\n", iIdx, byPwrOnEnable[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CDO_API_TestDlg::OnGetSafetyEnable() 
{
  int iErrCode;
  BYTE bySafetyEnable[(USBIO_DO_MAX_CHANNEL + 7) >> 3];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DO_GetSafetyEnable(bySafetyEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module safety enable:\n0x");
    for(int iIdx = 0; iIdx < (m_byDOTotal + 7) >> 3; iIdx++)
      sprintf(szMessage + strlen(szMessage), "%02X", bySafetyEnable[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CDO_API_TestDlg::OnGetSafetyVal() 
{
  int iErrCode;
  BYTE bySafetyVal[(USBIO_DO_MAX_CHANNEL + 7) >> 3];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DO_GetSafetyValue(bySafetyVal)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module safety value:\n0x");
    for(int iIdx = 0; iIdx < (m_byDOTotal + 7) >> 3; iIdx++)
      sprintf(szMessage + strlen(szMessage), "%02X", bySafetyVal[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CDO_API_TestDlg::OnSetPWRONEnable() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  DWORD dwPwrOnEnable;
  CString szChToSet, szPwrOnEnable;
  
  m_txtSetPwrOnEnable.GetWindowText(szPwrOnEnable);
  dwPwrOnEnable = _tcstoul(szPwrOnEnable, NULL, 16);
  
  m_txtSetPwrOnEnableCh.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DO_SetPowerOnEnable((BYTE)dwChToSet, (BYTE)dwPwrOnEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}

void CDO_API_TestDlg::OnSetSafetyEnable() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwSafetyEnable;
  CString szSafetyEnable;
  BYTE bySafetyEnable[(USBIO_DO_MAX_CHANNEL + 7) / 8];
  int iIdx;
  
  m_txtSafetyEnable.GetWindowText(szSafetyEnable);
  dwSafetyEnable = _tcstoul(szSafetyEnable, NULL, 16);
  
  for(iIdx = 0; iIdx < (USBIO_DO_MAX_CHANNEL + 7) / 8; iIdx++)
    bySafetyEnable[iIdx] = (BYTE)((dwSafetyEnable >> (iIdx * 8)) & 0xFF);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DO_SetSafetyEnable(bySafetyEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}

void CDO_API_TestDlg::OnSetSafetyVal() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwSafetyVal;
  CString szSafetyVal;
  BYTE bySafetyVal[(USBIO_DO_MAX_CHANNEL + 7) / 8];
  int iIdx;
  
  m_txtSafetyVal.GetWindowText(szSafetyVal);
  dwSafetyVal = _tcstoul(szSafetyVal, NULL, 16);
  
  for(iIdx = 0; iIdx < (USBIO_DO_MAX_CHANNEL + 7) / 8; iIdx++)
    bySafetyVal[iIdx] = (BYTE)((dwSafetyVal >> (iIdx * 8)) & 0xFF);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DO_SetSafetyValue(bySafetyVal)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}
