


#include "stdafx.h"
#include "evc8840.h"
#include "evc8840Dlg.h"
#include "evc8840lib.h"
#include "SYSPARASET.h"

SYSPARA syspara;
extern  int devnum;
/////////////////////////////////////////////////////////////////////////////
// SYSPARASET dialog


SYSPARASET::SYSPARASET(CWnd* pParent /*=NULL*/)
	: CDialog(SYSPARASET::IDD, pParent)
{
	//{{AFX_DATA_INIT(SYSPARASET)
	m_fHomeStart = 0.0;
	m_fHomeSpeed = 0.0;
	m_fHomeAdd = 0.0;
	m_fSearch = 0.0;
	m_fRang = 0.0;
	m_fWorkStart = 0.0;
	m_fWorkSpeed = 0.0;
	m_fWorkAdd = 0.0;
	m_nPulseMode = -1;
	m_nPulseLog = -1;
	m_nDirLogic = -1;
	m_nLimitLevel = -1;
	m_bLimitN = FALSE;
	m_bLimitP = FALSE;
	m_homestep = 0;
	m_bZPhasic = FALSE;
	m_nZPhasicLevel = 0;
	m_nHomeLevel = -1;
	m_axis = 0;
	m_homeaxis = FALSE;
	m_bHome = FALSE;
	m_mmlPusle = 0;
	//}}AFX_DATA_INIT
}


void SYSPARASET::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(SYSPARASET)
	DDX_Text(pDX, IDC_EDIT2, m_fHomeStart);
	DDX_Text(pDX, IDC_EDIT3, m_fHomeSpeed);
	DDX_Text(pDX, IDC_EDIT4, m_fHomeAdd);
	DDV_MinMaxDouble(pDX, m_fHomeAdd, 0., 20000.);
	DDX_Text(pDX, IDC_EDIT5, m_fSearch);
	DDV_MinMaxDouble(pDX, m_fSearch, 0., 100000.);
	DDX_Text(pDX, IDC_EDIT6, m_fRang);
	DDV_MinMaxDouble(pDX, m_fRang, 0., 10000.);
	DDX_Text(pDX, IDC_EDIT7, m_fWorkStart);
	DDX_Text(pDX, IDC_EDIT8, m_fWorkSpeed);
	DDV_MinMaxDouble(pDX, m_fWorkSpeed, 0., 100000.);
	DDX_Text(pDX, IDC_EDIT9, m_fWorkAdd);
	DDV_MinMaxDouble(pDX, m_fWorkAdd, 0., 10000.);
	DDX_Radio(pDX, IDC_RADIO5, m_nPulseMode);
	DDX_Radio(pDX, IDC_RADIO8, m_nPulseLog);
	DDX_Radio(pDX, IDC_RADIO10, m_nDirLogic);
	DDX_Radio(pDX, IDC_RADIO12, m_nLimitLevel);
	DDX_Check(pDX, IDC_CHECK1, m_bLimitN);
	DDX_Check(pDX, IDC_CHECK2, m_bLimitP);
	DDX_Text(pDX, IDC_EDIT1, m_homestep);
	DDV_MinMaxInt(pDX, m_homestep, 0, 4);
	DDX_Check(pDX, IDC_CHECK5, m_bZPhasic);
	DDX_Radio(pDX, IDC_RADIO16, m_nZPhasicLevel);
	DDX_Radio(pDX, IDC_RADIO14, m_nHomeLevel);
	DDX_Radio(pDX, IDC_AXISX, m_axis);
	DDX_Check(pDX, IDC_CHECK3, m_homeaxis);
	DDX_Check(pDX, IDC_CHECK4, m_bHome);
	DDX_Text(pDX, IDC_EDIT12, m_mmlPusle);
	DDV_MinMaxInt(pDX, m_mmlPusle, 0, 10000);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(SYSPARASET, CDialog)
	//{{AFX_MSG_MAP(SYSPARASET)
	ON_BN_CLICKED(IDC_AXISX, OnAxisx)
	ON_BN_CLICKED(IDC_AXISY, OnAxisy)
	ON_BN_CLICKED(IDC_AXISZ, OnAxisz)
	ON_BN_CLICKED(IDC_AXISA, OnAxisa)
	ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
	ON_BN_CLICKED(IDC_RADIO6, OnRadio6)
	ON_BN_CLICKED(IDC_RADIO8, OnRadio8)
	ON_BN_CLICKED(IDC_RADIO9, OnRadio9)
	ON_BN_CLICKED(IDC_RADIO10, OnRadio10)
	ON_BN_CLICKED(IDC_RADIO11, OnRadio11)
	ON_EN_CHANGE(IDC_EDIT12, OnChangeEdit12)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


void SYSPARASET::OnAxisx() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	ShowSystemParam();	//载入当前参数
	UpdateData(FALSE);	
}

void SYSPARASET::OnAxisy() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	ShowSystemParam();	//载入当前参数
	UpdateData(FALSE);	
}

void SYSPARASET::OnAxisz() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	ShowSystemParam();	//载入当前参数
	UpdateData(FALSE);	
}

void SYSPARASET::OnAxisa() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	ShowSystemParam();	//载入当前参数
	UpdateData(FALSE);	
}


void SYSPARASET::ShowSystemParam()
{
	
	// 回原点启始速度，驱动速度，加速度		
	m_fHomeStart	= syspara.fHomeStartv[m_axis];	
	m_fHomeSpeed	= syspara.fHomeSpeed[m_axis];
	m_fHomeAdd		= syspara.fHomeAccTime[m_axis];
	//回原点搜索速度，搜索范围
	m_fSearch		= syspara.fHomelSpeed[m_axis];
	m_fRang			= syspara.fHomelRange[m_axis];
	//加工启始速度，驱动速度，加速度
	m_fWorkAdd		= syspara.fAccTime[m_axis];
	m_fWorkSpeed	= syspara.fSpeed[m_axis];
	m_fWorkStart	= syspara.fStartv[m_axis];
	//脉冲当量
	m_mmlPusle		= syspara.lPulseMm[m_axis];	
	//回原点函数设置，Z相信号
	m_bHome         = syspara.home[m_axis];
	m_nHomeLevel    = syspara.home_logic[m_axis];
	m_bZPhasic      = syspara.ucZi[m_axis];	
	m_nZPhasicLevel = syspara.Zi_logic[m_axis];
	m_homeaxis		= syspara.home_axis[m_axis];
	m_homestep		= syspara.home_step[m_axis];
	//脉冲工作方式设置
	m_nPulseMode    = syspara.pulse_mode[m_axis];
	m_nPulseLog     = syspara.pulse_logic[m_axis];
	m_nDirLogic     = syspara.dir_logic[m_axis];
	//限位工作方式设置
	m_bLimitN       = syspara.limit_neg[m_axis];
	m_bLimitP       = syspara.limit_pos[m_axis];
	m_nLimitLevel   = syspara.limit_logic[m_axis];
}

void SYSPARASET::RefSysParam()
{
	UpdateData();
	// 回原点启始速度，驱动速度，加速度
	syspara.fHomeStartv[m_axis]	 = (float)m_fHomeStart;
	syspara.fHomeSpeed[m_axis]   = (float)m_fHomeSpeed;
	syspara.fHomeAccTime[m_axis] = (float)m_fHomeAdd;
	//回原点搜索速度，搜索范围
	syspara.fHomelSpeed[m_axis]	 = (float)m_fSearch;
	syspara.fHomelRange[m_axis]	 = (float)m_fRang;
	//加工启始速度，驱动速度，加速度
	syspara.fAccTime[m_axis]     = (float)m_fWorkAdd;
	syspara.fSpeed[m_axis]	     = (float)m_fWorkSpeed;
	syspara.fStartv[m_axis]	     = (float)m_fWorkStart;	
	//脉冲当量
	syspara.lPulseMm[m_axis]     = (int)m_mmlPusle;   
	//回原点函数设置，Z相信号
	syspara.home[m_axis]	     = m_bHome;
	syspara.home_logic[m_axis]	 = m_nHomeLevel ;
	syspara.ucZi[m_axis]         = m_bZPhasic ;
	syspara.Zi_logic[m_axis]	 = m_nZPhasicLevel;
	syspara.home_axis[m_axis]    = m_homeaxis ;
	syspara.home_step[m_axis]	 = m_homestep;
	//脉冲工作方式设置	
	syspara.pulse_mode[m_axis]	 = m_nPulseMode ;
	syspara.pulse_logic[m_axis]	 = m_nPulseLog ;
	syspara.dir_logic[m_axis]	 = m_nDirLogic ;
	//限位工作方式设置	
	syspara.limit_neg[m_axis]	 = m_bLimitN ;
	syspara.limit_pos[m_axis]	 = m_bLimitP ;
	syspara.limit_logic[m_axis]	 = m_nLimitLevel ;
}






BOOL SYSPARASET::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	adt8840a_upload_sysfile(devnum,0,"",&syspara);
	ShowSystemParam();	//载入当前参数
	UpdateData(FALSE);	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/////////////////////////////////////////////////////////////////////////////
// SYSPARASET message handlers

void SYSPARASET::OnRadio5() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	syspara.pulse_mode[m_axis]=m_nPulseMode;
}

void SYSPARASET::OnRadio6() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	syspara.pulse_mode[m_axis]=m_nPulseMode;
}

void SYSPARASET::OnRadio8() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	syspara.pulse_logic[m_axis]=m_nPulseLog;
}

void SYSPARASET::OnRadio9() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	syspara.pulse_logic[m_axis]=m_nPulseLog;
}

void SYSPARASET::OnRadio10() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	syspara.dir_logic[m_axis]=m_nDirLogic;
}

void SYSPARASET::OnRadio11() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	syspara.dir_logic[m_axis]=m_nDirLogic;
}


void SYSPARASET::OnChangeEdit12() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
	syspara.lPulseMm[m_axis]=m_mmlPusle;
}

void SYSPARASET::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CEvc8840Dlg* pDlg=(CEvc8840Dlg*)(this->GetParent());
	RefSysParam();	
	if(!pDlg->m_speedmode)
	{
		AfxMessageBox(_T("当前系统参数已更新，但未保存为系统文件中!"));
		adt8840a_download_sysfile(devnum,0,&syspara,"");
	}
	else 
	{
		RefSysParam();	
		adt8840a_download_sysfile(devnum,1,&syspara,"");
		AfxMessageBox(_T("参数写入系统文件成功!"));	
	}	
}

void SYSPARASET::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CEvc8840Dlg* pDlg=(CEvc8840Dlg*)(this->GetParent());
	RefSysParam();	
	if(!pDlg->m_speedmode)
	{
		AfxMessageBox(_T("当前系统参数已更新，但未保存为系统文件中!"));
		adt8840a_upload_sysfile(devnum,0,"",&syspara);
	}
	else 
	{
		RefSysParam();	
		adt8840a_upload_sysfile(devnum,1,"",&syspara);
		AfxMessageBox(_T("参数写入系统文件成功!"));	
	}
}
