﻿// ConditionDlg.cpp : 実装ファイル
//

#include "stdafx.h"
#include "AoGeneratingInfiniteDev.h"
#include "ConditionDlg.h"
#include "afxdialogex.h"
#include "Caio.h"


// CConditionDlg ダイアログ

IMPLEMENT_DYNAMIC(CConditionDlg, CDialogEx)

CConditionDlg::CConditionDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_CONDITION_DIALOG, pParent)
{

}

CConditionDlg::~CConditionDlg()
{
}

void CConditionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_START_CONDITION, m_ao_start_condition);
	DDX_Control(pDX, IDC_COMBO_CLOCK_TYPE, m_ao_clock_type);
	DDX_Control(pDX, IDC_EDIT_GENERATING_CLOCK, m_ao_generating_clock);
	DDX_Control(pDX, IDC_COMBO_EXTERNAL_CLOCK_INPUT_TIMING, m_ao_clock_edge);
	DDX_Control(pDX, IDC_COMBO_STOP_CONDITION, m_ao_stop_condition);
}


BEGIN_MESSAGE_MAP(CConditionDlg, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_CLOCK_TYPE, &CConditionDlg::OnCbnSelchangeComboClockType)
	ON_BN_CLICKED(IDC_BUTTON_OK, &CConditionDlg::OnBnClickedButtonOk)
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, &CConditionDlg::OnBnClickedButtonCancel)
END_MESSAGE_MAP()


//----------------------------------------
// グローバル変数の宣言
//----------------------------------------
extern	short	g_id;						// デバイスアクセス用ID
short	g_start_trigger[] = {	0,			// ソフトウェア
								1,			// 外部トリガ立ち上がり
								2 };		// 外部トリガ立ち下がり
short	g_clock_type[] = {		0,			// 内部クロック
								1 };		// 外部クロック
short	g_clock_edge[] = {		0,			// 立ち下がりエッジ
								1 };		// 立ち上がりエッジ
short	g_stop_trigger[] = {	1,			// 外部トリガ立ち上がり
								2 };		// 外部トリガ立ち下がり


// CConditionDlg メッセージ ハンドラー


BOOL CConditionDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	TCHAR	text_temp[256];			// テキスト一時格納用
	char	error_string[256];		// エラーコード文字列
	CString	text_string;			// テキストボックス更新用
	CString	message_string;			// メッセージ表示用
	long	ret;					// 戻り値取得用
	short	index;					// コンボボックスのインデックス
	short	ao_start_trigger;		// 変換開始条件
	short	ao_stop_trigger;		// 変換停止条件
	short	ao_clock_type;			// クロックの種類
	float	ao_generating_clock;	// ジェネレーティングクロック
	short	ao_clock_edge;			// 外部クロックの入力タイミング
	short	err_flag;				// 関数の実行エラーフラグ

	//----------------------------------------
	// パラメータの初期化
	//----------------------------------------
	err_flag	= 0;
	//----------------------------------------
	// アナログ出力変換開始条件用コンボボックスの作成
	//----------------------------------------
	m_ao_start_condition.AddString(_T("ソフトウェア"));
	m_ao_start_condition.AddString(_T("外部トリガ立ち上がり"));
	m_ao_start_condition.AddString(_T("外部トリガ立ち下がり"));
	//----------------------------------------
	// アナログ出力変換開始条件を取得
	//----------------------------------------
	ret	= AioGetAoStartTrigger(g_id, &ao_start_trigger);
	//----------------------------------------
	// 異常終了の処理
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// エラー文字列取得
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// エラーメッセージ生成
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoStartTrigger = %d : %s\n\n※エラーが発生したため表示を更新できませんでした。"), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// コントロールを無効に設定
			//--------------------
			m_ao_start_condition.EnableWindow(FALSE);
		}
		//--------------------
		// アナログ出力変換開始条件用コンボボックスの選択を0番に変更
		//--------------------
		m_ao_start_condition.SetCurSel(0);
	//----------------------------------------
	// 正常終了の処理
	//----------------------------------------
	}else{
		//--------------------
		// 変換開始条件からインデックスに変換
		//--------------------
		index	= 0;
		switch(ao_start_trigger){
		case 0:
			index	= 0;
			break;
		case 1:
			index	= 1;
			break;
		case 2:
			index	= 2;
			break;
		default:
			index	= 0;
			break;
		}
		//--------------------
		// アナログ出力変換開始条件用コンボボックスの選択を変更
		//--------------------
		m_ao_start_condition.SetCurSel(index);
	}
	//----------------------------------------
	// アナログ出力クロック種類用コンボボックスの作成
	//----------------------------------------
	m_ao_clock_type.AddString(_T("内部クロック"));
	m_ao_clock_type.AddString(_T("外部クロック"));
	//----------------------------------------
	// クロックの種類を取得
	//----------------------------------------
	ret	= AioGetAoClockType(g_id, &ao_clock_type);
	//----------------------------------------
	// 異常終了の処理
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// エラー文字列取得
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// エラーメッセージ生成
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoClockType = %d : %s\n\n※エラーが発生したため表示を更新できませんでした。"), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// コントロールを無効に設定
			//--------------------
			m_ao_clock_type.EnableWindow(FALSE);
		}
		//--------------------
		// アナログ出力クロック種類用コンボボックスの選択を0番に変更
		//--------------------
		m_ao_clock_type.SetCurSel(0);
		OnCbnSelchangeComboClockType();
	//----------------------------------------
	// 正常終了の処理
	//----------------------------------------
	}else{
		//--------------------
		// クロック種類からインデックスに変換
		//--------------------
		index	= 0;
		switch(ao_clock_type){
		case 0:
			index	= 0;
			break;
		case 1:
			index	= 1;
			break;
		default:
			index	= 0;
			break;
		}
		//--------------------
		// アナログ出力クロック種類用コンボボックスの選択を変更
		//--------------------
		m_ao_clock_type.SetCurSel(index);
		OnCbnSelchangeComboClockType();
	}
	//----------------------------------------
	// ジェネレーティングクロックを取得
	//----------------------------------------
	ret	= AioGetAoSamplingClock(g_id, &ao_generating_clock);
	//----------------------------------------
	// 異常終了の処理
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// エラー文字列取得
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// エラーメッセージ生成
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoSamplingClock = %d : %s\n\n※エラーが発生したため表示を更新できませんでした。"), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// コントロールを無効に設定
			//--------------------
			m_ao_generating_clock.EnableWindow(FALSE);
		}
		//--------------------
		// ジェネレーティングクロックの初期表示を更新
		//--------------------
		m_ao_generating_clock.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// 正常終了の処理
	//----------------------------------------
	}else{
		//--------------------
		// ジェネレーティングクロックを更新
		//--------------------
		text_string.Format(_T("%f"), ao_generating_clock);
		m_ao_generating_clock.SetWindowTextW(text_string);
	}
	//----------------------------------------
	// アナログ出力外部クロック入力タイミング用コンボボックスの作成
	//----------------------------------------
	m_ao_clock_edge.AddString(_T("立ち下がりエッジ"));
	m_ao_clock_edge.AddString(_T("立ち上がりエッジ"));
	//----------------------------------------
	// 外部クロックの場合
	//----------------------------------------
	if(ao_clock_type == 1){
		//----------------------------------------
		// 外部クロックの入力タイミングを取得
		//----------------------------------------
		ret	= AioGetAoClockEdge(g_id, &ao_clock_edge);
		//----------------------------------------
		// 異常終了の処理
		//----------------------------------------
		if(ret != 0){
			if(ret != 20001){
				//--------------------
				// エラー文字列取得
				//--------------------
				AioGetErrorString(ret, error_string);
				//--------------------
				// エラーメッセージ生成
				//--------------------
				wsprintf(text_temp, _T("%S"), error_string);
				message_string.Format(_T("AioGetAoClockEdge = %d : %s\n\n※エラーが発生したため表示を更新できませんでした。"), ret, text_temp);
				err_flag	= 1;
			}else{
				//--------------------
				// コントロールを無効に設定
				//--------------------
				m_ao_clock_edge.EnableWindow(FALSE);
			}
			//----------------------------------------
			// 外部クロック入力タイミング用コンボボックスの選択を0番に変更
			//----------------------------------------
			m_ao_clock_edge.SetCurSel(0);
		//----------------------------------------
		// 正常終了の処理
		//----------------------------------------
		}else{
			//--------------------
			// 入力タイミングからインデックスに変換
			//--------------------
			index	= 0;
			switch(ao_clock_edge){
			case 0:
				index	= 0;
				break;
			case 1:
				index	= 1;
				break;
			default:
				index	= 0;
				break;
			}
			//----------------------------------------
			// 外部クロック入力タイミング用コンボボックスの選択を変更
			//----------------------------------------
			m_ao_clock_edge.SetCurSel(index);
		}
	//----------------------------------------
	// 外部クロック以外の場合
	//----------------------------------------
	}else{
		//----------------------------------------
		// アナログ出力外部クロック入力タイミング用コンボボックスの選択を0番に変更
		//----------------------------------------
		m_ao_clock_edge.SetCurSel(0);
	}
	//----------------------------------------
	// アナログ出力変換停止条件用コンボボックスの作成
	//----------------------------------------
	m_ao_stop_condition.AddString(_T("外部トリガ立ち上がり"));
	m_ao_stop_condition.AddString(_T("外部トリガ立ち下がり"));
	//----------------------------------------
	// アナログ出力変換停止条件を取得
	//----------------------------------------
	ret	= AioGetAoStopTrigger(g_id, &ao_stop_trigger);
	//----------------------------------------
	// 異常終了の処理
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// エラー文字列取得
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// エラーメッセージ生成
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoStopTrigger = %d : %s\n\n※エラーが発生したため表示を更新できませんでした。"), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// コントロールを無効に設定
			//--------------------
			m_ao_stop_condition.EnableWindow(FALSE);
		}
		//--------------------
		// アナログ出力変換停止条件用コンボボックスの選択を0番に変更
		//--------------------
		m_ao_stop_condition.SetCurSel(0);
	//----------------------------------------
	// 正常終了の処理
	//----------------------------------------
	}else{
		//--------------------
		// 変換停止条件からインデックスに変換
		//--------------------
		index	= 0;
		switch(ao_stop_trigger){
		case 0:
			index	= 0;
			break;
		case 1:
			index	= 0;
			break;
		case 2:
			index	= 1;
			break;
		default:
			index	= 0;
			break;
		}
		//--------------------
		// アナログ出力変換停止条件用コンボボックスの選択を変更
		//--------------------
		m_ao_stop_condition.SetCurSel(index);
	}
	//----------------------------------------
	// エラーが発生していた場合メッセージを表示
	//----------------------------------------
	if(err_flag != 0){
		MessageBox(message_string, _T("エラー"), MB_OK);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 例外 : OCX プロパティ ページは必ず FALSE を返します。
}

//================================================================================
// クロック種類コンボボックスのインデックスが変更された際の処理になります
// 使用可能コントロールを更新します
//================================================================================
void CConditionDlg::OnCbnSelchangeComboClockType()
{
	short	index;				// コンボボックスのインデックス

	//----------------------------------------
	// コンボボックスからクロック種類のインデックスを取得
	//----------------------------------------
	index	= m_ao_clock_type.GetCurSel();
	//----------------------------------------
	// クロック種類による分岐処理
	//----------------------------------------
	switch(g_clock_type[index]){
	case 0:		// 内部クロック
		m_ao_generating_clock.EnableWindow(TRUE);
		m_ao_clock_edge.EnableWindow(FALSE);
		break;
	case 1:		// 外部クロック
		m_ao_generating_clock.EnableWindow(FALSE);
		m_ao_clock_edge.EnableWindow(TRUE);
		break;
	default:
		m_ao_generating_clock.EnableWindow(TRUE);
		m_ao_clock_edge.EnableWindow(FALSE);
		break;
	}
}

//================================================================================
// OKボタンをクリックした際の処理になります
// ジェネレーティング条件の設定を行います
//================================================================================
void CConditionDlg::OnBnClickedButtonOk()
{
	TCHAR	text_temp[256];			// テキスト一時格納用
	char	error_string[256];		// エラーコード文字列
	CString	text_string;			// テキストボックス更新用
	long	ret;					// 戻り値取得用
	short	index;					// コンボボックスのインデックス
	float	ao_generating_clock;	// ジェネレーティングクロック

	//----------------------------------------
	// アナログ出力の変換データ転送方式をデバイスバッファモードに設定
	// 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
	//----------------------------------------
	ret	= AioSetAoTransferMode(g_id, 0);
	//----------------------------------------
	// エラー処理
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// エラー文字列取得
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// メッセージ表示
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoTransferMode = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("エラー"), MB_OK);
		return;
	}
	//----------------------------------------
	// データ格納用メモリ形式をFIFOに設定
	// 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
	//----------------------------------------
	ret	= AioSetAoMemoryType(g_id, 0);
	//----------------------------------------
	// エラー処理
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// エラー文字列取得
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// メッセージ表示
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoMemoryType = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("エラー"), MB_OK);
		return;
	}
	//----------------------------------------
	// コンボボックスから変換開始条件設定値のインデックスを取得
	//----------------------------------------
	index	= m_ao_start_condition.GetCurSel();
	//----------------------------------------
	// アナログ出力変換開始条件の設定
	//----------------------------------------
	ret	= AioSetAoStartTrigger(g_id, g_start_trigger[index]);
	//----------------------------------------
	// エラー処理
	//----------------------------------------
	//--------------------
	// 正常終了でなければ処理を抜ける
	//--------------------
	if(ret != 0){
		//--------------------
		// エラー文字列取得
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// メッセージ表示
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoStartTrigger = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("エラー"), MB_OK);
		return;
	}
	//----------------------------------------
	// コンボボックスからクロック種類設定値のインデックスを取得
	//----------------------------------------
	index	= m_ao_clock_type.GetCurSel();
	//----------------------------------------
	// アナログ出力クロック種類の設定
	//----------------------------------------
	ret	= AioSetAoClockType(g_id, g_clock_type[index]);
	//----------------------------------------
	// エラー処理
	//----------------------------------------
	//--------------------
	// 正常終了でなければ処理を抜ける
	//--------------------
	if(ret != 0){
		//--------------------
		// エラー文字列取得
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// メッセージ表示
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoClockType = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("エラー"), MB_OK);
		return;
	}
	//----------------------------------------
	// 内部クロックの場合
	//----------------------------------------
	if(g_clock_type[index] == 0){
		//----------------------------------------
		// テキストボックスからジェネレーティングクロックを取得
		//----------------------------------------
		m_ao_generating_clock.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%f"), &ao_generating_clock);
		//----------------------------------------
		// アナログ出力ジェネレーティングクロックの設定
		//----------------------------------------
		ret	= AioSetAoSamplingClock(g_id, ao_generating_clock);
		//----------------------------------------
		// エラー処理
		//----------------------------------------
		//--------------------
		// 正常終了でなければ処理を抜ける
		//--------------------
		if(ret != 0){
			//--------------------
			// エラー文字列取得
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// メッセージ表示
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioSetAoSamplingClock = %d : %s"), ret, text_temp);
			MessageBox(text_string, _T("エラー"), MB_OK);
			return;
		}
	//----------------------------------------
	// 外部クロックの場合
	//----------------------------------------
	}else if(g_clock_type[index] == 1){
		//----------------------------------------
		// コンボボックスから外部クロック入力タイミング設定値のインデックスを取得
		//----------------------------------------
		index	= m_ao_clock_edge.GetCurSel();
		//----------------------------------------
		// 外部クロックの入力タイミングの設定
		//----------------------------------------
		ret	= AioSetAoClockEdge(g_id, g_clock_edge[index]);
		//----------------------------------------
		// エラー処理
		//----------------------------------------
		//--------------------
		// 正常終了でなくかつ使用不可でもなければ処理を抜ける
		//--------------------
		if((ret != 0) && (ret != 20001)){
			//--------------------
			// エラー文字列取得
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// メッセージ表示
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioSetAoClockEdge = %d : %s"), ret, text_temp);
			MessageBox(text_string, _T("エラー"), MB_OK);
			return;
		}
	}
	//----------------------------------------
	// コンボボックスから変換停止条件設定値のインデックスを取得
	//----------------------------------------
	index	= m_ao_stop_condition.GetCurSel();
	//----------------------------------------
	// アナログ出力変換停止条件の設定
	//----------------------------------------
	ret	= AioSetAoStopTrigger(g_id, g_stop_trigger[index]);
	//----------------------------------------
	// エラー処理
	//----------------------------------------
	//--------------------
	// 正常終了でなければ処理を抜ける
	//--------------------
	if(ret != 0){
		//--------------------
		// エラー文字列取得
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// メッセージ表示
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoStopTrigger  = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("エラー"), MB_OK);
		return;
	}
	//----------------------------------------
	// OK処理
	//----------------------------------------
	CDialogEx::OnOK();
}

//================================================================================
// Cancelボタンをクリックした際の処理になります
// ダイアログを終了します
//================================================================================
void CConditionDlg::OnBnClickedButtonCancel()
{
	//----------------------------------------
	// Cancel処理
	//----------------------------------------
	CDialogEx::OnCancel();
}
