// KDIOForm.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "K16DIOSample.h"
#include "KDIOForm.h"
#include "TUSBKDIO.h"

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

#define TIMER1	1//監視タイマー

/////////////////////////////////////////////////////////////////////////////
// CKDIOForm ダイアログ


CKDIOForm::CKDIOForm(CWnd* pParent /*=NULL*/)
	: CDialog(CKDIOForm::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKDIOForm)
	m_P1IoSel = -1;
	m_P2IoSel = -1;
	m_P1Out0 = FALSE;
	m_P1Out1 = FALSE;
	m_P1Out2 = FALSE;
	m_P1Out3 = FALSE;
	m_P1Out4 = FALSE;
	m_P1Out5 = FALSE;
	m_P1Out6 = FALSE;
	m_P1Out7 = FALSE;
	m_P2Out0 = FALSE;
	m_P2Out1 = FALSE;
	m_P2Out2 = FALSE;
	m_P2Out3 = FALSE;
	m_P2Out4 = FALSE;
	m_P2Out5 = FALSE;
	m_P2Out6 = FALSE;
	m_P2Out7 = FALSE;
	m_P1Status0 = _T("");
	m_P1Status1 = _T("");
	m_P1Status2 = _T("");
	m_P1Status3 = _T("");
	m_P1Status4 = _T("");
	m_P1Status5 = _T("");
	m_P1Status6 = _T("");
	m_P1Status7 = _T("");
	m_P2Status0 = _T("");
	m_P2Status1 = _T("");
	m_P2Status2 = _T("");
	m_P2Status3 = _T("");
	m_P2Status4 = _T("");
	m_P2Status5 = _T("");
	m_P2Status6 = _T("");
	m_P2Status7 = _T("");
	//}}AFX_DATA_INIT
}


void CKDIOForm::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKDIOForm)
	DDX_Radio(pDX, IDC_P1IOSEL1, m_P1IoSel);
	DDX_Radio(pDX, IDC_P2IOSEL1, m_P2IoSel);
	DDX_Check(pDX, IDC_P1OUT0, m_P1Out0);
	DDX_Check(pDX, IDC_P1OUT1, m_P1Out1);
	DDX_Check(pDX, IDC_P1OUT2, m_P1Out2);
	DDX_Check(pDX, IDC_P1OUT3, m_P1Out3);
	DDX_Check(pDX, IDC_P1OUT4, m_P1Out4);
	DDX_Check(pDX, IDC_P1OUT5, m_P1Out5);
	DDX_Check(pDX, IDC_P1OUT6, m_P1Out6);
	DDX_Check(pDX, IDC_P1OUT7, m_P1Out7);
	DDX_Check(pDX, IDC_P2OUT0, m_P2Out0);
	DDX_Check(pDX, IDC_P2OUT1, m_P2Out1);
	DDX_Check(pDX, IDC_P2OUT2, m_P2Out2);
	DDX_Check(pDX, IDC_P2OUT3, m_P2Out3);
	DDX_Check(pDX, IDC_P2OUT4, m_P2Out4);
	DDX_Check(pDX, IDC_P2OUT5, m_P2Out5);
	DDX_Check(pDX, IDC_P2OUT6, m_P2Out6);
	DDX_Check(pDX, IDC_P2OUT7, m_P2Out7);
	DDX_Text(pDX, IDC_P1STATUS0, m_P1Status0);
	DDX_Text(pDX, IDC_P1STATUS1, m_P1Status1);
	DDX_Text(pDX, IDC_P1STATUS2, m_P1Status2);
	DDX_Text(pDX, IDC_P1STATUS3, m_P1Status3);
	DDX_Text(pDX, IDC_P1STATUS4, m_P1Status4);
	DDX_Text(pDX, IDC_P1STATUS5, m_P1Status5);
	DDX_Text(pDX, IDC_P1STATUS6, m_P1Status6);
	DDX_Text(pDX, IDC_P1STATUS7, m_P1Status7);
	DDX_Text(pDX, IDC_P2STATUS0, m_P2Status0);
	DDX_Text(pDX, IDC_P2STATUS1, m_P2Status1);
	DDX_Text(pDX, IDC_P2STATUS2, m_P2Status2);
	DDX_Text(pDX, IDC_P2STATUS3, m_P2Status3);
	DDX_Text(pDX, IDC_P2STATUS4, m_P2Status4);
	DDX_Text(pDX, IDC_P2STATUS5, m_P2Status5);
	DDX_Text(pDX, IDC_P2STATUS6, m_P2Status6);
	DDX_Text(pDX, IDC_P2STATUS7, m_P2Status7);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CKDIOForm, CDialog)
	//{{AFX_MSG_MAP(CKDIOForm)
	ON_BN_CLICKED(IDC_P1IOSEL1, OnP1iosel1)
	ON_BN_CLICKED(IDC_P1IOSEL2, OnP1iosel2)
	ON_BN_CLICKED(IDC_P2IOSEL1, OnP2iosel1)
	ON_BN_CLICKED(IDC_P2IOSEL2, OnP2iosel2)
	ON_BN_CLICKED(IDC_P1OUT0, OnP1out0)
	ON_BN_CLICKED(IDC_P1OUT1, OnP1out1)
	ON_BN_CLICKED(IDC_P1OUT2, OnP1out2)
	ON_BN_CLICKED(IDC_P1OUT3, OnP1out3)
	ON_BN_CLICKED(IDC_P1OUT4, OnP1out4)
	ON_BN_CLICKED(IDC_P1OUT5, OnP1out5)
	ON_BN_CLICKED(IDC_P1OUT6, OnP1out6)
	ON_BN_CLICKED(IDC_P1OUT7, OnP1out7)
	ON_BN_CLICKED(IDC_P2OUT0, OnP2out0)
	ON_BN_CLICKED(IDC_P2OUT1, OnP2out1)
	ON_BN_CLICKED(IDC_P2OUT2, OnP2out2)
	ON_BN_CLICKED(IDC_P2OUT3, OnP2out3)
	ON_BN_CLICKED(IDC_P2OUT4, OnP2out4)
	ON_BN_CLICKED(IDC_P2OUT5, OnP2out5)
	ON_BN_CLICKED(IDC_P2OUT6, OnP2out6)
	ON_BN_CLICKED(IDC_P2OUT7, OnP2out7)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKDIOForm メッセージ ハンドラ

void CKDIOForm::SetId(short Id)
{
	short retErr;
	char str[30];
	BYTE Dir1,Dir2;
	BYTE DOUTStatus[2];

	DevId = Id;

	UpdateData(TRUE);

	sprintf(str,"ID = %d  Panel",DevId);
	SendMessage(WM_SETTEXT,0,(LPARAM)str);

	retErr = TUSBK16DIO_DirRead(DevId, &Dir1, &Dir2);
	if( retErr != 0 )
	{
		ErrorDisp(retErr);
		ShowWindow(SW_HIDE);
	}
	else
	{
		m_P1IoSel = Dir1;
		m_P2IoSel = Dir2;
	}

    retErr = TUSBK16DIO_OutputRead(DevId, &DOUTStatus[0], &DOUTStatus[1]);
	if( retErr != 0 )
	{
		ErrorDisp(retErr);
		ShowWindow(SW_HIDE);
	}
	else
	{
		m_P1Out0 = ( DOUTStatus[0] >> 0 ) & 0x01;
		m_P1Out1 = ( DOUTStatus[0] >> 1 ) & 0x01;
		m_P1Out2 = ( DOUTStatus[0] >> 2 ) & 0x01;
		m_P1Out3 = ( DOUTStatus[0] >> 3 ) & 0x01;
		m_P1Out4 = ( DOUTStatus[0] >> 4 ) & 0x01;
		m_P1Out5 = ( DOUTStatus[0] >> 5 ) & 0x01;
		m_P1Out6 = ( DOUTStatus[0] >> 6 ) & 0x01;
		m_P1Out7 = ( DOUTStatus[0] >> 7 ) & 0x01;
		
		m_P2Out0 = ( DOUTStatus[1] >> 0 ) & 0x01;
		m_P2Out1 = ( DOUTStatus[1] >> 1 ) & 0x01;
		m_P2Out2 = ( DOUTStatus[1] >> 2 ) & 0x01;
		m_P2Out3 = ( DOUTStatus[1] >> 3 ) & 0x01;
		m_P2Out4 = ( DOUTStatus[1] >> 4 ) & 0x01;
		m_P2Out5 = ( DOUTStatus[1] >> 5 ) & 0x01;
		m_P2Out6 = ( DOUTStatus[1] >> 6 ) & 0x01;
		m_P2Out7 = ( DOUTStatus[1] >> 7 ) & 0x01;
	}

	UpdateData(FALSE);

	SetTimer(TIMER1,100,NULL);
}

void CKDIOForm::ErrorDisp(short ErrCode)
{
	char str[50];

	switch( ErrCode )
	{
	case 0:
		strcpy(str,"正常に終了しました");
		break;
	case 2:
		strcpy(str,"ドライバが初期化されていません");
		break;
	case 6:
		strcpy(str,"デバイスが見つかりませんでした");
		break;
	case 8:
		strcpy(str,"パラメータエラー");
		break;
	case 9:
		strcpy(str,"USB通信エラー");
		break;
	default:
		strcpy(str,"不明なエラーです");
		break;
	}
	MessageBox(str,"リターンエラー",MB_OK);
}

void CKDIOForm::OnP1iosel1() 
{
	short retErr;

	retErr = TUSBK16DIO_Dir(DevId, 0, 0);
	if( retErr )
		ErrorDisp(retErr);
}

void CKDIOForm::OnP1iosel2() 
{
	short retErr;

	retErr = TUSBK16DIO_Dir(DevId, 0, 1);
	if( retErr )
		ErrorDisp(retErr);
}

void CKDIOForm::OnP2iosel1() 
{
	short retErr;

	retErr = TUSBK16DIO_Dir(DevId, 1, 0);
	if( retErr )
		ErrorDisp(retErr);
}

void CKDIOForm::OnP2iosel2() 
{
	short retErr;

	retErr = TUSBK16DIO_Dir(DevId, 1, 1);
	if( retErr )
		ErrorDisp(retErr);
}

void CKDIOForm::P1Out()
{
	BYTE Data;
	short retErr;

	UpdateData(TRUE);
	Data = 0;
	if( m_P1Out0 )
		Data += 0x01;
	if( m_P1Out1 )
		Data += 0x02;
	if( m_P1Out2 )
		Data += 0x04;
	if( m_P1Out3 )
		Data += 0x08;
	if( m_P1Out4 )
		Data += 0x10;
	if( m_P1Out5 )
		Data += 0x20;
	if( m_P1Out6 )
		Data += 0x40;
	if( m_P1Out7 )
		Data += 0x80;

	retErr = TUSBK16DIO_PortWrite(DevId, 0, Data);

	if( retErr )
		ErrorDisp(retErr);
}

void CKDIOForm::OnP1out0() 
{
	P1Out();
}

void CKDIOForm::OnP1out1() 
{
	P1Out();
}

void CKDIOForm::OnP1out2() 
{
	P1Out();
}

void CKDIOForm::OnP1out3() 
{
	P1Out();
}

void CKDIOForm::OnP1out4() 
{
	P1Out();
}

void CKDIOForm::OnP1out5() 
{
	P1Out();
}

void CKDIOForm::OnP1out6() 
{
	P1Out();
}

void CKDIOForm::OnP1out7() 
{
	P1Out();
}

void CKDIOForm::P2Out()
{
	BYTE Data;
	short retErr;

	UpdateData(TRUE);
	Data = 0;
	if( m_P2Out0 )
		Data += 0x01;
	if( m_P2Out1 )
		Data += 0x02;
	if( m_P2Out2 )
		Data += 0x04;
	if( m_P2Out3 )
		Data += 0x08;
	if( m_P2Out4 )
		Data += 0x10;
	if( m_P2Out5 )
		Data += 0x20;
	if( m_P2Out6 )
		Data += 0x40;
	if( m_P2Out7 )
		Data += 0x80;

	retErr = TUSBK16DIO_PortWrite(DevId, 1, Data);

	if( retErr )
		ErrorDisp(retErr);
}

void CKDIOForm::OnP2out0() 
{
	P2Out();	
}

void CKDIOForm::OnP2out1() 
{
	P2Out();
}

void CKDIOForm::OnP2out2() 
{
	P2Out();
}

void CKDIOForm::OnP2out3() 
{
	P2Out();
}

void CKDIOForm::OnP2out4() 
{
	P2Out();
}

void CKDIOForm::OnP2out5() 
{
	P2Out();
}

void CKDIOForm::OnP2out6() 
{
	P2Out();
}

void CKDIOForm::OnP2out7() 
{
	P2Out();
}

void CKDIOForm::OnTimer(UINT nIDEvent) 
{

	if( TUSBK16DIO_Chk(DevId) != 0 )
	{
		ShowWindow(SW_HIDE);
		KillTimer(TIMER1);
	}
	else
	{
		IoCheck();
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CKDIOForm::IoCheck()
{
	short retErr;
	BYTE Data1,Data2;

	UpdateData(TRUE);

    retErr = TUSBK16DIO_Read(DevId, &Data1, &Data2);
	if( retErr != 0 )
	{
		KillTimer(TIMER1);
		ShowWindow(SW_HIDE);
	}

	m_P1Status0 = HLCheck(Data1,0);
	m_P1Status1 = HLCheck(Data1,1);
	m_P1Status2 = HLCheck(Data1,2);
	m_P1Status3 = HLCheck(Data1,3);
	m_P1Status4 = HLCheck(Data1,4);
	m_P1Status5 = HLCheck(Data1,5);
	m_P1Status6 = HLCheck(Data1,6);
	m_P1Status7 = HLCheck(Data1,7);
    
	m_P2Status0 = HLCheck(Data2,0);
	m_P2Status1 = HLCheck(Data2,1);
	m_P2Status2 = HLCheck(Data2,2);
	m_P2Status3 = HLCheck(Data2,3);
	m_P2Status4 = HLCheck(Data2,4);
	m_P2Status5 = HLCheck(Data2,5);
	m_P2Status6 = HLCheck(Data2,6);
	m_P2Status7 = HLCheck(Data2,7);

	UpdateData(FALSE);
}

CString CKDIOForm::HLCheck(BYTE Data, int Bit)
{
	if( Data & ( 0x01 << Bit ) )
		return "High";
	else
		return "Low";
}
