// TDIView.cpp : implementation file
//

#include "stdafx.h"
#include "CTMS.h"
#include "TDIView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTDIView

IMPLEMENT_DYNCREATE(CTDIView, CFormView)

CTDIView::CTDIView()
	: CFormView(CTDIView::IDD)
{
	//{{AFX_DATA_INIT(CTDIView)
	//}}AFX_DATA_INIT

  //load the bitmaps
  m_oGreen.LoadBitmap( IDB_LEDGREEN);
  m_oGray.LoadBitmap( IDB_LEDGRAY);
  m_oRed.LoadBitmap( IDB_LEDRED);

  m_iTimerId = 0;
}

CTDIView::~CTDIView()
{
}

void CTDIView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTDIView)
	DDX_Control(pDX, IDC_TXLED, m_LEDtx);
	DDX_Control(pDX, IDC_RXLED, m_LEDrx);
	DDX_Control(pDX, IDC_YLED, m_LEDyellow);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTDIView, CFormView)
	//{{AFX_MSG_MAP(CTDIView)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTDIView diagnostics

#ifdef _DEBUG
void CTDIView::AssertValid() const
{
	CFormView::AssertValid();
}

void CTDIView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTDIView message handlers

BOOL CTDIView::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
  if (m_iTimerId)
	  KillTimer( m_iTimerId);

  m_iTimerId = 0;

	return CFormView::DestroyWindow();
}

void CTDIView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
  //IP address TDI
  CString adr = "";
  int idx = ((CCTMSApp *)AfxGetApp())->GetTMIndex();
  CTM *tm = ((CCTMSApp *)AfxGetApp())->GetTM(idx);

 	CWnd *pWndLED = (CWnd *)GetDlgItem(IDC_YLED);
  m_LEDyellow.Blinking( 0);
	m_LEDyellow.SetLED(pWndLED,ID_LED_YELLOW, ID_SHAPE_ROUND, 0);

 	pWndLED = (CWnd *)GetDlgItem(IDC_RXLED);
  m_LEDrx.Blinking( 0);
	m_LEDrx.SetLED(pWndLED,ID_LED_GREEN, ID_SHAPE_ROUND, 0);
 	pWndLED = (CWnd *)GetDlgItem(IDC_TXLED);
  m_LEDtx.Blinking( 0);
	m_LEDtx.SetLED(pWndLED,ID_LED_RED, ID_SHAPE_ROUND, 0);

  if (tm)
  {

    tm->SetSendBit( 0);
    tm->SetRecvBit( 0);

    GetDlgItem(IDC_TMFRAME)->SetWindowText( tm->GetName());
  }

  //GetDlgItem(IDC_TDIADDRESS)->SetWindowText( tdi->m_sTDIName);

  //get the four port numbers
  //adr.Format( "%d", tdi->GetPort( 1));
  //GetDlgItem(IDC_TDIPORT1)->SetWindowText( adr);
  //adr.Format( "%d", tdi->GetPort( 2));
  //GetDlgItem(IDC_TDIPORT2)->SetWindowText( adr);
  //adr.Format( "%d", tdi->GetPort( 3));
  //GetDlgItem(IDC_TDIPORT3)->SetWindowText( adr);
  //adr.Format( "%d", tdi->GetPort( 4));
  //GetDlgItem(IDC_TDIPORT4)->SetWindowText( adr);

	UpdateView();

  //create 500 msec timer event
  m_iTimerId = SetTimer( TIMER_TDI, TIME_TDI, NULL);
}

void CTDIView::UpdateView()
{

  int idx = ((CCTMSApp *)AfxGetApp())->GetTMIndex();
  CTM *tm = ((CCTMSApp *)AfxGetApp())->GetTM(idx);
  //CBitmap *bm = NULL;
  char buf[ 80];

  //we just need a tm
  if (!tm)
  {
    
    GetDlgItem(IDC_TDIPORT1)->SetWindowText( "");
    GetDlgItem(IDC_TDIADDRESS)->SetWindowText( "");
    return;
  }

	CWnd *pWnd;
  
  //update the receive led
  if (tm->GetRecvBit())
  {

 	  pWnd = (CWnd *)GetDlgItem(IDC_RXLED);
  	m_LEDrx.SetLED(pWnd,ID_LED_GREEN, ID_SHAPE_ROUND, 250);
    tm->SetRecvBit(0);
  }

  //update transmit led
  if (tm->GetSendBit())
  {
 	  pWnd = (CWnd *)GetDlgItem(IDC_TXLED);
	  m_LEDtx.SetLED(pWnd,ID_LED_RED, ID_SHAPE_ROUND, 250);
    tm->SetSendBit( 0);
  }

  //lifesign registration
  pWnd = (CWnd *)GetDlgItem(IDC_YLED);

  if (tm->IsConnected())
  {

    //if (Force)
      m_LEDyellow.SwitchOn(); //SetLED(pWnd,ID_LED_YELLOW, ID_SHAPE_ROUND, 0);
    //else
    //  m_LEDyellow.SetLED(pWnd,ID_LED_YELLOW, ID_SHAPE_ROUND, 0);

    GetDlgItem(IDC_TDIADDRESS)->GetWindowText( buf, sizeof(buf));
    if (strcmp( buf, "") == 0)
    {
      CString sTmp;
      sTmp = tm->GetTMip();
      GetDlgItem(IDC_TDIADDRESS)->SetWindowText( sTmp);
      sTmp.Format( "%d", tm->GetTMport());
      GetDlgItem(IDC_TDIPORT1)->SetWindowText( sTmp);
    }
  }
  else
  {

    m_LEDyellow.SwitchOff();
    GetDlgItem(IDC_TDIPORT1)->SetWindowText( "");
    GetDlgItem(IDC_TDIADDRESS)->SetWindowText( "");
  }
} //UpdateView


void CTDIView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
  if (nIDEvent == TIMER_TDI) 
    UpdateView();	

	CFormView::OnTimer(nIDEvent);
}
