// Lc4hs_mfcDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Lc4hs_mfc.h"
#include "Lc4hs_mfcDlg.h"

#include "..\include\lc4hs.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLc4hs_mfcDlg dialog

CLc4hs_mfcDlg::CLc4hs_mfcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLc4hs_mfcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLc4hs_mfcDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	DriverHandle = Lc4hsOpen("4HS1");
	if (DriverHandle == INVALID_HANDLE_VALUE) 
		MessageBox("Could not Open driver", 
			"Error", MB_OK | MB_ICONEXCLAMATION);

}

void CLc4hs_mfcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLc4hs_mfcDlg)
	DDX_Control(pDX, IDC_STATUS, m_Status);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLc4hs_mfcDlg, CDialog)
	//{{AFX_MSG_MAP(CLc4hs_mfcDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_READBUTTON, OnReadbutton)
	ON_BN_CLICKED(IDC_INITBUTTON, OnInitbutton)
	ON_BN_CLICKED(IDC_FINISHBUTTON, OnFinishbutton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLc4hs_mfcDlg message handlers

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

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	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 CLc4hs_mfcDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

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

HCURSOR CLc4hs_mfcDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLc4hs_mfcDlg::OnInitbutton() 
{
	m_Status.SetWindowText("Initialising...");
	m_Status.UpdateWindow();
	if (Lc4hsInit(DriverHandle)) {
		MessageBox("Initialised OK", "Information", MB_OK | MB_ICONINFORMATION);
	} else {
		MessageBox("Could not initialise", 
			"Error", MB_OK | MB_ICONEXCLAMATION);
	}
	m_Status.SetWindowText("");
}

void CLc4hs_mfcDlg::OnFinishbutton() 
{
	if (Lc4hsFinish(DriverHandle)) 
		MessageBox("Finished OK", "Information", MB_OK | MB_ICONINFORMATION);
	else 
		MessageBox("Could not Finish", "Error", MB_OK | MB_ICONEXCLAMATION);
}

void CLc4hs_mfcDlg::OnReadbutton() 
{
	FLOAT Val[4];
	char MsgBuf[0x100];
	if (Lc4hsGetCurrentWeight4(DriverHandle, Val)) {
		sprintf(MsgBuf, "%.3f %.3f %.3f %.3f", Val[0],Val[1],Val[2],Val[3]);
	} else {
		strcpy(MsgBuf, "read error");
	}
	m_Status.SetWindowText(MsgBuf);
}
