
// StatusDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Status.h"
#include "StatusDlg.h"
#include "afxdialogex.h"
#include "CSmc.h"
#include "CSmcdef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

TCHAR szComment[1024];
char ErrorString[256];
TCHAR szErrorString[256];
extern void	ErrorCheck(DWORD, char *, char *);
/////////////////////////////////////////////////
// SMC access function
/////////////////////////////////////////////////
short DevId = -1;
long dwRet;
short AxisNo;
long lOutPulse, lCountPulse;
short PulseSts, MoveSts, StopSts, LimitSts;

// CStatusDlg dialog



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

void CStatusDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMMENT, m_TextComment);
	DDX_Control(pDX, IDC_LABEL_BANK, m_LabelBank);
	DDX_Control(pDX, IDC_DEVICE_NAME, m_DeviceName);
	DDX_Control(pDX, IDC_LABEL_ALM, m_LabelAlm);
	DDX_Control(pDX, IDC_LABEL_PLIM, m_LabelPLim);
	DDX_Control(pDX, IDC_LABEL_MLIM, m_LabelMLim);
	DDX_Control(pDX, IDC_LABEL_ORG, m_LabelOrg);
	DDX_Control(pDX, IDC_LABEL_SD, m_LabelSd);
	DDX_Control(pDX, IDC_LABEL_STOP_STS, m_LabelStopSts);
	DDX_Control(pDX, IDC_LABEL_MOVE_STS, m_LabelMoveSts);
	DDX_Control(pDX, IDC_LABEL_PULSE_STS, m_LabelPulseSts);
	DDX_Control(pDX, IDC_LABEL_COUNT_PULSE, m_LabelCountPulse);
	DDX_Control(pDX, IDC_LABEL_OUT_PULSE, m_LabelOutPulse);
	DDX_Control(pDX, IDC_COMBO_TARGET_AXIS, m_ComboTargetAxis);
}

BEGIN_MESSAGE_MAP(CStatusDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_DEVICE_NAME, &CStatusDlg::OnEnChangeDeviceName)
	ON_CBN_SELCHANGE(IDC_COMBO_TARGET_AXIS, &CStatusDlg::OnCbnSelchangeComboTargetAxis)
	ON_BN_CLICKED(IDC_COM_OUT_PULSE, &CStatusDlg::OnBnClickedComOutPulse)
	ON_BN_CLICKED(IDC_COM_ENC_PULSE, &CStatusDlg::OnBnClickedComEncPulse)
	ON_BN_CLICKED(IDCANCEL, &CStatusDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CStatusDlg message handlers

BOOL CStatusDlg::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

	AxisNo = 1;
	// AxisNo ComboBox
	for (int i = 0; i<8; i++){
		TCHAR buf[10];
		wsprintf(buf, _T("%d"), i + 1);
		m_ComboTargetAxis.AddString(buf);
	}
	m_ComboTargetAxis.SetCurSel(AxisNo - 1);

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

	SetTimer(10, 100, NULL);		// eventid=10 100msec 

	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 CStatusDlg::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 CStatusDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CStatusDlg::DispComment(TCHAR *func, TCHAR *msg)
{
	CString wStr;
	wStr.Format(_T("%s: %s"), func, msg);
	m_TextComment.SetWindowText(wStr);
}

void CStatusDlg::PollingTimer_Timer()
{
	CString wStr;
	//---------------------------
	// Get OutPulse from Driver
	//---------------------------
	dwRet = SmcWGetOutPulse(DevId, AxisNo, &lOutPulse);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetOutPulse"), szErrorString);
		return;
	}
	wStr.Format(_T("%ld"), lOutPulse);
	m_LabelOutPulse.SetWindowText(wStr.GetBuffer(1));

	//---------------------------
	// Get CountPulse from Driver
	//---------------------------
	dwRet = SmcWGetCountPulse(DevId, AxisNo, &lCountPulse);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetCountPulse"), szErrorString);
		return;
	}
	wStr.Format(_T("%ld"), lCountPulse);
	m_LabelCountPulse.SetWindowText(wStr.GetBuffer(1));

	//---------------------------
	// Get BankNo from Driver
	//---------------------------
	short wBankNo;
	dwRet = SmcWGetBankNo(DevId, AxisNo, &wBankNo);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetBankNo"), szErrorString);
		return;
	}
	wStr.Format(_T("%d"), wBankNo);
	m_LabelBank.SetWindowText(wStr.GetBuffer(1));

	//-------------------------------
	// Get Pulse Status from Driver
	//-------------------------------
	dwRet = SmcWGetPulseStatus(DevId, AxisNo, &PulseSts);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetPulseStatus"), szErrorString);
		return;
	}
	switch (PulseSts){
	case CSMC_PLS_STOP:
		wStr = _T("Pulse Stop"); break;
	case CSMC_PLS_FLCONST:
		wStr = _T("Pulse FL Constant"); break;
	case CSMC_PLS_FHCONST:
		wStr = _T("Pulse FH Constant"); break;
	case CSMC_PLS_READY:
		wStr = _T("Pulse Ready"); break;
	case CSMC_PLS_ERCW:
		wStr = _T("Pulse ERC Timer"); break;
	case CSMC_PLS_DTMW:
		wStr = _T("Pulse Dir Timer"); break;
	case CSMC_PLS_ACCEL:
		wStr = _T("Pulse Accel"); break;
	case CSMC_PLS_DECEL:
		wStr = _T("Pulse Decel"); break;
	case CSMC_PLS_INPW:
		wStr = _T("Pulse INP Wait"); break;
	case CSMC_PLS_PAPBW:
		wStr = _T("Pulser input wait"); break;
	default:
		wStr = _T("Unknown Status"); break;
	}
	m_LabelPulseSts.SetWindowText(wStr.GetBuffer(1));

	//-------------------------------
	// Get Move Status from Driver
	//-------------------------------
	dwRet = SmcWGetMoveStatus(DevId, AxisNo, &MoveSts);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetMoveStatus"), szErrorString);
		return;
	}
	switch (MoveSts){
	case CSMC_STOP:
		wStr = _T("Stop Motion"); break;
	case CSMC_PTP:
		wStr = _T("PTP Motion"); break;
	case CSMC_JOG:
		wStr = _T("JOG Motion"); break;
	case CSMC_ORG:
		wStr = _T("ORG Motion"); break;
	case CSMC_SINGLE:
		wStr = _T("Single (Bank) Motion"); break;
	case CSMC_LOOP:
		wStr = _T("Loop (Bank) Motion"); break;
	case CSMC_ZMOVE:
		wStr = _T("Z-phase count Motion"); break;
	case CSMC_PLSER:
		wStr = _T("Pulser Motion"); break;
	default:
		wStr = _T("Unknown Status"); break;
	}
	m_LabelMoveSts.SetWindowText(wStr.GetBuffer(1));

	//-------------------------------
	// Get Stop Status from Driver
	//-------------------------------
	dwRet = SmcWGetStopStatus(DevId, AxisNo, &StopSts);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetStopStatus"), szErrorString);
		return;
	}
	switch (StopSts){
	case CSMC_MOVE:
		wStr = _T("Moving"); break;
	case CSMC_STOP_COMMAND:
		wStr = _T("Stop Command"); break;
	case CSMC_SD_COMMAND:
		wStr = _T("Slowdown Stop Command"); break;
	case CSMC_STOP_OTHER:
		wStr = _T("Stop by other axis"); break;
	case CSMC_STOP_ALARM:
		wStr = _T("Stop by alarm"); break;
	case CSMC_STOP_PLIM:
		wStr = _T("Stop by +LIM"); break;
	case CSMC_STOP_MLIM:
		wStr = _T("Stop by -LIM"); break;
	case CSMC_STOP_SD:
		wStr = _T("Stop by SD"); break;
	case CSMC_ERROR_ORG:
		wStr = _T("Stop by ORG ERROR"); break;
	case CSMC_ERROR_PULSER:
		wStr = _T("Stop by PULSER ERROR"); break;
	case CSMC_STOP_NORMAL:
		wStr = _T("Normal Stop"); break;
	default:
		wStr = _T("Unknown Status"); break;
	}
	m_LabelStopSts.SetWindowText(wStr.GetBuffer(1));
	//-------------------------------
	// Get Limit Status from Driver
	//-------------------------------
	dwRet = SmcWGetLimitStatus(DevId, AxisNo, &LimitSts);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWGetLimitStatus"), szErrorString);
		return;
	}
	if ((LimitSts & CSMC_SD) == CSMC_SD)
		m_LabelSd.SetWindowText(_T("ON"));
	else
		m_LabelSd.SetWindowText(_T("OFF"));

	if ((LimitSts & CSMC_ORGLIM) == CSMC_ORGLIM)
		m_LabelOrg.SetWindowText(_T("ON"));
	else
		m_LabelOrg.SetWindowText(_T("OFF"));

	if ((LimitSts & CSMC_MLIM) == CSMC_MLIM)
		m_LabelMLim.SetWindowText(_T("ON"));
	else
		m_LabelMLim.SetWindowText(_T("OFF"));

	if ((LimitSts & CSMC_PLIM) == CSMC_PLIM)
		m_LabelPLim.SetWindowText(_T("ON"));
	else
		m_LabelPLim.SetWindowText(_T("OFF"));

	if ((LimitSts & CSMC_ALM) == CSMC_ALM)
		m_LabelAlm.SetWindowText(_T("ON"));
	else
		m_LabelAlm.SetWindowText(_T("OFF"));
}


void CStatusDlg::OnEnChangeDeviceName()
{
	CString wStr = _T("OK");
	CString DeviceName;
	char	cDeviceName[256];
	if (DevId != -1){
		SmcWExit(DevId);
	}
	m_DeviceName.GetWindowText(DeviceName);
	sprintf_s(cDeviceName, "%S", DeviceName.GetBuffer(0));
	dwRet = SmcWInit(cDeviceName, &DevId);
	if (dwRet){
		SmcWGetErrorString(dwRet, ErrorString);
		wsprintf(szErrorString, _T("%S"), ErrorString);
		DispComment(_T("SmcWInit"), szErrorString);
		DevId = -1;
		return;
	}

	m_TextComment.SetWindowText(wStr);
}


void CStatusDlg::OnCbnSelchangeComboTargetAxis()
{
	AxisNo = m_ComboTargetAxis.GetCurSel() + 1;
}


void CStatusDlg::OnBnClickedComOutPulse()
{
	//---------------------------
	// Set OutPulse for Driver
	//---------------------------
	dwRet = SmcWSetOutPulse(DevId, AxisNo, 0);
	SmcWGetErrorString(dwRet, ErrorString);
	wsprintf(szErrorString, _T("%S"), ErrorString);
	DispComment(_T("SmcWSetOutPulse"), szErrorString);
}


void CStatusDlg::OnBnClickedComEncPulse()
{
	//---------------------------
	// Set CountPulse for Driver
	//---------------------------
	dwRet = SmcWSetCountPulse(DevId, AxisNo, 0);
	SmcWGetErrorString(dwRet, ErrorString);
	wsprintf(szErrorString, _T("%S"), ErrorString);
	DispComment(_T("SmcWSetCountPulse"), szErrorString);
}

void CStatusDlg::OnTimer(UINT_PTR nIDEvent)
{
	PollingTimer_Timer();
	CDialog::OnTimer(nIDEvent);
}

void CStatusDlg::OnBnClickedCancel()
{
	KillTimer(10);
	CDialogEx::OnCancel();
}
