﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
// Counter headder
using CcntCs;

namespace Sync
{
	/// <summary>
	/// SSampMode の概要の説明です。
	/// </summary>
	public class SSampMode : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txtStopNumber;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.ComboBox cboStopWith;
		private System.Windows.Forms.ComboBox cboStop;
		private System.Windows.Forms.ComboBox cboClock;
		private System.Windows.Forms.ComboBox cboStartWith;
		private System.Windows.Forms.ComboBox cboStart;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		public System.Windows.Forms.Button btnCancel;
		public System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.RadioButton rdoRing;
		private System.Windows.Forms.RadioButton rdoOnce;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox txtNumberData;
		private System.Windows.Forms.Label label16;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public struct StrNum
		{
			public string	Str;
			public short	Num;
			public StrNum(string Str, short Num)
			{
				this.Str = Str;
				this.Num = Num;
			}
		};
		
		// Sampling Start
		static StrNum[] Start = 
		{
			new StrNum("ExtSig1",						(short)CcntConst.CNTS_START_EXTSIG_1			),
			new StrNum("ExtSig2",						(short)CcntConst.CNTS_START_EXTSIG_2			),
			new StrNum("ExtSig3",						(short)CcntConst.CNTS_START_EXTSIG_3			),
			new StrNum("End",							0												)
		};
		// Counter Start
		static StrNum[] StartWith = 
		{
			new StrNum("サンプリングとは無関係に開始",	(short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP	),
			new StrNum("サンプリングと同時に開始",		(short)CcntConst.CNTS_START_CNT_WITH_SAMP		),
			new StrNum("End",							0												)
		};
		// Sampling Clock
		static StrNum[] Clock =
		{
			new StrNum("ExtSig1",						(short)CcntConst.CNTS_CLOCK_EXTSIG_1			),
			new StrNum("ExtSig2",						(short)CcntConst.CNTS_CLOCK_EXTSIG_2			),
			new StrNum("ExtSig3",						(short)CcntConst.CNTS_CLOCK_EXTSIG_3			),
			new StrNum("End",							0												)
		};
		// Sampling Stop
		static StrNum[] Stop = 
		{
			new StrNum("ExtSig1",						(short)CcntConst.CNTS_STOP_EXTSIG_1				),
			new StrNum("ExtSig2",						(short)CcntConst.CNTS_STOP_EXTSIG_2				),
			new StrNum("ExtSig3",						(short)CcntConst.CNTS_STOP_EXTSIG_3				),
			new StrNum("指定個数転送完了",				(short)CcntConst.CNTS_STOP_NUM					),
			new StrNum("End",							0												)
		};

		static StrNum[] StopWith = 
		{
			new StrNum("サンプリングとは無関係に停止",	(short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP	),
			new StrNum("サンプリングと同時に停止",		(short)CcntConst.CNTS_STOP_CNT_WITH_SAMP		),
			new StrNum("End",							0												)
		};

		static string		szStart			= "ExtSig2";
		static string		szStartWith		= "サンプリングとは無関係に開始";
		static string		szClock			= "ExtSig1";
		static string		szStop			= "ExtSig3";
		static string		szStopWith		= "サンプリングとは無関係に停止";
		static uint			StopNum			= 1000;

		public static short	S_IsRing		= (short)CcntConst.CNTS_WRITE_ONCE;
		public static uint	S_NotifyCount	= 500;

		Ccnt				cnt				= new Ccnt();

		static short str_to_num(StrNum[] strnum, string str)
		{
			ushort	i;
			for (i=0; string.Compare(strnum[i].Str, "End") != 0; i++) {
				if (string.Compare(strnum[i].Str, str) == 0) {
					return strnum[i].Num;
				}
			}
			return 0;
		}

		static string num_to_str(StrNum[] strnum, short num)
		{
			ushort	i;
			for (i=0; string.Compare(strnum[i].Str, "End") != 0; i++) {
				if (strnum[i].Num  == num) {
					return strnum[i].Str;
				}
			}
			return strnum[0].Str;
		}

		public SSampMode()
		{
			//
			// 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.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtStopNumber = new System.Windows.Forms.TextBox();
			this.label12 = new System.Windows.Forms.Label();
			this.cboStopWith = new System.Windows.Forms.ComboBox();
			this.cboStop = new System.Windows.Forms.ComboBox();
			this.cboClock = new System.Windows.Forms.ComboBox();
			this.cboStartWith = new System.Windows.Forms.ComboBox();
			this.cboStart = new System.Windows.Forms.ComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.rdoRing = new System.Windows.Forms.RadioButton();
			this.rdoOnce = new System.Windows.Forms.RadioButton();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.txtNumberData = new System.Windows.Forms.TextBox();
			this.label16 = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtStopNumber,
																					this.label12,
																					this.cboStopWith,
																					this.cboStop,
																					this.cboClock,
																					this.cboStartWith,
																					this.cboStart,
																					this.label5,
																					this.label4,
																					this.label3,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(480, 160);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "サンプリングモード";
			// 
			// txtStopNumber
			// 
			this.txtStopNumber.Location = new System.Drawing.Point(371, 103);
			this.txtStopNumber.Name = "txtStopNumber";
			this.txtStopNumber.Size = new System.Drawing.Size(96, 19);
			this.txtStopNumber.TabIndex = 20;
			this.txtStopNumber.Text = "";
			// 
			// label12
			// 
			this.label12.AutoSize = true;
			this.label12.Location = new System.Drawing.Point(307, 106);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(60, 12);
			this.label12.TabIndex = 16;
			this.label12.Text = "停止個数：";
			// 
			// cboStopWith
			// 
			this.cboStopWith.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStopWith.Location = new System.Drawing.Point(128, 128);
			this.cboStopWith.Name = "cboStopWith";
			this.cboStopWith.Size = new System.Drawing.Size(168, 20);
			this.cboStopWith.TabIndex = 9;
			// 
			// cboStop
			// 
			this.cboStop.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStop.Location = new System.Drawing.Point(128, 102);
			this.cboStop.Name = "cboStop";
			this.cboStop.Size = new System.Drawing.Size(168, 20);
			this.cboStop.TabIndex = 8;
			this.cboStop.SelectedIndexChanged += new System.EventHandler(this.cboStop_SelectedIndexChanged);
			// 
			// cboClock
			// 
			this.cboClock.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboClock.Location = new System.Drawing.Point(128, 76);
			this.cboClock.Name = "cboClock";
			this.cboClock.Size = new System.Drawing.Size(168, 20);
			this.cboClock.TabIndex = 7;
			// 
			// cboStartWith
			// 
			this.cboStartWith.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStartWith.Location = new System.Drawing.Point(128, 50);
			this.cboStartWith.Name = "cboStartWith";
			this.cboStartWith.Size = new System.Drawing.Size(168, 20);
			this.cboStartWith.TabIndex = 6;
			// 
			// cboStart
			// 
			this.cboStart.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStart.Location = new System.Drawing.Point(128, 24);
			this.cboStart.Name = "cboStart";
			this.cboStart.Size = new System.Drawing.Size(168, 20);
			this.cboStart.TabIndex = 5;
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(9, 132);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(109, 12);
			this.label5.TabIndex = 4;
			this.label5.Text = "カウンタストップモード：";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(9, 106);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(70, 12);
			this.label4.TabIndex = 3;
			this.label4.Text = "ストップ条件：";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(9, 80);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(69, 12);
			this.label3.TabIndex = 2;
			this.label3.Text = "クロック条件：";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(9, 54);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(112, 12);
			this.label2.TabIndex = 1;
			this.label2.Text = "カウンタスタートモード：";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(9, 28);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "スタート条件：";
			// 
			// btnCancel
			// 
			this.btnCancel.BackColor = System.Drawing.SystemColors.Control;
			this.btnCancel.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnCancel.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCancel.Location = new System.Drawing.Point(392, 248);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCancel.Size = new System.Drawing.Size(96, 23);
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.BackColor = System.Drawing.SystemColors.Control;
			this.btnOK.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnOK.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnOK.Location = new System.Drawing.Point(288, 248);
			this.btnOK.Name = "btnOK";
			this.btnOK.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnOK.Size = new System.Drawing.Size(96, 23);
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoRing,
																					this.rdoOnce});
			this.groupBox3.Location = new System.Drawing.Point(248, 176);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(240, 56);
			this.groupBox3.TabIndex = 4;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "転送方式";
			// 
			// rdoRing
			// 
			this.rdoRing.Location = new System.Drawing.Point(120, 24);
			this.rdoRing.Name = "rdoRing";
			this.rdoRing.Size = new System.Drawing.Size(96, 16);
			this.rdoRing.TabIndex = 1;
			this.rdoRing.Text = "無限回転送";
			// 
			// rdoOnce
			// 
			this.rdoOnce.Location = new System.Drawing.Point(10, 24);
			this.rdoOnce.Name = "rdoOnce";
			this.rdoOnce.Size = new System.Drawing.Size(88, 16);
			this.rdoOnce.TabIndex = 0;
			this.rdoOnce.Text = "1回転送";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtNumberData,
																					this.label16});
			this.groupBox2.Location = new System.Drawing.Point(8, 176);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(232, 56);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "転送個数イベント通知";
			// 
			// txtNumberData
			// 
			this.txtNumberData.Location = new System.Drawing.Point(112, 21);
			this.txtNumberData.Name = "txtNumberData";
			this.txtNumberData.Size = new System.Drawing.Size(96, 19);
			this.txtNumberData.TabIndex = 18;
			this.txtNumberData.Text = "";
			// 
			// label16
			// 
			this.label16.AutoSize = true;
			this.label16.Location = new System.Drawing.Point(64, 24);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(35, 12);
			this.label16.TabIndex = 1;
			this.label16.Text = "個数：";
			// 
			// SSampMode
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(496, 278);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1,
																		  this.btnCancel,
																		  this.btnOK,
																		  this.groupBox3,
																		  this.groupBox2});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SSampMode";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "スレーブサンプリング条件設定";
			this.Load += new System.EventHandler(this.SSampMode_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//===================================================
		//	ダイアログ初期化
		//===================================================
		private void SSampMode_Load(object sender, System.EventArgs e)
		{
			ushort	i;
			string	szBuf = new string(' ', 100);

			//--------------------------------
			// 表示初期化
			//--------------------------------
			// Start
			for (i=0; string.Compare(Start[i].Str, "End") != 0; i++) 
			{
				cboStart.Items.Add(Start[i].Str);
			}
			// Start with
			for (i=0; string.Compare(StartWith[i].Str, "End") != 0; i++) 
			{
				cboStartWith.Items.Add(StartWith[i].Str);
			}
			// Clock
			for (i=0; string.Compare(Clock[i].Str, "End") != 0; i++) 
			{
				cboClock.Items.Add(Clock[i].Str);
			}
			// Stop
			for (i=0; string.Compare(Stop[i].Str, "End") != 0; i++) 
			{
				cboStop.Items.Add(Stop[i].Str);
			}
			// Stop with
			for (i=0; string.Compare(StopWith[i].Str, "End") != 0; i++) 
			{
				cboStopWith.Items.Add(StopWith[i].Str);
			}

			cboStart.Text = szStart;
			cboStartWith.Text = szStartWith;
			cboClock.Text = szClock;
			cboStop.Text = szStop;
			cboStopWith.Text = szStopWith;

			txtStopNumber.Text = StopNum.ToString();

			txtNumberData.Text = S_NotifyCount.ToString();
			
			if (S_IsRing == (short)CcntConst.CNTS_WRITE_ONCE) 
			{
				rdoOnce.Checked = true;
				rdoRing.Checked = false;
			} 
			else 
			{
				rdoOnce.Checked = false;
				rdoRing.Checked = true;
			}
			cboStop_SelectedIndexChanged(cboStop, new System.EventArgs());;
		}

		//===================================================
		//	OKボタン処理
		//===================================================
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string	szBuf = new string(' ', 100);
			bool	Checked;
			short	dwStart;
			short	ExtSig1, ExtSig2, ExtSig3;
			ushort	wStartWith;
			short	dwClock;
			short	dwStop;
			ushort	wStopWith;
			int		ret;
			string	strMsg;
			string	error_string = new string(' ', 256);

			//--------------------------------
			// 表示データを取得
			//--------------------------------
			szStart = cboStart.Text;
			szStartWith = cboStartWith.Text;
			szClock = cboClock.Text;
			szStop = cboStop.Text;
			szStopWith = cboStopWith.Text;

			szBuf = txtStopNumber.Text;
			try
			{
				StopNum = uint.Parse(szBuf);
			}
			catch (System.FormatException)
			{
				StopNum = 1000;
			}
			
			szBuf = txtNumberData.Text;
			try
			{
				S_NotifyCount = uint.Parse(szBuf);
			}
			catch (System.FormatException)
			{
				S_NotifyCount = 500;
			}

			Checked = rdoOnce.Checked;

			//--------------------------------
			// 設定値を数値に変換
			//--------------------------------
			dwStart = str_to_num(Start, szStart);
			wStartWith = (ushort)str_to_num(StartWith, szStartWith);

			dwClock = str_to_num(Clock, szClock);

			dwStop = str_to_num(Stop, szStop);
			wStopWith = (ushort)str_to_num(StopWith, szStopWith);

			if (Checked	== true) 
			{
				S_IsRing = (short)CcntConst.CNTS_WRITE_ONCE;
			} 
			else 
			{
				S_IsRing = (short)CcntConst.CNTS_WRITE_RING;
			}

			//--------------------------------
			// サンプリング条件を設定
			//--------------------------------
			ExtSig1 = 1;
			ExtSig2 = 1;
			ExtSig3 = 1;
			ret = cnt.SetSlave(Sync.S_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				strMsg = string.Format("CntSetSlave ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
				return;
			}

			ret = cnt.SetSamplingStartTrigger(Sync.S_Id, dwStart, (short)wStartWith, Sync.CHANNEL_NUM);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				strMsg = string.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
				return;
			}
			ret = cnt.SetSamplingClockTrigger(Sync.S_Id, dwClock);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				strMsg = string.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
				return;
			}
			ret = cnt.SetSamplingStopTrigger(Sync.S_Id, dwStop, (short)wStopWith, Sync.CHANNEL_NUM);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				strMsg = string.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
				return;
			}
			ret = cnt.SetSamplingStopNumber(Sync.S_Id, StopNum);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				strMsg = string.Format("CntSetSamplingStopNumber ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
			}

			this.Close();
		}

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

		//===================================================
		//	ストップ条件設定変更
		//===================================================
		private void cboStop_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuf = new string(' ', 100);

			szBuf = cboStop.Text;
			if (string.Compare(szBuf, "指定個数転送完了") == 0) 
			{
				txtStopNumber.Enabled = true;
			} 
			else 
			{
				txtStopNumber.Enabled = false;
			}
		}
	}
}
