using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.Globalization;

namespace Counter
{
	/// <summary>
	/// Summary description for Mode.
	/// </summary>
	public class Mode : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label7;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.Button btnSet;
		internal System.Windows.Forms.ComboBox cboClr;
		internal System.Windows.Forms.ComboBox cboMul;
		internal System.Windows.Forms.TextBox txtFilter;
		internal System.Windows.Forms.ComboBox cboPhase;
		internal System.Windows.Forms.ComboBox cboDir;
		internal System.Windows.Forms.ComboBox cboSource;
		internal System.Windows.Forms.ComboBox cboZLogic;
		internal System.Windows.Forms.ComboBox cboZMode;
		internal System.Windows.Forms.ComboBox cboChNo;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		//---------------------------------------------------------
		// Global variable
		//---------------------------------------------------------
		Int32				Ret;									// Return value
		StringBuilder	szError = new StringBuilder(256);		// Error string
		string			szReturnCode = new string(' ', 256);	// The string for error
		string			szText = new string(' ', 100);			// Temporary string
		// Additional data
		short	ChNo;
		short	ZMode;
		short	ZLogic;
		short	SigType;
		short	Dir;
		short	Phase;
		short	Mul;
		short	SyncClr;
		short	FilterValue;

		//---------------------------------------------------------
		// Define mode
		//---------------------------------------------------------
		public struct StrNum
		{
			public string Str;
			public short	Num;
			public StrNum(string Str, short Num)
			{
				this.Str = Str;
				this.Num = Num;
			}
		};

		static StrNum[] cboZModeL = 
		{
			new StrNum(	"Not Use",			CNT.Ccapcnt.CNT_ZPHASE_NOT_USE		),
			new StrNum(	"Next Time",		CNT.Ccapcnt.CNT_ZPHASE_NEXT_ONE		),
			new StrNum(	"Everytime",		CNT.Ccapcnt.CNT_ZPHASE_EVERY_TIME	),
			new StrNum(	"End",				0								)
		};

		static StrNum[] cboZLogicL = 
		{
			new StrNum(	"Positive Logic",	CNT.Ccapcnt.CNT_ZLOGIC_POSITIVE		),
			new StrNum(	"Negative Logic",	CNT.Ccapcnt.CNT_ZLOGIC_NEGATIVE		),
			new StrNum(	"End",				0								)
		};

		static StrNum[] cboChannelSignalL = 
		{
			new StrNum(	"Photoisolator",	CNT.Ccapcnt.CNT_SIGTYPE_ISOLATE		),
			new StrNum(	"TTL",				CNT.Ccapcnt.CNT_SIGTYPE_TTL			),
			new StrNum(	"Differential",		CNT.Ccapcnt.CNT_SIGTYPE_LINERECEIVER),
			new StrNum(	"End",				0								)
		};

		static StrNum[] cboCntDirL = 
		{
			new StrNum(	"Down-count",		CNT.Ccapcnt.CNT_DIR_DOWN	),
			new StrNum(	"Up-count",			CNT.Ccapcnt.CNT_DIR_UP		),
			new StrNum(	"End",				0						)
		};

		static StrNum[] cboPhaseL = 
		{
			new StrNum(	"1 Phase",			CNT.Ccapcnt.CNT_MODE_1PHASE		),
			new StrNum(	"2 Phase",			CNT.Ccapcnt.CNT_MODE_2PHASE		),
			new StrNum(	"Gate Control",		CNT.Ccapcnt.CNT_MODE_GATECONTROL),
			new StrNum(	"End",				0							)
		};

		static StrNum[] cboMulL=
		{
			new StrNum(	"x1",				CNT.Ccapcnt.CNT_MUL_X1		),
			new StrNum(	"x2",				CNT.Ccapcnt.CNT_MUL_X2		),
			new StrNum(	"x4",				CNT.Ccapcnt.CNT_MUL_X4		),
			new StrNum(	"End",				0						)
		};
			
		static StrNum[] cboSyncClrL=
		{
			new StrNum(	"Asynchronous Clear",	CNT.Ccapcnt.CNT_CLR_ASYNC	),
			new StrNum(	"Synchronous Clear",	CNT.Ccapcnt.CNT_CLR_SYNC	),
			new StrNum(	"End",					0						)
		};

		public Mode()
		{
			//
			// 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.btnCancel = new System.Windows.Forms.Button();
            this.btnSet = new System.Windows.Forms.Button();
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label10 = new System.Windows.Forms.Label();
            this.Label9 = new System.Windows.Forms.Label();
            this.Label8 = new System.Windows.Forms.Label();
            this.Label7 = new System.Windows.Forms.Label();
            this.Label6 = new System.Windows.Forms.Label();
            this.Label5 = new System.Windows.Forms.Label();
            this.Label4 = new System.Windows.Forms.Label();
            this.Label3 = new System.Windows.Forms.Label();
            this.cboClr = new System.Windows.Forms.ComboBox();
            this.cboMul = new System.Windows.Forms.ComboBox();
            this.txtFilter = new System.Windows.Forms.TextBox();
            this.cboPhase = new System.Windows.Forms.ComboBox();
            this.cboDir = new System.Windows.Forms.ComboBox();
            this.cboSource = new System.Windows.Forms.ComboBox();
            this.cboZLogic = new System.Windows.Forms.ComboBox();
            this.cboZMode = new System.Windows.Forms.ComboBox();
            this.cboChNo = new System.Windows.Forms.ComboBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(230, 371);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(96, 25);
            this.btnCancel.TabIndex = 8;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSet
            // 
            this.btnSet.Location = new System.Drawing.Point(118, 371);
            this.btnSet.Name = "btnSet";
            this.btnSet.Size = new System.Drawing.Size(96, 25);
            this.btnSet.TabIndex = 7;
            this.btnSet.Text = "Set";
            this.btnSet.Click += new System.EventHandler(this.btnSet_Click);
            // 
            // GroupBox1
            // 
            this.GroupBox1.Controls.Add(this.Label10);
            this.GroupBox1.Controls.Add(this.Label9);
            this.GroupBox1.Controls.Add(this.Label8);
            this.GroupBox1.Controls.Add(this.Label7);
            this.GroupBox1.Controls.Add(this.Label6);
            this.GroupBox1.Controls.Add(this.Label5);
            this.GroupBox1.Controls.Add(this.Label4);
            this.GroupBox1.Controls.Add(this.Label3);
            this.GroupBox1.Controls.Add(this.cboClr);
            this.GroupBox1.Controls.Add(this.cboMul);
            this.GroupBox1.Controls.Add(this.txtFilter);
            this.GroupBox1.Controls.Add(this.cboPhase);
            this.GroupBox1.Controls.Add(this.cboDir);
            this.GroupBox1.Controls.Add(this.cboSource);
            this.GroupBox1.Controls.Add(this.cboZLogic);
            this.GroupBox1.Controls.Add(this.cboZMode);
            this.GroupBox1.Controls.Add(this.cboChNo);
            this.GroupBox1.Controls.Add(this.Label2);
            this.GroupBox1.Controls.Add(this.Label1);
            this.GroupBox1.Location = new System.Drawing.Point(14, 10);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(313, 335);
            this.GroupBox1.TabIndex = 6;
            this.GroupBox1.TabStop = false;
            this.GroupBox1.Text = "Counter Mode";
            // 
            // Label10
            // 
            this.Label10.Location = new System.Drawing.Point(280, 189);
            this.Label10.Name = "Label10";
            this.Label10.Size = new System.Drawing.Size(26, 18);
            this.Label10.TabIndex = 18;
            this.Label10.Text = "Hex";
            // 
            // Label9
            // 
            this.Label9.Location = new System.Drawing.Point(11, 285);
            this.Label9.Name = "Label9";
            this.Label9.Size = new System.Drawing.Size(109, 19);
            this.Label9.TabIndex = 17;
            this.Label9.Text = "Synchronous Clear :";
            // 
            // Label8
            // 
            this.Label8.Location = new System.Drawing.Point(11, 253);
            this.Label8.Name = "Label8";
            this.Label8.Size = new System.Drawing.Size(85, 19);
            this.Label8.TabIndex = 16;
            this.Label8.Text = "Multiplication :";
            // 
            // Label7
            // 
            this.Label7.Location = new System.Drawing.Point(11, 221);
            this.Label7.Name = "Label7";
            this.Label7.Size = new System.Drawing.Size(104, 19);
            this.Label7.TabIndex = 15;
            this.Label7.Text = "Number of Phases :";
            // 
            // Label6
            // 
            this.Label6.Location = new System.Drawing.Point(11, 157);
            this.Label6.Name = "Label6";
            this.Label6.Size = new System.Drawing.Size(88, 18);
            this.Label6.TabIndex = 14;
            this.Label6.Text = "Count Direction :";
            // 
            // Label5
            // 
            this.Label5.Location = new System.Drawing.Point(11, 126);
            this.Label5.Name = "Label5";
            this.Label5.Size = new System.Drawing.Size(85, 18);
            this.Label5.TabIndex = 13;
            this.Label5.Text = "Signal Source :";
            // 
            // Label4
            // 
            this.Label4.Location = new System.Drawing.Point(11, 94);
            this.Label4.Name = "Label4";
            this.Label4.Size = new System.Drawing.Size(85, 19);
            this.Label4.TabIndex = 12;
            this.Label4.Text = "Z Phase Logic :";
            // 
            // Label3
            // 
            this.Label3.Location = new System.Drawing.Point(11, 63);
            this.Label3.Name = "Label3";
            this.Label3.Size = new System.Drawing.Size(85, 18);
            this.Label3.TabIndex = 11;
            this.Label3.Text = "Z Phase Mode :";
            // 
            // cboClr
            // 
            this.cboClr.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboClr.Location = new System.Drawing.Point(121, 283);
            this.cboClr.Name = "cboClr";
            this.cboClr.Size = new System.Drawing.Size(151, 21);
            this.cboClr.TabIndex = 10;
            // 
            // cboMul
            // 
            this.cboMul.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboMul.Location = new System.Drawing.Point(121, 251);
            this.cboMul.Name = "cboMul";
            this.cboMul.Size = new System.Drawing.Size(151, 21);
            this.cboMul.TabIndex = 9;
            // 
            // txtFilter
            // 
            this.txtFilter.Location = new System.Drawing.Point(121, 188);
            this.txtFilter.Name = "txtFilter";
            this.txtFilter.Size = new System.Drawing.Size(151, 20);
            this.txtFilter.TabIndex = 8;
            // 
            // cboPhase
            // 
            this.cboPhase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboPhase.Location = new System.Drawing.Point(121, 219);
            this.cboPhase.Name = "cboPhase";
            this.cboPhase.Size = new System.Drawing.Size(151, 21);
            this.cboPhase.TabIndex = 7;
            // 
            // cboDir
            // 
            this.cboDir.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboDir.Location = new System.Drawing.Point(121, 156);
            this.cboDir.Name = "cboDir";
            this.cboDir.Size = new System.Drawing.Size(151, 21);
            this.cboDir.TabIndex = 6;
            // 
            // cboSource
            // 
            this.cboSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboSource.Location = new System.Drawing.Point(121, 124);
            this.cboSource.Name = "cboSource";
            this.cboSource.Size = new System.Drawing.Size(151, 21);
            this.cboSource.TabIndex = 5;
            // 
            // cboZLogic
            // 
            this.cboZLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboZLogic.Location = new System.Drawing.Point(121, 92);
            this.cboZLogic.Name = "cboZLogic";
            this.cboZLogic.Size = new System.Drawing.Size(151, 21);
            this.cboZLogic.TabIndex = 4;
            // 
            // cboZMode
            // 
            this.cboZMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboZMode.Location = new System.Drawing.Point(121, 60);
            this.cboZMode.Name = "cboZMode";
            this.cboZMode.Size = new System.Drawing.Size(151, 21);
            this.cboZMode.TabIndex = 3;
            // 
            // cboChNo
            // 
            this.cboChNo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboChNo.Location = new System.Drawing.Point(121, 28);
            this.cboChNo.Name = "cboChNo";
            this.cboChNo.Size = new System.Drawing.Size(151, 21);
            this.cboChNo.TabIndex = 2;
            this.cboChNo.SelectedIndexChanged += new System.EventHandler(this.cboChNo_SelectedIndexChanged);
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(11, 189);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(85, 19);
            this.Label2.TabIndex = 1;
            this.Label2.Text = "Digital Filter :";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(11, 31);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(85, 19);
            this.Label1.TabIndex = 0;
            this.Label1.Text = "Channel No. :";
            // 
            // Mode
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(341, 407);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnSet);
            this.Controls.Add(this.GroupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Mode";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Set Mode";
            this.Load += new System.EventHandler(this.Mode_Load);
            this.GroupBox1.ResumeLayout(false);
            this.GroupBox1.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		//---------------------------------------------------------
		//	Convert a number to a string and convert a string to a number
		//---------------------------------------------------------
		private string NumToStr(StrNum[] p, short Num)
		{
			short	i;
			string None = new string(' ', 2);
			for (i=0; string.Compare(p[i].Str, "End") != 0; i++)
			{
				if (p[i].Num == Num)
				{
					return p[i].Str;
				}
			}
			return None;
		}

		private short StrToNum(StrNum[] p, string Str)
		{
			short	i;
			for (i=0; string.Compare(p[i].Str, "End") != 0; i++)
			{
				if (string.Compare(p[i].Str, Str) == 0)
				{
					return p[i].Num;
				}
			}
			return 0;
		}

		//---------------------------------------------------------
		//	Initialize dialog
		//---------------------------------------------------------
		private void Mode_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Initialize combobox
			//-----------------------------------
			short	i;
			for (i=0; i<CNT.Common.ChannelNum; i++)
			{
				szText = string.Format("{0:d}", i);
				cboChNo.Items.Add(szText);
			}
			cboChNo.SelectedIndex = 0;
			for (i=0; string.Compare(cboZModeL[i].Str, "End") != 0; i++)
			{
				cboZMode.Items.Add(cboZModeL[i].Str);
			}
			for (i=0; string.Compare(cboZLogicL[i].Str, "End") != 0; i++)
			{
				cboZLogic.Items.Add(cboZLogicL[i].Str);
			}
			for (i=0; string.Compare(cboChannelSignalL[i].Str, "End") != 0; i++)
			{
				cboSource.Items.Add(cboChannelSignalL[i].Str);
			}
			for (i=0; string.Compare(cboCntDirL[i].Str, "End") != 0; i++)
			{
				cboDir.Items.Add(cboCntDirL[i].Str);
			}
			for (i=0; string.Compare(cboPhaseL[i].Str, "End") != 0; i++)
			{
				cboPhase.Items.Add(cboPhaseL[i].Str);
			}
			for (i=0; string.Compare(cboMulL[i].Str, "End") != 0; i++)
			{
				cboMul.Items.Add(cboMulL[i].Str);
			}
			for (i=0; string.Compare(cboSyncClrL[i].Str, "End") != 0; i++)
			{
				cboClr.Items.Add(cboSyncClrL[i].Str);
			} 
			//-----------------------------------
			// Select channel 0
			//-----------------------------------
			cboChNo_SelectedIndexChanged(cboChNo, new System.EventArgs());
		}

		//---------------------------------------------------------
		//	Select channel combobox
		//---------------------------------------------------------
		private void cboChNo_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Get mode
			//-----------------------------------
			szText = cboChNo.Text;
			ChNo = short.Parse(szText, NumberStyles.Integer);

			CNT.Ccapcnt.CntGetZMode(CNT.Common.Id, ChNo, ref ZMode);
			CNT.Ccapcnt.CntGetZLogic(CNT.Common.Id, ChNo, ref ZLogic);
			CNT.Ccapcnt.CntGetChannelSignal(CNT.Common.Id, ChNo, ref SigType);
			CNT.Ccapcnt.CntGetCountDirection(CNT.Common.Id, ChNo, ref Dir);
			CNT.Ccapcnt.CntGetOperationMode(CNT.Common.Id, ChNo, ref Phase, ref Mul, ref SyncClr);
			CNT.Ccapcnt.CntGetDigitalFilter(CNT.Common.Id, ChNo, ref FilterValue);
			//-----------------------------------
			// Set mode
			//-----------------------------------
			cboZMode.Text = NumToStr(cboZModeL, ZMode);
			cboZLogic.Text = NumToStr(cboZLogicL, ZLogic);
			cboSource.Text = NumToStr(cboChannelSignalL, SigType);
			cboDir.Text = NumToStr(cboCntDirL, Dir);
			cboPhase.Text = NumToStr(cboPhaseL, Phase);
			cboMul.Text = NumToStr(cboMulL, Mul);
			cboClr.Text = NumToStr(cboSyncClrL, SyncClr);
			szText = string.Format("{0:X}", FilterValue);
			txtFilter.Text = szText;
		}

		//---------------------------------------------------------
		//	Process Set button
		//---------------------------------------------------------
		private void btnSet_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Get mode from screen
			//-----------------------------------
			szText = cboZMode.Text;
			ZMode = StrToNum(cboZModeL, szText);
			szText = cboZLogic.Text;
			ZLogic = StrToNum(cboZLogicL,szText);
			szText = cboSource.Text;
			SigType = StrToNum(cboChannelSignalL, szText);
			szText = cboDir.Text;
			Dir = StrToNum(cboCntDirL, szText);
			szText = cboPhase.Text;
			Phase = StrToNum(cboPhaseL, szText);
			szText = cboMul.Text;
			Mul = StrToNum(cboMulL, szText);
			szText = cboClr.Text;
			SyncClr = StrToNum(cboSyncClrL, szText);
			szText = txtFilter.Text;
			FilterValue = short.Parse(szText, NumberStyles.HexNumber);
			//-----------------------------------
			// Set mode
			//-----------------------------------
			Ret = CNT.Ccapcnt.CntSetZMode(CNT.Common.Id, ChNo, ZMode);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetZMode : " + szError;
				MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetZLogic(CNT.Common.Id, ChNo, ZLogic);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetZLogic : " + szError;
				MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSelectChannelSignal(CNT.Common.Id, ChNo, SigType);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSelectChannelSignal : " + szError;
				MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetCountDirection(CNT.Common.Id, ChNo, Dir);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetCountDirection : " + szError;
				MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetOperationMode(CNT.Common.Id, ChNo, Phase, Mul, SyncClr);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetOperationMode : " + szError;
				MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetDigitalFilter(CNT.Common.Id, ChNo, FilterValue);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetDigitalFilter : " + szError;
				MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
			}

			this.Close();
		}

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