﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using CdioCs;

namespace Sync
{
	/// <summary>
	/// SCondition の概要の説明です。
	/// </summary>
	public class SCondition : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.RadioButton rdoRing;
		private System.Windows.Forms.RadioButton rdoOnce;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtNumEvent;
		private System.Windows.Forms.ComboBox cboStopTrg;
		private System.Windows.Forms.ComboBox cboClockTrg;
		private System.Windows.Forms.ComboBox cboStartTrg;
		private System.Windows.Forms.TextBox txtStopNum;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		struct StrToNum
		{													// structure of condition and parameters
			public string	Str;
			public short	Num;
			public StrToNum(string Str, short Num)
			{
				this.Str = Str;
				this.Num = Num;
			}
		};
		// Start Condition
		static	StrToNum[] StartTrgTable = 
		{
			new StrToNum("ExtSig1",	(short)CdioConst.DIODM_START_EXTSIG_1),
			new StrToNum("ExtSig2",	(short)CdioConst.DIODM_START_EXTSIG_2),
			new StrToNum("ExtSig3",	(short)CdioConst.DIODM_START_EXTSIG_3),
			new StrToNum("End",		0)
		};
		// Clock Condition
		static	StrToNum[] ClockTrgTable = 
		{
			new StrToNum("ExtSig1",	(short)CdioConst.DIODM_CLK_EXTSIG_1),
			new StrToNum("ExtSig2",	(short)CdioConst.DIODM_CLK_EXTSIG_2),
			new StrToNum("ExtSig3",	(short)CdioConst.DIODM_CLK_EXTSIG_3),
			new StrToNum("End",		0)
		};
		// Stop Condition
		static	StrToNum[] StopTrgTable = 
		{
			new StrToNum("ExtSig1",	(short)CdioConst.DIODM_STOP_EXTSIG_1),
			new StrToNum("ExtSig2",	(short)CdioConst.DIODM_STOP_EXTSIG_2),
			new StrToNum("ExtSig3",	(short)CdioConst.DIODM_STOP_EXTSIG_3),
			new StrToNum("Number",	(short)CdioConst.DIODM_STOP_NUM),
			new StrToNum("End",		0)
		};

		Cdio				dio				= new Cdio();
		static string		szStart			= "ExtSig2";
		static string		szClock			= "ExtSig1";
		static string		szStop			= "ExtSig3";
		static uint			StopNumber		= 1000;
		public static uint	S_NumberOfData	= 500;
		public static short	S_IsRing		= (short)CdioConst.DIODM_WRITE_ONCE;

		public SCondition()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txtNumEvent = new System.Windows.Forms.TextBox();
			this.label15 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.rdoRing = new System.Windows.Forms.RadioButton();
			this.rdoOnce = new System.Windows.Forms.RadioButton();
			this.label14 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtStopNum = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.cboStopTrg = new System.Windows.Forms.ComboBox();
			this.cboClockTrg = new System.Windows.Forms.ComboBox();
			this.cboStartTrg = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox3.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtNumEvent,
																					this.label15});
			this.groupBox3.Location = new System.Drawing.Point(9, 188);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(410, 56);
			this.groupBox3.TabIndex = 4;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "転送個数イベント通知";
			// 
			// txtNumEvent
			// 
			this.txtNumEvent.Location = new System.Drawing.Point(8, 24);
			this.txtNumEvent.Name = "txtNumEvent";
			this.txtNumEvent.Size = new System.Drawing.Size(72, 19);
			this.txtNumEvent.TabIndex = 8;
			this.txtNumEvent.Text = "";
			this.txtNumEvent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label15
			// 
			this.label15.Location = new System.Drawing.Point(88, 28);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(96, 16);
			this.label15.TabIndex = 0;
			this.label15.Text = "データ毎に通知";
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(331, 253);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 24);
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "キャンセル";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(232, 253);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(88, 24);
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoRing,
																					this.rdoOnce,
																					this.label14});
			this.groupBox2.Location = new System.Drawing.Point(9, 133);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(410, 48);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "バッファ条件設定";
			// 
			// rdoRing
			// 
			this.rdoRing.Location = new System.Drawing.Point(160, 22);
			this.rdoRing.Name = "rdoRing";
			this.rdoRing.Size = new System.Drawing.Size(89, 16);
			this.rdoRing.TabIndex = 7;
			this.rdoRing.Text = "無限回転送";
			// 
			// rdoOnce
			// 
			this.rdoOnce.Location = new System.Drawing.Point(80, 22);
			this.rdoOnce.Name = "rdoOnce";
			this.rdoOnce.Size = new System.Drawing.Size(73, 16);
			this.rdoOnce.TabIndex = 6;
			this.rdoOnce.Text = "一回転送";
			// 
			// label14
			// 
			this.label14.Location = new System.Drawing.Point(8, 22);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(64, 16);
			this.label14.TabIndex = 0;
			this.label14.Text = "転送条件：";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtStopNum,
																					this.label4,
																					this.cboStopTrg,
																					this.cboClockTrg,
																					this.cboStartTrg,
																					this.label3,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(9, 13);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(410, 113);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "サンプリング条件";
			// 
			// txtStopNum
			// 
			this.txtStopNum.Location = new System.Drawing.Point(312, 81);
			this.txtStopNum.Name = "txtStopNum";
			this.txtStopNum.Size = new System.Drawing.Size(80, 19);
			this.txtStopNum.TabIndex = 5;
			this.txtStopNum.Text = "";
			this.txtStopNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(248, 82);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 16);
			this.label4.TabIndex = 5;
			this.label4.Text = "停止個数：";
			// 
			// cboStopTrg
			// 
			this.cboStopTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStopTrg.Location = new System.Drawing.Point(87, 80);
			this.cboStopTrg.Name = "cboStopTrg";
			this.cboStopTrg.Size = new System.Drawing.Size(152, 20);
			this.cboStopTrg.Sorted = true;
			this.cboStopTrg.TabIndex = 4;
			this.cboStopTrg.SelectedIndexChanged += new System.EventHandler(this.cboStopTrg_SelectedIndexChanged);
			// 
			// cboClockTrg
			// 
			this.cboClockTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboClockTrg.Location = new System.Drawing.Point(87, 52);
			this.cboClockTrg.Name = "cboClockTrg";
			this.cboClockTrg.Size = new System.Drawing.Size(152, 20);
			this.cboClockTrg.Sorted = true;
			this.cboClockTrg.TabIndex = 3;
			// 
			// cboStartTrg
			// 
			this.cboStartTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStartTrg.Location = new System.Drawing.Point(87, 24);
			this.cboStartTrg.Name = "cboStartTrg";
			this.cboStartTrg.Size = new System.Drawing.Size(152, 20);
			this.cboStartTrg.Sorted = true;
			this.cboStartTrg.TabIndex = 2;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 82);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "ストップ条件：";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 54);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "クロック条件：";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 26);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "スタート条件：";
			// 
			// SCondition
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(432, 294);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox3,
																		  this.btnCancel,
																		  this.btnOK,
																		  this.groupBox2,
																		  this.groupBox1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SCondition";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "スレーブサンプリング条件設定";
			this.Load += new System.EventHandler(this.SCondition_Load);
			this.groupBox3.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//===================================================
		//	ダイアログ初期化
		//===================================================
		private void SCondition_Load(object sender, System.EventArgs e)
		{
			//--------------------------------
			// 表示初期化
			//--------------------------------
			uint	i;
			string	strTmp;

			for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++) 
			{
				cboStartTrg.Items.Add(StartTrgTable[i].Str);
			}
			for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++) 
			{
				cboClockTrg.Items.Add(ClockTrgTable[i].Str);
			}
			for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++) 
			{
				cboStopTrg.Items.Add(StopTrgTable[i].Str);
			}

			cboStartTrg.Text = szStart;
			cboClockTrg.Text = szClock;
			cboStopTrg.Text = szStop;

			strTmp = string.Format("{0:d}", StopNumber);
			txtStopNum.Text = strTmp;
			strTmp = string.Format("{0:d}", S_NumberOfData);
			txtNumEvent.Text = strTmp;
			if (S_IsRing == (short)CdioConst.DIODM_WRITE_ONCE) 
			{
				rdoOnce.Checked = true;
				rdoRing.Checked = false;
			} 
			else 
			{
				rdoOnce.Checked = false;
				rdoRing.Checked = true;
			}

			cboStopTrg_SelectedIndexChanged(cboStopTrg, new System.EventArgs());
		}

		//===================================================
		//	ストップ条件設定変更
		//===================================================
		private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Stop Numberの有効、無効を切り替え
			//--------------------------------;
			szBuff = cboStopTrg.Text;
			if (string.Compare(szBuff, "Number") == 0) 
			{
				txtStopNum.Enabled = true;
			} 
			else 
			{
				txtStopNum.Enabled = false;
			}
		}

		//===================================================
		//	OKボタン処理
		//===================================================
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	szTmp = new string(' ', 100);
			short	StartTrg;
			short	ClockTrg;
			short	StopTrg;
			short	ExtSig1, ExtSig2, ExtSig3;
			short	i;
			short	Direction = (short)CdioConst.DIODM_DIR_IN;
			int		ret;
			string	strMsg;

			//--------------------------------
			// 表示データを取得
			//--------------------------------
			szStart = cboStartTrg.Text;
			szClock = cboClockTrg.Text;
			szStop = cboStopTrg.Text;
			szTmp = txtNumEvent.Text;
			try
			{
				S_NumberOfData = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				S_NumberOfData= 500;
			}
			szTmp = txtStopNum.Text;
			try
			{
				StopNumber = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				StopNumber = 1000;
			}
			if (rdoOnce.Checked == true) 
			{
				S_IsRing = (short)CdioConst.DIODM_WRITE_ONCE;
			} 
			else 
			{
				S_IsRing = (short)CdioConst.DIODM_WRITE_RING;
			}
			//--------------------------------
			// 設定値を数値に変換
			//--------------------------------
			StartTrg = 0;
			for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szStart, StartTrgTable[i].Str) == 0) 
				{
					StartTrg	= StartTrgTable[i].Num;
					break;
				}
			}
			ClockTrg = 0;
			for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szClock, ClockTrgTable[i].Str) == 0) 
				{
					ClockTrg	= ClockTrgTable[i].Num;
					break;
				}
			}
			StopTrg	= 0;
			for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szStop, StopTrgTable[i].Str) == 0) 
				{
					StopTrg	= StopTrgTable[i].Num;
					break;
				}
			}
			//--------------------------------
			// サンプリング条件を設定
			//--------------------------------
			// 入出力方向設定(全点入力)
			ret = dio.DmSetDirection(Sync.S_Id, (short)CdioConst.PI_32);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetDirection ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// スレーブ設定
			ExtSig1 = 1;
			ExtSig2 = 1;
			ExtSig3 = 1;
			ret = dio.DmSetSlave(Sync.S_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetSlave ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// スタート条件設定
			ret = dio.DmSetStartTrigger(Sync.S_Id, Direction, StartTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStartTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// クロック条件設定
			ret = dio.DmSetClockTrigger(Sync.S_Id, Direction, ClockTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetClockTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// ストップ条件設定
			ret = dio.DmSetStopTrigger(Sync.S_Id, Direction, StopTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStopTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// 停止個数設定
			ret = dio.DmSetStopNumber(Sync.S_Id, Direction, StopNumber);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStopNumber ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
	
			this.Close();
		}

		//===================================================
		//	キャンセルボタン処理
		//===================================================
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
