﻿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 Comp.
	/// </summary>
	public class Comp : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox_Comparison_register_set;
		private System.Windows.Forms.RadioButton radioButton_Dec;
		private System.Windows.Forms.RadioButton radioButton_Hex;
		private System.Windows.Forms.Label label_Channel0;
		private System.Windows.Forms.Label label_Channel1;
		private System.Windows.Forms.Label label_Channel2;
		private System.Windows.Forms.Label label_Channel3;
		private System.Windows.Forms.Label label_Channel4;
		private System.Windows.Forms.Label label_Channel5;
		private System.Windows.Forms.Label label_Channel6;
		private System.Windows.Forms.Label label_Channel7;
		private System.Windows.Forms.TextBox textBox_Channel0;
		private System.Windows.Forms.TextBox textBox_Channel1;
		private System.Windows.Forms.TextBox textBox_Channel2;
		private System.Windows.Forms.TextBox textBox_Channel3;
		private System.Windows.Forms.TextBox textBox_Channel4;
		private System.Windows.Forms.TextBox textBox_Channel5;
		private System.Windows.Forms.TextBox textBox_Channel6;
		private System.Windows.Forms.TextBox textBox_Channel7;
		private System.Windows.Forms.GroupBox groupBox_Pulse_set_consistent_with_count;
		private System.Windows.Forms.Label label_PulseWidth;
		private System.Windows.Forms.ComboBox comboBox_PulseWidth;
		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();

		private System.Windows.Forms.TextBox[] textBox_Channel = new System.Windows.Forms.TextBox[8];

		// Comparison value
		static private uint[] CompValue ={ 100, 200, 300, 400, 500, 600, 700, 800 };

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			textBox_Channel[0] = textBox_Channel0;
			textBox_Channel[1] = textBox_Channel1;
			textBox_Channel[2] = textBox_Channel2;
			textBox_Channel[3] = textBox_Channel3;
			textBox_Channel[4] = textBox_Channel4;
			textBox_Channel[5] = textBox_Channel5;
			textBox_Channel[6] = textBox_Channel6;
			textBox_Channel[7] = textBox_Channel7;
			//-----------------------------
			// Exprocess invalidate
			//-----------------------------
			for (short ChNo = Counter.m_ChannelNum; ChNo < 8; ChNo++) 
			{
				textBox_Channel[ChNo].Enabled = false;	
			}
			//-----------------------------------
			// Screen infomation renew
			//-----------------------------------
			for (int ChNo = 0; ChNo < 8; ChNo++)
			{
				textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo]);
			}
			
			for (int i = 0; i < 0x10; i++)
			{
				comboBox_PulseWidth.Items.Add(System.Convert.ToString(i, 16));
			}
			
			int		Ret;
			short	PlsWidth;
			short   Ch_No=0;
			Ret = cnt.GetPulseWidth(Counter.m_Id, Ch_No, out PlsWidth);
			comboBox_PulseWidth.SelectedIndex = PlsWidth;
		}

		/// <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_Comparison_register_set = new System.Windows.Forms.GroupBox();
            this.textBox_Channel7 = new System.Windows.Forms.TextBox();
            this.textBox_Channel6 = new System.Windows.Forms.TextBox();
            this.textBox_Channel5 = new System.Windows.Forms.TextBox();
            this.textBox_Channel4 = new System.Windows.Forms.TextBox();
            this.textBox_Channel3 = new System.Windows.Forms.TextBox();
            this.textBox_Channel2 = new System.Windows.Forms.TextBox();
            this.textBox_Channel1 = new System.Windows.Forms.TextBox();
            this.textBox_Channel0 = new System.Windows.Forms.TextBox();
            this.label_Channel7 = new System.Windows.Forms.Label();
            this.label_Channel6 = new System.Windows.Forms.Label();
            this.label_Channel5 = new System.Windows.Forms.Label();
            this.label_Channel4 = new System.Windows.Forms.Label();
            this.label_Channel3 = new System.Windows.Forms.Label();
            this.label_Channel2 = new System.Windows.Forms.Label();
            this.label_Channel1 = new System.Windows.Forms.Label();
            this.label_Channel0 = new System.Windows.Forms.Label();
            this.radioButton_Hex = new System.Windows.Forms.RadioButton();
            this.radioButton_Dec = new System.Windows.Forms.RadioButton();
            this.groupBox_Pulse_set_consistent_with_count = new System.Windows.Forms.GroupBox();
            this.label_PulseWidth = new System.Windows.Forms.Label();
            this.comboBox_PulseWidth = new System.Windows.Forms.ComboBox();
            this.button_OK = new System.Windows.Forms.Button();
            this.button_Cancel = new System.Windows.Forms.Button();
            this.groupBox_Comparison_register_set.SuspendLayout();
            this.groupBox_Pulse_set_consistent_with_count.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox_Comparison_register_set
            // 
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel7);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel6);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel5);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel4);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel3);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel2);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel1);
            this.groupBox_Comparison_register_set.Controls.Add(this.textBox_Channel0);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel7);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel6);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel5);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel4);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel3);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel2);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel1);
            this.groupBox_Comparison_register_set.Controls.Add(this.label_Channel0);
            this.groupBox_Comparison_register_set.Controls.Add(this.radioButton_Hex);
            this.groupBox_Comparison_register_set.Controls.Add(this.radioButton_Dec);
            this.groupBox_Comparison_register_set.Location = new System.Drawing.Point(16, 17);
            this.groupBox_Comparison_register_set.Name = "groupBox_Comparison_register_set";
            this.groupBox_Comparison_register_set.Size = new System.Drawing.Size(248, 364);
            this.groupBox_Comparison_register_set.TabIndex = 0;
            this.groupBox_Comparison_register_set.TabStop = false;
            this.groupBox_Comparison_register_set.Text = "Comparison register set";
            // 
            // textBox_Channel7
            // 
            this.textBox_Channel7.Location = new System.Drawing.Point(120, 329);
            this.textBox_Channel7.Name = "textBox_Channel7";
            this.textBox_Channel7.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel7.TabIndex = 17;
            this.textBox_Channel7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel6
            // 
            this.textBox_Channel6.Location = new System.Drawing.Point(120, 295);
            this.textBox_Channel6.Name = "textBox_Channel6";
            this.textBox_Channel6.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel6.TabIndex = 16;
            this.textBox_Channel6.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel5
            // 
            this.textBox_Channel5.Location = new System.Drawing.Point(120, 260);
            this.textBox_Channel5.Name = "textBox_Channel5";
            this.textBox_Channel5.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel5.TabIndex = 15;
            this.textBox_Channel5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel4
            // 
            this.textBox_Channel4.Location = new System.Drawing.Point(120, 225);
            this.textBox_Channel4.Name = "textBox_Channel4";
            this.textBox_Channel4.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel4.TabIndex = 14;
            this.textBox_Channel4.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel3
            // 
            this.textBox_Channel3.Location = new System.Drawing.Point(120, 191);
            this.textBox_Channel3.Name = "textBox_Channel3";
            this.textBox_Channel3.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel3.TabIndex = 13;
            this.textBox_Channel3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel2
            // 
            this.textBox_Channel2.Location = new System.Drawing.Point(120, 156);
            this.textBox_Channel2.Name = "textBox_Channel2";
            this.textBox_Channel2.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel2.TabIndex = 12;
            this.textBox_Channel2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel1
            // 
            this.textBox_Channel1.Location = new System.Drawing.Point(120, 121);
            this.textBox_Channel1.Name = "textBox_Channel1";
            this.textBox_Channel1.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel1.TabIndex = 11;
            this.textBox_Channel1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Channel0
            // 
            this.textBox_Channel0.Location = new System.Drawing.Point(120, 87);
            this.textBox_Channel0.Name = "textBox_Channel0";
            this.textBox_Channel0.Size = new System.Drawing.Size(100, 20);
            this.textBox_Channel0.TabIndex = 10;
            this.textBox_Channel0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // label_Channel7
            // 
            this.label_Channel7.Location = new System.Drawing.Point(16, 329);
            this.label_Channel7.Name = "label_Channel7";
            this.label_Channel7.Size = new System.Drawing.Size(100, 25);
            this.label_Channel7.TabIndex = 9;
            this.label_Channel7.Text = "Channel7:";
            // 
            // label_Channel6
            // 
            this.label_Channel6.Location = new System.Drawing.Point(16, 295);
            this.label_Channel6.Name = "label_Channel6";
            this.label_Channel6.Size = new System.Drawing.Size(100, 25);
            this.label_Channel6.TabIndex = 8;
            this.label_Channel6.Text = "Channel6:";
            // 
            // label_Channel5
            // 
            this.label_Channel5.Location = new System.Drawing.Point(16, 260);
            this.label_Channel5.Name = "label_Channel5";
            this.label_Channel5.Size = new System.Drawing.Size(100, 25);
            this.label_Channel5.TabIndex = 7;
            this.label_Channel5.Text = "Channel5:";
            // 
            // label_Channel4
            // 
            this.label_Channel4.Location = new System.Drawing.Point(16, 225);
            this.label_Channel4.Name = "label_Channel4";
            this.label_Channel4.Size = new System.Drawing.Size(100, 25);
            this.label_Channel4.TabIndex = 6;
            this.label_Channel4.Text = "Channel4:";
            // 
            // label_Channel3
            // 
            this.label_Channel3.Location = new System.Drawing.Point(16, 191);
            this.label_Channel3.Name = "label_Channel3";
            this.label_Channel3.Size = new System.Drawing.Size(100, 25);
            this.label_Channel3.TabIndex = 5;
            this.label_Channel3.Text = "Channel3:";
            // 
            // label_Channel2
            // 
            this.label_Channel2.Location = new System.Drawing.Point(16, 156);
            this.label_Channel2.Name = "label_Channel2";
            this.label_Channel2.Size = new System.Drawing.Size(100, 25);
            this.label_Channel2.TabIndex = 4;
            this.label_Channel2.Text = "Channel2:";
            // 
            // label_Channel1
            // 
            this.label_Channel1.Location = new System.Drawing.Point(16, 121);
            this.label_Channel1.Name = "label_Channel1";
            this.label_Channel1.Size = new System.Drawing.Size(100, 25);
            this.label_Channel1.TabIndex = 3;
            this.label_Channel1.Text = "Channel1:";
            // 
            // label_Channel0
            // 
            this.label_Channel0.Location = new System.Drawing.Point(16, 87);
            this.label_Channel0.Name = "label_Channel0";
            this.label_Channel0.Size = new System.Drawing.Size(100, 25);
            this.label_Channel0.TabIndex = 2;
            this.label_Channel0.Text = "Channel0:";
            // 
            // radioButton_Hex
            // 
            this.radioButton_Hex.Location = new System.Drawing.Point(16, 52);
            this.radioButton_Hex.Name = "radioButton_Hex";
            this.radioButton_Hex.Size = new System.Drawing.Size(104, 26);
            this.radioButton_Hex.TabIndex = 1;
            this.radioButton_Hex.Text = "Hex";
            this.radioButton_Hex.CheckedChanged += new System.EventHandler(this.radioButton_Hex_CheckedChanged);
            // 
            // radioButton_Dec
            // 
            this.radioButton_Dec.Checked = true;
            this.radioButton_Dec.Location = new System.Drawing.Point(16, 26);
            this.radioButton_Dec.Name = "radioButton_Dec";
            this.radioButton_Dec.Size = new System.Drawing.Size(104, 26);
            this.radioButton_Dec.TabIndex = 0;
            this.radioButton_Dec.TabStop = true;
            this.radioButton_Dec.Text = "Dec";
            this.radioButton_Dec.CheckedChanged += new System.EventHandler(this.radioButton_Dec_CheckedChanged);
            // 
            // groupBox_Pulse_set_consistent_with_count
            // 
            this.groupBox_Pulse_set_consistent_with_count.Controls.Add(this.label_PulseWidth);
            this.groupBox_Pulse_set_consistent_with_count.Controls.Add(this.comboBox_PulseWidth);
            this.groupBox_Pulse_set_consistent_with_count.Location = new System.Drawing.Point(16, 390);
            this.groupBox_Pulse_set_consistent_with_count.Name = "groupBox_Pulse_set_consistent_with_count";
            this.groupBox_Pulse_set_consistent_with_count.Size = new System.Drawing.Size(248, 61);
            this.groupBox_Pulse_set_consistent_with_count.TabIndex = 1;
            this.groupBox_Pulse_set_consistent_with_count.TabStop = false;
            this.groupBox_Pulse_set_consistent_with_count.Text = "Pulse set consistent with count";
            // 
            // label_PulseWidth
            // 
            this.label_PulseWidth.Location = new System.Drawing.Point(16, 26);
            this.label_PulseWidth.Name = "label_PulseWidth";
            this.label_PulseWidth.Size = new System.Drawing.Size(100, 25);
            this.label_PulseWidth.TabIndex = 2;
            this.label_PulseWidth.Text = "Pulse Width:";
            // 
            // comboBox_PulseWidth
            // 
            this.comboBox_PulseWidth.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_PulseWidth.Location = new System.Drawing.Point(120, 26);
            this.comboBox_PulseWidth.Name = "comboBox_PulseWidth";
            this.comboBox_PulseWidth.Size = new System.Drawing.Size(121, 21);
            this.comboBox_PulseWidth.TabIndex = 2;
            // 
            // button_OK
            // 
            this.button_OK.Location = new System.Drawing.Point(96, 459);
            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(184, 459);
            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);
            // 
            // Comp
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(280, 498);
            this.Controls.Add(this.button_Cancel);
            this.Controls.Add(this.button_OK);
            this.Controls.Add(this.groupBox_Pulse_set_consistent_with_count);
            this.Controls.Add(this.groupBox_Comparison_register_set);
            this.Name = "Comp";
            this.Text = "Comp";
            this.groupBox_Comparison_register_set.ResumeLayout(false);
            this.groupBox_Comparison_register_set.PerformLayout();
            this.groupBox_Pulse_set_consistent_with_count.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		private void radioButton_Dec_CheckedChanged(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Screen infomation renew
			//-----------------------------------
			for (short ChNo = 0; ChNo < 8; ChNo++)
			{
				textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo]);
			}
		}

		private void radioButton_Hex_CheckedChanged(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Screen infomation renew
			//-----------------------------------
			for (short ChNo = 0; ChNo < 8; ChNo++)
			{
				textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo], 16);
			}
		}

		private void button_OK_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Set CntNotifyCountUp
			//-----------------------------------
			if (radioButton_Dec.Checked == true) 
			{
				for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
				{
					try 
					{
						CompValue[ChNo] = System.Convert.ToUInt32(textBox_Channel[ChNo].Text); 
					} 
					catch
					{
						CompValue[ChNo] = 0;
					}
				}
			}
			else 
			{
				for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
				{
					try 
					{
						CompValue[ChNo] = System.Convert.ToUInt32(textBox_Channel[ChNo].Text, 16);
					}
					catch	
					{
						CompValue[ChNo] = 0;
					}
				}
			}

			for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				int Ret;
				Ret = cnt.NotifyCountUp(Counter.m_Id, ChNo, 0, CompValue[ChNo], Counter.m_Handle);
				if (Ret != 0)
				{
					string csTmp;
					csTmp = string.Format("CntNotifyCountUp Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
					return;
				}
			}

			//-----------------------------------
			// Set CntSetPulseWidth
			//-----------------------------------
			for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				int		Ret;
				short	PlsWidth;
				PlsWidth = System.Convert.ToInt16(comboBox_PulseWidth.Text,16);
				Ret = cnt.SetPulseWidth(Counter.m_Id, ChNo, PlsWidth);
				if (Ret != 0)
				{
					string csTmp;
					csTmp = string.Format("CntSetPulseWidth 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();
		}
	}
}
