// AO_WriteValueDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AO_WriteValue.h"
#include "AO_WriteValueDlg.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;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAO_WriteValueDlg dialog

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

void CAO_WriteValueDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAO_WriteValueDlg)
	DDX_Control(pDX, IDC_TXTDIGITAL_VAL, m_txtDigital_Value);
	DDX_Control(pDX, IDC_TXTDIGITAL_CH, m_txtDigital_CH);
	DDX_Control(pDX, IDC_TXTANALOG_VAL, m_txtAnalog_Value);
	DDX_Control(pDX, IDC_TXTANALOG_CH, m_txtAnalog_CH);
	DDX_Control(pDX, IDC_BTNWRITE_DIGITAL, m_WriteValueDigital);
	DDX_Control(pDX, IDC_BTNWRITE_CH_DIGITAL, m_WriteChValueDigital);
	DDX_Control(pDX, IDC_BTNWRITE_CH_ANALOG, m_WrireChValueAnalog);
	DDX_Control(pDX, IDC_BTNWRITE_ANALOG, m_WriteValueAnalog);
	DDX_Control(pDX, IDC_TXTBOARDID, m_txtBID);
	DDX_Control(pDX, IDC_LBINFORMATION, m_lbModuleInfo);
	DDX_Control(pDX, IDC_LBEXPVALUE, m_lbExpValue);
	DDX_Control(pDX, IDC_LBCURVALUE, m_lbCurValue);
	DDX_Control(pDX, IDC_CBXDEVID, m_cbxDID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAO_WriteValueDlg, CDialog)
	//{{AFX_MSG_MAP(CAO_WriteValueDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNOPEN, OnOpenDevice)
	ON_BN_CLICKED(IDC_BTNCLOSE, OnCloseDevice)
	ON_BN_CLICKED(IDC_BTNWRITE_DIGITAL, OnWriteValueDigital)
	ON_BN_CLICKED(IDC_BTNWRITE_CH_DIGITAL, OnWriteChValueDigital)
	ON_BN_CLICKED(IDC_BTNWRITE_ANALOG, OnWriteValueAnalog)
	ON_BN_CLICKED(IDC_BTNWRITE_CH_ANALOG, OnWriteChValueAnalog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAO_WriteValueDlg message handlers

BOOL CAO_WriteValueDlg::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
	m_txtBID.SetWindowText("1");
	m_cbxDID.SetCurSel(0);


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAO_WriteValueDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		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 CAO_WriteValueDlg::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 CAO_WriteValueDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAO_WriteValueDlg::OnOpenDevice() 
{
  CString szBID;
  DWORD dwBID;
  int iSelIdx;
  int iErrCode;
  

  m_txtBID.GetWindowText(szBID);
  dwBID = _tcstoul(szBID, NULL, 10);
  
  iSelIdx = m_cbxDID.GetCurSel();

  USBIO.SetCommTimeout(500);
  if(ERR_NO_ERR != (iErrCode = USBIO.OpenDevice(USBIO_MINPID+iSelIdx+26, (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.GetDITotal(&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 CAO_WriteValueDlg::OnCloseDevice() 
{
  int iErrCode;
  
  if(ERR_NO_ERR != (iErrCode = USBIO.CloseDevice()))
  {
    char szMessage[10];
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
    
  m_lbModuleInfo.ResetContent();
  m_lbExpValue.ResetContent();
  m_lbCurValue.ResetContent();

}


void CAO_WriteValueDlg::ShowValue()
{
  int iErrCode;
  DWORD dwAOExpValue[USBIO_AO_MAX_CHANNEL];
  float fAOExpValue[USBIO_AO_MAX_CHANNEL];
  DWORD dwAOCurValue[USBIO_AO_MAX_CHANNEL];
  float fAOCurValue[USBIO_AO_MAX_CHANNEL];
  char szMessage[300];
 
  
  m_lbExpValue.ResetContent();
  m_lbCurValue.ResetContent();
  
  if((ERR_NO_ERR != (iErrCode = USBIO.AO_ReadExpValue(dwAOExpValue))) & (ERR_NO_ERR != (iErrCode = USBIO.AO_ReadExpValue(fAOExpValue))))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    char szItemString[200];
    int iIdx;
    
    for(iIdx = 0; iIdx < m_byAOTotal; iIdx++)
    {
      sprintf(szItemString, "Ch%d : 0x%04x / %04f", iIdx, dwAOExpValue[iIdx], fAOExpValue[iIdx]);
      
      m_lbExpValue.AddString(szItemString);
    }
  }

  
  if((ERR_NO_ERR != (iErrCode = USBIO.AO_ReadCurValue(dwAOCurValue))) & (ERR_NO_ERR != (iErrCode = USBIO.AO_ReadCurValue(fAOCurValue))))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    char szItemString[200];
    int iIdx;
    
    for(iIdx = 0; iIdx < m_byAOTotal; iIdx++)
    {
      sprintf(szItemString, "Ch%d : 0x%04x / %04f", iIdx, dwAOCurValue[iIdx], fAOCurValue[iIdx]);
      
      m_lbCurValue.AddString(szItemString);
    }
  }

}

void CAO_WriteValueDlg::OnWriteValueDigital() 
{
  int iErrCode;
  DWORD dwAOValue[USBIO_AO_MAX_CHANNEL];
  DWORD dwtmpValue;
  char szMessage[300];
  CString szValue;


  m_txtDigital_Value.GetWindowText(szValue);
  if(szValue != "")
  {
		dwtmpValue = _tcstoul(szValue, NULL, 16);
		for(int idx = 0; idx < m_byAOTotal; idx ++)
		  dwAOValue[idx] = dwtmpValue;


		if(ERR_NO_ERR != (iErrCode = USBIO.AO_WriteValue(dwAOValue)))
		{
		  sprintf(szMessage, "[%d]", iErrCode);
    	  MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}


		ShowValue();

		m_txtDigital_CH.SetWindowText("");
  }
  else
  {
	sprintf(szMessage, "Please enter the AO Value!");
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CAO_WriteValueDlg::OnWriteChValueDigital() 
{
  int iErrCode;
  BYTE byChannel;
  DWORD dwAOValue;
  char szMessage[300];
  CString szValue;
  CString szCh;


  m_txtDigital_Value.GetWindowText(szValue);
  m_txtDigital_CH.GetWindowText(szCh);
  if(szValue != "" | szCh != "")
  {
		dwAOValue = _tcstoul(szValue, NULL, 16);
		byChannel = (BYTE)_tcstoul(szCh, NULL, 10);


		if(ERR_NO_ERR != (iErrCode = USBIO.AO_WriteValue(byChannel, dwAOValue)))
		{
		  sprintf(szMessage, "[%d]", iErrCode);
    	  MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}


		ShowValue();
  }
  else
  {
	sprintf(szMessage, "Please enter the AO Channel and Value!");  
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
	
}

void CAO_WriteValueDlg::OnWriteValueAnalog() 
{
  int iErrCode;
  float fAOValue[USBIO_AO_MAX_CHANNEL];
  DWORD dwtmpValue;
  char szMessage[300];
  CString szValue;


  m_txtAnalog_Value.GetWindowText(szValue);
  if(szValue != "")
  {
		dwtmpValue = _tcstoul(szValue, NULL, 10);
		for(int idx = 0; idx < m_byAOTotal; idx ++)
		  fAOValue[idx] = dwtmpValue;


		if(ERR_NO_ERR != (iErrCode = USBIO.AO_WriteValue(fAOValue)))
		{
		  sprintf(szMessage, "[%d]", iErrCode);
		  MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}


		ShowValue();
		m_txtAnalog_CH.SetWindowText("");
  }
  else
  {
	sprintf(szMessage, "Please enter the AO Value!");
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
	
}

void CAO_WriteValueDlg::OnWriteChValueAnalog() 
{
  int iErrCode;
  BYTE byChannel;
  float fAOValue;
  char szMessage[300];
  CString szValue;
  CString szCh;


  m_txtAnalog_Value.GetWindowText(szValue);
  m_txtAnalog_CH.GetWindowText(szCh);
  if(szValue != "" | szCh != "")
  {
		fAOValue = (float)_tcstoul(szValue, NULL, 10);
		byChannel = (BYTE)_tcstoul(szCh, NULL, 10);


		if(ERR_NO_ERR != (iErrCode = USBIO.AO_WriteValue(byChannel, fAOValue)))
		{
		  sprintf(szMessage, "[%d]", iErrCode);   
		  MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}


		ShowValue();
  }
  else
  {
	sprintf(szMessage, "Please enter the AO Channel and Value!");   
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
	
}
