// 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面 
// ("Fluent UI")，该示例仅作为参考资料提供， 
// 用以补充《Microsoft 基础类参考》和 
// MFC C++ 库软件随附的相关电子文档。
// 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
// 若要了解有关 Fluent UI 许可计划的详细信息，请访问  
// http://msdn.microsoft.com/officeui。
//
// 版权所有 (C) Microsoft Corporation
// 保留所有权利。

// ScriptDemoDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "ScriptDemo.h"
#include "ScriptDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////////


// CScriptDemoDlg 对话框
CScriptDemoDlg::CScriptDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScriptDemoDlg::IDD, pParent)
	, m_userVarIdx(1)
	, m_userVarVal(1)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_devIdx = 0;
	m_instIdx = 0;


}

void CScriptDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_userVarIdx);
	DDV_MinMaxDouble(pDX, m_userVarIdx, 1, 128);
	DDX_Text(pDX, IDC_EDIT2, m_userVarVal);
}

BEGIN_MESSAGE_MAP(CScriptDemoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CScriptDemoDlg::OnBnClickedButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_DOWN, &CScriptDemoDlg::OnBnClickedButtonDown)
	ON_BN_CLICKED(IDC_BUTTON_RUN, &CScriptDemoDlg::OnBnClickedButtonRun)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, &CScriptDemoDlg::OnBnClickedButtonPause)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CScriptDemoDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_EMG, &CScriptDemoDlg::OnBnClickedButtonEmg)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_VW, &CScriptDemoDlg::OnBnClickedButtonVw)
	ON_BN_CLICKED(IDC_BUTTON_VR, &CScriptDemoDlg::OnBnClickedButtonVr)
	ON_CBN_SELCHANGE(IDC_COMBO_DEVINDX, &CScriptDemoDlg::OnCbnSelchangeComboDevindx)
END_MESSAGE_MAP()


// CScriptDemoDlg 消息处理程序

BOOL CScriptDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	SetDlgItemText( IDC_EDIT_BINFILE,_T("test.bin"));

	((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->AddString(_T("double"));
	((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->AddString(_T("float"));
	((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->AddString(_T("Int32"));
	((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->AddString(_T("Int16"));
	((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->SetCurSel(0);

	((CComboBox *)GetDlgItem(IDC_COMBO_DEVINDX))->AddString(_T("0"));
	((CComboBox *)GetDlgItem(IDC_COMBO_DEVINDX))->AddString(_T("1"));
	((CComboBox *)GetDlgItem(IDC_COMBO_DEVINDX))->AddString(_T("2"));
	((CComboBox *)GetDlgItem(IDC_COMBO_DEVINDX))->AddString(_T("3"));
	((CComboBox *)GetDlgItem(IDC_COMBO_DEVINDX))->SetCurSel(m_devIdx);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CScriptDemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CScriptDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

#define INIT_BY_HANLDE
void CScriptDemoDlg::OnBnClickedButtonConnect()
{
	int rtn;
	int mode = 0;

#ifdef INIT_BY_HANLDE
	HAND hDev;
	rtn = NMC_DevOpen(m_devIdx,&hDev);
	if (rtn != 0)
	{
		return;
	}
	rtn = ScptItf_InitByHandle(m_instIdx,hDev,mode,100,50);
	if (rtn != 0)
	{
		return;
	}
#else
	HAND hDev;
	rtn = NMC_DevOpen(m_devIdx,&hDev);
	if (rtn != 0)
	{
		return;
	}

	rtn = ScptItf_Init(m_instIdx,m_devIdx,mode,100,50);
	if (rtn != 0)
	{
		return;
	}
#endif
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(1);
	SetTimer(1,150,NULL);
	GetDlgItem(IDC_BUTTON_DOWN)->EnableWindow(TRUE);
	
}

void CScriptDemoDlg::OnBnClickedButtonDown()
{
	CString path; 
	GetModuleFileName(NULL,path.GetBufferSetLength(MAX_PATH+1),MAX_PATH); 
	path.ReleaseBuffer(); 
	int pos = path.ReverseFind('\\'); 
	path = path.Left(pos); 
	CString str;
	str.Format(_T("%s\\test.bin"),path);

	char szPath[128];
	WideCharToMultiByte(CP_ACP, 0, str, -1, szPath, 128, NULL, NULL);

	int rtn = ScptItf_Download(m_instIdx,szPath,0);
	if (rtn != 0)
	{
		AddLog(rtn,_T("下载错误"));
		return;
	}
	GetDlgItem(IDC_BUTTON_RUN)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_EMG)->EnableWindow(TRUE);
	SetTimer(1,150,NULL);
}

void CScriptDemoDlg::OnBnClickedButtonRun()
{
	int rtn = ScptItf_RunControl(m_instIdx,RUN_CTRL_RUN);
	if (rtn != 0)
	{
		AddLog(rtn,_T("启动运行错误"));
	}
}


void CScriptDemoDlg::AddLog(int rtn ,CString msg)
{
	CString str;
	str.Format(_T("%s:%d"),msg,rtn);
	((CListBox *)GetDlgItem(IDC_LIST_LOG))->AddString(str);	
}

void CScriptDemoDlg::OnBnClickedButtonPause()
{
	int rtn = ScptItf_RunControl(m_instIdx,RUN_CTRL_PAUSE);
	if (rtn != 0)
	{
		AddLog(rtn,_T("暂停错误"));
	}
}

void CScriptDemoDlg::OnBnClickedButtonStop()
{
	int rtn = ScptItf_RunControl(m_instIdx,RUN_CTRL_STOP);
	if (rtn != 0)
	{
		AddLog(rtn,_T("停止运行错误"));
	}
}

void CScriptDemoDlg::OnBnClickedButtonEmg()
{
	int rtn = ScptItf_RunControl(m_instIdx,RUN_CTRL_EMG);
	if (rtn != 0)
	{
		AddLog(rtn,_T("急停错误"));
	}
}

void CScriptDemoDlg::OnTimer(UINT_PTR nIDEvent)
{
	TScptStsUser sts;
	int rtn = ScptItf_GetStatus(m_instIdx,&sts);
	if (rtn != 0)
	{
		SetDlgItemText(IDC_EDIT_ST_RUN,_T("error"));
		//AddLog(rtn,_T("读取状态错误"));
	}
	else
	{
		CString str;
		str.Format(_T("%d"),sts.runState);
		SetDlgItemText(IDC_EDIT_ST_RUN,str);

		str.Format(_T("0x%X"),sts.di1);
		SetDlgItemText(IDC_EDIT_ST_DI,str);

		str.Format(_T("0x%X"),sts.do1);
		SetDlgItemText(IDC_EDIT_ST_DO,str);

		str.Format(_T("%.2f,%.2f,%.2f"),sts.axisPos[0],sts.axisPos[1],sts.axisPos[2]);
		SetDlgItemText(IDC_EDIT_ST_POS,str);

		str.Format(_T("%d"),sts.errCode);
		SetDlgItemText(IDC_EDIT_ST_ERR,str);		
	}

	CDialog::OnTimer(nIDEvent);
}


void CScriptDemoDlg::OnBnClickedButtonVw()
{
	UpdateData(1);
	int varType = ((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->GetCurSel();
	//int rtn = ScptItf_UserVarWriteEx(m_instIdx,varType,m_userVarIdx,1,&m_userVarVal);
	int rtn = ScptItf_UserVarWrite(m_instIdx,varType,m_userVarIdx,m_userVarVal);
	if (rtn != 0)
	{
		AddLog(rtn,_T("写变量错误"));
	}
}


void CScriptDemoDlg::OnBnClickedButtonVr()
{
	UpdateData(1);
	int varType = ((CComboBox *)GetDlgItem(IDC_COMBO_VARTYPE))->GetCurSel();
	//int rtn = ScptItf_UserVarReadEx(m_instIdx,varType,m_userVarIdx,1,&m_userVarVal);
	int rtn = ScptItf_UserVarRead(m_instIdx,varType,m_userVarIdx,&m_userVarVal);
	if (rtn != 0)
	{
		AddLog(rtn,_T("读变量错误"));
	}
	UpdateData(0);
}


void CScriptDemoDlg::OnCbnSelchangeComboDevindx()
{
	m_devIdx= ((CComboBox *)GetDlgItem(IDC_COMBO_DEVINDX))->GetCurSel();
	m_instIdx = m_devIdx;
}
