﻿
// TimerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Timer.h"
#include "TimerDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTimerDlg dialog



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

void CTimerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_EDIT_INTERVAL, m_interval);
	DDX_Control(pDX, IDC_EDIT_EVENT_COUNT, m_event_count);
	DDX_Control(pDX, IDC_EDIT_LAP_TIME, m_lap_time);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CTimerDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CTimerDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_AIO_RESET_DEV, &CTimerDlg::OnBnClickedButtonAioResetDev)
	ON_BN_CLICKED(IDC_BUTTON_INTERVAL_START, &CTimerDlg::OnBnClickedButtonIntervalStart)
	ON_BN_CLICKED(IDC_BUTTON_INTERVAL_STOP, &CTimerDlg::OnBnClickedButtonIntervalStop)
	ON_BN_CLICKED(IDC_BUTTON_WATCH_START, &CTimerDlg::OnBnClickedButtonWatchStart)
	ON_BN_CLICKED(IDC_BUTTON_LAP_TM_COUNT, &CTimerDlg::OnBnClickedButtonLapTmCount)
	ON_BN_CLICKED(IDC_BUTTON_RESET_TM_COUNT, &CTimerDlg::OnBnClickedButtonResetTmCount)
	ON_BN_CLICKED(IDC_BUTTON_WATCH_STOP, &CTimerDlg::OnBnClickedButtonWatchStop)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CTimerDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CTimerDlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;						// ID for device access
short	g_timer_id;					// Timer ID
long	g_event_count;				// Interval elapsed event occurrence count

// CTimerDlg message handlers

BOOL CTimerDlg::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

	//----------------------------------------
	// Update the initial display of device name
	//----------------------------------------
	m_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Update the initial display of interval
	//----------------------------------------
	m_interval.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// Update the initial display of notification count
	//----------------------------------------
	m_event_count.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of elapsed time
	//----------------------------------------
	m_lap_time.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_id			= 0;
	g_timer_id		= 0;
	g_event_count	= 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 CTimerDlg::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 CTimerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CTimerDlg::OnBnClickedButtonAioInit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	device_name[256];	// Device name
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Device initialization
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioInit(device_name, &g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioResetDevice button is clicked
// Reset the device
//================================================================================
void CTimerDlg::OnBnClickedButtonAioResetDev()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Reset device
	//----------------------------------------
	ret	= AioResetDevice(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioResetDevice = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Operation Start (Interval Timer) button is clicked
// Start operation
//================================================================================
void CTimerDlg::OnBnClickedButtonIntervalStart()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	float	interval;			// Interval

	//----------------------------------------
	// Set the event factor to 'Event that interval elapses'
	//----------------------------------------
	ret	= AioSetTmEvent(g_id, g_timer_id, m_hWnd, TME_INT);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Operation Start: AioSetTmEvent = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Initialize notification count
	//----------------------------------------
	g_event_count	= 0;
	//----------------------------------------
	// Get the interval from textbox
	//----------------------------------------
	m_interval.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%f"), &interval);
	//----------------------------------------
	// Start operation
	//----------------------------------------
	ret	= AioStartTmTimer(g_id, g_timer_id, interval);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Operation Start: AioStartTmTimer = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that operation start is completed
	//----------------------------------------
	text_string.Format(_T("Operation Start: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Operation Stop (Interval Timer) button is clicked
// Stop operation
//================================================================================
void CTimerDlg::OnBnClickedButtonIntervalStop()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Stop operation
	//----------------------------------------
	ret	= AioStopTmTimer(g_id, g_timer_id);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Operation Stop: AioStopTmTimer = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that operation stop is completed
	//----------------------------------------
	text_string.Format(_T("Operation Stop: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Operation Start (Stop Watch Timer) button is clicked
// Start measurement 
//================================================================================
void CTimerDlg::OnBnClickedButtonWatchStart()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Start measurement
	//----------------------------------------
	ret	= AioStartTmCount(g_id, g_timer_id);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Operation Start: AioStartTmCount = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that measurement start is completed
	//----------------------------------------
	text_string.Format(_T("Operation Start: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioLapTmCount button is clicked
// Get the elapsed time
//================================================================================
void CTimerDlg::OnBnClickedButtonLapTmCount()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	long	lap;				// Elapsed time

	//----------------------------------------
	// Get the elapsed time
	//----------------------------------------
	ret	= AioLapTmCount(g_id, g_timer_id, &lap);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioLapTmCount = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the elapsed time acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Display the elapsed time
		//----------------------------------------
		text_string.Format(_T("%ld"), lap);
		m_lap_time.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the AioResetTmCount button is clicked
// Reset the elapsed time
//================================================================================
void CTimerDlg::OnBnClickedButtonResetTmCount()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Reset
	//----------------------------------------
	ret	= AioResetTmCount(g_id, g_timer_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioResetTmCount = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Operation Stop (Stop Watch Timer) button is clicked
// Stop measurement
//================================================================================
void CTimerDlg::OnBnClickedButtonWatchStop()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Stop measurement
	//----------------------------------------
	ret	= AioStopTmCount(g_id, g_timer_id);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Operation Stop: AioStopTmCount = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that measurement stop is completed
	//----------------------------------------
	text_string.Format(_T("Operation Stop: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioExit button is clicked
// Perform the exit process of device
//================================================================================
void CTimerDlg::OnBnClickedButtonAioExit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Exit process
	//----------------------------------------
	ret	= AioExit(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioExit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Close button is clicked
// Perform the exit process of device and terminate the application
//================================================================================
void CTimerDlg::OnBnClickedClose()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}

//================================================================================
// Event message routine
//================================================================================
LRESULT CTimerDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	CString	text_string;		// For updating the textbox

	//----------------------------------------
	// Process of Event that interval elapses
	//----------------------------------------
	if(message == AIOM_TME_INT){
		//----------------------------------------
		// Display the interval elapsed event occurrence count
		//----------------------------------------
		g_event_count++;
		text_string.Format(_T("%ld"), g_event_count);
		m_event_count.SetWindowText(text_string);
		return TRUE;
	}

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