// HomingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Homing.h"
#include "HomingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CHomingDlg dialog

U8 bSlotNo = 0;
eSINGLE_AXIS eAxisNo = AXIS_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); \
	}

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

void CHomingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_cmbAxisNo, m_cmbAxisNo);
	DDX_Control(pDX, IDC_cmbSlotNo, m_cmbSlotNo);
	DDX_Control(pDX, IDC_stcFunErr, m_stcFunErr);
}

BEGIN_MESSAGE_MAP(CHomingDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_cmbSlotNo, &CHomingDlg::OnCbnSelchangecmbslotno)
	ON_CBN_SELCHANGE(IDC_cmbAxisNo, &CHomingDlg::OnCbnSelchangecmbaxisno)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_btnReset, &CHomingDlg::OnBnClickedbtnreset)
	ON_BN_CLICKED(IDC_btnSvon, &CHomingDlg::OnBnClickedbtnsvon)
	ON_BN_CLICKED(IDC_btnRALM, &CHomingDlg::OnBnClickedbtnralm)

	ON_BN_CLICKED(IDC_btnMove, &CHomingDlg::OnBnClickedbtnmove)
	ON_BN_CLICKED(IDC_btnStop, &CHomingDlg::OnBnClickedbtnstop)
	ON_BN_CLICKED(IDC_btnSetHome, &CHomingDlg::OnBnClickedbtnsethome)
	ON_BN_CLICKED(IDC_btnHomeMove, &CHomingDlg::OnBnClickedbtnhomemove)
	ON_BN_CLICKED(IDC_btnStopHome, &CHomingDlg::OnBnClickedbtnstophome)
	ON_STN_CLICKED(IDC_stcFunErr, &CHomingDlg::OnStnClickedstcfunerr)
END_MESSAGE_MAP()


// CHomingDlg message handlers

BOOL CHomingDlg::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;	

	for(i = 0; i < MAX_AXIS_QTY; i++)
	{
		SetDlgItemInt(IDC_edtSpeed, 10000);
		SetDlgItemInt(IDC_edtAcc, 20000);
		SetDlgItemInt(IDC_edtDist, 20000);

	}

	m_cmbAxisNo.SetCurSel(0);
	eAxisNo = AXIS_0;

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

	((CButton *)GetDlgItem(IDC_rdoHLogicH))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_rdoNHLogicH))->SetCheck(TRUE);
 	((CButton *)GetDlgItem(IDC_rdoEZLogicH))->SetCheck(TRUE);

	((CButton *)GetDlgItem(IDC_chkHomeStep1))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_chkHomeStep2))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_chkHomeStep3))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_chkHomeStep4))->SetCheck(TRUE);

	SetDlgItemInt(IDC_edtHomeOffset, 0);

	SetDlgItemInt(IDC_edtHStrSpeed, 1000);
	SetDlgItemInt(IDC_edtHAcc, 80000);
	SetDlgItemInt(IDC_edtHDec, 80000);
	SetDlgItemInt(IDC_edtHNHSpeed, 10000);
	SetDlgItemInt(IDC_edtHHSpeed, 2000);

	((CButton *)GetDlgItem(IDC_rdoHDirP))->SetCheck(TRUE);




	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 CHomingDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_HOMING_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_HOMING_DIALOG));
	}
}
#endif

void CHomingDlg::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_CW_CCW, 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),
			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_HIGH ),
			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 CHomingDlg::OnCbnSelchangecmbslotno()
{
	CString str;
	int Index = m_cmbSlotNo.GetCurSel();

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

void CHomingDlg::OnCbnSelchangecmbaxisno()
{
	U8 SelAxis = m_cmbAxisNo.GetCurSel();
	eAxisNo = (eSINGLE_AXIS)(0x01 << SelAxis);
}

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

	_CHKAPI(i8196f_get_cmdcounter( bSlotNo, eAxisNo, &LP),
		L"i8196f_get_cmdcounter", eAxisNo);
	SetDlgItemInt(IDC_edtCmdCnt, LP);

	_CHKAPI(i8196f_get_enccounter( bSlotNo, eAxisNo, &EP),
		L"i8196f_get_enccounter", eAxisNo);
	SetDlgItemInt(IDC_edtEncCnt, EP);

	_CHKAPI(i8196f_get_speed(bSlotNo, eAxisNo, &Spd),
		L"i8196f_get_speed", eAxisNo);
	SetDlgItemInt(IDC_edtCurSpeed, Spd);

	_CHKAPI(i8196f_get_motion_done(bSlotNo, eAxisNo, &bDone, &wStopStatus),
		L"i8196f_get_motion_done", eAxisNo);
	str.Format(_T("(%d)0x%04x"),bDone,wStopStatus);
	SetDlgItemText(IDC_edtMotionSts,str);

	_CHKAPI(i8196f_get_mdi_status(bSlotNo, eAxisNo, &wDIStatus),
		L"i8196f_get_mdi_status", eAxisNo);
	//str.Format(_T("0x%04x"),wDIStatus);
	//SetDlgItemText(IDC_edtIOSts,str);

	_CHKAPI(i8196f_get_servo_on_status(bSlotNo, eAxisNo, &eOutSts),
		L"i8196f_get_out1_status", eAxisNo);
	if (eOutSts == ON)
	{wDIStatus |= 0x2000;}
	else
	{wDIStatus &= (~0x2000);}
	_CHKAPI(i8196f_get_servo_erc_status(bSlotNo, eAxisNo, &eOutSts),
		L"i8196f_get_out1_status", eAxisNo);
	if (eOutSts == ON)
	{wDIStatus |= 0x4000;}
	else
	{wDIStatus &= (~0x4000);}
	_CHKAPI(i8196f_get_servo_almrst_status(bSlotNo, eAxisNo, &eOutSts),
		L"i8196f_get_out1_status", eAxisNo);
	if (eOutSts == ON)
	{wDIStatus |= 0x8000;}
	else
	{wDIStatus &= (~0x8000);}

	str.Format(_T("0x%04x"),wDIStatus);
	SetDlgItemText(IDC_edtIOSts,str);


	if (wDIStatus & 0x0008)
	{ ((CStatic *)GetDlgItem(IDC_stcEMG))->SetBitmap(m_hBmpOnDI);}
	else
	{ ((CStatic *)GetDlgItem(IDC_stcEMG))->SetBitmap(m_hBmpOff);}

	if (wDIStatus & 0x0020)
	{ ((CStatic *)GetDlgItem(IDC_stcORG))->SetBitmap(m_hBmpOnDI);}
	else
	{ ((CStatic *)GetDlgItem(IDC_stcORG))->SetBitmap(m_hBmpOff);}

	if (wDIStatus & 0x0040)
	{ ((CStatic *)GetDlgItem(IDC_stcSLD))->SetBitmap(m_hBmpOnDI);}
	else
	{ ((CStatic *)GetDlgItem(IDC_stcSLD))->SetBitmap(m_hBmpOff);}

	if (wDIStatus & 0x0100)
	{ ((CStatic *)GetDlgItem(IDC_stcEZ))->SetBitmap(m_hBmpOnDI);}
	else
	{ ((CStatic *)GetDlgItem(IDC_stcEZ))->SetBitmap(m_hBmpOff);}

	if (wDIStatus & 0x0002)
	{ ((CStatic *)GetDlgItem(IDC_stcPEL))->SetBitmap(m_hBmpOnDI);}
	else
	{ ((CStatic *)GetDlgItem(IDC_stcPEL))->SetBitmap(m_hBmpOff);}

	if (wDIStatus & 0x0004)
	{ ((CStatic *)GetDlgItem(IDC_stcMEL))->SetBitmap(m_hBmpOnDI);}
	else
	{ ((CStatic *)GetDlgItem(IDC_stcMEL))->SetBitmap(m_hBmpOff);}

	CDialog::OnTimer(nIDEvent);
}

void CHomingDlg::OnBnClickedbtnreset()
{
	I32 LogicPos = 0;
	I32 EncPos = 0;
	eRET ret = I8196_SUCCESS;

	_CHKAPI(i8196f_set_cmdcounter(bSlotNo, eAxisNo, LogicPos),
		L"i8196f_set_cmdcounter", eAxisNo);
	_CHKAPI(i8196f_set_enccounter(bSlotNo, eAxisNo, EncPos),
		L"i8196f_set_enccounter", eAxisNo);

}

void CHomingDlg::OnBnClickedbtnsvon()
{
	
	eRET ret = I8196_SUCCESS;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_servo_on_status(bSlotNo, eAxisNo, &OnOff ),
		L"i8196f_get_servo_on_status", eAxisNo);

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

void CHomingDlg::OnBnClickedbtnralm()
{
	eRET ret = I8196_SUCCESS;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_servo_almrst_status(bSlotNo, eAxisNo, &OnOff ),
		L"i8196f_get_servo_almrst_status", eAxisNo);

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

void CHomingDlg::OnBnClickedbtnmove()
{
	eRET ret;
	int Speed = GetDlgItemInt(IDC_edtSpeed);
	int Acc = GetDlgItemInt(IDC_edtAcc);
	int Dist = GetDlgItemInt(IDC_edtDist);

	_CHKAPI(i8196f_t_move( bSlotNo, eAxisNo, (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
			(U32)Acc, (U32)Acc, (I32)Dist ),
			L"i8196f_t_move", eAxisNo);
}

void CHomingDlg::OnBnClickedbtnstop()
{
	eRET ret = I8196_SUCCESS;
	STOP_MODE StopMode = STOP_SUDDEN;

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

void CHomingDlg::OnBnClickedbtnsethome()
{
	int i = 0;
	I16 ret = 0;
	eLOGIC_ACTIVE_LEVEL HomeLogic, SLDLogic, EZLogic;
	U8 HomeSteps = 0;
	I32 Step4Offset = 0;

	if (((CButton *)GetDlgItem(IDC_rdoHLogicH))->GetCheck())
	{HomeLogic = LOGIC_ACTIVE_HIGH;}
	else
	{HomeLogic = LOGIC_ACTIVE_LOW;}

	if (((CButton *)GetDlgItem(IDC_rdoNHLogicH))->GetCheck())
	{SLDLogic = LOGIC_ACTIVE_HIGH;}
	else
	{SLDLogic = LOGIC_ACTIVE_LOW;}

	if (((CButton *)GetDlgItem(IDC_rdoEZLogicH))->GetCheck())
	{EZLogic = LOGIC_ACTIVE_HIGH;}
	else
	{EZLogic = LOGIC_ACTIVE_LOW;}

	if (((CButton *)GetDlgItem(IDC_chkHomeStep1))->GetCheck())
	{HomeSteps |= AUTO_HOME_STEP1_ENABLE;}

	if (((CButton *)GetDlgItem(IDC_chkHomeStep2))->GetCheck())
	{HomeSteps |= AUTO_HOME_STEP2_ENABLE;}

	if (((CButton *)GetDlgItem(IDC_chkHomeStep3))->GetCheck())
	{HomeSteps |= AUTO_HOME_STEP3_ENABLE;}

	if (((CButton *)GetDlgItem(IDC_chkHomeStep4))->GetCheck())
	{HomeSteps |= AUTO_HOME_STEP4_ENABLE;}

	Step4Offset = GetDlgItemInt(IDC_edtHomeOffset);

	_CHKAPI(i8196f_set_home_cfg(bSlotNo, eAxisNo, HomeLogic, SLDLogic, EZLogic, HomeSteps, Step4Offset),
		L"i8196f_set_home_cfg", eAxisNo);

}

void CHomingDlg::OnBnClickedbtnhomemove()
{
	eRET ret;
	int i = 0;

	U32 StartSpeed = (U32)GetDlgItemInt(IDC_edtHStrSpeed);
	U32 Acceleration = (U32)GetDlgItemInt(IDC_edtHAcc);
	U32 Deceleration = (U32)GetDlgItemInt(IDC_edtHDec);
	U32 HighSpeed = (U32)GetDlgItemInt(IDC_edtHNHSpeed);
	U32 LowSpeed = (U32)GetDlgItemInt(IDC_edtHHSpeed);
	eHOME_DIR  HomingDirection;


	if (((CButton *)GetDlgItem(IDC_rdoHDirP))->GetCheck())
	{HomingDirection = AUTO_HOME_FORWARD;}
	else
	{HomingDirection = AUTO_HOME_REVERSE;}

	_CHKAPI(i8196f_home_start(bSlotNo, eAxisNo, StartSpeed, Acceleration, Deceleration,		
		HighSpeed, LowSpeed, HomingDirection), 
		L"i8196f_home_start", eAxisNo);

}

void CHomingDlg::OnBnClickedbtnstophome()
{
	I16 ret = 0;
	_CHKAPI(i8196f_stop_move( bSlotNo, eAxisNo, STOP_SLOWDOWN ),
		L"i8196f_stop_move", eAxisNo);

}

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

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