
// InterruptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Interrupt.h"
#include "InterruptDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// DIO Header File
//---------------------------------------------------------
#include "cdio.h"

// CInterruptDlg dialog



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

void CInterruptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_ReturnCode);
	DDX_Control(pDX, IDC_RADIO_RISE, m_Rise);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_DeviceName);
	DDX_Control(pDX, IDC_EDIT_UP3, m_Up3);
	DDX_Control(pDX, IDC_EDIT_UP2, m_Up2);
	DDX_Control(pDX, IDC_EDIT_UP1, m_Up1);
	DDX_Control(pDX, IDC_EDIT_UP0, m_Up0);
	DDX_Control(pDX, IDC_EDIT_DOWN3, m_Down3);
	DDX_Control(pDX, IDC_EDIT_DOWN2, m_Down2);
	DDX_Control(pDX, IDC_EDIT_DOWN1, m_Down1);
	DDX_Control(pDX, IDC_EDIT_DOWN0, m_Down0);
}

BEGIN_MESSAGE_MAP(CInterruptDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DIO_INIT, &CInterruptDlg::OnBnClickedButtonDioInit)
	ON_BN_CLICKED(IDC_BUTTON_INTSTART, &CInterruptDlg::OnBnClickedButtonIntstart)
	ON_BN_CLICKED(IDC_BUTTON_INTSTOP, &CInterruptDlg::OnBnClickedButtonIntstop)
	ON_BN_CLICKED(IDC_BUTTON_DIO_EXIT, &CInterruptDlg::OnBnClickedButtonDioExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CInterruptDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CInterruptDlg message handlers

BOOL CInterruptDlg::OnInitDialog()
{
	CDialogEx::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

	short	i;
	//-----------------------------------
	// Screen Item Initialization
	//-----------------------------------
	m_DeviceName.SetWindowText(_T("DIO000"));
	m_ReturnCode.SetWindowText(_T(""));
	// Up counter
	m_Up[0] = &m_Up0;
	m_Up[1] = &m_Up1;
	m_Up[2] = &m_Up2;
	m_Up[3] = &m_Up3;
	for (i = 0; i<4; i++) {
		m_Up[i]->SetWindowText(_T(""));
	}
	// Down counter
	m_Down[0] = &m_Down0;
	m_Down[1] = &m_Down1;
	m_Down[2] = &m_Down2;
	m_Down[3] = &m_Down3;
	for (i = 0; i<4; i++) {
		m_Down[i]->SetWindowText(_T(""));
	}

	m_Rise.SetCheck(TRUE);
	SendDlgItemMessage(IDC_RADIO_FALL, BM_SETCHECK, 0, 0);

	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 CInterruptDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
	{
		CDialogEx::OnPaint();
	}
}

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


//---------------------------------------------------------
// DioInit()
//---------------------------------------------------------
void CInterruptDlg::OnBnClickedButtonDioInit()
{
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	//-----------------------------
	// Get device name from screen
	//-----------------------------
	m_DeviceName.GetWindowText(szDeviceName, sizeof(szDeviceName));
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------
	// Execute initialization function and get ID
	//-----------------------------
	Ret = DioInit(DeviceName, &Id);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Interrupt observe start
//---------------------------------------------------------
void CInterruptDlg::OnBnClickedButtonIntstart()
{
	short	i;
	short	BitNo;
	short	Kind;
	//-----------------------------
	// Parameters initialization
	//-----------------------------
	for (i = 0; i<4; i++) {
		UpCount[i] = 0;			// Up counter
		DownCount[i] = 0;		// Down counter
	}

	if (m_Rise.GetCheck() == 1) {
		Kind = DIO_INT_RISE;
	}
	else {
		Kind = DIO_INT_FALL;
	}

	//-----------------------------
	// Interrupt start
	//-----------------------------
	for (BitNo = 0; BitNo<4; BitNo++) {
		Ret = DioNotifyInterrupt(Id, BitNo, Kind, m_hWnd);
		if (Ret != DIO_ERR_SUCCESS) {
			break;
		}
	}
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Interrupt observe stop
//---------------------------------------------------------
void CInterruptDlg::OnBnClickedButtonIntstop()
{
	short	BitNo;

	for (BitNo = 0; BitNo<4; BitNo++) {
		Ret = DioNotifyInterrupt(Id, BitNo, DIO_INT_NONE, m_hWnd);
		if (Ret != DIO_ERR_SUCCESS) {
			break;
		}
	}
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------
//	DioExit()
//---------------------------------------------------
void CInterruptDlg::OnBnClickedButtonDioExit()
{
	//-----------------------------
	// Get device name from screen
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Dialog exit
//---------------------------------------------------------
void CInterruptDlg::OnBnClickedButtonExit()
{
	//-----------------------------
	// Exit function execute
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Dialog exit
	//-----------------------------
	CDialogEx::OnOK();
}

//---------------------------------------------------------
// Event message routine
//---------------------------------------------------------
LRESULT CInterruptDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	WORD	BitNo;
	short	GetId;
	short	Kind;

	if (message == DIOM_INTERRUPT){
		//-----------------------------
		// Message interpret
		//-----------------------------
		GetId = LOWORD(wParam);	// ID getted from DioInit function
		BitNo = LOWORD(lParam);	// Interrupt BitNo
		Kind = HIWORD(lParam);	// StartUp:1, StartDown:2
		if (Kind & DIO_INT_RISE) {
			UpCount[BitNo]++;		// Up counter
		}
		if (Kind & DIO_INT_FALL) {
			DownCount[BitNo]++;		// Down counter
		}
		//-----------------------------
		// Expression
		//-----------------------------
		wsprintf(szText, _T("%d"), UpCount[BitNo]);
		m_Up[BitNo]->SetWindowText(szText);
		wsprintf(szText, _T("%d"), DownCount[BitNo]);
		m_Down[BitNo]->SetWindowText(szText);
	}

	return CDialogEx::DefWindowProc(message, wParam, lParam);
}
