// 8949G1VCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "8949G1VC.h"
#include "8949G1VCDlg.h"
#include "CtrlCardSingletion.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//extern CCtrlCardSingletion g_CCtrcard;
extern bool g_bStop;
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()

/////////////////////////////////////////////////////////////////////////////
// CMy8949G1VCDlg dialog

CMy8949G1VCDlg::CMy8949G1VCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy8949G1VCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy8949G1VCDlg)
		// 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 CMy8949G1VCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy8949G1VCDlg)
	DDX_Control(pDX, IDC_CB_CARD_ID, m_cbCardID);
	DDX_Control(pDX, IDC_MAIN_TAB, m_wndTabControl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy8949G1VCDlg, CDialog)
	//{{AFX_MSG_MAP(CMy8949G1VCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(TCN_SELCHANGE, IDC_MAIN_TAB, OnSelchangeMainTab)
	ON_BN_CLICKED(IDC_BUTTON_MAIN_STOP, OnButtonMainStop)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_MAIN_CLEAR, OnButtonMainClear)
	ON_WM_CLOSE()
	ON_WM_CREATE()
	ON_WM_CANCELMODE()
	ON_BN_CLICKED(IDC_BUTTON_MAIN_CLEAR2, OnButtonMainClear2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy8949G1VCDlg message handlers

BOOL CMy8949G1VCDlg::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_WaitDlg.DoModal();
	
	//InitTitleBar();
	InitTabControl();

	int num = 0;
	int index[10];
	CString str;
	adt8949_get_card_index(&num,index);
	if(num)
	{
		for(int i=0; i<num;i++)
		{
			str.Format("%ld",index[i]);		   
			m_cbCardID.AddString(str);
		} 
		
		g_cardID = index[0];//初始值-1
		
	}
	
	 m_cbCardID.SetCurSel(0);
	 UpdateData();//插入索引列表

	 InitTitleBar();//显示版本信息

    SetTimer(MAINTIMER,100,NULL);    
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


// [12/11/2012 Administrator]:标签页的内容
BOOL CMy8949G1VCDlg::InitTabControl()
{
	BOOL bIsCreate = FALSE;
	
 	bIsCreate = m_dlgBasicMove.Create(IDD_BASICMOVE_DIALOG, &m_wndTabControl);
 	if (!bIsCreate)
 	{
		return bIsCreate;
	}
	m_dlgBasicMove.SetLogicalParent(this);
	m_wndTabControl.InsertItem(MAIN_TAB_BASIC_MOVE, "BasicMotion", MAIN_TAB_BASIC_MOVE);
	
	
	bIsCreate = m_dlgMoveHome.Create(IDD_MOVEHOME_DIALOG, &m_wndTabControl);
	if (!bIsCreate)
	{
 		return bIsCreate;
	}
 	m_dlgMoveHome.SetLogicalParent(this);
	m_wndTabControl.InsertItem(MAIN_TAB_MOVEHOME, "ReturnZero+DA", MAIN_TAB_MOVEHOME);
 	
 	bIsCreate = m_dlgIOTest.Create(IDD_IOTEST_DIALOG, &m_wndTabControl);
 	if (!bIsCreate)
 	{
 		return bIsCreate;
 	}
 	m_dlgIOTest.SetLogicalParent(this);
 	m_wndTabControl.InsertItem(MAIN_TAB_IO_TEST, "IOTest", MAIN_TAB_IO_TEST);
 	
 	bIsCreate = m_dlgBaseParamSet.Create(IDD_BASEPARAMSET_DIALOG, &m_wndTabControl);
 	if (!bIsCreate)
 	{
 		return bIsCreate;
 	}
 	m_dlgBaseParamSet.SetLogicalParent(this);
// 	//////////////////////////////////////////////////////////////////////////
// 	// 将m_dlgBaseParamSet 对话框的指针传递给m_dlgDelayAndLock对话框
// //	m_dlgDelayAndLock.SetLogicalBother(&m_dlgBaseParamSet);
// 	//////////////////////////////////////////////////////////////////////////
 	m_wndTabControl.InsertItem(MAIN_TAB_BASEPARAM_SET, "BasParaSetup", MAIN_TAB_BASEPARAM_SET);
 	
 	bIsCreate = m_dlgDrawDemo.Create(IDD_DRAWDEMO_DIALOG, &m_wndTabControl);
 	if (!bIsCreate)
 	{
 		return bIsCreate;
 	}
 	m_dlgDrawDemo.SetLogicalParent(this);
 	m_wndTabControl.InsertItem(MAIN_TAB_DRAWDEMO, "DrawSample", MAIN_TAB_DRAWDEMO);/**/
	
 	CRect rcTab;
 	m_wndTabControl.GetClientRect(&rcTab);
 	rcTab.top = rcTab.top + 20;
 	
 	m_dlgBasicMove.MoveWindow(rcTab);
 
 	m_dlgMoveHome.MoveWindow(rcTab);
 	m_dlgIOTest.MoveWindow(rcTab);
 	m_dlgBaseParamSet.MoveWindow(rcTab);
 	m_dlgDrawDemo.MoveWindow(rcTab);
 	
 	m_dlgBasicMove.ShowWindow(SW_SHOW);
 	m_wndTabControl.SetCurSel(0);
	
	return bIsCreate;
}
// [12/11/2012 Administrator]:初始化标题
BOOL CMy8949G1VCDlg::InitTitleBar()
{
	BOOL bResult = FALSE;
	
	CWnd   *pWnd;   	
	m_Font.CreateFont(26, 0, 0, 0, 800, FALSE, FALSE, 0, ANSI_CHARSET, OUT_STROKE_PRECIS,
		CLIP_STROKE_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH|FF_SWISS, _T("宋体"));   
    pWnd = (CWnd*)GetDlgItem(IDC_STC_TITLE);
	pWnd->SetFont(&m_Font); 
	
	int fLibVer = 0;          // 库版本号
	int fHardwareVer = 0;     // 硬件版本号
	CCtrlCardSingletion::Instance()->GetVersion(fLibVer, fHardwareVer);
	
	CStatic *pStcVersion;
	CString strVersionInfo;
	pStcVersion = (CStatic*)GetDlgItem(IDC_STC_VERSION);
	//strVersionInfo.Format("库版本号:  %1.1f \r\n\n硬件版本号:  %1.1f", fLibVer, fHardwareVer);
	
	
	strVersionInfo.Format("DLL Version:%d.%d\nHardware Version:%d.%d.%d",fLibVer>>16,(fLibVer&0xffff),(fHardwareVer&0x07000000)>>24,(fHardwareVer&0x00ff0000)>>16,fHardwareVer&0xffff );
	

	
	
	pStcVersion->SetWindowText(strVersionInfo);
	
	m_FontVersion.CreateFont(15, 0, 0, 0, 800, FALSE, FALSE, 0, ANSI_CHARSET, OUT_STROKE_PRECIS,
		CLIP_STROKE_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH|FF_SWISS, _T("宋体"));
	pStcVersion->SetFont(&m_FontVersion);
	
		
	return bResult;
}


// [12/11/2012 Administrator]:初始化控制卡
int CMy8949G1VCDlg::InitCtrlCard()
{
	int nResult = -1;
	nResult = CCtrlCardSingletion::Instance()->InitBoard();

	return nResult;
}
// [12/11/2012 Administrator]:测试是否初始化成功
BOOL CMy8949G1VCDlg::TestInitCtrlCard()
{
	BOOL bOK = FALSE;
	int nResult = InitCtrlCard();
	//g_bInitFlag = false;
	
	return bOK;
}

void CMy8949G1VCDlg::OnSelchangeMainTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nCurSel = m_wndTabControl.GetCurSel();
	switch (nCurSel)
	{
	case MAIN_TAB_BASIC_MOVE:
		{
			m_dlgBasicMove.ShowWindow(SW_SHOW);
			m_dlgMoveHome.ShowWindow(SW_HIDE);
			m_dlgIOTest.ShowWindow(SW_HIDE);
			m_dlgBaseParamSet.ShowWindow(SW_HIDE);
			m_dlgDrawDemo.ShowWindow(SW_HIDE);
		}
		break;

	case MAIN_TAB_MOVEHOME:
		{
			m_dlgMoveHome.ShowWindow(SW_SHOW);
			//
			m_dlgBasicMove.ShowWindow(SW_HIDE);
			m_dlgIOTest.ShowWindow(SW_HIDE);
			m_dlgBaseParamSet.ShowWindow(SW_HIDE);
			m_dlgDrawDemo.ShowWindow(SW_HIDE);
		}
		break;
		
	case MAIN_TAB_IO_TEST:
		{
			m_dlgBasicMove.ShowWindow(SW_HIDE);
			m_dlgMoveHome.ShowWindow(SW_HIDE);
			m_dlgIOTest.ShowWindow(SW_SHOW);
			m_dlgBaseParamSet.ShowWindow(SW_HIDE);
			m_dlgDrawDemo.ShowWindow(SW_HIDE);
		}
		break;

	case MAIN_TAB_BASEPARAM_SET:
		{
			m_dlgBasicMove.ShowWindow(SW_HIDE);
			m_dlgMoveHome.ShowWindow(SW_HIDE);
			m_dlgIOTest.ShowWindow(SW_HIDE);
			m_dlgBaseParamSet.ShowWindow(SW_SHOW);
			m_dlgDrawDemo.ShowWindow(SW_HIDE);
		}
		break;

	case MAIN_TAB_DRAWDEMO:
		{
			m_dlgBasicMove.ShowWindow(SW_HIDE);
			m_dlgMoveHome.ShowWindow(SW_HIDE);
			m_dlgIOTest.ShowWindow(SW_HIDE);
			m_dlgBaseParamSet.ShowWindow(SW_HIDE);
			m_dlgDrawDemo.ShowWindow(SW_SHOW);
		}
		break;
		
	default:
		;
	}
	*pResult = 0;
}

void CMy8949G1VCDlg::OnButtonMainStop() 
{
	// TODO: Add your control notification handler code here

	for (int i = 0; i<MAXAXIS ; i++)
	{
		adt8949_sudden_stop(g_cardID, i+1);
	}
	g_bStop = true;
}

void CMy8949G1VCDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	long nLogPos = 0;
	long nActPos = 0;
	float fCurSpeed = 0.0;

	CString strCadrID;	
	GetDlgItem(IDC_CB_CARD_ID)->GetWindowText(strCadrID);
	g_cardID = atoi(strCadrID);//实时获取卡号索引

	//InitTitleBar();//显示版本信息

	switch (nIDEvent)
	{
	case  MAINTIMER:		                 //主程序定时器
		
		
		UINT nIDLogPos[] = {IDC_STC_POSLOG_X, IDC_STC_POSLOG_Y, IDC_STC_POSLOG_Z, IDC_STC_POSLOG_A};
		UINT nIDActPos[] = {IDC_STC_POSACT_X, IDC_STC_POSACT_Y, IDC_STC_POSACT_Z, IDC_STC_POSACT_A};
		UINT nIDSpeed[]  = {IDC_STC_RUNSPEED_X, IDC_STC_RUNSPEED_Y, IDC_STC_RUNSPEED_Z, IDC_STC_RUNSPEED_A};
		UINT nIDSTOPINFO[]={IDC_STOPINFO_X,IDC_STOPINFO_Y,IDC_STOPINFO_Z,IDC_STOPINFO_A};
		
		CStatic* pStc = NULL;
		CString strInfo = "";
		int stopdata[4]={0};
		int fHardwareVer = 0;
		for (int i=1; i<=4; i++)
		{
			CCtrlCardSingletion::Instance()->GetCurrentInfo(i, nLogPos, nActPos, fCurSpeed);
			// 逻辑位置
			pStc = (CStatic*)GetDlgItem(nIDLogPos[i-1]);
			strInfo.Format("%ld", nLogPos);
			pStc->SetWindowText(strInfo);                  
			// 实际位置
			pStc = (CStatic*)GetDlgItem(nIDActPos[i-1]);
			strInfo.Format("%ld", nActPos);
			pStc->SetWindowText(strInfo);  
			// 实时速度
         
			pStc = (CStatic*)GetDlgItem(nIDSpeed[i-1]);
			strInfo.Format("%5.3f", fCurSpeed);
			pStc->SetWindowText(strInfo); 
		
		
			
		
				adt8949_get_stopdata(g_cardID,i,&stopdata[i-1]);
				
				pStc=(CStatic*)GetDlgItem(nIDSTOPINFO[i-1]);
				
				strInfo.Format("%ld",stopdata[i-1]);		
	
				pStc->SetWindowText(strInfo);
		
		
			
		}
		// 监控输入信号
		ReadInPutInfo();
		break;
		
		//default:
	}
	
       
	
	CDialog::OnTimer(nIDEvent);
}
//////////////////////////////////////////////////////////////////////////
//
// [12/17/2012 Administrator]:监控输入信号
void CMy8949G1VCDlg::ReadInPutInfo()
{
	UINT nID[] = {IDC_CHECK_LIMIT_X2 ,IDC_CHECK_LIMIT_X1,  
		IDC_CHECK_STOP0_X, IDC_CHECK_STOP1_X,
		IDC_CHECK_LIMIT_Y2, IDC_CHECK_LIMIT_Y1, 
		IDC_CHECK_STOP0_Y, IDC_CHECK_STOP1_Y,
		IDC_CHECK_LIMIT_Z2,IDC_CHECK_LIMIT_Z1,  
		IDC_CHECK_STOP0_Z, IDC_CHECK_STOP1_Z,
		IDC_CHECK_LIMIT_A2, IDC_CHECK_LIMIT_A1, 
		IDC_CHECK_STOP0_A, IDC_CHECK_STOP1_A,};
	
	int nIO[]={4,5,12,20,6,7,13,21,8,9,14,22,10,11,15,23};
	//int io[]={4,5,12,20,6,7,13,21,8,9,14,22,10,11,15,23};
	CButton *pBtn;
	int nValue;
	
	for (int i=0; i<16; i++)
	{
		nValue = CCtrlCardSingletion::Instance()->ReadInput(nIO[i]);
		pBtn = (CButton *)GetDlgItem(nID[i]);
		pBtn->SetCheck(0 == nValue ?  1:0);
	}
}

void CMy8949G1VCDlg::OnButtonMainClear() 
{
	// TODO: Add your control notification handler code here
	for(int i=1; i<MAXAXIS+1; i++)
	{
		//Sleep(1);
		adt8949_set_command_pos(g_cardID,i, 0);    //逻辑位置设置为零
		
		adt8949_set_actual_pos(g_cardID, i, 0);    //实际位置设置为零
	}
}

void CMy8949G1VCDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
		
		CCtrlCardSingletion::ReleaseCtrlCardSingletion();

	for (int i=1; i<=MAXAXIS; i++)
	{	 
		// 立即停止
		CCtrlCardSingletion::Instance()->StopRun(i, 0);
    }
    OnButtonMainStop();
	KillTimer(MAINTIMER);
	//adt8949_close_card();

	CDialog::OnClose();
}

int CMy8949G1VCDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	return 0;
}

void CMy8949G1VCDlg::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CMy8949G1VCDlg::OnButtonMainClear2() 
{
	// TODO: Add your control notification handler code here
	adt8949_reset_card(g_cardID);
}
