
// ApiBasicLoggerDlg.cpp : implementation file
//

#include "pch.h"
#include "framework.h"
#include "ApiBasicLogger.h"
#include "ApiBasicLoggerDlg.h"
#include "afxdialogex.h"
#include "clogger_api.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CApiBasicLoggerDlg dialog

CApiBasicLoggerDlg::CApiBasicLoggerDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_APIBASICLOGGER_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CApiBasicLoggerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_OPENMODE, m_combo_openmode);
	DDX_Control(pDX, IDC_EDIT_DEVICENAME, m_edit_devicename);
	DDX_Control(pDX, IDC_EDIT_RETURNNUM, m_edit_returnnum);
	DDX_Control(pDX, IDC_EDIT_SAMPLINGTIMES, m_edit_samplingtimes);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_edit_status);
	DDX_Control(pDX, IDC_EDIT_STOREDSAMPLINGCOUNT, m_edit_storedsamplingcount);
	DDX_Control(pDX, IDC_EDIT_TOTALSAMPLINGCOUNT, m_edit_totalsamplingcount);
	DDX_Control(pDX, IDC_MFCEDITBROWSE_IMPORTFILE, m_edit_importfile);
}

BEGIN_MESSAGE_MAP(CApiBasicLoggerDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_LOGGERSETDEVICENAME, &CApiBasicLoggerDlg::OnBnClickedButtonLoggersetdevicename)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERGETTOTALSAMPLINGCOUNT, &CApiBasicLoggerDlg::OnBnClickedButtonLoggergettotalsamplingcount)
	ON_BN_CLICKED(IDCCLOSE, &CApiBasicLoggerDlg::OnBnClickedCclose)
	ON_BN_CLICKED(IDC_BUTTON_LOGGEROPENAPPLICATION, &CApiBasicLoggerDlg::OnBnClickedButtonLoggeropenapplication)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERIMPORTSETTINGFILE, &CApiBasicLoggerDlg::OnBnClickedButtonLoggerimportsettingfile)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERSETEVENTSAMPLINGTIMES, &CApiBasicLoggerDlg::OnBnClickedButtonLoggerseteventsamplingtimes)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERGETEVENTSAMPLINGTIMES, &CApiBasicLoggerDlg::OnBnClickedButtonLoggergeteventsamplingtimes)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERSTARTOPERATION, &CApiBasicLoggerDlg::OnBnClickedButtonLoggerstartoperation)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERGETSTOREDSAMPLINGCOUNT, &CApiBasicLoggerDlg::OnBnClickedButtonLoggergetstoredsamplingcount)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERGETAPPLICATIONSTATUS, &CApiBasicLoggerDlg::OnBnClickedButtonLoggergetapplicationstatus)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERCLOSEAPPLICATION, &CApiBasicLoggerDlg::OnBnClickedButtonLoggercloseapplication)
	ON_BN_CLICKED(IDC_BUTTON_LOGGERSTOPOPERATION, &CApiBasicLoggerDlg::OnBnClickedButtonLoggerstopoperation)
END_MESSAGE_MAP()

//----------------------------------------
// Declaration of global variables
//----------------------------------------
short			g_appid;									// ID for application access
TCHAR			g_file_name[256];							// Sampling data output file name
unsigned long	g_samplingdata;								// Sampling data number
unsigned short	SequenceNum;								// Sequence number

// CApiBasicLoggerDlg message handlers
//================================================================================
// Initial settings
//================================================================================
BOOL CApiBasicLoggerDlg::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_edit_devicename.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Create the combobox for screen size
	//----------------------------------------
	m_combo_openmode.AddString(_T("Minimize"));	
	m_combo_openmode.AddString(_T("Normal"));
	m_combo_openmode.AddString(_T("Maximize"));
	//----------------------------------------
	// Change the selection of screen size combobox to Minimize
	//----------------------------------------
	m_combo_openmode.SetCurSel(LOGGER_PARAM_WINDOW_OFF);
	//----------------------------------------
	// Initializing global variables
    //----------------------------------------
	wsprintf(g_file_name, _T("SamplingData.csv"));
	g_samplingdata = 1;
	SequenceNum    = 1;
	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 CApiBasicLoggerDlg::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 CApiBasicLoggerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//================================================================================
// It is the process when the LoggerOpenApplication button is clicked
// Launch the app and enable access
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggeropenapplication()
{
	TCHAR			text_temp[256];			// For temporary text storage
	char			device_name[256];		// Device name
	char			error_string[256];		// Error code string
	unsigned short	openmode;				// Screen size
	long			ret;					// For return code
	CString			text_string;			// For updating textbox

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_edit_devicename.GetWindowText(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Get screen size from combobox(index=parameter)
	//----------------------------------------
	openmode = (unsigned short)m_combo_openmode.GetCurSel();
	//----------------------------------------
	// Initialize the application
	//----------------------------------------
	ret = LoggerOpenApplication(device_name, &g_appid, openmode);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerOpenApplication = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

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

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_edit_devicename.GetWindowText(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Set the device name
	//----------------------------------------
	ret = LoggerSetDeviceName(g_appid, device_name);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerSetDeviceName = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

//================================================================================
// It is the process when the LoggerImportSettingFile button is clicked
// Import the settings file
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggerimportsettingfile()
{
	TCHAR				text_temp[256];			// For temporary text storage
	char				filepath[256];			// File path
	char				error_string[256];		// Error code string
	long				ret;					// For return code
	CString				text_string;			// For error code display
	unsigned long long	status;					// For status storage

	//----------------------------------------
	// Get setting file path from the textbox
	//----------------------------------------
	m_edit_importfile.GetWindowText(text_temp, _countof(text_temp));
	WideCharToMultiByte(CP_ACP, 0, &text_temp[0], -1, &filepath[0], 256, NULL, NULL);
	//----------------------------------------
	// Import the settings file
	//----------------------------------------
	ret = LoggerImportSettingFile(g_appid, filepath, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerImportSettingFile = %ld : %s\r\nstatus=0x%016x"), ret, text_temp, status);
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

//================================================================================
// It is the process when the LoggerSetEventSamplingTimes button is clicked
// Set the specified sampling count
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggerseteventsamplingtimes()
{
	TCHAR			text_temp[256];			// For temporary text storage
	char			error_string[256];		// Error code string
	long			ret;					// For return code
	CString			text_string;			// For updating textbox
	unsigned long	sampling_times;			// For storing the specified sampling count

	//----------------------------------------
	//  Get the specified sampling count from the text box
	//----------------------------------------
	m_edit_samplingtimes.GetWindowText(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%lu"), &sampling_times);
	//----------------------------------------
	// Set the specified sampling count
	//----------------------------------------
	ret = LoggerSetEventSamplingTimes(g_appid, sampling_times);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerSetEventSamplingTimes = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

//================================================================================
// It is the process when the LoggerGetEventSamplingTimes button is clicked
// Get the specified sampling count
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggergeteventsamplingtimes()
{
	TCHAR			text_temp[256];			// For temporary text storage
	char			error_string[256];		// Error code string
	long			ret;					// For return code
	CString			text_string;			// For updating textbox
	unsigned long	sampling_times = 0;		// For storing the specified sampling count

	//----------------------------------------
	// Get the specified sampling count from the text box
	//----------------------------------------
	ret = LoggerGetEventSamplingTimes(g_appid, &sampling_times);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerGetEventSamplingTimes = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// If the specified sampling count acquisition function completes successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Display total sampling count
		//----------------------------------------
		text_string.Format(_T("%lu "), sampling_times);
		m_edit_samplingtimes.SetWindowText(text_string);
	}
	return;
}

//================================================================================
// It is the process when the Logging Start button is clicked
// Start the application input and output
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggerstartoperation()
{
	TCHAR			text_temp[256];			// For temporary text storage
	char			error_string[256];		// Error code string
	long			ret;					// For return code
	CString			text_string;			// For updating textbox
	unsigned long	status;					// For status storage
	unsigned long	event_setting;			// For event settings storage
	FILE			*fp;					// File Pointer
	errno_t			file_ret;				// Return code ​​of file operation functions
	unsigned short	InputType;				// Input method
	unsigned short	ChannelNo[256];			// Channel number
	char			ChName[256][256];		// Channel name
	unsigned short	Range[256];				// Range
	unsigned short	pre_sequence = 256;		// Sequence number

	//----------------------------------------
	// Close file
	//----------------------------------------
	ret = LoggerGraphWindowOperation(	g_appid,
										LOGGER_PARAM_GRAPH_CLOSE, 
										LOGGER_PARAM_FILE,
										0);							// Reserved
	if(ret != 0){
		//----------------------------------------
		// Get error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerGraphWindowOperation = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Delete file
	//----------------------------------------
	file_ret = _wfopen_s(&fp, g_file_name, _T("r"));
	if(file_ret == 0){
		fclose(fp);
		if(_wremove(g_file_name) != 0) {
			text_string.Format(_T("Data file deletion error"));
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
	}
	//----------------------------------------
	// Get the channel settings
	//----------------------------------------
	ret = LoggerGetChannelSetting(g_appid,
									&InputType,
									&pre_sequence,
									&ChannelNo[0],
									ChName[0],
									&Range[0]);
	if (ret != 0) {
		//----------------------------------------
		// Get the error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display the error code and the message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerGetChannelSetting = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Initialize the number of sampling data
	//----------------------------------------
	g_samplingdata = 1;
	//----------------------------------------
	// Specify the buffer mode (API acquisition mode)
	//----------------------------------------
	ret = LoggerSetAcquiredMode(g_appid, LOGGER_PARAM_MODE_API);
	if(ret != 0){
		//----------------------------------------
		// Get error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerSetAcquiredMode = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set the event
	//----------------------------------------
	event_setting = LOGGER_PARAM_EVENT_DATANUM | LOGGER_PARAM_EVENT_END;
	ret = LoggerSetNotifyEvent(g_appid, CallBackProc, event_setting, this);
	if(ret != 0) {
		//----------------------------------------
		// Get error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerSetNotifyEvent = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Put the sequence number into a global variable
	//----------------------------------------
	SequenceNum = pre_sequence;
	//----------------------------------------
	// Start the application input and output
	//----------------------------------------
	ret = LoggerStartOperation(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerStartOperation = %ld : %s\r\nstatus=%ld"), ret, text_temp, status);
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

//================================================================================
// It is the process when the LoggerGetTotalSamplingCount button is clicked
// Get the total number of samples
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggergettotalsamplingcount()
{
	TCHAR				text_temp[256];			// For temporary text storage
	char				error_string[256];		// Error code string
	long				ret;					// For return code
	CString				text_string;			// For updating textbox
	unsigned long long	sampling_count;			// For total sampling count storage

	//----------------------------------------
	// Get total sampling count
	//----------------------------------------
	ret = LoggerGetTotalSamplingCount(g_appid, &sampling_count);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerGetTotalSamplingCount = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// If the total sampling count acquisition function completes successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Display total sampling count
		//----------------------------------------
		text_string.Format(_T("%llu "), sampling_count);
		m_edit_totalsamplingcount.SetWindowText(text_string);
	}
	return;
}

//================================================================================
// It is the process when the LoggerGetStoredSamplingCount button is clicked
// Get the number of samples
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggergetstoredsamplingcount()
{
	TCHAR			text_temp[256];				// For temporary text storage
	char			error_string[256];			// Error code string
	long			ret;						// For return code
	CString			text_string;				// For updating textbox
	unsigned long	storead_sampling_count;		// For sampling count storage

	//----------------------------------------
	// Get sampling count
	//----------------------------------------
	ret = LoggerGetStoredSamplingCount(g_appid, &storead_sampling_count);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerGetStoredSamplingCount = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// If the specified sampling count acquisition function completes successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Display sampling count
		//----------------------------------------
		text_string.Format(_T("%lu "), storead_sampling_count);
		m_edit_storedsamplingcount.SetWindowText(text_string);
	}
	return;
}

//================================================================================
// It is the process when the LoggerGetApplicationStatus button is clicked
// Get the device status
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggergetapplicationstatus()
{
	TCHAR			text_temp[256];							// For temporary text storage
	TCHAR			status_string[256];						// Status string
	char			error_string[256];						// Error code string
	long			ret;									// For return code
	CString			text_string;							// For updating textbox
	unsigned long	status = LOGGER_STATUS_STOP;			// For status storage

	//----------------------------------------
	// Get the device status
	//----------------------------------------
	ret = LoggerGetApplicationStatus(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerGetApplicationStatus = %ld : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// If the status acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Get status string
		//----------------------------------------
		switch(status){
		case LOGGER_STATUS_STOP:
			wsprintf(status_string, _T("Stop"));
			break;
		case LOGGER_STATUS_START_TRG:
			wsprintf(status_string, _T("Wait the start trigger"));
			break;
		case LOGGER_STATUS_BUSY:
			wsprintf(status_string, _T("Operating"));
			break;
		default:
			wsprintf(status_string, _T("Unknown status"));
			break;
		}
		//----------------------------------------
		// Display the status
		//----------------------------------------
		text_string.Format(_T("%lu (%s)"), status, status_string);
		m_edit_status.SetWindowText(text_string);
	}
	return;
}

//================================================================================
// It is the process when the Logging Stop button is clicked
// Stop the application input and output
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggerstopoperation()
{
	TCHAR			text_temp[256];					// For temporary text storage
	char			error_string[256];				// Error code string
	long			ret;							// For return code
	CString			text_string;					// For updating textbox
	CString			text_string_temp;				// For updating textbox
	unsigned long	sampling_times;					// For storing the specified sampling count
	float			*sampling_data;					// For storing sampling data
	FILE			*fp;							// File pointer
	errno_t			file_ret;						// Return code ​​of file operation functions
	unsigned long	i;								// For loop counting
	unsigned long	j;								// For loop counting

	//----------------------------------------
	// Stop the application input and output
	//----------------------------------------
	ret = LoggerStopOperation(g_appid);
	if(ret != 0){
		//----------------------------------------
		// Get error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerStopOperation = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the sampling count
	//----------------------------------------
	ret = LoggerGetStoredSamplingCount(g_appid, &sampling_times);
	if(ret != 0) {
		//----------------------------------------
		// Get error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerGetStoredSamplingCount = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// No sampling data
	//----------------------------------------
	if(sampling_times <= 0) {
		return;
	}
	//----------------------------------------
	// For storing data for a specified number of samplings
	//----------------------------------------
	sampling_data = (float *)malloc(sizeof(float) * sampling_times * SequenceNum);
	if(sampling_data == NULL) {
		text_string.Format(_T("Store specified sampling count: Failed to allocate array for data storage"));
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get sampling data
	//----------------------------------------
	ret = LoggerGetSamplingData(g_appid, &sampling_times, sampling_data);
	if(ret != 0) {
		//----------------------------------------
		// Get error string
		//----------------------------------------
		LoggerGetErrorString(ret, error_string);
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerGetEventSamplingTimes = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		//--------------------
		// Free the array used for data storage
		//--------------------
		free(sampling_data);
		sampling_data = NULL;
		return;
	}
	//--------------------
	// Open file
	//--------------------
	file_ret = _wfopen_s(&fp, g_file_name, _T("a"));
	if(file_ret != 0) {
		text_string.Format(_T("Stores the specified number of sampling times：fopen = file open error."));
		m_edit_returnnum.SetWindowText(text_string);
		//--------------------
		// Free the array used for data storage
		//--------------------
		free(sampling_data);
		sampling_data = NULL;
		return;
	}
	//--------------------
	// Repeat processing until the specified number of samples is reached
	//--------------------
	for(i = 0; i < sampling_times; i++) {
		text_string.Empty();
		//--------------------
		// Add a display string
		//--------------------
		text_string_temp.Format(_T("%5d,"), (g_samplingdata++));
		text_string += text_string_temp;
		//--------------------
		// Add sampling data (Repeat the process for the specified number of channels)
		//--------------------
		for (j = 0; j < (unsigned long)(SequenceNum - 1); j++) {
			text_string_temp.Format(_T(" % 10.5f, "), sampling_data[i * SequenceNum + j]); 
			text_string += text_string_temp;
		}
		text_string_temp.Format(_T(" % 10.5f\n"), sampling_data[i * SequenceNum + j]);
		text_string += text_string_temp;
		//--------------------
		// Data writing
		//--------------------
		fputws(text_string, fp);
	}
	//--------------------
	// Close file
	//--------------------
	fclose(fp);
	//--------------------
	// Free the array used for data storage
	//--------------------
	free(sampling_data);
	sampling_data = NULL;
	//----------------------------------------
	// Display of collection stop completion message
	//----------------------------------------
	text_string.Format(_T("LoggerStopOperation : Successful completion"));
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

//================================================================================
// It is the process when the C-LOGGER Exit button is clicked
// Stop the app and disable access
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedButtonLoggercloseapplication()
{
	TCHAR			text_temp[256];										// For temporary text storage
	TCHAR			status_string[256];									// Status string
	char			error_string[256];									// Error code string
	long			ret;												// For return code
	CString			text_string;										// For updating textbox
	unsigned long	status;												// For status storage

	//----------------------------------------
	// Close the file(Considering that C-LOGGER may have exited, do not check the result)
	//----------------------------------------
	LoggerGraphWindowOperation(	g_appid,
								LOGGER_PARAM_GRAPH_CLOSE,
								LOGGER_PARAM_FILE,
								0);							// Reserved
	//----------------------------------------
	// Stop the application and disable access
	//----------------------------------------
	ret = LoggerCloseApplication(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	LoggerGetErrorString(ret, error_string);
	if (ret != 0) {
		//----------------------------------------
		// Display error code and message
		//----------------------------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("LoggerCloseApplication = %ld : %s"), ret, text_temp);
		m_edit_returnnum.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get status string
	//----------------------------------------
	switch(status){
	case LOGGER_STATUS_SUCCESS:
		wsprintf(status_string, _T("Successful completion"));
		break;
	case LOGGER_STATUS_FAILURE:
		wsprintf(status_string, _T("C-LOGGER exit error"));
		break;
	default:
		wsprintf(status_string, _T("Unknown status"));
		break;
	}
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("LoggerCloseApplication = %ld : %s\r\nstatus=%ld : %s"), ret, text_temp, status, status_string);
	m_edit_returnnum.SetWindowText(text_string);
	return;
}

//================================================================================
// It is the process when the Close button is clicked
// Exit the application
//================================================================================
void CApiBasicLoggerDlg::OnBnClickedCclose()
{
	unsigned long	status				= LOGGER_STATUS_SUCCESS;		// For status storage

	//----------------------------------------
	// Close the file(Considering that C-LOGGER may have exited, do not check the result)
	//----------------------------------------
	LoggerGraphWindowOperation(	g_appid,
										LOGGER_PARAM_GRAPH_CLOSE,
										LOGGER_PARAM_FILE,
										0);							// Reserved
	//----------------------------------------
	// Stop the application and disable access
	//----------------------------------------
	LoggerCloseApplication(g_appid, &status);
	//----------------------------------------
	// Exit the dialog
	//----------------------------------------
	CDialog::OnOK();
	return;
}

//================================================================================
// Callback routine
// 
//================================================================================
long CApiBasicLoggerDlg::CallBackProc(short Id, long AiEvent, WPARAM wParam, LPARAM lParam, void *Param)
{
	TCHAR				text_temp[256];					// For temporary text storage
	char				error_string[256];				// Error code string
	CString				text_string;					// For updating textbox
	CString				text_string_temp;				// For temporary text box storage
	unsigned long		sampling_times		= 0;		// For storing the specified number of samples
	float				*sampling_data		= NULL;		// For storing sampling data
	FILE				*fp;							// File Pointer
	errno_t				file_ret;						// Return code ​​of file operation functions
	unsigned long		i;								// For loop count
	long				ret;							// For return code
	CApiBasicLoggerDlg	*dlg;							// Caller dialog
	unsigned long		j;								// For loop count

	//----------------------------------------
	// Create the class object
	//----------------------------------------
	dlg = (CApiBasicLoggerDlg *)Param;
	//----------------------------------------
	// Specified Number Storage Event
	//----------------------------------------
	if(AiEvent == LOGGER_PARAM_EVENT_DATANUM){
		//----------------------------------------
		// Get the sampling count
		//----------------------------------------
		ret = LoggerGetStoredSamplingCount(g_appid, &sampling_times);
		if(ret != 0){
			//----------------------------------------
			// Get error string
			//----------------------------------------
			LoggerGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("LoggerGetStoredSamplingCount = %ld : %s"), ret, text_temp);
			dlg->m_edit_returnnum.SetWindowText(text_string);
			return 0;
		}
		//----------------------------------------
		// For storing data for the specified number of samples
		//----------------------------------------
		sampling_data = (float *)malloc(sizeof(float) * sampling_times * SequenceNum);
		if(sampling_data == NULL){
			text_string.Format(_T("Store specified sampling count: Failed to allocate array for data storage"));
			dlg->m_edit_returnnum.SetWindowText(text_string);
			return 0;
		}
		//----------------------------------------
		// Get sampling data
		//----------------------------------------
		ret = LoggerGetSamplingData(g_appid, &sampling_times, sampling_data);
		if(ret != 0){
			//----------------------------------------
			// Get error string
			//----------------------------------------
			LoggerGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("LoggerGetEventSamplingTimes = %ld : %s"), ret, text_temp);
			dlg->m_edit_returnnum.SetWindowText(text_string);
			//--------------------
			// Set the eventFree the array used for data storage
			//--------------------
			free(sampling_data);
			sampling_data = NULL;
			return 0;
		}
		//--------------------
		// Open file
		//--------------------
		file_ret = _wfopen_s(&fp, g_file_name, _T("a"));
		if(file_ret != 0){
			text_string.Format(_T("Stores the specified number of sampling times：fopen = file open error."));
			dlg->m_edit_returnnum.SetWindowText(text_string);
			//--------------------
			// Set the eventFree the array used for data storage
			//--------------------
			free(sampling_data);
			sampling_data = NULL;
			return 0;
		}
		//--------------------
		// Repeat processing until the specified number of samples is reached
		//--------------------
		for(i = 0; i < sampling_times; i++){
			text_string.Empty();
			//--------------------
			// Add a display string
			//--------------------
			text_string_temp.Format(_T("%5d,"), (g_samplingdata++));
			text_string += text_string_temp;
			//--------------------
			// Add sampling data (Repeat the process for the specified number of channels)
			//--------------------
			for (j = 0; j < (unsigned long)(SequenceNum - 1); j++) {
				text_string_temp.Format(_T(" % 10.5f, "), sampling_data[i * SequenceNum + j]);
				text_string += text_string_temp;
			}
			text_string_temp.Format(_T(" % 10.5f\n"), sampling_data[i * SequenceNum + j]);
			text_string += text_string_temp;
			//--------------------
			// Data writing
			//--------------------
			fputws(text_string, fp);
		}
		//--------------------
		// Close file
		//--------------------
		fclose(fp);
		//--------------------
		// Set the eventFree the array used for data storage
		//--------------------
		free(sampling_data);
		sampling_data = NULL;
	}
	//----------------------------------------
	// Specified Number End Event
	//----------------------------------------
	else if(AiEvent == LOGGER_PARAM_EVENT_END){
		//----------------------------------------
		// Get the sampling count
		//----------------------------------------
		ret = LoggerGetStoredSamplingCount(g_appid, &sampling_times);
		if(ret != 0){
			//----------------------------------------
			// Get error string
			//----------------------------------------
			LoggerGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("LoggerGetStoredSamplingCount = %ld : %s"), ret, text_temp);
			dlg->m_edit_returnnum.SetWindowText(text_string);
			return 0;
		}
		//----------------------------------------
		// For storing data for the specified number of samples
		//----------------------------------------
		sampling_data = (float *)malloc(sizeof(float) * sampling_times * SequenceNum);
		if(sampling_data == NULL){
			text_string.Format(_T("Store specified sampling count: Failed to allocate array for data storage"));
			dlg->m_edit_returnnum.SetWindowText(text_string);
			return 0;
		}
		//----------------------------------------
		// Get sampling data
		//----------------------------------------
		ret = LoggerGetSamplingData(g_appid, &sampling_times, sampling_data);
		if(ret != 0){
			//----------------------------------------
			// Get error string
			//----------------------------------------
			LoggerGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("LoggerGetEventSamplingTimes = %ld : %s"), ret, text_temp);
			dlg->m_edit_returnnum.SetWindowText(text_string);
			//--------------------
			// Set the eventFree the array used for data storage
			//--------------------
			free(sampling_data);
			sampling_data = NULL;
			return 0;
		}
		//--------------------
		// Open file
		//--------------------
		file_ret = _wfopen_s(&fp, g_file_name, _T("a"));
		if(file_ret != 0){
			text_string.Format(_T("Stores the specified number of sampling times：fopen = file open error."));
			dlg->m_edit_returnnum.SetWindowText(text_string);
			//--------------------
			// Set the eventFree the array used for data storage
			//--------------------
			free(sampling_data);
			sampling_data = NULL;
			return 0;
		}
		//--------------------
		// Repeat processing until the specified number of samples is reached
		//--------------------
		for(i = 0; i < sampling_times; i++){
			text_string.Empty();
			//--------------------
			// Add a display string
			//--------------------
			text_string_temp.Format(_T("%5d,"), (g_samplingdata++));
			text_string += text_string_temp;
			//--------------------
			// Add sampling data (Repeat the process for the specified number of channels)
			//--------------------
			for (j = 0; j < (unsigned long)(SequenceNum - 1); j++) {
				text_string_temp.Format(_T(" % 10.5f, "), sampling_data[i * SequenceNum + j]);
				text_string += text_string_temp;
			}
			text_string_temp.Format(_T(" % 10.5f\n"), sampling_data[i * SequenceNum + j]);
			text_string += text_string_temp;
			//--------------------
			// Data writing
			//--------------------
			fputws(text_string, fp);
		}
		//--------------------
		// Close file
		//--------------------
		fclose(fp);
		//--------------------
		// Set the eventFree the array used for data storage
		//--------------------
		free(sampling_data);
		sampling_data = NULL;
	}
	return 0;
}

