// Manual_Test.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "Manual_Test.h"
#include "ValExt.h"
#include "WinConSDK.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//int Star_A;
bool Auto_GetT;
WORD m_nTimerT;
//===============================
BYTE Xflag[20],X_new[20],X_old[20]={0,0,0,0,0,0,0,0,0,0};
BYTE Yflag[20],Y_new[20],Y_old[20]={0,0,0,0,0,0,0,0,0,0};



/////////////////////////////////////////////////////////////////////////////
// Manual_Test dialog


Manual_Test::Manual_Test(CWnd* pParent /*=NULL*/)
	: CDialog(Manual_Test::IDD, pParent)
{
	//{{AFX_DATA_INIT(Manual_Test)
	m_XAcc = 0;
	m_XE = 0;
	m_XP = 0;
	m_XSpeed = 0;
	m_YAcc = 0;
	m_YE = 0;
	m_YP = 0;
	m_YSpeed = 0;
	m_RA = 0;
	m_SA = 0;
	//}}AFX_DATA_INIT
}


void Manual_Test::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(Manual_Test)
	DDX_Control(pDX, IDC_CB_RA, m_CB_RA);
	DDX_Control(pDX, IDC_CB_SA, m_CB_SA);
	DDX_Control(pDX, IDC_SXSLMT_P, m_SXSLMT_P);
	DDX_Control(pDX, IDC_SXDRV, m_SXDRV);
	DDX_Control(pDX, IDC_SYSLMT_P, m_SYSLMT_P);
	DDX_Control(pDX, IDC_SYSLMT_M, m_SYSLMT_M);
	DDX_Control(pDX, IDC_SYREADY, m_SYREADY);
	DDX_Control(pDX, IDC_SYNHome, m_SYNHome);
	DDX_Control(pDX, IDC_SYIN3, m_SYIN3);
	DDX_Control(pDX, IDC_SYHome, m_SYHome);
	DDX_Control(pDX, IDC_SYHLMT_P, m_SYHLMT_P);
	DDX_Control(pDX, IDC_SYHLMT_M, m_SYHLMT_M);
	DDX_Control(pDX, IDC_SYDRV, m_SYDRV);
	DDX_Control(pDX, IDC_SYALARM, m_SYALARM);
	DDX_Control(pDX, IDC_SXIN3, m_SXIN3);
	DDX_Control(pDX, IDC_SXEMG, m_SXEMG);
	DDX_Control(pDX, IDC_SXSLMT_M, m_SXSLMT_M);
	DDX_Control(pDX, IDC_SXREADY, m_SXREADY);
	DDX_Control(pDX, IDC_SXNHome, m_SXNHome);
	DDX_Control(pDX, IDC_SXHome, m_SXHome);
	DDX_Control(pDX, IDC_SXHLMT_M, m_SXHLMT_M);
	DDX_Control(pDX, IDC_SXALARM, m_SXALARM);
	DDX_Control(pDX, IDC_ON, m_ON);
	DDX_Control(pDX, IDC_OFF, m_OFF);
	DDX_Control(pDX, IDC_SXHLMT_P, m_SXHLMT_P);
	DDX_Control(pDX, IDC_Auto, m_CAuto);
	DDX_Text(pDX, IDC_XAcc, m_XAcc);
	DDX_Text(pDX, IDC_XE, m_XE);
	DDX_Text(pDX, IDC_XP, m_XP);
	DDX_Text(pDX, IDC_XSpeed, m_XSpeed);
	DDX_Text(pDX, IDC_YAcc, m_YAcc);
	DDX_Text(pDX, IDC_YE, m_YE);
	DDX_Text(pDX, IDC_YP, m_YP);
	DDX_Text(pDX, IDC_YSpeed, m_YSpeed);
	DDX_Text(pDX, IDC_RA, m_RA);
	DDX_Text(pDX, IDC_SA, m_SA);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Manual_Test, CDialog)
	//{{AFX_MSG_MAP(Manual_Test)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_Auto, OnAuto)
	ON_BN_CLICKED(IDC_Manual_Get, OnManualGet)
	ON_BN_CLICKED(IDC_X_Jog, OnXJog)
	ON_BN_CLICKED(IDC_Y_Jog, OnYJog)
	ON_BN_CLICKED(IDC_X_Jog_Stop, OnXJogStop)
	ON_BN_CLICKED(IDC_Y_Jog_Stop, OnYJogStop)
	ON_BN_CLICKED(IDC_Servo_On_All, OnServoOnAll)
	ON_BN_CLICKED(IDC_Servo_Off_All, OnServoOffAll)
	ON_BN_CLICKED(IDC_Write_SA, OnWriteSA)
	ON_BN_CLICKED(IDC_Read_RA, OnReadRA)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Manual_Test message handlers

void Manual_Test::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent==1)
	{
	m_XP=i8092MF_GET_LP(cardNo, AXIS_X);
	m_XSpeed=i8092MF_GET_CV(cardNo, AXIS_X);
	m_XAcc=i8092MF_GET_CA(cardNo, AXIS_X);
	m_XE=i8092MF_GET_EP(cardNo, AXIS_X);

	m_YP=i8092MF_GET_LP(cardNo, AXIS_Y);
	m_YSpeed=i8092MF_GET_CV(cardNo, AXIS_Y);
	m_YAcc=i8092MF_GET_CA(cardNo, AXIS_Y);
	m_YE=i8092MF_GET_EP(cardNo, AXIS_Y);

	OnManualGet();
	UpdateData(false);
    }
    //========================================================

	

	CDialog::OnTimer(nIDEvent);
}

void Manual_Test::OnAuto() 
{
	// TODO: Add your control notification handler code here
	Auto_GetT= ! Auto_GetT;
	if (Auto_GetT)
    {
		SetTimer(1, 50, 0);
		//SetTimer(2, 150, 0);	
		m_CAuto.SetWindowText(_T("Auto Update Off"));
	}
	else
	{
		KillTimer(1);  
		//KillTimer(2);
		m_CAuto.SetWindowText(_T("Auto Update On"));
	}


//	Star_A=100;
}

void Manual_Test::OnManualGet() 
{
	// TODO: Add your control notification handler code here
	/*/
	nType:		
	0 a DRIVING		(檢查I8092有沒有輸出脈波)
	1 a LIMIT+   	(檢查有沒有碰觸前極限)
	2 a LIMIT-		(檢查有沒有碰觸後極限)
	3 a EMERGENCY   (檢查警急停止信號)
	4 a ALARM		(檢查警報信號)
	5 a HOME 		(檢查原點信號)
	6 a NEAR HOME	(檢查近原點信號)
	7 a IN3			(檢查IN3信號)
	8 a INPOS		(檢查INPOS信號)
	/*/
	//
	BYTE i;
	//Xflag[10],X_new[10],X_old[10];
	//===========  For AXIS_X  =============================================
	i=0;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXDRV.SetBitmap(m_ON.GetBitmap());
		else
			m_SXDRV.SetBitmap(m_OFF.GetBitmap());
	}
	//============================================
	i=1;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXHLMT_P.SetBitmap(m_ON.GetBitmap());
		else
			m_SXHLMT_P.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	i=2;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXHLMT_M.SetBitmap(m_ON.GetBitmap());
		else
			m_SXHLMT_M.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	i=3;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXEMG.SetBitmap(m_ON.GetBitmap());
		else
			m_SXEMG.SetBitmap(m_OFF.GetBitmap()); 

	}
	//============================================
	i=4;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXALARM.SetBitmap(m_ON.GetBitmap());
		else
			m_SXALARM.SetBitmap(m_OFF.GetBitmap()); 

	}
	//============================================
	i=5;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXHome.SetBitmap(m_ON.GetBitmap());
		else
			m_SXHome.SetBitmap(m_OFF.GetBitmap()); 
	
	}
	//============================================
	i=6;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXNHome.SetBitmap(m_ON.GetBitmap());
		else
			m_SXNHome.SetBitmap(m_OFF.GetBitmap()); 
	
	}
	//============================================
	i=7;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXIN3.SetBitmap(m_ON.GetBitmap());
		else
			m_SXIN3.SetBitmap(m_OFF.GetBitmap()); 
	}
	i=8;
	X_new[i]=i8092MF_GET_DI(cardNo, AXIS_X, i);
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXREADY.SetBitmap(m_ON.GetBitmap());
		else
			m_SXREADY.SetBitmap(m_OFF.GetBitmap()); 
	}
	i=11;
	WORD KK=i8092MF_GET_ERROR_CODE(cardNo, AXIS_X);
	X_new[i]=KK & 0x1;
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXSLMT_P.SetBitmap(m_ON.GetBitmap());
		else
			m_SXSLMT_P.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	i=12;
	if (KK & 0x2)
		X_new[i]=1;
	else
		X_new[i]=0;
	if (X_new[i]!=X_old[i])	
	{
		X_old[i]=X_new[i];
		if (X_old[i] == 1)
			m_SXSLMT_M.SetBitmap(m_ON.GetBitmap());
		else
			m_SXSLMT_M.SetBitmap(m_OFF.GetBitmap()); 
	}
    //===========  For AXIS_Y  =============================================
	i=0;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYDRV.SetBitmap(m_ON.GetBitmap());
		else
			m_SYDRV.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	i=1;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYHLMT_P.SetBitmap(m_ON.GetBitmap());
		else
			m_SYHLMT_P.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	i=2;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYHLMT_M.SetBitmap(m_ON.GetBitmap());
		else
			m_SYHLMT_M.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	//if (i8092MF_GET_DI(cardNo, AXIS_Y, 3) == YES)
	//	m_SYEMG.SetBitmap(m_ON.GetBitmap());
	//else
	//	m_SYEMG.SetBitmap(m_OFF.GetBitmap());
	//============================================
	//============================================
	i=4;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYALARM.SetBitmap(m_ON.GetBitmap());
		else
			m_SYALARM.SetBitmap(m_OFF.GetBitmap()); 
	}
	//=============================================
	i=5;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYHome.SetBitmap(m_ON.GetBitmap());
		else
			m_SYHome.SetBitmap(m_OFF.GetBitmap()); 
	}
	//=============================================
	i=6;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYNHome.SetBitmap(m_ON.GetBitmap());
		else
			m_SYNHome.SetBitmap(m_OFF.GetBitmap()); 
	}
	//=============================================
	i=7;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYIN3.SetBitmap(m_ON.GetBitmap());
		else
			m_SYIN3.SetBitmap(m_OFF.GetBitmap()); 
	}
	//================================================
	i=8;
	Y_new[i]=i8092MF_GET_DI(cardNo, AXIS_Y, i);
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYREADY.SetBitmap(m_ON.GetBitmap());
		else
			m_SYREADY.SetBitmap(m_OFF.GetBitmap()); 
	}
	//============================================
	i=11;
	KK=i8092MF_GET_ERROR_CODE(cardNo, AXIS_Y);
	Y_new[i]=KK & 0x1;
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYSLMT_P.SetBitmap(m_ON.GetBitmap());
		else
			m_SYSLMT_P.SetBitmap(m_OFF.GetBitmap()); 
	}
    //============================================
	i=12;
	if (KK & 0x2)
		Y_new[i]=1;
	else
		Y_new[i]=0;
	if (Y_new[i]!=Y_old[i])	
	{
		Y_old[i]=Y_new[i];
		if (Y_old[i] == 1)
			m_SYSLMT_M.SetBitmap(m_ON.GetBitmap());
		else
			m_SYSLMT_M.SetBitmap(m_OFF.GetBitmap()); 
	}
  
}

void Manual_Test::OnXJog() 
{
	// TODO: Add your control notification handler code here
	BYTE m_Axis=AXIS_X;

	i8092MF_SET_MAX_V(cardNo, m_Axis, 320000); 
	i8092MF_NORMAL_SPEED(cardNo, m_Axis, 0);	//set axis as Symmetrical T curve mode
	i8092MF_SET_A(cardNo, m_Axis, 500000);		//set Acc =50000 PPS/S 
	i8092MF_SET_V(cardNo, m_Axis, 100000); 
	i8092MF_SET_SV(cardNo, m_Axis, 50000); 
	i8092MF_EXD_MP(cardNo, AXIS_X, 100);
}
void Manual_Test::OnXJogStop() 
{
	// TODO: Add your control notification handler code here
	i8092MF_EXD_DISABLE(cardNo, AXIS_X);
}


void Manual_Test::OnYJog() 
{
	// TODO: Add your control notification handler code here
	BYTE m_Axis=AXIS_Y;
	i8092MF_SET_MAX_V(cardNo, m_Axis, 320000); 
	i8092MF_NORMAL_SPEED(cardNo, m_Axis, 0);	//set axis as Symmetrical T curve mode
	i8092MF_SET_A(cardNo, m_Axis, 500000);		//set Acc =50000 PPS/S 
	i8092MF_SET_V(cardNo, m_Axis, 100000);
	i8092MF_SET_SV(cardNo, m_Axis, 50000); 
	i8092MF_EXD_MP(cardNo, AXIS_Y, 100);
}
void Manual_Test::OnYJogStop() 
{
	// TODO: Add your control notification handler code here
	i8092MF_EXD_DISABLE(cardNo, AXIS_Y);
}









void Manual_Test::OnServoOnAll() 
{
	// TODO: Add your control notification handler code here
	i8092MF_SERVO_ON(cardNo, 0x1);
	i8092MF_SERVO_ON(cardNo, 0x2);
	i8092MF_SERVO_ON(cardNo, 0x3);
}

void Manual_Test::OnServoOffAll() 
{
	// TODO: Add your control notification handler code here
	i8092MF_SERVO_OFF(cardNo, 0x3);
}



BOOL Manual_Test::OnInitDialog() 
{
	CDialog::OnInitDialog();
	i8092MF_SET_V(cardNo, AXIS_XY,80000); 
	i8092MF_SET_A(cardNo, AXIS_XY,800000); 
	int i;
	for (i=0;i<20;i++)
	{
		X_old[i]=0;
		Y_old[i]=0;
	}
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void Manual_Test::OnWriteSA() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
    WORD data;
    if (m_SA<0)
		data=0;
	else if(m_SA>65535)
		data=65535;
	else
		data=(WORD)m_SA;
	i8092MF_FRNET_SA(cardNo, m_CB_SA.GetCurSel(), data);
	//I8094.SET_SLMT(cardNo,Select_Axis,m_Soft_F,m_Soft_R,m_StopMode.GetCurSel());
}

void Manual_Test::OnReadRA() 
{
	// TODO: Add your control notification handler code here
	m_RA=i8092MF_FRNET_RA(cardNo, m_CB_RA.GetCurSel()+8);
	UpdateData(false);
}
