
// InterruptCallbackDlg.cpp : implementation file
//

#include "stdafx.h"
#include "InterruptCallback.h"
#include "InterruptCallbackDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

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

// CInterruptCallbackDlg dialog



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

void CInterruptCallbackDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO_RISE, m_Rise);
	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_RETURN_CODE, m_ReturnCode);
	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);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_DeviceName);
}

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


// CInterruptCallbackDlg message handlers

BOOL CInterruptCallbackDlg::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 CInterruptCallbackDlg::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 CInterruptCallbackDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//---------------------------------------------------------
// DioInit()
//---------------------------------------------------------
void CInterruptCallbackDlg::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);
}

//---------------------------------------------------------
// Start monitoring interrupt
//---------------------------------------------------------
void CInterruptCallbackDlg::OnBnClickedButtonIntstart()
{
	short	i;
	short	BitNo;
	short	Kind;

	//-----------------------------
	// Set callback routine
	//-----------------------------
	Ret = DioSetInterruptCallBackProc(Id, CallBackProc, this);
	if (Ret != 0){
		DioGetErrorString(Ret, szError);
		wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
		m_ReturnCode.SetWindowText(szReturnCode);
		return;
	}

	//-----------------------------
	// 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;
	}

	//-----------------------------
	// Start interrupt
	//-----------------------------
	for (BitNo = 0; BitNo<4; BitNo++) {
		Ret = DioNotifyInterrupt(Id, BitNo, Kind, 0);
		if (Ret != DIO_ERR_SUCCESS) {
			break;
		}
	}

	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Stop monitoring interrupt
//---------------------------------------------------------
void CInterruptCallbackDlg::OnBnClickedButtonIntstop()
{
	short	BitNo;

	//-----------------------------
	// Stop interrupt
	//-----------------------------
	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 CInterruptCallbackDlg::OnBnClickedButtonDioExit()
{
	//-----------------------------
	// Perform exit-process
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Error process
	//-----------------------------
	DioGetErrorString(Ret, szError);
	wsprintf(szReturnCode, _T("Ret = %d : %S"), Ret, szError);
	m_ReturnCode.SetWindowText(szReturnCode);
}

//---------------------------------------------------------
// Dialog exit
//---------------------------------------------------------
void CInterruptCallbackDlg::OnBnClickedButtonExit()
{
	//-----------------------------
	// Perform exit-process
	//-----------------------------
	Ret = DioExit(Id);
	//-----------------------------
	// Dialog exit
	//-----------------------------
	CDialogEx::OnOK();
}

//--------------------------------------------------------------------------------------------------
//  Callback routine
//--------------------------------------------------------------------------------------------------
void CInterruptCallbackDlg::CallBackProc(short Id, WPARAM wParam, LPARAM lParam, void *Param)
{
	WORD	BitNo;
	short	Kind;
	TCHAR	TextTemp[100];

	//Create the class object
	CInterruptCallbackDlg *dlg;
	dlg = (CInterruptCallbackDlg *)Param;

	Id = LOWORD(wParam);	// ID getted from DioInit function
	BitNo = LOWORD(lParam);	// Interrupt BitNo
	Kind = HIWORD(lParam);	// Rise: 1, Fall: 2, Both: 

	if (Kind & DIO_INT_RISE) {
		dlg->UpCount[BitNo]++;		// Up counter
	}
	if (Kind & DIO_INT_FALL) {
		dlg->DownCount[BitNo]++;	// Down counter
	}

	//-----------------------------
	// Expression
	//-----------------------------
	wsprintf(TextTemp, _T("%d"), dlg->UpCount[BitNo]);
	dlg->m_Up[BitNo]->SetWindowText(TextTemp);
	wsprintf(TextTemp, _T("%d"), dlg->DownCount[BitNo]);
	dlg->m_Down[BitNo]->SetWindowText(TextTemp);

	return;
}