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

namespace Sync
{
	/// <summary>
	/// Summary description for SSampMode.
	/// </summary>
	public class SSampMode : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox txtNumberData;
		private System.Windows.Forms.Label label16;
		public System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.RadioButton rdoRing;
		private System.Windows.Forms.RadioButton rdoOnce;
		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 btnOK;
		/// <summary>
		/// Required designer variable.
		/// </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("Start counter regardless of sampling",	(short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP	),
			new StrNum("Start counter when sampling starts",	(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("Stop transfer by specified times",		(short)CcntConst.CNTS_STOP_NUM					),
			new StrNum("End",									0												)
		};

		static StrNum[] StopWith = 
		{
			new StrNum("Stop counter regardless of sampling",	(short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP	),
			new StrNum("Stop counter when sampling stops",		(short)CcntConst.CNTS_STOP_CNT_WITH_SAMP		),
			new StrNum("End",									0												)
		};

		static string		szStart			= "ExtSig2";
		static string		szStartWith		= "Start counter regardless of sampling";
		static string		szClock			= "ExtSig1";
		static string		szStop			= "ExtSig3";
		static string		szStopWith		= "Stop counter regardless of sampling";
		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()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.txtNumberData = new System.Windows.Forms.TextBox();
			this.label16 = new System.Windows.Forms.Label();
			this.btnCancel = 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.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.btnOK = new System.Windows.Forms.Button();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtNumberData,
																					this.label16});
			this.groupBox2.Location = new System.Drawing.Point(10, 205);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(246, 65);
			this.groupBox2.TabIndex = 8;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Transfer Number Event Notification";
			// 
			// txtNumberData
			// 
			this.txtNumberData.Location = new System.Drawing.Point(112, 24);
			this.txtNumberData.Name = "txtNumberData";
			this.txtNumberData.Size = new System.Drawing.Size(88, 20);
			this.txtNumberData.TabIndex = 18;
			this.txtNumberData.Text = "";
			// 
			// label16
			// 
			this.label16.AutoSize = true;
			this.label16.Location = new System.Drawing.Point(9, 28);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(87, 13);
			this.label16.TabIndex = 1;
			this.label16.Text = "Number of Data:";
			// 
			// 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(424, 283);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCancel.Size = new System.Drawing.Size(96, 26);
			this.btnCancel.TabIndex = 6;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoRing,
																					this.rdoOnce});
			this.groupBox3.Location = new System.Drawing.Point(274, 205);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(246, 65);
			this.groupBox3.TabIndex = 9;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Transfer Mode";
			// 
			// rdoRing
			// 
			this.rdoRing.Location = new System.Drawing.Point(120, 28);
			this.rdoRing.Name = "rdoRing";
			this.rdoRing.Size = new System.Drawing.Size(56, 19);
			this.rdoRing.TabIndex = 1;
			this.rdoRing.Text = "Ring";
			// 
			// rdoOnce
			// 
			this.rdoOnce.Location = new System.Drawing.Point(24, 28);
			this.rdoOnce.Name = "rdoOnce";
			this.rdoOnce.Size = new System.Drawing.Size(64, 19);
			this.rdoOnce.TabIndex = 0;
			this.rdoOnce.Text = "Once";
			// 
			// 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(10, 9);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(510, 186);
			this.groupBox1.TabIndex = 7;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Sampling Mode";
			// 
			// txtStopNumber
			// 
			this.txtStopNumber.Location = new System.Drawing.Point(416, 120);
			this.txtStopNumber.Name = "txtStopNumber";
			this.txtStopNumber.Size = new System.Drawing.Size(80, 20);
			this.txtStopNumber.TabIndex = 20;
			this.txtStopNumber.Text = "";
			// 
			// label12
			// 
			this.label12.AutoSize = true;
			this.label12.Location = new System.Drawing.Point(344, 123);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(74, 13);
			this.label12.TabIndex = 16;
			this.label12.Text = "Stop Number:";
			// 
			// cboStopWith
			// 
			this.cboStopWith.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStopWith.Location = new System.Drawing.Point(115, 149);
			this.cboStopWith.Name = "cboStopWith";
			this.cboStopWith.Size = new System.Drawing.Size(210, 21);
			this.cboStopWith.TabIndex = 9;
			// 
			// cboStop
			// 
			this.cboStop.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStop.Location = new System.Drawing.Point(115, 119);
			this.cboStop.Name = "cboStop";
			this.cboStop.Size = new System.Drawing.Size(210, 21);
			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(115, 88);
			this.cboClock.Name = "cboClock";
			this.cboClock.Size = new System.Drawing.Size(210, 21);
			this.cboClock.TabIndex = 7;
			// 
			// cboStartWith
			// 
			this.cboStartWith.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStartWith.Location = new System.Drawing.Point(115, 58);
			this.cboStartWith.Name = "cboStartWith";
			this.cboStartWith.Size = new System.Drawing.Size(210, 21);
			this.cboStartWith.TabIndex = 6;
			// 
			// cboStart
			// 
			this.cboStart.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStart.Location = new System.Drawing.Point(115, 28);
			this.cboStart.Name = "cboStart";
			this.cboStart.Size = new System.Drawing.Size(210, 21);
			this.cboStart.TabIndex = 5;
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(9, 154);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(105, 13);
			this.label5.TabIndex = 4;
			this.label5.Text = "Counter Stop Mode:";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(9, 123);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(31, 13);
			this.label4.TabIndex = 3;
			this.label4.Text = "Stop:";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(9, 93);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(36, 13);
			this.label3.TabIndex = 2;
			this.label3.Text = "Clock:";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(9, 63);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(106, 13);
			this.label2.TabIndex = 1;
			this.label2.Text = "Counter Start Mode:";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(9, 33);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(31, 13);
			this.label1.TabIndex = 0;
			this.label1.Text = "Start:";
			// 
			// 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(320, 283);
			this.btnOK.Name = "btnOK";
			this.btnOK.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnOK.Size = new System.Drawing.Size(96, 26);
			this.btnOK.TabIndex = 5;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// SSampMode
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(528, 318);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.btnCancel,
																		  this.groupBox3,
																		  this.groupBox1,
																		  this.btnOK});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SSampMode";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Sampling Condition Setting for Slave";
			this.Load += new System.EventHandler(this.SSampMode_Load);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//===================================================
		// Dialog initialization
		//===================================================
		private void SSampMode_Load(object sender, System.EventArgs e)
		{
			ushort	i;
			string	szBuf = new string(' ', 100);

			//--------------------------------
			// Initialize display
			//--------------------------------
			// 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 button processing
		//===================================================
		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);

			//--------------------------------
			// Get displayed data
			//--------------------------------
			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;

			//--------------------------------
			// Convert setting value to a number
			//--------------------------------
			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;
			}

			//--------------------------------
			// Set sampling condition
			//--------------------------------
			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();
		}

		//===================================================
		// Cancel button processing
		//===================================================
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//===================================================
		// Stop condition setting is changed
		//===================================================
		private void cboStop_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuf = new string(' ', 100);

			szBuf = cboStop.Text;
			if (string.Compare(szBuf, "Stop transfer by specified times") == 0) 
			{
				txtStopNumber.Enabled = true;
			} 
			else 
			{
				txtStopNumber.Enabled = false;
			}
		}
	}
}
