// Vcc5AxaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Vcc5Axa.h"
#include "Vcc5AxaDlg.h"

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


//  PatnTilt1 access macro
#define PANTILTSTOP 0L	//	pan/tilte stop
#define PANRIGHT	4L	//	pan right start
#define PANLEFT		3L	//	pan left start
#define TILTUP		1L	//	tilte up start
#define TILTDOWN	2L	//	tilte down start


/////////////////////////////////////////////////////////////////////////////
// CVcc5AxaDlg dialog

CVcc5AxaDlg::CVcc5AxaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVcc5AxaDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVcc5AxaDlg)
	m_text = _T("");
	m_sndrcvstr = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVcc5AxaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVcc5AxaDlg)
	DDX_Control(pDX, IDC_POWER, m_Power);
	DDX_Control(pDX, IDC_NOTIFY, m_Notify);
	DDX_Control(pDX, IDC_HOST, m_Host);
	DDX_Control(pDX, IDC_VCC5CTRL1, m_vcc5a);
	DDX_Text(pDX, IDC_EDIT1, m_text);
	DDV_MaxChars(pDX, m_text, 255);
	DDX_LBString(pDX, IDC_LIST1, m_sndrcvstr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVcc5AxaDlg, CDialog)
	//{{AFX_MSG_MAP(CVcc5AxaDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpenClicked)
	ON_BN_CLICKED(IDC_CLOSE, OnCloseClicked)
	ON_BN_CLICKED(IDC_AUTOINIT, OnAutoinit)
	ON_BN_CLICKED(IDC_UP, OnUpClicked)
	ON_BN_CLICKED(IDC_LEFT, OnLeftClicked)
	ON_BN_CLICKED(IDC_HOME, OnHomeClicked)
	ON_BN_CLICKED(IDC_RIGHT, OnRightClicked)
	ON_BN_CLICKED(IDC_DOWN, OnDownClicked)
	ON_BN_CLICKED(IDC_STOP, OnStopClicked)
	ON_BN_CLICKED(IDC_POWER, OnPowerClicked)
	ON_BN_CLICKED(IDC_NOTIFY, OnNotifyClicked)
	ON_BN_CLICKED(IDC_HOST, OnHostClicked)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVcc5AxaDlg message handlers

BOOL CVcc5AxaDlg::OnInitDialog()
{
	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
    m_connected = FALSE;
    m_sndrcvstr = "";
	m_text = "";
	m_unit = 0L;
	
	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 CVcc5AxaDlg::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();
	}
}

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

void CVcc5AxaDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if( m_connected )
      m_vcc5a.CommClose();
	
	CDialog::OnClose();
}

void CVcc5AxaDlg::OnOpenClicked() 
{
	// TODO: Add your control notification handler code here
	long err,errnum;
	char temp[50];
	if( !m_connected )
	{
      BOOL sts = m_vcc5a.CommOpen( 1L, 9600L, 8L, 0L, 0L );    // Open COM Port 
	  CEdit *pe = (CEdit *)GetDlgItem(IDC_EDIT1);
	  if( sts )
	  {
		  m_text = "Connected";
	      m_connected = TRUE;
	  }
	  else
	  {
          err = m_vcc5a.GetLastError( &errnum );
		  wsprintf( temp, "Connection failed (%d)", err );
		  m_text = temp;
	  }
	  pe->SetWindowText( m_text );
    }
}

void CVcc5AxaDlg::OnCloseClicked() 
{
	// TODO: Add your control notification handler code here
	if( m_connected )
	{
      m_connected = !(m_vcc5a.CommClose());
	  CEdit *pe = (CEdit *)GetDlgItem(IDC_EDIT1);
	  if( m_connected )
		  m_text = "Disconnection failed";
	  else
		  m_text = "Disconnected";
	  pe->SetWindowText( m_text );
	}
}

void CVcc5AxaDlg::OnAutoinit() 
{
	// TODO: Add your control notification handler code here
	MSG message;
	CEdit *pe = (CEdit *)GetDlgItem(IDC_EDIT1);
    m_text = "Host Mode setting";
	pe->SetWindowText( m_text );
	m_Ackreceived = FALSE;
	if( m_vcc5a.SetCmdMode( m_unit, 0L ) )
	{
		GetLastCmd();
        m_Host.SetCheck(1);
	}
	else
	{
		pe->SetWindowText( "Host mode setting error" );
		return;
	}
	for( int i =0 ; i < 10 &&!m_Ackreceived ; i++ )
	{
		Sleep( 20 );
		if( ::PeekMessage(&message, NULL, 0, 0, PM_REMOVE )) 
		{
			::TranslateMessage( &message);
			::DispatchMessage( &message );
		}
	}
    if( !m_Ackreceived )
	{
		pe->SetWindowText( "Host mode error" );
		return;
	}
    m_text = "Notify Mode setting";
	pe->SetWindowText( m_text );
	m_Ackreceived = FALSE;
	Sleep( 100 );
    if( m_vcc5a.SetNotify( m_unit, 1L ) )    // set Notify mode on
	{
		GetLastCmd();
        m_Notify.SetCheck(1);
	}
	else
	{
		pe->SetWindowText( "Notify mode setting error" );
		return;
	}
	for( i =0 ; i < 10 &&!m_Ackreceived ; i++ )  // wait ACK, max 2 sec. 
	{
		Sleep( 20 );
		if( ::PeekMessage(&message, NULL, 0, 0, PM_REMOVE )) 
		{
			::TranslateMessage( &message);
			::DispatchMessage( &message );
		}
	}
    if( !m_Ackreceived )
	{
		pe->SetWindowText( "Notify mode error" );
		return;
	}
    m_text = "Set camera on";
	pe->SetWindowText( m_text );
	m_Ackreceived = FALSE;
	Sleep( 100 );
    if( m_vcc5a.SetCameraPower( m_unit, 1L ) )    // set Camera mode on
	{
		GetLastCmd();
        m_Power.SetCheck(1);
	}
	else
	{
		pe->SetWindowText( "Camera power failed" );
		return;
	}
	for( i =0 ; i < 10 && !m_Ackreceived ; i++ )  // wait ACK, max 2 sec. 
	{
		Sleep( 20 );
		if( ::PeekMessage(&message, NULL, 0, 0, PM_REMOVE )) 
		{
			::TranslateMessage( &message);
			::DispatchMessage( &message );
		}
	}
    if( !m_Ackreceived )
	{
		pe->SetWindowText( "Camera power error" );
		return;
	}
	CListBox *cl = (CListBox *)GetDlgItem( IDC_LIST1 );
	int pos = cl->InsertString( -1, "Auto init end" );
	cl->SetCaretIndex( pos , FALSE );

    pe->SetWindowText( "Auto Initialize succeeded" );
	return;	
}

void CVcc5AxaDlg::OnUpClicked() 
{
	// TODO: Add your control notification handler code here
    BOOL sts = m_vcc5a.SetPanTilt2( m_unit, TILTUP );
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnLeftClicked() 
{
	// TODO: Add your control notification handler code here
    BOOL sts = m_vcc5a.SetPanTilt2( m_unit, PANLEFT );
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnHomeClicked() 
{
	// TODO: Add your control notification handler code here
    BOOL sts = m_vcc5a.GotoHomePosition( m_unit );
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnRightClicked() 
{
	// TODO: Add your control notification handler code here
    BOOL sts = m_vcc5a.SetPanTilt2( m_unit, PANRIGHT );	
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnDownClicked() 
{
	// TODO: Add your control notification handler code here
    BOOL sts = m_vcc5a.SetPanTilt2( m_unit, TILTDOWN );
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnStopClicked() 
{
	// TODO: Add your control notification handler code here
    BOOL sts = m_vcc5a.SetPanTilt2( m_unit, PANTILTSTOP );
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnPowerClicked() 
{
	// TODO: Add your control notification handler code here
	BOOL sts;
	if( m_Power.GetCheck() )
		sts = m_vcc5a.SetCameraPower( m_unit, 1L );  // Power on
	else 
		sts = m_vcc5a.SetCameraPower( m_unit, 0L );
	if( sts )
		GetLastCmd();
}

void CVcc5AxaDlg::OnNotifyClicked() 
{
	// TODO: Add your control notification handler code here
	BOOL sts;
	if( m_Notify.GetCheck() )
       sts = m_vcc5a.SetNotify( m_unit, 1L );    // set Notify mode on
	else
       sts = m_vcc5a.SetNotify( m_unit, 0L );    // reset Notify mode
    if( sts )
       GetLastCmd();
}

void CVcc5AxaDlg::OnHostClicked() 
{
	// TODO: Add your control notification handler code here
	BOOL sts;
	if( m_Host.GetCheck() )                    // Checked ?
	{
       sts = m_vcc5a.SetCmdMode( m_unit, 0L );     // Yes, Host mode
	}
	else
	{
       sts = m_vcc5a.SetCmdMode( m_unit, 1L );     // No, Local mode
	}
    if( sts )
       GetLastCmd();
}

// send command data read
BOOL CVcc5AxaDlg::GetLastCmd()
{
    char tmp[30];
    long lBytes = m_vcc5a.GetLastCmdStr();
    m_sndrcvstr = "SEND >";
	CListBox *cl = (CListBox *)GetDlgItem( IDC_LIST1 );
	for( int i=0 ; i<lBytes ; i++ )
	{
	   int Recvchar = m_vcc5a.GetPicLastCmd( (long)i );
       wsprintf( tmp, "%02X ", Recvchar );
	   m_sndrcvstr = m_sndrcvstr + tmp ;
	}
	int pos = cl->InsertString( -1, m_sndrcvstr );
	cl->SetCaretIndex( pos , FALSE );
    return TRUE;
}


BEGIN_EVENTSINK_MAP(CVcc5AxaDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CVcc5AxaDlg)
	ON_EVENT(CVcc5AxaDlg, IDC_VCC5CTRL1, 1 /* NotifyRxEvent */, OnNotifyRxEventVcc5ctrl1, VTS_I4 VTS_I4)
	ON_EVENT(CVcc5AxaDlg, IDC_VCC5CTRL1, 2 /* AckRxEvent */, OnAckRxEventVcc5ctrl1, VTS_I4 VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CVcc5AxaDlg::OnNotifyRxEventVcc5ctrl1(long wParam, long lBytes) 
{
	// TODO: Add your control notification handler code here
	char tmp[30];

	m_sndrcvstr = "";
	switch(wParam)
	{
	case   1:
		m_sndrcvstr = "Notify < ";
		break;
	case   2:
		m_sndrcvstr = "IR ON < ";
		break;
	case   3:
		m_sndrcvstr = "IR OFF < ";
		break;
	case   4:
		m_sndrcvstr = "Global < ";
		break;
	}

	CListBox *cl = (CListBox *)GetDlgItem( IDC_LIST1 );
	for( int i=0 ; i<lBytes ; i++ )
	{
	   int Recvchar = m_vcc5a.GetPicData( (long)i );
       wsprintf( tmp, "%02X ", Recvchar );
	   m_sndrcvstr = m_sndrcvstr + tmp ;
	}
	int pos = cl->InsertString( -1, m_sndrcvstr );
	cl->SetCaretIndex( pos , FALSE );	
}

void CVcc5AxaDlg::OnAckRxEventVcc5ctrl1(long wParam, long lBytes) 
{
	// TODO: Add your control notification handler code here
	char tmp[30];
	m_sndrcvstr = "ACK  <";
	CListBox *cl = (CListBox *)GetDlgItem( IDC_LIST1 );
	for( int i=0 ; i<lBytes ; i++ )
	{
	   int Recvchar = m_vcc5a.GetPicData( (long)i );
       wsprintf( tmp, "%02X ", Recvchar );
	   m_sndrcvstr = m_sndrcvstr + tmp ;
	}
    m_Ackreceived = TRUE;
	int pos = cl->InsertString( -1, m_sndrcvstr );
	cl->SetCaretIndex( pos , FALSE );
}
