
// SlaveDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Slave.h"
#include "SlaveDlg.h"
#include "afxdialogex.h"

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

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// Setting ///////////////////////////////////////
#define		TIMER_ID  1							// Timer_ID
#define		TIMER_CNT 100						// Interval

TCHAR		szTextBuffer[1024];					// Text buffer
long		Ret;								// Return value
DWORD		Myadr, Yradr, MasterMode;			// MyAddress, Address of device, ModeSelect(Master=0,Slave=1)
DWORD		Delim, Eoi;							// Delimiter
DWORD		Timeout, Ifctime, Dmamode, Srlen;	// 10000, 1, 1(Default), Length of str
DWORD		Cmd[32];							// Cmd Array
DWORD		P_stb, P_eoi;						// 
BYTE		srBuffer[10000];					// Buffer
TCHAR		szBuffer[10000];
UINT		uTimerTrm;							// Used Timer
UINT_PTR	uTimerEvent;						// Used	Timer
CString		csBuf;								// csBuf is String type
LPTSTR		pStb;								// Pointer to Status Byte
//////////////////////////////////////////////////

// CSlaveDlg dialog



CSlaveDlg::CSlaveDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CSlaveDlg::IDD, pParent)
{
	m_MyAddress = 0;
	m_StsByte = _T("");
	m_SendData = _T("");
	m_ReceiveData = _T("");
	m_TextRet = _T("");

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

void CSlaveDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_DELIMBOX, m_DelimBox);
	DDX_Text(pDX, IDC_MYADDRESS, m_MyAddress);
	DDX_CBString(pDX, IDC_STSBYTE, m_StsByte);
	DDX_Text(pDX, IDC_SENDDATA, m_SendData);
	DDX_Text(pDX, IDC_RECEIVEDATA, m_ReceiveData);
	DDX_Text(pDX, IDC_TEXT_RET, m_TextRet);
}

BEGIN_MESSAGE_MAP(CSlaveDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INIT, &CSlaveDlg::OnBnClickedInit)
	ON_BN_CLICKED(IDC_SRQ_ON, &CSlaveDlg::OnBnClickedSrqOn)
	ON_BN_CLICKED(IDC_SRQ_OFF, &CSlaveDlg::OnBnClickedSrqOff)
	ON_BN_CLICKED(IDC_SEND, &CSlaveDlg::OnBnClickedSend)
	ON_BN_CLICKED(IDC_RECEIVE, &CSlaveDlg::OnBnClickedReceive)
	ON_BN_CLICKED(IDC_DEL_RET, &CSlaveDlg::OnBnClickedDelRet)
	ON_BN_CLICKED(IDC_DEL_DATA, &CSlaveDlg::OnBnClickedDelData)
	ON_BN_CLICKED(IDC_EXT, &CSlaveDlg::OnBnClickedExt)
	ON_CBN_SELCHANGE(IDC_DELIMBOX, &CSlaveDlg::OnCbnSelchangeDelimbox)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CSlaveDlg message handlers

BOOL CSlaveDlg::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_DelimBox.SetCurSel(1);
	m_SendData = _T("_0123456789_ Test Data / 33 Bytes");
	m_StsByte = _T("40");			// Default
	UpdateData(FALSE);
	// ******************************************************************

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

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

//////////////////////////////////////////// Initialize ///
void CSlaveDlg::OnBnClickedInit()
{
	GpibInit(&csBuf);									// Some Initialize
	GpBoardsts(0x0a, &MasterMode);
	GpBoardsts(0x08, &Myadr);
	m_MyAddress = Myadr;
	UpdateData(FALSE);

	if (MasterMode == 0){								// Not Slave
		m_TextRet = _T("This is not Slave.");
		UpdateData(FALSE);
		return;
	}
	else{
		GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
		GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
		GetDlgItem(IDC_SRQ_ON)->EnableWindow(TRUE);
		m_TextRet = csBuf;
	}
	UpdateData(FALSE);

	OnCbnSelchangeDelimbox();
}

//////////////////////////////////////////////// SRQ ON ///
void CSlaveDlg::OnBnClickedSrqOn()
{
	GetDlgItem(IDC_SRQ_ON)->EnableWindow(FALSE);
	GetDlgItem(IDC_SRQ_OFF)->EnableWindow(TRUE);
	GetDlgItem(IDC_EXT)->EnableWindow(FALSE);

	UpdateData(TRUE);

	pStb = m_StsByte.GetBuffer(2);						// Get count of m_StsByte
	P_stb = chr2hex(pStb);								// SubClass Files
	Ret = GpPpollmode(2);								// Parelell mode set
	CheckRet(_T("GpPollmode"), Ret, &csBuf);			// Error check 
	Ret = GpSrqEx(P_stb, 1, 1);							// SRQ mode set
	CheckRet(_T("GpSrqEx"), Ret, &csBuf);				// Error check

	uTimerEvent = SetTimer(ID_TIMER, TIMERCOUNT, NULL);	// TimerEvent

	if (uTimerEvent == 0){								// If TimerEvent is ON then
		m_TextRet = _T("Make of Timer was falsed.");
	}
	else{
		GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
		GetDlgItem(IDC_RECEIVE)->EnableWindow(FALSE);
		GetDlgItem(IDC_INIT)->EnableWindow(FALSE);
		m_TextRet = _T("Waitting for polling by Master...");
		UpdateWindow();
	}
	UpdateData(FALSE);
}

/////////////////////////////////////////////// SRQ OFF ///
void CSlaveDlg::OnBnClickedSrqOff()
{
	GetDlgItem(IDC_SRQ_ON)->EnableWindow(TRUE);
	GetDlgItem(IDC_SRQ_OFF)->EnableWindow(FALSE);

	uTimerTrm = KillTimer(TIMER_ID);					// Stop Timer
	m_TextRet = _T("Send to SRQ stopped.");

	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
	GetDlgItem(IDC_INIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_SRQ_ON)->EnableWindow(TRUE);
	GetDlgItem(IDC_EXT)->EnableWindow(TRUE);

	UpdateData(FALSE);
}

////////////////////////////////////////////////// Send ///
void CSlaveDlg::OnBnClickedSend()
{
	UpdateData(TRUE);
	m_TextRet = _T("Data sending...");
	UpdateData(FALSE);
	UpdateWindow();										// Redraw

	lstrcpy(szBuffer, m_SendData);						// Copy m_SendData to szBuffer
	Srlen = lstrlen(szBuffer);							// Length of szBuffer
	sprintf_s((char*)srBuffer, Srlen + 1, "%S", szBuffer);// Copy szBuffer to srBuffer

	if (Srlen != 0x0){									// Srlen is not 0
		Cmd[0] = 0;										// Total commands
		Ret = GpTalk(Cmd, Srlen, srBuffer);				// Send data
		CheckRet(_T("GpTalk"), Ret, &csBuf);			// Error Check
	}
	else
		csBuf = _T("There is no data.");

	m_TextRet = csBuf;
	UpdateData(FALSE);									// Refresh dialog
}

/////////////////////////////////////////////// Receive ///
void CSlaveDlg::OnBnClickedReceive()
{
	GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(FALSE);
	GetDlgItem(IDC_INIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_SRQ_ON)->EnableWindow(FALSE);
	GetDlgItem(IDC_EXT)->EnableWindow(FALSE);

	UpdateData(TRUE);									// Get dialog informations
	m_TextRet = _T("Data Receiving...");
	UpdateData(FALSE);
	UpdateWindow();										// Redraw

	Cmd[0] = 0x00;										// Total commands
	Srlen = sizeof(srBuffer);							// Length of Srlen
	Ret = GpListen(Cmd, &Srlen, srBuffer);				// Receive Data
	if (CheckRet(_T("GpListen"), Ret, &csBuf) == 0){	// Error check
		srBuffer[Srlen] = 0x0;							// Put data of end to 0x0(NULL).
		m_ReceiveData = srBuffer;
		wsprintf(szTextBuffer, _T("Data Receiving is Finished. Get [ %d ] byte."), Srlen);
		csBuf = szTextBuffer;
	}
	m_TextRet = csBuf;
	UpdateData(FALSE);
	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
	GetDlgItem(IDC_INIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_SRQ_ON)->EnableWindow(TRUE);
	GetDlgItem(IDC_EXT)->EnableWindow(TRUE);
}

///////////////////////////////////////////////// Timer ///
void CSlaveDlg::OnTimer(UINT_PTR nIDEvent)
{
	Ret = GpChkstb(&P_stb, &P_eoi);
	CheckRet(_T("StatusByte"), Ret, &csBuf);

	if ((P_stb & 0x40) != 0x40){						// 0x40	mask
		m_TextRet = _T("[SRQ] was received by master.");
		KillTimer(uTimerEvent);							// TimerKill
		uTimerEvent = 0;								// Event stop

		GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
		GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
		GetDlgItem(IDC_INIT)->EnableWindow(TRUE);
		GetDlgItem(IDC_SRQ_ON)->EnableWindow(TRUE);
		GetDlgItem(IDC_SRQ_OFF)->EnableWindow(FALSE);
		GetDlgItem(IDC_EXT)->EnableWindow(TRUE);

		UpdateData(FALSE);
	}
	CDialog::OnTimer(nIDEvent);
}

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

/////////////////////////////////////////// Delete Data ///
void CSlaveDlg::OnBnClickedDelData()
{
	UpdateData(TRUE);
	m_SendData = "";
	m_ReceiveData = "";
	UpdateData(FALSE);
}

////////////////////////////////////////////////// Exit ///
void CSlaveDlg::OnBnClickedExt()
{
	GpibExit();											// Function of SubClass 
	CDialog::OnCancel();								// End of dialog
}

BOOL CSlaveDlg::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);
}

/////////////////////////////////// Change Delimiter ///
void CSlaveDlg::OnCbnSelchangeDelimbox()
{
	UpdateData(TRUE);

	Eoi = 1;											// EOI : USE
	Delim = m_DelimBox.GetCurSel();
	Ret = GpDelim(Delim, Eoi);
}
