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

namespace Counter
{
	/// <summary>
	/// Summary description for Mode.
	/// </summary>
	public class Mode : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox_CounterMode;
		private System.Windows.Forms.Label label_ZPhaseLogic;
		private System.Windows.Forms.Label label_ZPhaseMode;
		private System.Windows.Forms.Label label_SignalSource;
		private System.Windows.Forms.Label label_Direction;
		private System.Windows.Forms.Label label_DigitalFilter;
		private System.Windows.Forms.Label label_Phasenumber;
		private System.Windows.Forms.Label label_Multiple;
		private System.Windows.Forms.Label label_Synclear;
		private System.Windows.Forms.ComboBox comboBox_ZPhaseMode;
		private System.Windows.Forms.ComboBox comboBox_ZPhaseLogic;
		private System.Windows.Forms.ComboBox comboBox_SignalSource;
		private System.Windows.Forms.ComboBox comboBox_Direction;
		private System.Windows.Forms.ComboBox comboBox_DigitalFilter;
		private System.Windows.Forms.ComboBox comboBox_Phasenumber;
		private System.Windows.Forms.ComboBox comboBox_Multiple;
		private System.Windows.Forms.ComboBox comboBox_Synclear;
		private System.Windows.Forms.Label label_Note;
		private System.Windows.Forms.Button button_OK;
		private System.Windows.Forms.Button button_Cancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		Ccnt cnt = new Ccnt();

		// static characters
		const int	ZMODE_NUM	=	3;
		const int	ZLOGIC_NUM	=	2;
		const int	CHSIG_NUM	=	3;
		const int	CNTDIR_NUM	=	2;
		const int	FILTER_NUM	=	15;
		const int	PHASE_NUM	=	3;
		const int	MUL_NUM		=	3;
		const int	SYNCCLR_NUM =	2;

		static	string[]	szCombo_ZMode = new string[ZMODE_NUM]
		{
			"Not Use",
			"Next Time",
			"Every Time"
		};
		static	string[]	szCombo_ZLogic = new string[ZLOGIC_NUM]
		{
			"Positive Logic",
			"Negative Logic"
		};
		static	string[]	szCombo_ChannelSignal = new string[CHSIG_NUM] 
		{
			"Photo-coupler Isolation",
			"TTL",
			"Differential"
		};
		static	string[]	szCombo_CntDir = new string[CNTDIR_NUM] 
		{
			"Down-count",
			"Up-count"
		};
		static	string[]	szCombo_Phase = new string[PHASE_NUM]
		{
			"1 Phase",
			"2 Phase",
			"Gate Control"
		};
		static	string[]	szCombo_Mul = new string[MUL_NUM]
		{
			"x1",
			"x2",
			"x4"
		};
		static	string[]	szCombo_SyncClr = new string[SYNCCLR_NUM]
		{
			"Asynchronous",
			"Synchronous"
		};

		public Mode()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			string	csTmp;
			uint	i;

			int		Ret;
			short	ChNo = 0;
			short	Mode = 0;
			short	ZLogic = 0;
			short	SigType = 0;
			short	Dir = 0;
			short	Phase = 0;
			short	Mul = 0;
			short	SyncClr = 0;
			short	FilterValue = 0;

			Ret = cnt.GetZMode(Counter.m_Id, ChNo, out Mode);
			if (Ret != 0)
			{
				csTmp = string.Format("CntGetZMode Return Code = {0:d5}",Ret);
				MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
				return;
			}
			Ret = cnt.GetZLogic(Counter.m_Id, ChNo,  out ZLogic);
			Ret = cnt.GetChannelSignal(Counter.m_Id, ChNo, out SigType);
			Ret = cnt.GetCountDirection(Counter.m_Id, ChNo, out Dir);
			Ret = cnt.GetOperationMode(Counter.m_Id, ChNo, out Phase, out Mul, out SyncClr);
			Ret = cnt.GetDigitalFilter(Counter.m_Id, ChNo, out FilterValue);

			for (i = 0; i < ZMODE_NUM; i++)
			{
				comboBox_ZPhaseMode.Items.Add(szCombo_ZMode[i]);
			}
			comboBox_ZPhaseMode.SelectedIndex = Mode-1;

			for (i = 0; i < ZLOGIC_NUM; i++)
			{
				comboBox_ZPhaseLogic.Items.Add(szCombo_ZLogic[i]);
			}
			comboBox_ZPhaseLogic.SelectedIndex = ZLogic;

			for (i = 0; i < CHSIG_NUM; i++)
			{
				comboBox_SignalSource.Items.Add(szCombo_ChannelSignal[i]);
			}
			comboBox_SignalSource.SelectedIndex = SigType;

			for (i = 0; i < CNTDIR_NUM; i++)
			{
				comboBox_Direction.Items.Add(szCombo_CntDir[i]);
			}
			comboBox_Direction.SelectedIndex = Dir;

			for (i = 0; i <= FILTER_NUM; i++)
			{
				csTmp = string.Format("{0:X}",i);
				comboBox_DigitalFilter.Items.Add(csTmp);
			}
			comboBox_DigitalFilter.SelectedIndex = FilterValue;

			for (i = 0; i < PHASE_NUM; i++)
			{
				comboBox_Phasenumber.Items.Add(szCombo_Phase[i]);
			}
			comboBox_Phasenumber.SelectedIndex = Phase;

			for (i = 0; i < MUL_NUM; i++)
			{
				comboBox_Multiple.Items.Add(szCombo_Mul[i]);
			}
			comboBox_Multiple.SelectedIndex = Mul;

			for (i = 0; i < SYNCCLR_NUM; i++)
			{
				comboBox_Synclear.Items.Add(szCombo_SyncClr[i]);
			}
			comboBox_Synclear.SelectedIndex = SyncClr;
		}

		/// <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.groupBox_CounterMode = new System.Windows.Forms.GroupBox();
            this.comboBox_Synclear = new System.Windows.Forms.ComboBox();
            this.comboBox_Multiple = new System.Windows.Forms.ComboBox();
            this.comboBox_Phasenumber = new System.Windows.Forms.ComboBox();
            this.comboBox_DigitalFilter = new System.Windows.Forms.ComboBox();
            this.comboBox_Direction = new System.Windows.Forms.ComboBox();
            this.comboBox_SignalSource = new System.Windows.Forms.ComboBox();
            this.comboBox_ZPhaseLogic = new System.Windows.Forms.ComboBox();
            this.comboBox_ZPhaseMode = new System.Windows.Forms.ComboBox();
            this.label_Synclear = new System.Windows.Forms.Label();
            this.label_Multiple = new System.Windows.Forms.Label();
            this.label_Phasenumber = new System.Windows.Forms.Label();
            this.label_DigitalFilter = new System.Windows.Forms.Label();
            this.label_Direction = new System.Windows.Forms.Label();
            this.label_SignalSource = new System.Windows.Forms.Label();
            this.label_ZPhaseLogic = new System.Windows.Forms.Label();
            this.label_ZPhaseMode = new System.Windows.Forms.Label();
            this.label_Note = new System.Windows.Forms.Label();
            this.button_OK = new System.Windows.Forms.Button();
            this.button_Cancel = new System.Windows.Forms.Button();
            this.groupBox_CounterMode.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox_CounterMode
            // 
            this.groupBox_CounterMode.Controls.Add(this.comboBox_Synclear);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_Multiple);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_Phasenumber);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_DigitalFilter);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_Direction);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_SignalSource);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_ZPhaseLogic);
            this.groupBox_CounterMode.Controls.Add(this.comboBox_ZPhaseMode);
            this.groupBox_CounterMode.Controls.Add(this.label_Synclear);
            this.groupBox_CounterMode.Controls.Add(this.label_Multiple);
            this.groupBox_CounterMode.Controls.Add(this.label_Phasenumber);
            this.groupBox_CounterMode.Controls.Add(this.label_DigitalFilter);
            this.groupBox_CounterMode.Controls.Add(this.label_Direction);
            this.groupBox_CounterMode.Controls.Add(this.label_SignalSource);
            this.groupBox_CounterMode.Controls.Add(this.label_ZPhaseLogic);
            this.groupBox_CounterMode.Controls.Add(this.label_ZPhaseMode);
            this.groupBox_CounterMode.Location = new System.Drawing.Point(16, 17);
            this.groupBox_CounterMode.Name = "groupBox_CounterMode";
            this.groupBox_CounterMode.Size = new System.Drawing.Size(336, 304);
            this.groupBox_CounterMode.TabIndex = 0;
            this.groupBox_CounterMode.TabStop = false;
            this.groupBox_CounterMode.Text = "Counter Mode";
            // 
            // comboBox_Synclear
            // 
            this.comboBox_Synclear.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_Synclear.Location = new System.Drawing.Point(120, 269);
            this.comboBox_Synclear.Name = "comboBox_Synclear";
            this.comboBox_Synclear.Size = new System.Drawing.Size(208, 21);
            this.comboBox_Synclear.TabIndex = 15;
            // 
            // comboBox_Multiple
            // 
            this.comboBox_Multiple.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_Multiple.Location = new System.Drawing.Point(120, 234);
            this.comboBox_Multiple.Name = "comboBox_Multiple";
            this.comboBox_Multiple.Size = new System.Drawing.Size(208, 21);
            this.comboBox_Multiple.TabIndex = 14;
            // 
            // comboBox_Phasenumber
            // 
            this.comboBox_Phasenumber.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_Phasenumber.Location = new System.Drawing.Point(120, 199);
            this.comboBox_Phasenumber.Name = "comboBox_Phasenumber";
            this.comboBox_Phasenumber.Size = new System.Drawing.Size(208, 21);
            this.comboBox_Phasenumber.TabIndex = 13;
            // 
            // comboBox_DigitalFilter
            // 
            this.comboBox_DigitalFilter.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_DigitalFilter.Location = new System.Drawing.Point(120, 165);
            this.comboBox_DigitalFilter.Name = "comboBox_DigitalFilter";
            this.comboBox_DigitalFilter.Size = new System.Drawing.Size(208, 21);
            this.comboBox_DigitalFilter.TabIndex = 12;
            // 
            // comboBox_Direction
            // 
            this.comboBox_Direction.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_Direction.Location = new System.Drawing.Point(120, 130);
            this.comboBox_Direction.Name = "comboBox_Direction";
            this.comboBox_Direction.Size = new System.Drawing.Size(208, 21);
            this.comboBox_Direction.TabIndex = 11;
            // 
            // comboBox_SignalSource
            // 
            this.comboBox_SignalSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_SignalSource.Location = new System.Drawing.Point(120, 95);
            this.comboBox_SignalSource.Name = "comboBox_SignalSource";
            this.comboBox_SignalSource.Size = new System.Drawing.Size(208, 21);
            this.comboBox_SignalSource.TabIndex = 10;
            // 
            // comboBox_ZPhaseLogic
            // 
            this.comboBox_ZPhaseLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_ZPhaseLogic.Location = new System.Drawing.Point(120, 61);
            this.comboBox_ZPhaseLogic.Name = "comboBox_ZPhaseLogic";
            this.comboBox_ZPhaseLogic.Size = new System.Drawing.Size(208, 21);
            this.comboBox_ZPhaseLogic.TabIndex = 9;
            // 
            // comboBox_ZPhaseMode
            // 
            this.comboBox_ZPhaseMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_ZPhaseMode.Location = new System.Drawing.Point(120, 26);
            this.comboBox_ZPhaseMode.Name = "comboBox_ZPhaseMode";
            this.comboBox_ZPhaseMode.Size = new System.Drawing.Size(208, 21);
            this.comboBox_ZPhaseMode.TabIndex = 8;
            // 
            // label_Synclear
            // 
            this.label_Synclear.Location = new System.Drawing.Point(16, 269);
            this.label_Synclear.Name = "label_Synclear";
            this.label_Synclear.Size = new System.Drawing.Size(100, 25);
            this.label_Synclear.TabIndex = 7;
            this.label_Synclear.Text = "Syn clear:";
            // 
            // label_Multiple
            // 
            this.label_Multiple.Location = new System.Drawing.Point(16, 234);
            this.label_Multiple.Name = "label_Multiple";
            this.label_Multiple.Size = new System.Drawing.Size(100, 25);
            this.label_Multiple.TabIndex = 6;
            this.label_Multiple.Text = "Multiple:";
            // 
            // label_Phasenumber
            // 
            this.label_Phasenumber.Location = new System.Drawing.Point(16, 199);
            this.label_Phasenumber.Name = "label_Phasenumber";
            this.label_Phasenumber.Size = new System.Drawing.Size(100, 25);
            this.label_Phasenumber.TabIndex = 5;
            this.label_Phasenumber.Text = "Phase number:";
            // 
            // label_DigitalFilter
            // 
            this.label_DigitalFilter.Location = new System.Drawing.Point(16, 165);
            this.label_DigitalFilter.Name = "label_DigitalFilter";
            this.label_DigitalFilter.Size = new System.Drawing.Size(100, 25);
            this.label_DigitalFilter.TabIndex = 4;
            this.label_DigitalFilter.Text = "Digital Filter:";
            // 
            // label_Direction
            // 
            this.label_Direction.Location = new System.Drawing.Point(16, 130);
            this.label_Direction.Name = "label_Direction";
            this.label_Direction.Size = new System.Drawing.Size(100, 25);
            this.label_Direction.TabIndex = 3;
            this.label_Direction.Text = "Direction:";
            // 
            // label_SignalSource
            // 
            this.label_SignalSource.Location = new System.Drawing.Point(16, 95);
            this.label_SignalSource.Name = "label_SignalSource";
            this.label_SignalSource.Size = new System.Drawing.Size(100, 25);
            this.label_SignalSource.TabIndex = 2;
            this.label_SignalSource.Text = "Signal Source:";
            // 
            // label_ZPhaseLogic
            // 
            this.label_ZPhaseLogic.Location = new System.Drawing.Point(16, 61);
            this.label_ZPhaseLogic.Name = "label_ZPhaseLogic";
            this.label_ZPhaseLogic.Size = new System.Drawing.Size(100, 25);
            this.label_ZPhaseLogic.TabIndex = 1;
            this.label_ZPhaseLogic.Text = "Z Phase Logic:";
            // 
            // label_ZPhaseMode
            // 
            this.label_ZPhaseMode.Location = new System.Drawing.Point(16, 26);
            this.label_ZPhaseMode.Name = "label_ZPhaseMode";
            this.label_ZPhaseMode.Size = new System.Drawing.Size(100, 25);
            this.label_ZPhaseMode.TabIndex = 0;
            this.label_ZPhaseMode.Text = "Z Phase Mode:";
            // 
            // label_Note
            // 
            this.label_Note.Location = new System.Drawing.Point(16, 329);
            this.label_Note.Name = "label_Note";
            this.label_Note.Size = new System.Drawing.Size(256, 25);
            this.label_Note.TabIndex = 1;
            this.label_Note.Text = "Note: All channel same mode.";
            // 
            // button_OK
            // 
            this.button_OK.Location = new System.Drawing.Point(184, 355);
            this.button_OK.Name = "button_OK";
            this.button_OK.Size = new System.Drawing.Size(75, 25);
            this.button_OK.TabIndex = 2;
            this.button_OK.Text = "OK";
            this.button_OK.Click += new System.EventHandler(this.button_OK_Click);
            // 
            // button_Cancel
            // 
            this.button_Cancel.Location = new System.Drawing.Point(272, 355);
            this.button_Cancel.Name = "button_Cancel";
            this.button_Cancel.Size = new System.Drawing.Size(75, 25);
            this.button_Cancel.TabIndex = 3;
            this.button_Cancel.Text = "Cancel";
            this.button_Cancel.Click += new System.EventHandler(this.button_Cancel_Click);
            // 
            // Mode
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(368, 392);
            this.Controls.Add(this.button_Cancel);
            this.Controls.Add(this.button_OK);
            this.Controls.Add(this.label_Note);
            this.Controls.Add(this.groupBox_CounterMode);
            this.Name = "Mode";
            this.Text = "Mode";
            this.groupBox_CounterMode.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		private void button_OK_Click(object sender, System.EventArgs e)
		{
			string	csTmp;
			int		Index;

			int		Ret;
			short	ChNo = 0;
			short	Mode = 0;
			short	ZLogic = 0;
			short	SigType = 0;
			short	Dir = 0;
			short	Phase = 0;
			short	Mul = 0;
			short	SyncClr = 0;
			short	FilterValue = 0;

			//---------------------------------------------
			// Set Z Phase Mode
			//---------------------------------------------
			Index = comboBox_ZPhaseMode.SelectedIndex;
			switch (Index)
			{
				case 0:
					Mode = (short)CcntConst.CNT_ZPHASE_NOT_USE;
					break;
				case 1:
					Mode = (short)CcntConst.CNT_ZPHASE_NEXT_ONE;
					break;
				case 2:
					Mode = (short)CcntConst.CNT_ZPHASE_EVERY_TIME;
					break;
			}
			for (ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++){
				Ret = cnt.SetZMode(Counter.m_Id, ChNo, (short)(Mode));
				if (Ret != 0)
				{
					csTmp = string.Format("CntSetZMode Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// Set Z Phase Logic
			//-----------------------------------
			Index = comboBox_ZPhaseLogic.SelectedIndex;
			switch (Index)
			{
				case 0:
					ZLogic = (short)CcntConst.CNT_ZLOGIC_POSITIVE;
					break;
				case 1:
					ZLogic = (short)CcntConst.CNT_ZLOGIC_NEGATIVE;
					break;				
			}
			for (ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				Ret = cnt.SetZLogic(Counter.m_Id, ChNo, ZLogic);
				if (Ret != 0)
				{
					csTmp = string.Format("CntSetZLogic Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// Set Channel Signal
			//-----------------------------------
			Index = comboBox_SignalSource.SelectedIndex;
			switch (Index)
			{
				case 0:
					SigType = (short)CcntConst.CNT_SIGTYPE_ISOLATE;
					break;
				case 1:
					SigType = (short)CcntConst.CNT_SIGTYPE_TTL;
					break;
				case 2:
					SigType = (short)CcntConst.CNT_SIGTYPE_LINERECEIVER;
					break;			
			}
			for (ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				Ret = cnt.SelectChannelSignal(Counter.m_Id, ChNo, SigType);
				if (Ret != 0)
				{
					csTmp = string.Format("CntSelectChannelSignal Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// Set Count Direction
			//-----------------------------------
			Index = comboBox_Direction.SelectedIndex;
			switch (Index)
			{
				case 0:
					Dir = (short)CcntConst.CNT_DIR_DOWN;
					break;
				case 1:
					Dir = (short)CcntConst.CNT_DIR_UP;
					break;
			}
			for (ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				Ret = cnt.SetCountDirection(Counter.m_Id, ChNo, Dir);
				if (Ret != 0)
				{
					csTmp = string.Format("CntSetCountDirection Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// Set Digital Filter
			//-----------------------------------
			FilterValue = (short)comboBox_DigitalFilter.SelectedIndex;
			for (ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				Ret = cnt.SetDigitalFilter(Counter.m_Id, ChNo, FilterValue);
				if (Ret != 0)
				{
					csTmp = string.Format("CntSetDigitalFilter Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// Set Operation Mode
			//-----------------------------------			
			//Set Phase
			Index = comboBox_Phasenumber.SelectedIndex;
			switch (Index)
			{
				case 0:
					Phase = (short)CcntConst.CNT_MODE_1PHASE;
					break;
				case 1:
					Phase = (short)CcntConst.CNT_MODE_2PHASE;
					break;
				case 2:
					Phase = (short)CcntConst.CNT_MODE_GATECONTROL;
					break;
			}
			//Set Multiple
			Index = comboBox_Multiple.SelectedIndex;
			switch (Index)
			{
				case 0:
					Mul = (short)CcntConst.CNT_MUL_X1;
					break;
				case 1:
					Mul = (short)CcntConst.CNT_MUL_X2;
					break;
				case 2:
					Mul = (short)CcntConst.CNT_MUL_X4;
					break;				
			}
			//Set Sync Clear
			Index = comboBox_Synclear.SelectedIndex;
			switch (Index)
			{
				case 0:
					SyncClr = (short)CcntConst.CNT_CLR_ASYNC;
					break;
				case 1:
					SyncClr = (short)CcntConst.CNT_CLR_SYNC;
					break;
			}
			for (ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				Ret = cnt.SetOperationMode(Counter.m_Id, ChNo, Phase, Mul, SyncClr);
				if (Ret != 0)
				{
					csTmp = string.Format("CntSetOperationMode Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}
			this.Close();
		}

		private void button_Cancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
