// IOControlDlg.cpp : 實作檔
//

#include "stdafx.h"
#include "IOControl.h"
#include "IOControlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CIOControlDlg 對話方塊
U8 bSlotNo = 0;

#define _CHKAPI(RetCode, FunName, GroupNo) \
	ret = RetCode; \
	if( ret != I8196_SUCCESS ) \
	{ \
		TRACE(L"%s(0x%02x) return: %d\n", FunName, GroupNo, ret);\
		m_stcFunErr.SetBitmap(m_hBmpErr); \
	}


CIOControlDlg::CIOControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIOControlDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIOControlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_stcFunErr, m_stcFunErr);
	DDX_Control(pDX, IDC_cmbSlotNo, m_cmbSlotNo);
}

BEGIN_MESSAGE_MAP(CIOControlDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_WM_TIMER()

	ON_CONTROL_RANGE(BN_CLICKED, IDC_stcGrp0_0, IDC_stcGrp7_15, OnstcGrp)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnSetGrp0, IDC_btnSetGrp7, OnbtnSetGrp)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_stcGDO0, IDC_stcGDO2, OnstcGDO)
	ON_BN_CLICKED(IDC_btnSetGPIO, &CIOControlDlg::OnBnClickedbtnsetgpio)
	ON_STN_CLICKED(IDC_stcFunErr, &CIOControlDlg::OnStnClickedstcfunerr)
	ON_CBN_SELCHANGE(IDC_cmbSlotNo, &CIOControlDlg::OnCbnSelchangecmbslotno)
END_MESSAGE_MAP()


// CIOControlDlg 訊息處理常式

BOOL CIOControlDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 設定此對話方塊的圖示。當應用程式的主視窗不是對話方塊時，
	// 框架會自動從事此作業
	SetIcon(m_hIcon, TRUE);			// 設定大圖示
	SetIcon(m_hIcon, FALSE);		// 設定小圖示


	int i = 0;

	eRET ret = I8196_SUCCESS;
	CString str;

	BOOL flgExistCard = FALSE;

	m_hBmpOnDO = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpGreen));
	m_hBmpOnDI = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpRed));
	m_hBmpOff = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpBlack));
	m_hBmpErr = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpRed));

	for (i = 0; i < 8; i++)
	{SetDlgItemInt(IDC_edtGrp0 + i, 0);}

	SetDlgItemInt(IDC_edtSetGPIODO, 0);

	m_cmbSlotNo.ResetContent();
	for (i = 0; i < 9; i++) // MAX_SLOT_QTY = 9
	{
		ret = i8196f_check_card_in_slot(i);
		if (ret == I8196_SUCCESS)
		{
			str.Format(L"%d", i);
			m_cmbSlotNo.AddString(str);
			flgExistCard = TRUE;

			_CHKAPI(i8196f_registration(i),
				L"i8196f_registration", 0);

			_CHKAPI(i8196f_load_power_on_cfg(i, DEFAULT_POWER_ON_SETTING),
				L"i8196f_load_power_on_cfg", 0);

		}
	}

	if (flgExistCard == FALSE)
	{
		m_cmbSlotNo.AddString(L"NULL");
		m_cmbSlotNo.SetCurSel(0);

		MessageBox(L"No I-8196F!", L"ERROR", MB_OK);
	}
	else
	{
		m_cmbSlotNo.SetCurSel(0);		
		m_cmbSlotNo.GetLBText(0, str);
		swscanf_s(str.GetBuffer(), L"%d", &bSlotNo);

		SetTimer( 1, 10, NULL );
	}

	
	return TRUE;  // 傳回 TRUE，除非您對控制項設定焦點
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CIOControlDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_IOCONTROL_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_IOCONTROL_DIALOG));
	}
}
#endif


void CIOControlDlg::OnTimer(UINT_PTR nIDEvent)
{
	eRET ret;
	int i = 0, j = 0;
	UINT stcGrp[16] = {IDC_stcGrp0_0, IDC_stcGrp1_0, IDC_stcGrp2_0, IDC_stcGrp3_0, IDC_stcGrp4_0, IDC_stcGrp5_0, IDC_stcGrp6_0, IDC_stcGrp7_0,
		IDC_stcGrp8_0, IDC_stcGrp9_0, IDC_stcGrp10_0, IDC_stcGrp11_0, IDC_stcGrp12_0, IDC_stcGrp13_0, IDC_stcGrp14_0, IDC_stcGrp15_0};
	CStatic *pStc;
	U16 wStatus = 0;
	U16 Status = 0;
	U16 DIModules = 0;	
	CString str;

		
	for (i = 0; i < 8; i++) //DO Group 0 ~ 7
	{
		_CHKAPI(i8196f_get_frnet_group_do( bSlotNo, (eFRNET_DO_ADDR)i, &wStatus),
			L"i8196f_get_frnet_group_do", i);
		for (j = 0; j < 16; j++) //Channel 0 ~15
		{
			pStc = (CStatic *)GetDlgItem(stcGrp[i]+j);
			if (wStatus & (0x0001 << j))
			{pStc->SetBitmap(m_hBmpOnDO);}
			else
			{pStc->SetBitmap(m_hBmpOff);}
		}
	}

	_CHKAPI(i8196f_scan_frnet_di(bSlotNo, &DIModules),
			L"i8196f_scan_frnet_di", 0);


	for (i = 8; i < 16; i++) //DI Group 8 ~ 15
	{
		_CHKAPI(i8196f_get_frnet_group_di( bSlotNo, ( eFRNET_DI_ADDR)i, &wStatus),
				L"i8196f_get_frnet_group_di", i);

		if ( DIModules & (0x0001<<(i-8)) ) //bit 0 ~ 7
		{
			for (j = 0; j < 16; j++) //Channel 0 ~15
			{
				pStc = (CStatic *)GetDlgItem(stcGrp[i]+j);
				if (wStatus & (0x0001 << j))
				{pStc->SetBitmap(m_hBmpOnDI);}
				else
				{pStc->SetBitmap(m_hBmpOff);}					
			}

			str.Format(L"0x%04x", wStatus);
			SetDlgItemText(IDC_edtGrp8+(i-8), str);
		}
		else
		{
			for (j = 0; j < 16; j++) //Channel 0 ~15
			{
				pStc = (CStatic *)GetDlgItem(stcGrp[i]+j);
				pStc->SetBitmap(m_hBmpOff);					
			}

			SetDlgItemText(IDC_edtGrp8+(i-8), L"NULL");
		}
	}

	_CHKAPI(i8196f_get_all_general_do( bSlotNo, &Status),
			L"i8196f_get_all_general_do", 0x07);
	for (i = 0; i < 3; i++)
	{
		pStc = (CStatic *)GetDlgItem(IDC_stcGDO0+i);
		if (Status & 0x0001 << i)
		{pStc->SetBitmap(m_hBmpOnDO);}
		else
		{pStc->SetBitmap(m_hBmpOff);}
	}

	_CHKAPI(i8196f_get_all_general_di( bSlotNo, &Status ),
		L"i8196f_get_all_general_di", 0x0FFF);
	for (i = 0; i < 12; i++)
	{
		pStc = (CStatic *)GetDlgItem(IDC_stcGDI0+i);
		if (Status & 0x0001 << i)
		{pStc->SetBitmap(m_hBmpOnDI);}
		else
		{pStc->SetBitmap(m_hBmpOff);}
	}
	str.Format(L"0x%04x", (Status & 0x0FFF) );
	SetDlgItemText(IDC_edtGPIODI, str);

	CDialog::OnTimer(nIDEvent);
}

void CIOControlDlg::OnstcGrp(UINT nID)
{
	WORD GroupNo = (nID-IDC_stcGrp0_0)/16;
	WORD Ch = (nID-IDC_stcGrp0_0)%16;

	ToggleFRnetDO(GroupNo, Ch);
}

int CIOControlDlg::ToggleFRnetDO(WORD GroupNo, WORD Ch)
{
	eRET ret;
	U16 Status = 0;

	_CHKAPI(i8196f_get_frnet_group_do(bSlotNo, (eFRNET_DO_ADDR)GroupNo, &Status), 
		L"i8196f_get_frnet_group_do", GroupNo);

	if ( Status & (0x0001<<Ch) )
	{Status &= ~(0x0001<<Ch);}
	else
	{Status |= (0x0001<<Ch);}

	_CHKAPI(i8196f_set_frnet_group_do(bSlotNo, (eFRNET_DO_ADDR)GroupNo, Status),
		L"i8196f_set_frnet_group_do", GroupNo);

	return 0;
}

void CIOControlDlg::OnbtnSetGrp(UINT nID)
{
	eRET ret;
	U16 GroupNo = nID - IDC_btnSetGrp0;
	CString str;
	unsigned int Value;
	GetDlgItemText(IDC_edtGrp0 + GroupNo, str);
	Value = wcstol(str, NULL, 16);

	_CHKAPI(i8196f_set_frnet_group_do(bSlotNo, (eFRNET_DO_ADDR)GroupNo, Value),
		L"i8196f_set_frnet_group_do", GroupNo);
}

void CIOControlDlg::OnstcGDO(UINT nID)
{ToggleGPIODO(nID - IDC_stcGDO0);}

int CIOControlDlg::ToggleGPIODO(WORD Ch)
{
	eRET ret;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_general_do( bSlotNo, (eGENERAL_DO)Ch, &OnOff ),
			L"i8196f_get_general_do", Ch);
	if (OnOff)
	{_CHKAPI(i8196f_set_general_do( bSlotNo, (eGENERAL_DO)Ch, OFF ),
			L"i8196f_set_general_do", Ch);}
	else
	{_CHKAPI(i8196f_set_general_do( bSlotNo, (eGENERAL_DO)Ch, ON ), 
			L"i8196f_set_general_do", Ch);}

	return 0;
}

void CIOControlDlg::OnBnClickedbtnsetgpio()
{
	eRET ret;
	CString str;
	unsigned int Value;
	GetDlgItemText(IDC_edtSetGPIODO, str);
	Value = wcstol(str, NULL, 16);

	_CHKAPI(i8196f_set_all_general_do( bSlotNo, Value ),
			L"i8196f_set_all_general_do", 0x07);
}

void CIOControlDlg::OnStnClickedstcfunerr()
{
	m_stcFunErr.SetBitmap(m_hBmpOff);
	//m_stcFunErr.SetBitmap(m_hBmpErr);

	TRACE(L"\n Reset Fun. Error\n");
}

void CIOControlDlg::OnCbnSelchangecmbslotno()
{
	CString str;
	int Index = m_cmbSlotNo.GetCurSel();

	m_cmbSlotNo.GetLBText(Index, str);
	swscanf_s(str.GetBuffer(), L"%d", &bSlotNo);
}
