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

namespace Counter
{
	/// <summary>
	/// Mode の概要の説明です。
	/// </summary>
	public class Mode : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox_CounterMode;
		private System.Windows.Forms.Label label_ZPhaseMode;
		private System.Windows.Forms.Label label_ZPhaseLogic;
		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.Label label_Note;
		private System.Windows.Forms.Button button_OK;
		private System.Windows.Forms.Button button_Cancel;
		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;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		Ccnt cnt=new Ccnt();
			
		// モード定義
		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]
		{
			"未使用",
			"次の１回",
			"毎回"
		};
		static	string[]	szCombo_ZLogic = new string[ZLOGIC_NUM]
		{
			"正論理",
			"負論理"
		};
		static	string[]	szCombo_ChannelSignal = new string[CHSIG_NUM] 
		{
			"フォトカプラ絶縁",
			"TTL",
			"差動"
		};
		static	string[]	szCombo_CntDir = new string[CNTDIR_NUM] 
		{
			"ダウンカウント",
			"アップカウント"
		};
		static	string[]	szCombo_Phase = new string[PHASE_NUM]
		{
			"単相",
			"２相",
			"ゲートコントロール"
		};
		static	string[]	szCombo_Mul = new string[MUL_NUM]
		{
			"１逓倍",
			"２逓倍",
			"４逓倍"
		};
		static	string[]	szCombo_SyncClr = new string[SYNCCLR_NUM]
		{
			"非同期クリア",
			"同期クリア"
		};

		public Mode()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
			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, "エラー", 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>
		/// 使用されているリソースに後処理を実行します。
		/// </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.groupBox_CounterMode = new System.Windows.Forms.GroupBox();
			this.label_ZPhaseMode = new System.Windows.Forms.Label();
			this.label_ZPhaseLogic = new System.Windows.Forms.Label();
			this.label_SignalSource = new System.Windows.Forms.Label();
			this.label_Direction = new System.Windows.Forms.Label();
			this.label_DigitalFilter = new System.Windows.Forms.Label();
			this.label_Phasenumber = new System.Windows.Forms.Label();
			this.label_Multiple = new System.Windows.Forms.Label();
			this.label_Synclear = 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.comboBox_ZPhaseMode = new System.Windows.Forms.ComboBox();
			this.comboBox_ZPhaseLogic = new System.Windows.Forms.ComboBox();
			this.comboBox_SignalSource = new System.Windows.Forms.ComboBox();
			this.comboBox_Direction = new System.Windows.Forms.ComboBox();
			this.comboBox_DigitalFilter = new System.Windows.Forms.ComboBox();
			this.comboBox_Phasenumber = new System.Windows.Forms.ComboBox();
			this.comboBox_Multiple = new System.Windows.Forms.ComboBox();
			this.comboBox_Synclear = new System.Windows.Forms.ComboBox();
			this.groupBox_CounterMode.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox_CounterMode
			// 
			this.groupBox_CounterMode.Controls.AddRange(new System.Windows.Forms.Control[] {
																							   this.comboBox_Synclear,
																							   this.comboBox_Multiple,
																							   this.comboBox_Phasenumber,
																							   this.comboBox_DigitalFilter,
																							   this.comboBox_Direction,
																							   this.comboBox_SignalSource,
																							   this.comboBox_ZPhaseLogic,
																							   this.comboBox_ZPhaseMode,
																							   this.label_Synclear,
																							   this.label_Multiple,
																							   this.label_Phasenumber,
																							   this.label_DigitalFilter,
																							   this.label_Direction,
																							   this.label_SignalSource,
																							   this.label_ZPhaseLogic,
																							   this.label_ZPhaseMode});
			this.groupBox_CounterMode.Location = new System.Drawing.Point(16, 16);
			this.groupBox_CounterMode.Name = "groupBox_CounterMode";
			this.groupBox_CounterMode.Size = new System.Drawing.Size(336, 280);
			this.groupBox_CounterMode.TabIndex = 0;
			this.groupBox_CounterMode.TabStop = false;
			this.groupBox_CounterMode.Text = "カウンタモード";
			// 
			// label_ZPhaseMode
			// 
			this.label_ZPhaseMode.Location = new System.Drawing.Point(16, 24);
			this.label_ZPhaseMode.Name = "label_ZPhaseMode";
			this.label_ZPhaseMode.TabIndex = 0;
			this.label_ZPhaseMode.Text = "Z相モード：";
			// 
			// label_ZPhaseLogic
			// 
			this.label_ZPhaseLogic.Location = new System.Drawing.Point(16, 56);
			this.label_ZPhaseLogic.Name = "label_ZPhaseLogic";
			this.label_ZPhaseLogic.TabIndex = 1;
			this.label_ZPhaseLogic.Text = "Z相論理：";
			// 
			// label_SignalSource
			// 
			this.label_SignalSource.Location = new System.Drawing.Point(16, 88);
			this.label_SignalSource.Name = "label_SignalSource";
			this.label_SignalSource.TabIndex = 2;
			this.label_SignalSource.Text = "信号源：";
			// 
			// label_Direction
			// 
			this.label_Direction.Location = new System.Drawing.Point(16, 120);
			this.label_Direction.Name = "label_Direction";
			this.label_Direction.TabIndex = 3;
			this.label_Direction.Text = "カウント方向：";
			// 
			// label_DigitalFilter
			// 
			this.label_DigitalFilter.Location = new System.Drawing.Point(16, 152);
			this.label_DigitalFilter.Name = "label_DigitalFilter";
			this.label_DigitalFilter.TabIndex = 4;
			this.label_DigitalFilter.Text = "デジタルフィルタ：";
			// 
			// label_Phasenumber
			// 
			this.label_Phasenumber.Location = new System.Drawing.Point(16, 184);
			this.label_Phasenumber.Name = "label_Phasenumber";
			this.label_Phasenumber.TabIndex = 5;
			this.label_Phasenumber.Text = "相数：";
			// 
			// label_Multiple
			// 
			this.label_Multiple.Location = new System.Drawing.Point(16, 216);
			this.label_Multiple.Name = "label_Multiple";
			this.label_Multiple.TabIndex = 6;
			this.label_Multiple.Text = "逓倍：";
			// 
			// label_Synclear
			// 
			this.label_Synclear.Location = new System.Drawing.Point(16, 248);
			this.label_Synclear.Name = "label_Synclear";
			this.label_Synclear.TabIndex = 7;
			this.label_Synclear.Text = "同期クリア：";
			// 
			// label_Note
			// 
			this.label_Note.Location = new System.Drawing.Point(16, 304);
			this.label_Note.Name = "label_Note";
			this.label_Note.Size = new System.Drawing.Size(256, 23);
			this.label_Note.TabIndex = 1;
			this.label_Note.Text = "注）全てのチャネルが同じモードになります。";
			// 
			// button_OK
			// 
			this.button_OK.Location = new System.Drawing.Point(184, 328);
			this.button_OK.Name = "button_OK";
			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, 328);
			this.button_Cancel.Name = "button_Cancel";
			this.button_Cancel.TabIndex = 3;
			this.button_Cancel.Text = "ｷｬﾝｾﾙ";
			this.button_Cancel.Click += new System.EventHandler(this.button_Cancel_Click);
			// 
			// comboBox_ZPhaseMode
			// 
			this.comboBox_ZPhaseMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_ZPhaseMode.DropDownWidth = 208;
			this.comboBox_ZPhaseMode.Location = new System.Drawing.Point(120, 24);
			this.comboBox_ZPhaseMode.Name = "comboBox_ZPhaseMode";
			this.comboBox_ZPhaseMode.Size = new System.Drawing.Size(208, 20);
			this.comboBox_ZPhaseMode.TabIndex = 8;
			// 
			// comboBox_ZPhaseLogic
			// 
			this.comboBox_ZPhaseLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_ZPhaseLogic.DropDownWidth = 208;
			this.comboBox_ZPhaseLogic.Location = new System.Drawing.Point(120, 56);
			this.comboBox_ZPhaseLogic.Name = "comboBox_ZPhaseLogic";
			this.comboBox_ZPhaseLogic.Size = new System.Drawing.Size(208, 20);
			this.comboBox_ZPhaseLogic.TabIndex = 9;
			// 
			// comboBox_SignalSource
			// 
			this.comboBox_SignalSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_SignalSource.DropDownWidth = 208;
			this.comboBox_SignalSource.Location = new System.Drawing.Point(120, 88);
			this.comboBox_SignalSource.Name = "comboBox_SignalSource";
			this.comboBox_SignalSource.Size = new System.Drawing.Size(208, 20);
			this.comboBox_SignalSource.TabIndex = 10;
			// 
			// comboBox_Direction
			// 
			this.comboBox_Direction.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_Direction.DropDownWidth = 208;
			this.comboBox_Direction.Location = new System.Drawing.Point(120, 120);
			this.comboBox_Direction.Name = "comboBox_Direction";
			this.comboBox_Direction.Size = new System.Drawing.Size(208, 20);
			this.comboBox_Direction.TabIndex = 11;
			// 
			// comboBox_DigitalFilter
			// 
			this.comboBox_DigitalFilter.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_DigitalFilter.DropDownWidth = 208;
			this.comboBox_DigitalFilter.Location = new System.Drawing.Point(120, 152);
			this.comboBox_DigitalFilter.Name = "comboBox_DigitalFilter";
			this.comboBox_DigitalFilter.Size = new System.Drawing.Size(208, 20);
			this.comboBox_DigitalFilter.TabIndex = 12;
			// 
			// comboBox_Phasenumber
			// 
			this.comboBox_Phasenumber.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_Phasenumber.DropDownWidth = 208;
			this.comboBox_Phasenumber.Location = new System.Drawing.Point(120, 184);
			this.comboBox_Phasenumber.Name = "comboBox_Phasenumber";
			this.comboBox_Phasenumber.Size = new System.Drawing.Size(208, 20);
			this.comboBox_Phasenumber.TabIndex = 13;
			// 
			// comboBox_Multiple
			// 
			this.comboBox_Multiple.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_Multiple.DropDownWidth = 208;
			this.comboBox_Multiple.Location = new System.Drawing.Point(120, 216);
			this.comboBox_Multiple.Name = "comboBox_Multiple";
			this.comboBox_Multiple.Size = new System.Drawing.Size(208, 20);
			this.comboBox_Multiple.TabIndex = 14;
			// 
			// comboBox_Synclear
			// 
			this.comboBox_Synclear.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_Synclear.DropDownWidth = 208;
			this.comboBox_Synclear.Location = new System.Drawing.Point(120, 248);
			this.comboBox_Synclear.Name = "comboBox_Synclear";
			this.comboBox_Synclear.Size = new System.Drawing.Size(208, 20);
			this.comboBox_Synclear.TabIndex = 15;
			// 
			// Mode
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(368, 366);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button_Cancel,
																		  this.button_OK,
																		  this.label_Note,
																		  this.groupBox_CounterMode});
			this.Name = "Mode";
			this.Text = "モード設定";
			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;

			//---------------------------------------------
			// Z相モード 設定
			//---------------------------------------------
			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, "エラー", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// Z相論理 設定
			//-----------------------------------
			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, "エラー", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// 外部信号源 設定
			//-----------------------------------
			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, "エラー", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// カウント方向 設定
			//-----------------------------------
			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, "エラー", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// デジタルフィルタ値 設定
			//-----------------------------------
			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, "エラー", MessageBoxButtons.OK);
					return;
				}
			}
			//-----------------------------------
			// 動作モード 設定
			//-----------------------------------			
			// 相数 設定
			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;
			}
			// 逓倍 設定
			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;				
			}
			// 同期クリア 設定
			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, "エラー", MessageBoxButtons.OK);
					return;
				}
			}
			this.Close();
		}

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