
// BankArcDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BankArc.h"
#include "BankArcDlg.h"
#include "afxdialogex.h"
#include "CSmc.h"
#include "CSmcdef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

long	dwRet;
short	ArcStartDir;
short	MotionType;
short	Id;
short	AxisNo;
short	BankNum;
CString	szErrorString;
char	tmp[256];
TCHAR	sztmp[256];
double	ArcSpeed;
long	Center_X;
long	Center_Y;
long	End_X;
long	End_Y;

// CBankArcDlg dialog



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

void CBankArcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO_OPTION_SINGLE, m_Radio_OptionSingle);
	DDX_Control(pDX, IDC_RADIO_DIR_CW, m_Radio_DirCw);
	DDX_Control(pDX, IDC_BUTTON_COM_MOVE, m_Button_ComMove);
	DDX_Control(pDX, IDC_BUTTON_COM_STOP, m_Button_ComStop);
	DDX_Control(pDX, IDC_COMBO_TARGET_AXIS, m_Combo_TargetAxis);
	DDX_Control(pDX, IDC_EDIT_ARC_SPEED, m_Edit_ArcSpeed);
	DDX_Control(pDX, IDC_STATIC_COMMENT, m_Static_Comment);
	DDX_Control(pDX, IDC_EDIT_TARGET_AXIS, m_Edit_TargetAxis);
	DDX_Control(pDX, IDC_EDIT_DEVICENAME, m_Edit_DeviceName);
	DDX_Control(pDX, IDC_COMBO_TARGET_BANKNUM, m_Combo_TargetBankNum);
}

BEGIN_MESSAGE_MAP(CBankArcDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_COM_MOVE, &CBankArcDlg::OnBnClickedButtonComMove)
	ON_BN_CLICKED(IDC_BUTTON_COM_STOP, &CBankArcDlg::OnBnClickedButtonComStop)
	ON_BN_CLICKED(IDC_RADIO_DIR_CW, &CBankArcDlg::OnBnClickedRadioDirCw)
	ON_BN_CLICKED(IDC_RADIO_DIR_CCW, &CBankArcDlg::OnBnClickedRadioDirCcw)
	ON_BN_CLICKED(IDC_RADIO_OPTION_SINGLE, &CBankArcDlg::OnBnClickedRadioOptionSingle)
	ON_BN_CLICKED(IDC_RADIO_OPTION_LOOP, &CBankArcDlg::OnBnClickedRadioOptionLoop)
	ON_EN_CHANGE(IDC_EDIT_DEVICENAME, &CBankArcDlg::OnEnChangeEditDevicename)
	ON_CBN_SELCHANGE(IDC_COMBO_TARGET_AXIS, &CBankArcDlg::OnCbnSelchangeComboTargetAxis)
	ON_CBN_SELCHANGE(IDC_COMBO_TARGET_BANKNUM, &CBankArcDlg::OnCbnSelchangeComboTargetBanknum)
	ON_BN_CLICKED(IDOK, &CBankArcDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CBankArcDlg message handlers

BOOL CBankArcDlg::OnInitDialog()
{
	CDialogEx::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

	short	i = 0;
	CString wStr = _T("OK");

	//-------------
	// Initialize
	//-------------
	AxisNo = 1;

	m_Edit_ArcSpeed.SetWindowText(_T("10000"));
	m_Radio_OptionSingle.SetCheck(1);
	MotionType = CSMC_SINGLE;
	m_Radio_DirCw.SetCheck(1);
	ArcStartDir = CSMC_CW;

	_itow_s(AxisNo + 1, sztmp, 10);
	m_Edit_TargetAxis.SetWindowText(sztmp);

	//AxisNo ComboBox
	m_Combo_TargetAxis.Clear();
	for (i = 0; i < 8; i++){
		wStr.Format(_T("%d"), i + 1);
		m_Combo_TargetAxis.AddString(wStr.GetBuffer(0));
	}

	//ComboBox Initialise
	m_Combo_TargetAxis.SetCurSel(AxisNo - 1);

	BankNum = 4;
	//BankNum ComboBox
	m_Combo_TargetBankNum.Clear();
	for (i = 1; i <= 4; i++){
		wStr.Format(_T("%d"), i);
		m_Combo_TargetBankNum.AddString(wStr.GetBuffer(0));

	}

	//ComboBox Initialise
	m_Combo_TargetBankNum.SetCurSel(BankNum - 1);

	CString DeviceName;
	DeviceName = _T("SMC000");
	m_Edit_DeviceName.SetWindowText(DeviceName);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CBankArcDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBankArcDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CBankArcDlg::OnBnClickedButtonComMove()
{
	BOOL		bRet;
	CString wStr = _T("OK");

	//----------------------------------
	// Set parameters to Driver
	//----------------------------------
	bRet = SetBankParam();
	if (bRet == false){
		return;
	}

	//---------------
	// Ready motion
	//---------------
	dwRet = SmcWSetBankReady(Id, AxisNo, MotionType);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWSetBankReady = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return;
	}

	dwRet = SmcWGetBankArcPoint(Id, AxisNo, 1, &ArcSpeed, &Center_X, &Center_Y, &End_X, &End_Y);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWGetBankArcPoint = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return;
	}
	_stprintf_s(sztmp, _T("%.1f"), ArcSpeed);
	m_Edit_ArcSpeed.SetWindowText(sztmp);

	//---------------
	// Start Motion
	//---------------
	dwRet = SmcWMotionStart(Id, AxisNo);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWMotionStart = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return;
	}
	m_Static_Comment.SetWindowText(wStr);
}


void CBankArcDlg::OnBnClickedButtonComStop()
{
	CString wStr = _T("OK");

	dwRet = SmcWMotionStop(Id, AxisNo);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWMotionStop = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return;
	}
	m_Static_Comment.SetWindowText(wStr);
}

BOOL CBankArcDlg::SetBankParam(void)
{
	short	InterAxis;
	short	InterType;
	short	AutoEndPointPull;
	short	BankNo;
	short	Reserved = 0;
	short	Coordinates;

	//----------------------------
	// Set Bank Number to Driver
	//----------------------------
	dwRet = SmcWSetBankNumber(Id, AxisNo, BankNum);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWSetBankNumber = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return false;
	}

	//----------------------------
	// Set Interpolation to Driver
	//----------------------------
	int		cal1, cal2;
	cal1 = int(pow((double)2, (double)(AxisNo - 1)));
	cal2 = int(pow((double)2, (double)AxisNo));
	InterAxis = cal1 | cal2;

	//Circular arc interpolation
	InterType = 3;

	//Automatically enter an end point pull in operation at the end of arc interpolation operation.
	AutoEndPointPull = 1;

	for (BankNo = 1; BankNo <= BankNum; BankNo++){
		dwRet = SmcWSetBankInterpolation(Id, AxisNo, BankNo, InterType, InterAxis, Reserved);
		if (dwRet){
			SmcWGetErrorString(dwRet, tmp);
			wsprintf(sztmp, _T("%S"), tmp);
			szErrorString.Format(_T("SmcWSetBankInterpolation = %d : %s"), dwRet, sztmp);
			m_Static_Comment.SetWindowText(szErrorString);
			return false;
		}
		dwRet = SmcWSetBankArcParam(Id, AxisNo, BankNo, ArcStartDir, AutoEndPointPull);
		if (dwRet){
			SmcWGetErrorString(dwRet, tmp);
			wsprintf(sztmp, _T("%S"), tmp);
			szErrorString.Format(_T("SmcWSetBankArcParam = %d : %s"), dwRet, sztmp);
			m_Static_Comment.SetWindowText(szErrorString);
			return false;
		}
	}

	//----------------------------
	// Set Arc Point to Driver
	//----------------------------
	m_Edit_ArcSpeed.GetWindowText(sztmp, 256);
	ArcSpeed = _ttoi(sztmp);
	Coordinates = 5000;

	Center_X = Coordinates;
	Center_Y = 0;
	if (ArcStartDir == CSMC_CW){
		End_X = Coordinates;
		End_Y = Coordinates;
	}
	else{
		End_X = Coordinates;
		End_Y = (-1) * Coordinates;
	}
	dwRet = SmcWSetBankArcPoint(Id, AxisNo, 1, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWSetBankArcPoint = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return false;
	}

	if (ArcStartDir == CSMC_CW){
		Center_X = 0;
		Center_Y = (-1) * Coordinates;
		End_X = Coordinates;
		End_Y = (-1) * Coordinates;
	}
	else{
		Center_X = 0;
		Center_Y = Coordinates;
		End_X = Coordinates;
		End_Y = Coordinates;
	}
	dwRet = SmcWSetBankArcPoint(Id, AxisNo, 2, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWSetBankArcPoint = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return false;
	}

	Center_X = (-1) * Coordinates;
	Center_Y = 0;
	if (ArcStartDir == CSMC_CW){
		End_X = (-1) * Coordinates;
		End_Y = (-1) * Coordinates;
	}
	else{
		End_X = (-1) * Coordinates;
		End_Y = Coordinates;
	}

	dwRet = SmcWSetBankArcPoint(Id, AxisNo, 3, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWSetBankArcPoint = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return false;
	}

	if (ArcStartDir == CSMC_CW){
		Center_X = 0;
		Center_Y = Coordinates;
		End_X = (-1) * Coordinates;
		End_Y = Coordinates;
	}
	else{
		Center_X = 0;
		Center_Y = (-1) * Coordinates;
		End_X = (-1) * Coordinates;
		End_Y = (-1) * Coordinates;
	}

	dwRet = SmcWSetBankArcPoint(Id, AxisNo, 4, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWSetBankArcPoint = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		return false;
	}

	return true;
}


void CBankArcDlg::OnBnClickedRadioDirCw()
{
	ArcStartDir = CSMC_CW;
}


void CBankArcDlg::OnBnClickedRadioDirCcw()
{
	ArcStartDir = CSMC_CCW;
}


void CBankArcDlg::OnBnClickedRadioOptionSingle()
{
	MotionType = CSMC_SINGLE;
}


void CBankArcDlg::OnBnClickedRadioOptionLoop()
{
	MotionType = CSMC_LOOP;
}


void CBankArcDlg::OnEnChangeEditDevicename()
{
	CString wStr = _T("OK");
	char	cDeviceName[256];

	if (Id != -1){
		SmcWExit(Id);
	}
	m_Edit_DeviceName.GetWindowText(wStr);
	sprintf_s(cDeviceName, "%S", wStr.GetBuffer(0));
	dwRet = SmcWInit(cDeviceName, &Id);
	if (dwRet){
		SmcWGetErrorString(dwRet, tmp);
		wsprintf(sztmp, _T("%S"), tmp);
		szErrorString.Format(_T("SmcWInit = %d : %s"), dwRet, sztmp);
		m_Static_Comment.SetWindowText(szErrorString);
		Id = -1;
		return;
	}

	wStr = _T("OK");
	m_Static_Comment.SetWindowText(wStr);
}


void CBankArcDlg::OnCbnSelchangeComboTargetAxis()
{
	AxisNo = m_Combo_TargetAxis.GetCurSel() + 1;

	_itow_s(AxisNo + 1, sztmp, 10);
	m_Edit_TargetAxis.SetWindowText(sztmp);
}


void CBankArcDlg::OnCbnSelchangeComboTargetBanknum()
{
	BankNum = m_Combo_TargetBankNum.GetCurSel() + 1;
}


void CBankArcDlg::OnBnClickedOk()
{
	SmcWExit(Id);
	CDialogEx::OnOK();
}
