// AxisMotionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AxisMotion.h"
#include "AxisMotionDlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAxisMotionDlg dialog


U8 bSlotNo = 0;


#define _CHKAPI(RetCode, FunName, AxisNo) \
	ret = RetCode; \
	if( ret != I8196_SUCCESS ) \
	{ \
		TRACE(L"%s(0x%02x) return: %d\n", FunName, AxisNo, ret);\
		m_stcFunErr.SetBitmap(m_hBmpErr); \
	};


CAxisMotionDlg::CAxisMotionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAxisMotionDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAxisMotionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_cmbSlotNo, m_cmbSlotNo);

	DDX_Control(pDX, IDC_chkSCurve, m_chkSCurve);
	DDX_Control(pDX, IDC_stcFunErr, m_stcFunErr);
}

BEGIN_MESSAGE_MAP(CAxisMotionDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnReset0, IDC_btnReset5, OnbtnReset)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnMove0, IDC_btnMove5, OnbtnMove)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnStop0, IDC_btnStop5, OnbtnStop)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnSvon0, IDC_btnSvon5, OnbtnSvon)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnRALM0, IDC_btnRALM5, OnbtnRALM)

	ON_CBN_SELCHANGE(IDC_cmbSlotNo, &CAxisMotionDlg::OnCbnSelchangecmbslotno)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_btnResetAll, &CAxisMotionDlg::OnBnClickedbtnresetall)
	ON_BN_CLICKED(IDC_btnStopAll, &CAxisMotionDlg::OnBnClickedbtnstopall)
	ON_STN_CLICKED(IDC_stcFunErr, &CAxisMotionDlg::OnStnClickedstcfunerr)
END_MESSAGE_MAP()


// CAxisMotionDlg message handlers

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

	// 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

	int i = 0;

	eRET ret = I8196_SUCCESS;
	CString str;

	BOOL flgExistCard = FALSE;	

	m_hBmpErr = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpRed));
	m_hBmpOff = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpBlack));


	for(i = 0; i < MAX_AXIS_QTY; i++)
	{
		SetDlgItemInt(IDC_edtSpeed0 + i, 10000);
		SetDlgItemInt(IDC_edtAcc0 + i, 20000);
		SetDlgItemInt(IDC_edtDist0 + i, 20000);

	}
	((CButton *)GetDlgItem(IDC_rdoR))->SetCheck(TRUE);

	m_chkSCurve.SetCheck(FALSE);



	m_cmbSlotNo.ResetContent();
	for (i = 0; i < 9; i++) // MAX_SLOT_QTY = 9
	{
		ret = i8196f_check_card_in_slot(i);
		if (ret == I8196_SUCCESS)
		{
			str.Format(L"%d", i);
			m_cmbSlotNo.AddString(str);
			flgExistCard = TRUE;

			InitialI8196F(i);
		}
	}

	if (flgExistCard == FALSE)
	{
		m_cmbSlotNo.AddString(L"NULL");
		m_cmbSlotNo.SetCurSel(0);

		MessageBox(L"No I-8196F!", L"ERROR", MB_OK);
	}
	else
	{
		m_cmbSlotNo.SetCurSel(0);		
		m_cmbSlotNo.GetLBText(0, str);
		swscanf_s(str.GetBuffer(), L"%d", &bSlotNo);


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

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CAxisMotionDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_AXISMOTION_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_AXISMOTION_DIALOG));
	}
}
#endif

void CAxisMotionDlg::InitialI8196F(U8 SlotNo)
{
	eRET ret;

	int i = 0;

	U8 AxisNo;

	_CHKAPI(i8196f_registration(SlotNo),
		L"i8196f_registration", 0);

	_CHKAPI(i8196f_load_power_on_cfg(SlotNo, DEFAULT_POWER_ON_SETTING),
		L"i8196f_load_power_on_cfg",0);

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		AxisNo = 0x01<<i;

		_CHKAPI(i8196f_set_pls_cfg( SlotNo, (eSINGLE_AXIS)AxisNo, PULSE_MODE_AB_DIVID_4, PULSE_LOGIC_ACTIVE_HIGH, PULSE_FORWARD_ACTIVE_HIGH), 
			L"i8196f_set_pls_cfg", AxisNo);

		_CHKAPI(i8196f_set_enc_cfg( SlotNo, (eSINGLE_AXIS)AxisNo, ENCODER_MODE_AB_DIVID_4),
			L"i8196f_set_enc_cfg", AxisNo);

		_CHKAPI(i8196f_set_limit( SlotNo, (eSINGLE_AXIS)AxisNo, LOGIC_ACTIVE_LOW, STOP_SUDDEN ),
			L"i8196f_set_limit", AxisNo);

		_CHKAPI(i8196f_set_alarm( SlotNo, (eSINGLE_AXIS)AxisNo, DISABLE, LOGIC_ACTIVE_LOW ),
			L"i8196f_set_alarm", AxisNo);

		_CHKAPI(i8196f_set_inp( SlotNo, (eSINGLE_AXIS)AxisNo, DISABLE, LOGIC_ACTIVE_LOW ),
			L"i8196f_set_inp", AxisNo);

		_CHKAPI(i8196f_set_ready( SlotNo, (eSINGLE_AXIS)AxisNo, DISABLE, LOGIC_ACTIVE_LOW ),
			L"i8196f_set_ready", AxisNo);
	}
}

void CAxisMotionDlg::OnCbnSelchangecmbslotno()
{
	CString str;
	int Index = m_cmbSlotNo.GetCurSel();

	m_cmbSlotNo.GetLBText(Index, str);
	swscanf_s(str.GetBuffer(), L"%d", &bSlotNo);
}

void CAxisMotionDlg::OnTimer(UINT_PTR nIDEvent)
{
	eRET ret = I8196_SUCCESS;
	int i = 0;
	CString str;
	U8 AxisNo = 0;
	U16 wDIStatus[MAX_AXIS_QTY] = {0};
	I32 LP[MAX_AXIS_QTY] = {0}, EP[MAX_AXIS_QTY] = {0}, Spd[MAX_AXIS_QTY] = {0};
	U8 bDone = 0;
	U16 wStopStatus = 0;
	eCHANNEL_STATE eOutSts;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{

		AxisNo = 0x01<<i;

		_CHKAPI(i8196f_get_cmdcounter( bSlotNo, (eSINGLE_AXIS)AxisNo, &LP[i]),
			L"i8196f_get_cmdcounter", AxisNo);
		SetDlgItemInt(IDC_edtGetCMD0+i, LP[i]);

		_CHKAPI(i8196f_get_enccounter( bSlotNo, (eSINGLE_AXIS)AxisNo, &EP[i]),
			L"i8196f_get_enccounter", AxisNo);
		SetDlgItemInt(IDC_edtGetENC0+i, EP[i]);

		_CHKAPI(i8196f_get_speed(bSlotNo, (eSINGLE_AXIS)AxisNo, &Spd[i]),
			L"i8196f_get_speed", AxisNo);
		SetDlgItemInt(IDC_edtGetSpd0+i, Spd[i]);

		_CHKAPI(i8196f_get_motion_done(bSlotNo, (eSINGLE_AXIS)AxisNo, &bDone, &wStopStatus),
			L"i8196f_get_motion_done", AxisNo);
		str.Format(_T("(%d)0x%04x"),bDone,wStopStatus);
		SetDlgItemText(IDC_edtGetMCsts0+i,str);
		
		_CHKAPI(i8196f_get_mdi_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &wDIStatus[i]),
			L"i8196f_get_mdi_status", AxisNo);
		//str.Format(_T("0x%04x"),wDIStatus[i]);
		//SetDlgItemText(IDC_edtGetIOSts0+i,str);

// 		_CHKAPI(i8196f_get_servo_on_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &eOutSts),
// 			L"i8196f_get_out1_status", AxisNo);
// 		if (eOutSts == ON)
// 		{wDIStatus[i] |= 0x2000;}
// 		else
// 		{wDIStatus[i] &= (~0x2000);}
// 		_CHKAPI(i8196f_get_servo_erc_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &eOutSts),
// 			L"i8196f_get_out1_status", AxisNo);
// 		if (eOutSts == ON)
// 		{wDIStatus[i] |= 0x4000;}
// 		else
// 		{wDIStatus[i] &= (~0x4000);}
// 		_CHKAPI(i8196f_get_servo_almrst_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &eOutSts),
// 			L"i8196f_get_out1_status", AxisNo);
// 		if (eOutSts == ON)
// 		{wDIStatus[i] |= 0x8000;}
// 		else
// 		{wDIStatus[i] &= (~0x8000);}

		str.Format(_T("0x%04x"),wDIStatus[i]);
		SetDlgItemText(IDC_edtGetIOSts0+i,str);
	}

	CDialog::OnTimer(nIDEvent);
}

void CAxisMotionDlg::OnbtnReset(UINT nID)
{
	U8 AxisNo = 0x01 << (nID-IDC_btnReset0);

	ResetCnt(AxisNo);
}

void CAxisMotionDlg::OnBnClickedbtnresetall()
{
	int i = 0;
	U8 AxisNo;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		AxisNo = 0x01 << i;
		ResetCnt(AxisNo);
	}
}

void CAxisMotionDlg::ResetCnt(U8 AxisNo)
{
	I32 LogicPos = 0;
	I32 EncPos = 0;
	eRET ret = I8196_SUCCESS;

	_CHKAPI(i8196f_set_cmdcounter(bSlotNo, (eSINGLE_AXIS)AxisNo, LogicPos),
		L"i8196f_set_cmdcounter", AxisNo);
	_CHKAPI(i8196f_set_enccounter(bSlotNo, (eSINGLE_AXIS)AxisNo, EncPos),
		L"i8196f_set_enccounter", AxisNo);
}

void CAxisMotionDlg::OnbtnMove(UINT nID)
{
	eRET ret;
	int i = 0;
	int Index = nID - IDC_btnMove0;
	U8 AxisNo = 0x01 << Index;
	int Speed = GetDlgItemInt(IDC_edtSpeed0+Index);
	int Acc = GetDlgItemInt(IDC_edtAcc0+Index);
	int Dist = GetDlgItemInt(IDC_edtDist0+Index);
	U8 Direction = 0;

	if (((CButton *)GetDlgItem(IDC_rdoR))->GetCheck())
	{
		if (m_chkSCurve.GetCheck())
		{
			_CHKAPI(i8196f_s_move( bSlotNo, (eSINGLE_AXIS)AxisNo, (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
				(U32)Acc, (U32)Acc, (I32)Dist ),
				L"i8196f_s_move", AxisNo);
		}
		else
		{
			_CHKAPI(i8196f_t_move( bSlotNo, (eSINGLE_AXIS)AxisNo, (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
				(U32)Acc, (U32)Acc, (I32)Dist ),
				L"i8196f_t_move", AxisNo);
		}
		
	}
	else if (((CButton *)GetDlgItem(IDC_rdoA))->GetCheck())
	{
		if (m_chkSCurve.GetCheck())
		{
			_CHKAPI(i8196f_abs_s_move( bSlotNo, (eSINGLE_AXIS)AxisNo, (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
				(U32)Acc, (U32)Acc, (I32)Dist ),
				L"i8196f_abs_s_move", AxisNo);
		}
		else
		{
			_CHKAPI(i8196f_abs_t_move( bSlotNo, (eSINGLE_AXIS)AxisNo, (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
				(U32)Acc, (U32)Acc, (I32)Dist ),
				L"i8196f_abs_t_move", AxisNo);
		}		
	}
	else if (((CButton *)GetDlgItem(IDC_rdoV))->GetCheck())
	{
		if (Dist > 0)
		{Direction = 1;}
		else
		{Direction = 0;}
		//Direction = 0:reverse, 1: forward
		_CHKAPI(i8196f_velocity_move( bSlotNo, (eSINGLE_AXIS)AxisNo, (U32)(Speed/10), (U32)Speed, (U32)Acc, (eMOVE_DIRECTION)Direction ),
			L"i8196f_velocity_move", AxisNo);		
	}
}

void CAxisMotionDlg::OnbtnStop(UINT nID)
{
	U8 AxisNo = 0x01 << (nID - IDC_btnStop0);

	StopAxis(AxisNo);
}

void CAxisMotionDlg::OnBnClickedbtnstopall()
{
	int i = 0;
	U8 AxisNo;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		AxisNo = 0x01 << i;
		StopAxis(AxisNo);
	}
}

void CAxisMotionDlg::StopAxis(U8 AxisNo)
{
	eRET ret = I8196_SUCCESS;
	STOP_MODE StopMode = STOP_SUDDEN;

	// StopMode = 1: slow down then stop, 2: sudden stop
	_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)AxisNo, StopMode ),
		L"i8196f_stop_move", AxisNo);
}

void CAxisMotionDlg::OnbtnSvon(UINT nID)
{
	U8 AxisNo = 0x01 << (nID - IDC_btnSvon0);
	eRET ret = I8196_SUCCESS;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_servo_on_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &OnOff ),
		L"i8196f_get_servo_on_status", AxisNo);

	if (OnOff == ON)
	{_CHKAPI(i8196f_set_servo_on( bSlotNo, (eSINGLE_AXIS)AxisNo, OFF ),
	L"i8196f_set_servo_on", AxisNo);}
	else
	{_CHKAPI(i8196f_set_servo_on( bSlotNo, (eSINGLE_AXIS)AxisNo, ON ),
	L"i8196f_set_servo_on", AxisNo);}
	
}

void CAxisMotionDlg::OnbtnRALM(UINT nID)
{
	U8 AxisNo = 0x01 << (nID - IDC_btnRALM0);
	eRET ret = I8196_SUCCESS;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_servo_almrst_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &OnOff ),
		L"i8196f_get_servo_almrst_status", AxisNo);

	if (OnOff == ON)
	{_CHKAPI(i8196f_set_alarm_reset( bSlotNo, (eSINGLE_AXIS)AxisNo, OFF ),
	L"i8196f_set_alarm_reset", AxisNo);}
	else
	{_CHKAPI(i8196f_set_alarm_reset( bSlotNo, (eSINGLE_AXIS)AxisNo, ON ),
	L"i8196f_set_alarm_reset", AxisNo);}
}

void CAxisMotionDlg::OnStnClickedstcfunerr()
{
	m_stcFunErr.SetBitmap(m_hBmpOff);
	//m_stcFunErr.SetBitmap(m_hBmpErr);

	TRACE(L"\n Reset Fun. Error\n");
}
