// AIPacerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AIPacer.h"
#include "AIPacerDlg.h"
#include "UniDAQ.h"
#pragma comment(lib,"UniDAQ.lib")

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

WORD wRtn;
WORD wBoardNo;

IXUD_DEVICE_INFO sDevInfo[MAX_BOARD_NUMBER];
IXUD_CARD_INFO sCardInfo[MAX_BOARD_NUMBER];
char szModelName[20]="Unknow Device";

const DWORD dwDataNo = 100;

float * fBuffer= new float[dwDataNo];
WORD wCfgcode;
float fRange;
bool bActive = false ;
long   lmx, lmy;
float  fDeltaX, fDeltaY;

/////////////////////////////////////////////////////////////////////////////
// CAIPacerDlg dialog

CAIPacerDlg::CAIPacerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAIPacerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAIPacerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAIPacerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAIPacerDlg)
	DDX_Control(pDX, IDCANCEL, m_IDCANCEL);
	DDX_Control(pDX, IDC_BTN_Active, m_IDC_BTN_Active);
	DDX_Control(pDX, IDC_EDIT_SamplingRate, m_IDC_EDIT_SamplingRate);
	DDX_Control(pDX, IDC_COMBO_Type, m_IDC_COMBO_Type);
	DDX_Control(pDX, IDC_COMBO_SelectBoard, m_IDC_COMBO_SelectBoard);
	DDX_Control(pDX, IDC_COMBO_Channel, m_IDC_COMBO_Channel);
	DDX_Control(pDX, IDC_COMBO_CfgCode, m_IDC_COMBO_CfgCode);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAIPacerDlg, CDialog)
	//{{AFX_MSG_MAP(CAIPacerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO_SelectBoard, OnSelchangeCOMBOSelectBoard)
	ON_BN_CLICKED(IDC_BTN_Active, OnBTNActive)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAIPacerDlg message handlers

BOOL CAIPacerDlg::OnInitDialog()
{
	WORD wTotalBoards;
	CDialog::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
	
	// TODO: Add extra initialization here
	//Initial the resource form  Driver
	wRtn=Ixud_DriverInit(&wTotalBoards);
	if (wRtn!=0) return wRtn;


	m_IDC_COMBO_SelectBoard.Clear();
	//_ltoa( wTotalBoards, string, 10 );
	//SetDlgItemText(IDC_EDIT_TotalBoards, string);
	
	//Get Card Information
	for(WORD wBoardIndex = 0; wBoardIndex<wTotalBoards ; wBoardIndex++)
	{
		wRtn = Ixud_GetCardInfo(wBoardIndex,&sDevInfo[wBoardIndex],&sCardInfo[wBoardIndex],szModelName);
		
		m_IDC_COMBO_SelectBoard.AddString(szModelName);
	}
	
	m_IDC_COMBO_CfgCode.SetCurSel(0);
	m_IDC_COMBO_Type.SetCurSel(0);
    m_IDC_EDIT_SamplingRate.SetWindowText("5000");
	for(WORD index = 0; index<100;index++)
	{
		fBuffer[index] = 0.0F;
	}
	
	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 CAIPacerDlg::OnPaint() 
{
	CDialog::OnPaint();
	CWnd *pwnd = (CWnd*) GetDlgItem(IDC_PIC_Data);
	CPaintDC dc(pwnd);
	CRect rect;
	pwnd->GetClientRect(&rect);

	lmx = rect.left;
	lmy = rect.top + (rect.Height() / 2);
	fDeltaX = (float)(rect.Width() / (float)dwDataNo) ;
	fDeltaY = (float)(rect.Height() / 20.0);


	CBrush brushGreen(RGB(0, 255, 0));
    CBrush* pOldBrush = dc.SelectObject(&brushGreen);
	CPen penBlack;
    penBlack.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
    CPen* pOldPen = dc.SelectObject(&penBlack);

    // draw a thick black rectangle filled with blue
    dc.Rectangle(rect);
	dc.MoveTo(lmx, lmy);
	dc.LineTo(rect.right, lmy);
	CPen penRed;
	penRed.CreatePen(PS_SOLID, 1, RGB(255, 0, 0) );
	dc.SelectObject(&penRed);

	//draw line
	dc.MoveTo(lmx,lmy - (int)(fBuffer[0]*fDeltaY));
	for(int index=1; index<dwDataNo; index++) 
	{
		dc.LineTo(lmx + (int)(index*fDeltaX),lmy - (int)(fBuffer[index]*fDeltaY));
	}

}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAIPacerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAIPacerDlg::OnSelchangeCOMBOSelectBoard() 
{
		// TODO: Add your control notification handler code here
	WORD wChannelIndex; 
	
	wBoardNo = m_IDC_COMBO_SelectBoard.GetCurSel();
	static const int nLength = 10;
	char string[nLength];

	//Total AI Channel
	m_IDC_COMBO_Channel.Clear();

	for (wChannelIndex = 0; wChannelIndex < sCardInfo[wBoardNo].wAIChannels ;wChannelIndex++)
	{	
		_ltoa(wChannelIndex,string,10);
		m_IDC_COMBO_Channel.AddString(string);  
    }

	m_IDC_COMBO_Channel.SetCurSel(0);
	
}

void CAIPacerDlg::OnBTNActive() 
{
	// TODO: Add your control notification handler code here
	bActive = !bActive;

	if(bActive != true )
	{
	    m_IDC_BTN_Active.SetWindowText("&ACTIVE");
		m_IDCANCEL.EnableWindow(true);
		return;
	}

    m_IDC_BTN_Active.SetWindowText("&STOP");
	m_IDCANCEL.EnableWindow(false);
	//Config AI 
    if(Ixud_ConfigAI(wBoardNo, 2, 2048, m_IDC_COMBO_Type.GetCurSel(), 0)!=0)
	{
		MessageBox("AI Config Error!");
        CDialog::OnOK();
	}

	// TODO: Add your control notification handler code here
	while(bActive == true )
	{
 
		//read data to fBuffer
    	if (Ixud_StartAI(wBoardNo, m_IDC_COMBO_Channel.GetCurSel(), m_IDC_COMBO_CfgCode.GetCurSel(),GetDlgItemFloat(IDC_EDIT_SamplingRate), dwDataNo) != 0)
		{
           MessageBox("AI Start Error!");
           CDialog::OnOK();
		}

		if(Ixud_GetAIBuffer(wBoardNo, dwDataNo, fBuffer))
		{
			MessageBox("Get AI Buffer Error!");
            CDialog::OnOK();
		}
		

		if(Ixud_StopAI(wBoardNo))
		{
			MessageBox("Stop AI Error!");
            CDialog::OnOK();
		}
		Invalidate();    //update windows
		//Sleep(100);
        UpdateWindow();  //update windows

		MSG msg;
		if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}	
	}
	
}

void CAIPacerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	wRtn = Ixud_DriverClose();
	CDialog::OnCancel();
}

float CAIPacerDlg::GetDlgItemFloat(int nID)
{
	static const int nLength = 10;
	CString str;
	char szStr[nLength];//, * stopstring;
	
	GetDlgItemText(nID, str);
	strcpy(szStr, str.GetBuffer(nLength) );
	return (float)atof(szStr);
}
