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

namespace Generating
{
	/// <summary>
	/// Summary description for Condition.
	/// </summary>
	public class Condition : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox txtStopNum;
		private System.Windows.Forms.TextBox txtInternalClock;
		private System.Windows.Forms.ComboBox cboStopTrg;
		private System.Windows.Forms.ComboBox cboClockTrg;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox cboStartTrg;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox txtNumEvent;
		private System.Windows.Forms.Label label11;
		private 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.Label label12;
		private System.Windows.Forms.Button btnOK;
		/// <summary>
		/// Required designer variable.
		/// </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("Software Start",	(short)CdioConst.DIODM_START_SOFT),
			new StrToNum("External UP",		(short)CdioConst.DIODM_START_EXT_RISE),
			new StrToNum("External Down",	(short)CdioConst.DIODM_START_EXT_FALL),
			new StrToNum("End",				0)
		};
		// Clock Condition
		static StrToNum[] ClockTrgTable =
		{
			new StrToNum("Internal Clock",	(short)CdioConst.DIODM_CLK_CLOCK),
			new StrToNum("External Clock",	(short)CdioConst.DIODM_CLK_EXT_TRG),
			new StrToNum("Hand Shake",		(short)CdioConst.DIODM_CLK_HANDSHAKE),
			new StrToNum("End",				0)
		};
		// Stop Condition
		static StrToNum[] StopTrgTable = 
		{
			new StrToNum("Software Stop",	(short)CdioConst.DIODM_STOP_SOFT),
			new StrToNum("External UP",		(short)CdioConst.DIODM_STOP_EXT_RISE),
			new StrToNum("External Down",	(short)CdioConst.DIODM_STOP_EXT_FALL),
			new StrToNum("Number",			(short)CdioConst.DIODM_STOP_NUM),
			new StrToNum("End",				0)
		};

		Cdio				dio				= new Cdio();
		static string		szStart			= "Software Start";
		static string		szClock			= "Internal Clock";
		static string		szStop			= "Software Stop";
		static uint			InternalClock	= 1000;
		static uint			StopNumber		= 1000;
		public static uint	NumberOfData	= 500;
		public static short	IsRing			= (short)CdioConst.DIODM_WRITE_ONCE;

		public Condition()
		{
			//
			// 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.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboClockTrg = new System.Windows.Forms.ComboBox();
			this.label10 = new System.Windows.Forms.Label();
			this.txtStopNum = new System.Windows.Forms.TextBox();
			this.txtInternalClock = new System.Windows.Forms.TextBox();
			this.cboStopTrg = new System.Windows.Forms.ComboBox();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			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.groupBox2 = new System.Windows.Forms.GroupBox();
			this.txtNumEvent = new System.Windows.Forms.TextBox();
			this.label11 = 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.label12 = new System.Windows.Forms.Label();
			this.btnOK = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboClockTrg,
																					this.label10,
																					this.txtStopNum,
																					this.txtInternalClock,
																					this.cboStopTrg,
																					this.label8,
																					this.label7,
																					this.cboStartTrg,
																					this.label3,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 3);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(437, 140);
			this.groupBox1.TabIndex = 14;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Generation Conditions";
			// 
			// cboClockTrg
			// 
			this.cboClockTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboClockTrg.Location = new System.Drawing.Point(96, 64);
			this.cboClockTrg.Name = "cboClockTrg";
			this.cboClockTrg.Size = new System.Drawing.Size(144, 21);
			this.cboClockTrg.Sorted = true;
			this.cboClockTrg.TabIndex = 4;
			this.cboClockTrg.SelectedIndexChanged += new System.EventHandler(this.cboClockTrg_SelectedIndexChanged);
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(416, 66);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(16, 19);
			this.label10.TabIndex = 16;
			this.label10.Text = "ns";
			// 
			// txtStopNum
			// 
			this.txtStopNum.Location = new System.Drawing.Point(323, 101);
			this.txtStopNum.Name = "txtStopNum";
			this.txtStopNum.Size = new System.Drawing.Size(88, 20);
			this.txtStopNum.TabIndex = 7;
			this.txtStopNum.Text = "";
			this.txtStopNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtInternalClock
			// 
			this.txtInternalClock.Location = new System.Drawing.Point(323, 65);
			this.txtInternalClock.Name = "txtInternalClock";
			this.txtInternalClock.Size = new System.Drawing.Size(88, 20);
			this.txtInternalClock.TabIndex = 6;
			this.txtInternalClock.Text = "";
			this.txtInternalClock.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// cboStopTrg
			// 
			this.cboStopTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStopTrg.Location = new System.Drawing.Point(96, 100);
			this.cboStopTrg.Name = "cboStopTrg";
			this.cboStopTrg.Size = new System.Drawing.Size(144, 21);
			this.cboStopTrg.Sorted = true;
			this.cboStopTrg.TabIndex = 5;
			this.cboStopTrg.SelectedIndexChanged += new System.EventHandler(this.cboStopTrg_SelectedIndexChanged);
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(246, 102);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(77, 19);
			this.label8.TabIndex = 9;
			this.label8.Text = "Stop Number:";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(246, 66);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(98, 19);
			this.label7.TabIndex = 8;
			this.label7.Text = "Internal Clock:";
			// 
			// cboStartTrg
			// 
			this.cboStartTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStartTrg.Location = new System.Drawing.Point(96, 28);
			this.cboStartTrg.Name = "cboStartTrg";
			this.cboStartTrg.Size = new System.Drawing.Size(144, 21);
			this.cboStartTrg.Sorted = true;
			this.cboStartTrg.TabIndex = 3;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 102);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 19);
			this.label3.TabIndex = 2;
			this.label3.Text = "Stop condition:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 66);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 19);
			this.label2.TabIndex = 1;
			this.label2.Text = "Clock Condition:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 30);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 19);
			this.label1.TabIndex = 0;
			this.label1.Text = "Start Condition:";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtNumEvent,
																					this.label11});
			this.groupBox2.Location = new System.Drawing.Point(8, 227);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(437, 65);
			this.groupBox2.TabIndex = 16;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Notification";
			// 
			// txtNumEvent
			// 
			this.txtNumEvent.Location = new System.Drawing.Point(96, 28);
			this.txtNumEvent.Name = "txtNumEvent";
			this.txtNumEvent.Size = new System.Drawing.Size(88, 20);
			this.txtNumEvent.TabIndex = 12;
			this.txtNumEvent.Text = "";
			this.txtNumEvent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(8, 31);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(96, 18);
			this.label11.TabIndex = 0;
			this.label11.Text = "Number of Data:";
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(349, 301);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 28);
			this.btnCancel.TabIndex = 13;
			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.label12});
			this.groupBox3.Location = new System.Drawing.Point(8, 152);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(437, 65);
			this.groupBox3.TabIndex = 15;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Set Buffer Condition";
			// 
			// rdoRing
			// 
			this.rdoRing.Location = new System.Drawing.Point(192, 30);
			this.rdoRing.Name = "rdoRing";
			this.rdoRing.Size = new System.Drawing.Size(88, 19);
			this.rdoRing.TabIndex = 10;
			this.rdoRing.Text = "Write Ring";
			// 
			// rdoOnce
			// 
			this.rdoOnce.Location = new System.Drawing.Point(104, 30);
			this.rdoOnce.Name = "rdoOnce";
			this.rdoOnce.Size = new System.Drawing.Size(80, 19);
			this.rdoOnce.TabIndex = 9;
			this.rdoOnce.Text = "Write Once";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(8, 30);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(72, 19);
			this.label12.TabIndex = 0;
			this.label12.Text = "Buffer Write:";
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(237, 301);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(96, 28);
			this.btnOK.TabIndex = 12;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// Condition
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(456, 334);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1,
																		  this.groupBox2,
																		  this.btnCancel,
																		  this.groupBox3,
																		  this.btnOK});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Condition";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Set Generating Conditions ";
			this.Load += new System.EventHandler(this.Condition_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//===================================================
		//	Dialog init
		//===================================================
		private void Condition_Load(object sender, System.EventArgs e)
		{
			//--------------------------------
			// Init
			//--------------------------------
			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}", InternalClock);
			txtInternalClock.Text = strTmp;
			strTmp = string.Format("{0:d}", StopNumber);
			txtStopNum.Text = strTmp;
			strTmp = string.Format("{0:d}", NumberOfData);
			txtNumEvent.Text = strTmp;

			if (IsRing == (short)CdioConst.DIODM_WRITE_ONCE) 
			{
				rdoOnce.Checked = true;
				rdoRing.Checked = false;
			} 
			else 
			{
				rdoOnce.Checked = false;
				rdoRing.Checked = true;
			}

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

		//===================================================
		//	Modify Clock condition
		//===================================================
		private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Enable/disable Internal Clock
			//--------------------------------
			szBuff = cboClockTrg.Text;
			if (string.Compare(szBuff, "Internal Clock") == 0) 
			{
				txtInternalClock.Enabled = true;
			} 
			else 
			{
				txtInternalClock.Enabled = false;
			}
		}

		//===================================================
		//	Modify stop condition 
		//===================================================
		private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Enable/disable Stop Number
			//--------------------------------
			szBuff = cboStopTrg.Text;
			if (string.Compare(szBuff, "Number") == 0) 
			{
				txtStopNum.Enabled = true;
			} 
			else 
			{
				txtStopNum.Enabled = false;
			}
		}

		//===================================================
		//	OK button handle
		//===================================================
		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	i;
			short	Direction = (short)CdioConst.DIODM_DIR_OUT;
			int		ret;
			string	strMsg;

			//--------------------------------
			// Get the displayed data
			//--------------------------------
			szStart = cboStartTrg.Text;
			szClock = cboClockTrg.Text;
			szStop = cboStopTrg.Text;
			szTmp = txtInternalClock.Text;
			try
			{
				InternalClock = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				InternalClock = 1000;
			}
			szTmp = txtStopNum.Text;
			try
			{
				StopNumber = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				StopNumber = 1000;
			}
			szTmp = txtNumEvent.Text;
			try
			{
				NumberOfData = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				NumberOfData = 500;
			}
			if (rdoOnce.Checked == true) 
			{
				IsRing = (short)CdioConst.DIODM_WRITE_ONCE;
			} 
			else 
			{
				IsRing = (short)CdioConst.DIODM_WRITE_RING;
			}
			//--------------------------------
			// Convert data to number 
			//--------------------------------
			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;
				}
			}
			//--------------------------------
			// Set Generating condition
			//--------------------------------
			//	Specify input/output direction(32-point input)
			ret = dio.DmSetDirection(Generating.Id, (short)CdioConst.PO_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;
			}
			// Start alone
			ret = dio.DmSetStandAlone(Generating.Id);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStandAlone ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			//Set start condition
			ret = dio.DmSetStartTrigger(Generating.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;
			}
			// Set clock condition
			ret = dio.DmSetClockTrigger(Generating.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;
			}
			// Set stop condition
			ret = dio.DmSetStopTrigger(Generating.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;
			}
			// Set internal clock
			ret = dio.DmSetInternalClock(Generating.Id, Direction, InternalClock, (short)CdioConst.DIODM_TIM_UNIT_NS);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetInternalClock ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Set stop number
			ret = dio.DmSetStopNumber(Generating.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();
		}

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