// pc-dasDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pc-das.h"
#include "pc-dasDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CpcdasDlg dialog

#define RED_BUTTON RGB(255,65,65)
#define GRN_BUTTON RGB(20,255,20)
#define YLW_BUTTON RGB(244,244,0)
#define BLK_TEXT   RGB(0,0,0)

CpcdasDlg::CpcdasDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CpcdasDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CpcdasDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CpcdasDlg, CDialog)
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()

	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_parosciPortSet, OnBnClickedparosciPortSet)
	ON_BN_CLICKED(IDC_kftPortSet, OnBnClickedkftPortSet)
	ON_BN_CLICKED(IDC_wfPortSet, OnBnClickedwfPortSet)
END_MESSAGE_MAP()


// CpcdasDlg message handlers

BOOL CpcdasDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    CComboBox *portList;

	// 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

	// TODO: Add extra initialization here
	_timerId = SetTimer(1, 25, NULL); //25 msec timer period for sampling serial ports

    portList = (CComboBox *)GetDlgItem(IDC_kftPort);
	portList->AddString("1");
	portList->AddString("2");
	portList->AddString("3");
	portList->AddString("4");
	portList->AddString("5");
	portList->AddString("6");
	portList->AddString("7");
	portList->AddString("8");
	portList->SetCurSel(0);

	portList = (CComboBox *)GetDlgItem(IDC_parosciPort);
	portList->AddString("1");
	portList->AddString("2");
	portList->AddString("3");
	portList->AddString("4");
	portList->AddString("5");
	portList->AddString("6");
	portList->AddString("7");
	portList->AddString("8");
	portList->SetCurSel(1);

	portList = (CComboBox *)GetDlgItem(IDC_winfrogPort);
	portList->AddString("1");
	portList->AddString("2");
	portList->AddString("3");
	portList->AddString("4");
	portList->AddString("5");
	portList->AddString("6");
	portList->AddString("7");
	portList->AddString("8");
	portList->SetCurSel(2);

	_kearfottBtn.SubclassDlgItem(IDC_kftPortSet, this);
	_kearfottBtn.SetColor(BLK_TEXT, RED_BUTTON);
	_kearfottConnected = FALSE;

	_parosciBtn.SubclassDlgItem(IDC_parosciPortSet, this);
	_parosciBtn.SetColor(BLK_TEXT, RED_BUTTON);
	_parosciConnected = FALSE;

	_winfrogBtn.SubclassDlgItem(IDC_wfPortSet, this);
	_winfrogBtn.SetColor(BLK_TEXT, RED_BUTTON);
	_winfrogConnected = FALSE;

	_resonAddrBtn.SubclassDlgItem(IDC_resonAddrSet, this);
	_resonAddrBtn.SetColor(BLK_TEXT, RED_BUTTON);
	_resonConnected = 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 CpcdasDlg::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
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CpcdasDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CpcdasDlg::OnBnClickedkftPortSet()
{
	// TODO: Add your control notification handler code here
    CString s;
	GetDlgItemText(IDC_kftPort, s);
	
	if (_kearfottPort.Open(atoi(s.GetString()), 115200, ODDPARITY, 8, ONESTOPBIT))
	{
	  _kearfottConnected = TRUE;
	  _kearfottBtn.SetColor(BLK_TEXT, GRN_BUTTON);
	}
}

void CpcdasDlg::OnBnClickedparosciPortSet()
{
	// TODO: Add your control notification handler code here
    CString s;
	GetDlgItemText(IDC_parosciPort, s);
	
	if (_parosciPort.Open(atoi(s.GetString()), 9600))
	{
	  _parosciConnected = TRUE;
	  _parosciBtn.SetColor(BLK_TEXT, GRN_BUTTON);
	}
}

void CpcdasDlg::OnBnClickedwfPortSet()
{
	// TODO: Add your control notification handler code here
	CString s;
	GetDlgItemText(IDC_winfrogPort, s);
	
	if (_winfrogPort.Open(atoi(s.GetString()), 9600))
	{
	  _winfrogConnected = TRUE;
	  _winfrogBtn.SetColor(BLK_TEXT, GRN_BUTTON);
	}
}

void CpcdasDlg::OnTimer(UINT nIDEvent)
{
	// Check for messages from the parosci and send to reson as necessary

	// Check for messages from winfrog

	// Check for messages from the kearfott, do any external aiding, send to reson as needed

}
