// Measure.cpp : implementation file
//

#include "stdafx.h"
#include "Pti4.h"
#include "Measure.h"
#include "ccappt.h"


// CMeasure dialog

IMPLEMENT_DYNAMIC(CMeasure, CDialog)
CMeasure::CMeasure(CWnd* pParent /*=NULL*/)
	: CDialog(CMeasure::IDD, pParent)
	, m_Edit_AvCondition(0)
{
}

CMeasure::~CMeasure()
{
}

void CMeasure::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_LINEARIZE, m_Combo_Linearize);
	DDX_Control(pDX, IDC_COMBO_UPDATE, m_Combo_Update);
	DDX_Control(pDX, IDC_COMBO_AVERAGE, m_Combo_Average);
	DDX_Text(pDX, IDC_EDIT_AVCONDITION, m_Edit_AvCondition);
	DDX_Control(pDX, IDC_EDIT_DISABLE, m_Disable);
}


BEGIN_MESSAGE_MAP(CMeasure, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMeasure message handlers

// Get the caller ID
void CMeasure::SetId(short id)
{
	m_Id = id;
}

// Initialize dialog
BOOL CMeasure::OnInitDialog() 
{
	CDialog::OnInitDialog();

	long	ret;
	short	linearize, averagetype;
	long	updatetiming, averagedata;
	long	channeldisable;
	TCHAR	szText[100];

	// Get the Linearize function
	ret = PtGetLinearise(m_Id, &linearize);
	if(ret != 0){
		ErrorHandling(_T("PtGetLinearise"), ret);
		return TRUE;
	}
	m_Combo_Linearize.SetCurSel(linearize);

	// Get the Update timing
	ret = PtGetUpdateTiming(m_Id, &updatetiming);
	if(ret != 0){
		ErrorHandling(_T("PtGetUpdateTiming"), ret);
		return TRUE;
	}
	switch(updatetiming){
	case SPEED_150MS:
		m_Combo_Update.SetCurSel(0);
		break;
	case SPEED_40MS:
		m_Combo_Update.SetCurSel(1);
		break;
	case SPEED_5MS:
		m_Combo_Update.SetCurSel(2);
		break;
	case SPEED_1P5MS:
		m_Combo_Update.SetCurSel(3);
		break;
	}

	// Get the Average function
	ret = PtGetAverage(m_Id, &averagetype, &averagedata);
	if(ret != 0){
		ErrorHandling(_T("PtGetAverage"), ret);
		return TRUE;
	}
	m_Combo_Average.SetCurSel(averagetype);
	m_Edit_AvCondition = averagedata;

	// Get the conversion disabled channels
	ret = PtGetChannelDisable(m_Id, &channeldisable);
	if(ret != 0){
		ErrorHandling(_T("PtGetChannelDisable"), ret);
		return TRUE;
	}
	wsprintf(szText, _T("%X"), channeldisable);
	m_Disable.SetWindowText(szText);

	UpdateData(FALSE);

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

// Click the [OK] button
void CMeasure::OnBnClickedOk()
{
	UpdateData(TRUE);
	long	ret;
	int		index;
	long	updatetiming;
	TCHAR	szText[100];
	long	channeldisable;

	// Set the Linearize function
	index = m_Combo_Linearize.GetCurSel();
	ret = PtSetLinearise(m_Id, index);
	if(ret != 0){
		ErrorHandling(_T("PtSetLinearise"), ret);
	}

	// Set the Update timing
	index = m_Combo_Update.GetCurSel();
	switch(index){
	case 0:
		updatetiming = SPEED_150MS;
		break;
	case 1:
		updatetiming = SPEED_40MS;
		break;
	case 2:
		updatetiming = SPEED_5MS;
		break;
	case 3:
		updatetiming = SPEED_1P5MS;
		break;
	}
	ret = PtSetUpdateTiming(m_Id, updatetiming);
	if(ret != 0){
		ErrorHandling(_T("PtSetUpdateTiming"), ret);
	}

	// Set the Average function
	index = m_Combo_Average.GetCurSel();
	ret = PtSetAverage(m_Id, index, m_Edit_AvCondition);
	if(ret != 0){
		ErrorHandling(_T("PtSetAverage"), ret);
	}

	// Set the conversion disabled channels
	m_Disable.GetWindowText(szText, sizeof(szText));
	swscanf_s(szText, _T("%X"), &channeldisable);
	ret = PtSetChannelDisable(m_Id, channeldisable);
	if(ret != 0){
		ErrorHandling(_T("PtSetChannelDisable"), ret);
	}

	CDialog::OnOK();
}

// Process error
void CMeasure::ErrorHandling(TCHAR *function, long ret)
{
	char	errstr[256];
	TCHAR	szerrstr[256];
	PtGetErrorString(ret, errstr);
	wsprintf(szerrstr, _T("%S"), errstr);
	MessageBox(szerrstr, function, MB_OK);
}
