// DI_ReadValueDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DI_ReadValue.h"
#include "DI_ReadValueDlg.h"
#include "..\..\..\Includes\ICPDAS_USBIO.h"

#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;

/////////////////////////////////////////////////////////////////////////////
// CDI_ReadValueDlg dialog

CDI_ReadValueDlg::CDI_ReadValueDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDI_ReadValueDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDI_ReadValueDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDI_ReadValueDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDI_ReadValueDlg)
	DDX_Control(pDX, IDC_BTNREADDIGIVALUE, m_btnReadDigitalValue);
	DDX_Control(pDX, IDC_BTNREADCNTVALUE, m_btnReadCntVal);
	DDX_Control(pDX, IDC_BTNCNTCLR, m_btnCntClr);
	DDX_Control(pDX, IDC_LBCNTVALUE, m_lbCounterValue);
	DDX_Control(pDX, IDC_LBINFORMATION, m_lbModuleInfo);
	DDX_Control(pDX, IDC_LBDIGIVALUE, m_lbDigiValue);
	DDX_Control(pDX, IDC_TXTCNTCLRMASK, m_txtCntClrMsk);
	DDX_Control(pDX, IDC_TXTBOARDID, m_txtBID);
	DDX_Control(pDX, IDC_CBXDEVID, m_cbxDID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDI_ReadValueDlg, CDialog)
	//{{AFX_MSG_MAP(CDI_ReadValueDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopendevice)
	ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclosedevice)
	ON_BN_CLICKED(IDC_BTNREADDIGIVALUE, OnBtnreaddigivalue)
	ON_BN_CLICKED(IDC_BTNREADCNTVALUE, OnBtnreadcntvalue)
	ON_BN_CLICKED(IDC_BTNCNTCLR, OnBtncntclr)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDI_ReadValueDlg message handlers

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

void CDI_ReadValueDlg::OnBtnopendevice() 
{
  CString szBID;
  DWORD dwBID;
  int iSelIdx;
  int iErrCode;
  int iModuleID;
  
  m_txtBID.GetWindowText(szBID);
  dwBID = _tcstoul(szBID, NULL, 10);
  
  iSelIdx = m_cbxDID.GetCurSel();
  
  USBIO.SetCommTimeout(500);

  switch (iSelIdx)
  {
  case 0:
	  iModuleID = USB2026;
    break;
  case 1:
      iModuleID = USB2051;
    break;
  case 2:
      iModuleID = USB2055;
    break;
  case 3:
      iModuleID = USB2060;
    break;
  case 4:
	  iModuleID = USB2051_32;
	  break;
  case 5:
	  iModuleID = USB2055_32;
	  break;
  case 6:
	  iModuleID = USB2068_18;
    break;
  case 7:
	  iModuleID = USB2255_32;
    break;
  case 8:
	  iModuleID = USB2255_64;
	  break;
  case 9:
	  iModuleID = USB2261_16;
	  break;
  case 10:
	  iModuleID = USB2261_32;
	  break;
  case 11:
	  iModuleID = USB2260_48;
	  break;
  case 12:
	  iModuleID = USB2260B_48;
	  break;
  case 13:
	  iModuleID = USB2260C_48;
	  break;
  case 14:
	  iModuleID = USB2260_24;
	  break;
  case 15:
	  iModuleID = USB2260B_24;
	  break;
  case 16:
	  iModuleID = USB2260C_24;
	  break;
  }
  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);
    }
    
    m_btnReadDigitalValue.EnableWindow(TRUE);
    m_btnReadCntVal.EnableWindow(TRUE);
    m_btnCntClr.EnableWindow(TRUE);
  }
}

void CDI_ReadValueDlg::OnBtnclosedevice() 
{
  int iErrCode;
  
  if(ERR_NO_ERR != (iErrCode = USBIO.CloseDevice()))
  {
    char szMessage[10];
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  
  m_btnReadDigitalValue.EnableWindow(FALSE);
  m_btnReadCntVal.EnableWindow(FALSE);
    m_btnCntClr.EnableWindow(FALSE);
  
  m_lbModuleInfo.ResetContent();
  m_lbDigiValue.ResetContent();
  m_lbCounterValue.ResetContent();
}

void CDI_ReadValueDlg::OnBtnreaddigivalue() 
{
  int iErrCode;
  BYTE byDIValue[(USBIO_DI_MAX_CHANNEL + 7) / 8];
  char szMessage[300];
  
  m_lbDigiValue.ResetContent();
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DI_ReadValue(byDIValue)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    char szItemString[100];
    int iIdx;
    
    for(iIdx = 0; iIdx < ((m_byDITotal + 7) / 8); iIdx++)
    {
      sprintf(szItemString, "Ch%d~%d : 0x%02x", iIdx * 8, iIdx * 8 + 7, byDIValue[iIdx]);
      
      m_lbDigiValue.AddString(szItemString);
    }
  }
}

void CDI_ReadValueDlg::OnBtnreadcntvalue() 
{
  int iErrCode;
  DWORD dwDICntValue[USBIO_DI_MAX_CHANNEL];
  char szMessage[300];
  
  m_lbCounterValue.ResetContent();
  
  if(ERR_NO_ERR != (iErrCode = USBIO.DI_ReadCounterValue(dwDICntValue)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    char szItemString[100];
    int iIdx;
    
    for(iIdx = 0; iIdx < m_byDITotal; iIdx++)
    {
      sprintf(szItemString, "Ch%d : %d", iIdx, dwDICntValue[iIdx]);
      
      m_lbCounterValue.AddString(szItemString);
    }
  }
}

void CDI_ReadValueDlg::OnBtncntclr() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwCntClrMsk;
  DWORD64 dwCntClrMsk64;
  DWORD dwCntClrMsk_high;
  DWORD dwCntClrMsk_low;
  CString szCntClrMsk;
  
  m_txtCntClrMsk.GetWindowText(szCntClrMsk);
  
  
  if(m_byDITotal>32){
	  dwCntClrMsk64 = _tcstoul(szCntClrMsk, NULL, 16);
	  dwCntClrMsk_high = (DWORD)(dwCntClrMsk64 & 0xFFFFFFFF);  
	  dwCntClrMsk_low =  (DWORD)((dwCntClrMsk64 >> 32) & 0xFFFFFFFF);
	  if(ERR_NO_ERR != (iErrCode = USBIO.DI_WriteClearCounters(dwCntClrMsk_high,dwCntClrMsk_low)))
	  {
		  sprintf(szMessage, "[%d]", iErrCode);
		  MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	  }
	  else
	  {
		  MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	  }

  }else{
	  dwCntClrMsk = _tcstoul(szCntClrMsk, NULL, 16);
	  if(ERR_NO_ERR != (iErrCode = USBIO.DI_WriteClearCounters(dwCntClrMsk)))
	  {
		  sprintf(szMessage, "[%d]", iErrCode);
		  MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	  }
	  else
	  {
		  MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	  }
  }
}
