
// PollingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Polling.h"
#include "PollingDlg.h"
#include "afxdialogex.h"

// Including Files ////////////////////////////////////////
#include "Gpibac.h"
#include "SubFunc.h"
///////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

///////////////////////////////////////////////////////// Setting ///
long	Ret, Count;
DWORD	Myadr, Yradr;
DWORD	Srlen;
DWORD	Cmd[32];
DWORD	Pstb[32];
BYTE	srBuffer[10000];
CString	csBuf;
CString	PollBuf;
/////////////////////////////////////////////////////////////////////

// CPollingDlg dialog



CPollingDlg::CPollingDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CPollingDlg::IDD, pParent)
{
	m_PollSts = _T("");
	m_TextRet = _T("");
	m_MyAdr = 0;

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPollingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_YRADRBOX, m_YrAdrBox);
	DDX_Text(pDX, IDC_POLLING_STS, m_PollSts);
	DDX_Text(pDX, IDC_TEXT_RET, m_TextRet);
	DDX_Text(pDX, IDC_MYADR, m_MyAdr);
}

BEGIN_MESSAGE_MAP(CPollingDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INIT, &CPollingDlg::OnBnClickedInit)
	ON_BN_CLICKED(IDC_POLLING, &CPollingDlg::OnBnClickedPolling)
	ON_BN_CLICKED(IDC_POLL_DEL, &CPollingDlg::OnBnClickedPollDel)
	ON_BN_CLICKED(IDC_RET_DEL, &CPollingDlg::OnBnClickedRetDel)
	ON_CBN_SELCHANGE(IDC_YRADRBOX, &CPollingDlg::OnCbnSelchangeYradrbox)
	ON_BN_CLICKED(IDC_EXT, &CPollingDlg::OnBnClickedExt)
END_MESSAGE_MAP()


// CPollingDlg message handlers

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

	// ****************************************************** Setting ***
	m_YrAdrBox.SetCurSel(1);
	UpdateData(FALSE);
	Yradr = GetDlgItemInt(IDC_YRADRBOX);
	CenterWindow();
	// ******************************************************************

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

/////////////////////////////////////////////////////////////////////
// This Sample Code is here.
/////////////////////////////////////////////////////////////////////

//////////////////////////////////////////// Initialize ///
void CPollingDlg::OnBnClickedInit()
{
	TCHAR	DelMyadr[16];

	UpdateData(TRUE);

	GetDlgItem(IDC_POLLING)->EnableWindow(TRUE);
	GpibInit(&csBuf);
	GpBoardsts(0x08, &Myadr);
	wsprintf(DelMyadr, _T("%d"), Myadr);
	Ret = m_YrAdrBox.FindStringExact(-1, DelMyadr);
	if (Ret >= 0){
		m_YrAdrBox.DeleteString(Ret);
		if (Myadr == Yradr){
			m_YrAdrBox.SetCurSel(0);
		}
	}
	m_TextRet = csBuf;
	m_MyAdr = Myadr;

	UpdateData(FALSE);
	Yradr = GetDlgItemInt(IDC_YRADRBOX);
}

/////////////////////////////////////////////// Polling ///
void CPollingDlg::OnBnClickedPolling()
{
	UpdateData(TRUE);

	m_TextRet = _T("Polling...");
	UpdateData(FALSE);
	UpdateWindow();
	csBuf = _T("GpPoll");
	for (Count = 0; Count < 16; Count++){
		Pstb[Count] = 0x00;
	}
	Cmd[0] = 1;
	Cmd[1] = Yradr;

	Ret = GpPoll(Cmd, Pstb);
	if ((Ret & 0xFF) == 0)
		PollBuf.Format(_T("[SRQ] is found. Talker = [ %x ] / StatusByte = [ %x ]H"), Cmd[Pstb[0]], Pstb[Pstb[0]]);
	else if ((Ret & 0xFF) == 1)
		PollBuf.Format(_T("[SRQ]&[EOI] is found. Talker = [%x] / StatusByte = [ %x ]H"), Cmd[Pstb[0]], Pstb[Pstb[0]]);
	else if ((Ret & 0xFF) == 128)
		PollBuf.Format(_T("[SRQ] was not found out."));
	CheckRet(_T("GpPoll"), Ret, &csBuf);			// Not 0,1,128

	m_PollSts = PollBuf;
	m_TextRet = csBuf;

	UpdateData(FALSE);
}

/////////////////////////////////////////// Delete Poll ///
void CPollingDlg::OnBnClickedPollDel()
{
	UpdateData(TRUE);
	m_PollSts = "";
	UpdateData(FALSE);
}

///////////////////////////////////////// Delete Return ///
void CPollingDlg::OnBnClickedRetDel()
{
	UpdateData(TRUE);
	m_TextRet = "";
	UpdateData(FALSE);
}

//////////////////////////////////////// Address of device Change ///
void CPollingDlg::OnCbnSelchangeYradrbox()
{
	UpdateData(TRUE);
	Yradr = GetDlgItemInt(IDC_YRADRBOX);
}

////////////////////////////////////////////////// Exit ///
void CPollingDlg::OnBnClickedExt()
{
	GpibExit();
	CDialog::OnCancel();
}

BOOL CPollingDlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_ESCAPE: 				// ESC Key is disebled.
			return TRUE;
		default:
			break;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}
