// IO_TEST.cpp : implementation file
//

#include "stdafx.h"
#include "evc8840.h"
#include "evc8840lib.h"
#include "IO_TEST.h"

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


#define TESTTIMER 2001                 //IO检测定时器
extern devnum;
/////////////////////////////////////////////////////////////////////////////
// IO_TEST dialog


IO_TEST::IO_TEST(CWnd* pParent /*=NULL*/)
	: CDialog(IO_TEST::IDD, pParent)
{
	//{{AFX_DATA_INIT(IO_TEST)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void IO_TEST::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(IO_TEST)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(IO_TEST, CDialog)
	//{{AFX_MSG_MAP(IO_TEST)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_OUT_ALLOPEN, OnOutAllopen)
	ON_BN_CLICKED(IDC_OUT_ALLCLOSE, OnOutAllclose)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// IO_TEST message handlers

BOOL IO_TEST::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetTimer(TESTTIMER,100,NULL);               //启动IO检测定时器
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void IO_TEST::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CheckInput();             //检测输入信号
	//CheckOutput();            //检测输出信号
	CDialog::OnTimer(nIDEvent);
}
//***************信号输入**********************//
void IO_TEST::CheckInput()
{
	int nID[]={IDC_IN0,IDC_IN1,IDC_IN2,IDC_IN3,IDC_IN4,IDC_IN5,IDC_IN6,IDC_IN7,IDC_IN8,IDC_IN9,
		      IDC_IN10,IDC_IN11,IDC_IN12,IDC_IN13,IDC_IN14,IDC_IN15,IDC_IN16,IDC_IN17,IDC_IN18,IDC_IN19,
			  IDC_IN20,IDC_IN21,IDC_IN22,IDC_IN23,IDC_IN24,IDC_IN25,IDC_IN26,IDC_IN27,IDC_IN28,IDC_IN29,
			  IDC_IN30,IDC_IN31,IDC_IN32,IDC_IN33,IDC_IN34,IDC_IN35,IDC_IN36,IDC_IN37,IDC_IN38,IDC_IN39,
			  IDC_IN40,IDC_IN41,IDC_IN42,IDC_IN43,IDC_IN44,IDC_IN45,IDC_IN46,IDC_IN47,IDC_IN48,IDC_IN49};

	CButton *btn;
	int value=0;

	for (int i=0; i<50; i++)
	{
		adt8840a_read_bit(devnum, i, &value); 
		btn=(CButton*)GetDlgItem(nID[i]);
		btn->SetCheck(value==0?1:0);
	}
}

//******************信号输出***********************//
void IO_TEST::CheckOutput()
{
	int nID[]={IDC_OUT0,IDC_OUT1,IDC_OUT2,IDC_OUT3,IDC_OUT4,IDC_OUT5,IDC_OUT6,IDC_OUT7,IDC_OUT8,IDC_OUT9,
		IDC_OUT10,IDC_OUT11,IDC_OUT12,IDC_OUT13,IDC_OUT14,IDC_OUT15,IDC_OUT6,IDC_OUT17,IDC_OUT18,IDC_OUT19,
		IDC_OUT20,IDC_OUT21};

	CButton *btn;
	for (int i=0; i<22; i++)
	{

		btn=(CButton*)GetDlgItem( nID[i]);
		adt8840a_write_bit(devnum, i, btn->GetCheck());
	}
}

void IO_TEST::OnOutAllopen() 
{
	// TODO: Add your control notification handler code here
	int nID[]={IDC_OUT0,IDC_OUT1,IDC_OUT2,IDC_OUT3,IDC_OUT4,IDC_OUT5,IDC_OUT6,IDC_OUT7,
			  IDC_OUT8,IDC_OUT9,IDC_OUT10,IDC_OUT11,IDC_OUT12,IDC_OUT13,IDC_OUT14,IDC_OUT15,
			  IDC_OUT16,IDC_OUT17,IDC_OUT18,IDC_OUT19,IDC_OUT20,IDC_OUT21};

	CButton *btn;

	for (int i=0; i<22; i++)
	{
		btn=(CButton*)GetDlgItem( nID[i]);
		btn->SetCheck(1);
		adt8840a_write_bit(devnum, i, 1);
	}
	UpdateData();
}

void IO_TEST::OnOutAllclose() 
{
	// TODO: Add your control notification handler code here
	int nID[]={IDC_OUT0,IDC_OUT1,IDC_OUT2,IDC_OUT3,IDC_OUT4,IDC_OUT5,IDC_OUT6,IDC_OUT7,
			  IDC_OUT8,IDC_OUT9,IDC_OUT10,IDC_OUT11,IDC_OUT12,IDC_OUT13,IDC_OUT14,IDC_OUT15,
			  IDC_OUT16,IDC_OUT17,IDC_OUT18,IDC_OUT19,IDC_OUT20,IDC_OUT21};

	CButton *btn;

	for (int i=0; i<22; i++)
	{
		btn=(CButton*)GetDlgItem( nID[i]);
		btn->SetCheck(0);
		adt8840a_write_bit(devnum, i, 0);
	}
	UpdateData();
}

void IO_TEST::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	OnOutAllclose();              //输出全关
	KillTimer(TESTTIMER);         //关闭IO检测定时器
}
