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 Comp.
	/// </summary>
	public class Comp : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label Label11;
		internal System.Windows.Forms.Label Label7;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.TextBox txtPulse;
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.TextBox txtComp7;
		internal System.Windows.Forms.TextBox txtComp6;
		internal System.Windows.Forms.TextBox txtComp5;
		internal System.Windows.Forms.TextBox txtComp4;
		internal System.Windows.Forms.TextBox txtComp3;
		internal System.Windows.Forms.TextBox txtComp2;
		internal System.Windows.Forms.TextBox txtComp1;
		internal System.Windows.Forms.TextBox txtComp0;
		internal System.Windows.Forms.RadioButton rdoComp1;
		internal System.Windows.Forms.RadioButton rdoComp0;
		/// <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
		static	Int32[]	CompValue = new Int32[8];					// Comparison value
		static	Int32		PulseWidth = 0;
		internal System.Windows.Forms.Button btnOK;							// Pulse width
		// Additional data
		TextBox[]		m_Comp = new TextBox[8];				// Controls of comparison value

		public Comp()
		{
			//
			// 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.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.Label2 = new System.Windows.Forms.Label();
			this.txtPulse = new System.Windows.Forms.TextBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.txtComp7 = new System.Windows.Forms.TextBox();
			this.txtComp6 = new System.Windows.Forms.TextBox();
			this.txtComp5 = new System.Windows.Forms.TextBox();
			this.txtComp4 = new System.Windows.Forms.TextBox();
			this.txtComp3 = new System.Windows.Forms.TextBox();
			this.txtComp2 = new System.Windows.Forms.TextBox();
			this.txtComp1 = new System.Windows.Forms.TextBox();
			this.txtComp0 = new System.Windows.Forms.TextBox();
			this.Label11 = new System.Windows.Forms.Label();
			this.Label7 = new System.Windows.Forms.Label();
			this.Label10 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.Label8 = 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.rdoComp1 = new System.Windows.Forms.RadioButton();
			this.rdoComp0 = new System.Windows.Forms.RadioButton();
			this.GroupBox2.SuspendLayout();
			this.GroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label2,
																					this.txtPulse,
																					this.Label1});
			this.GroupBox2.Location = new System.Drawing.Point(22, 330);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(210, 61);
			this.GroupBox2.TabIndex = 9;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "Set Comparison Register";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(177, 31);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(24, 19);
			this.Label2.TabIndex = 2;
			this.Label2.Text = "Hex";
			// 
			// txtPulse
			// 
			this.txtPulse.Location = new System.Drawing.Point(85, 27);
			this.txtPulse.Name = "txtPulse";
			this.txtPulse.Size = new System.Drawing.Size(85, 20);
			this.txtPulse.TabIndex = 1;
			this.txtPulse.Text = "";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(17, 28);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(72, 19);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "Pulse Width :";
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(136, 407);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 25);
			this.btnCancel.TabIndex = 11;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(22, 407);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(96, 25);
			this.btnOK.TabIndex = 10;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtComp7,
																					this.txtComp6,
																					this.txtComp5,
																					this.txtComp4,
																					this.txtComp3,
																					this.txtComp2,
																					this.txtComp1,
																					this.txtComp0,
																					this.Label11,
																					this.Label7,
																					this.Label10,
																					this.Label9,
																					this.Label8,
																					this.Label6,
																					this.Label5,
																					this.Label4,
																					this.rdoComp1,
																					this.rdoComp0});
			this.GroupBox1.Location = new System.Drawing.Point(22, 11);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(210, 312);
			this.GroupBox1.TabIndex = 8;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "Set Comparison";
			// 
			// txtComp7
			// 
			this.txtComp7.Location = new System.Drawing.Point(85, 270);
			this.txtComp7.Name = "txtComp7";
			this.txtComp7.TabIndex = 61;
			this.txtComp7.Text = "";
			// 
			// txtComp6
			// 
			this.txtComp6.Location = new System.Drawing.Point(85, 242);
			this.txtComp6.Name = "txtComp6";
			this.txtComp6.TabIndex = 60;
			this.txtComp6.Text = "";
			// 
			// txtComp5
			// 
			this.txtComp5.Location = new System.Drawing.Point(85, 214);
			this.txtComp5.Name = "txtComp5";
			this.txtComp5.TabIndex = 59;
			this.txtComp5.Text = "";
			// 
			// txtComp4
			// 
			this.txtComp4.Location = new System.Drawing.Point(85, 186);
			this.txtComp4.Name = "txtComp4";
			this.txtComp4.TabIndex = 58;
			this.txtComp4.Text = "";
			// 
			// txtComp3
			// 
			this.txtComp3.Location = new System.Drawing.Point(85, 158);
			this.txtComp3.Name = "txtComp3";
			this.txtComp3.TabIndex = 57;
			this.txtComp3.Text = "";
			// 
			// txtComp2
			// 
			this.txtComp2.Location = new System.Drawing.Point(85, 130);
			this.txtComp2.Name = "txtComp2";
			this.txtComp2.TabIndex = 56;
			this.txtComp2.Text = "";
			// 
			// txtComp1
			// 
			this.txtComp1.Location = new System.Drawing.Point(85, 102);
			this.txtComp1.Name = "txtComp1";
			this.txtComp1.TabIndex = 55;
			this.txtComp1.Text = "";
			// 
			// txtComp0
			// 
			this.txtComp0.Location = new System.Drawing.Point(85, 74);
			this.txtComp0.Name = "txtComp0";
			this.txtComp0.TabIndex = 54;
			this.txtComp0.Text = "";
			// 
			// Label11
			// 
			this.Label11.Location = new System.Drawing.Point(17, 271);
			this.Label11.Name = "Label11";
			this.Label11.Size = new System.Drawing.Size(56, 19);
			this.Label11.TabIndex = 53;
			this.Label11.Text = "Channel 7";
			// 
			// Label7
			// 
			this.Label7.Location = new System.Drawing.Point(17, 160);
			this.Label7.Name = "Label7";
			this.Label7.Size = new System.Drawing.Size(56, 18);
			this.Label7.TabIndex = 49;
			this.Label7.Text = "Channel 3";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(17, 243);
			this.Label10.Name = "Label10";
			this.Label10.Size = new System.Drawing.Size(56, 19);
			this.Label10.TabIndex = 52;
			this.Label10.Text = "Channel 6";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(17, 215);
			this.Label9.Name = "Label9";
			this.Label9.Size = new System.Drawing.Size(56, 19);
			this.Label9.TabIndex = 51;
			this.Label9.Text = "Channel 5";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(17, 187);
			this.Label8.Name = "Label8";
			this.Label8.Size = new System.Drawing.Size(56, 19);
			this.Label8.TabIndex = 50;
			this.Label8.Text = "Channel 4";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(17, 133);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(56, 18);
			this.Label6.TabIndex = 48;
			this.Label6.Text = "Channel 2";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(17, 106);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(56, 18);
			this.Label5.TabIndex = 47;
			this.Label5.Text = "Channel 1";
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(17, 79);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(56, 18);
			this.Label4.TabIndex = 46;
			this.Label4.Text = "Channel 0";
			// 
			// rdoComp1
			// 
			this.rdoComp1.Location = new System.Drawing.Point(84, 51);
			this.rdoComp1.Name = "rdoComp1";
			this.rdoComp1.Size = new System.Drawing.Size(64, 19);
			this.rdoComp1.TabIndex = 1;
			this.rdoComp1.Text = "Hex";
			this.rdoComp1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.rdoComp1_MouseDown);
			// 
			// rdoComp0
			// 
			this.rdoComp0.Location = new System.Drawing.Point(84, 28);
			this.rdoComp0.Name = "rdoComp0";
			this.rdoComp0.Size = new System.Drawing.Size(64, 19);
			this.rdoComp0.TabIndex = 0;
			this.rdoComp0.Text = "Dec";
			this.rdoComp0.MouseDown += new System.Windows.Forms.MouseEventHandler(this.rdoComp0_MouseDown);
			// 
			// Comp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(254, 441);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.GroupBox2,
																		  this.btnCancel,
																		  this.btnOK,
																		  this.GroupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Comp";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Set Comparison";
			this.Load += new System.EventHandler(this.Comp_Load);
			this.GroupBox2.ResumeLayout(false);
			this.GroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//---------------------------------------------------------
		//	Initialize dialog
		//---------------------------------------------------------
		private void Comp_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Initialize screen items
			//-----------------------------------
			short	i;
			rdoComp0.Checked = true;
			m_Comp[0] = txtComp0;
			m_Comp[1] = txtComp1;
			m_Comp[2] = txtComp2;
			m_Comp[3] = txtComp3;
			m_Comp[4] = txtComp4;
			m_Comp[5] = txtComp5;
			m_Comp[6] = txtComp6;
			m_Comp[7] = txtComp7;
			for (i=0; i<8; i++) 
			{
				if (i >= CNT.Common.ChannelNum) 
				{
					m_Comp[i].Enabled = false;
				}
			}
			//-----------------------------------
			// Get and display the count-match pulse
			//-----------------------------------
			Int32	PlsWidth;
			PlsWidth = 0;
			Ret = CNT.Ccapcnt.CntGetPulseWidth(CNT.Common.Id, 0, ref PlsWidth);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
			{
				PlsWidth = 0;
			}
			szText = string.Format("{0:X}", PlsWidth);
			txtPulse.Text = szText;
			//-----------------------------------
			// Get and display count comparison value
			//-----------------------------------
			for (i=0; i<CNT.Common.ChannelNum; i++) 
			{
				CompValue[i] = 0;
				Ret = CNT.Ccapcnt.CntGetCountMatchReg(CNT.Common.Id, i, 0, ref CompValue[i]);
				szText = string.Format("{0:d}", CompValue[i]);
				m_Comp[i].Text = szText;
			}
		}

		//---------------------------------------------------------
		//	Process OK button
		//---------------------------------------------------------
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			short	i;
			//-----------------------------------
			// Get setting value from screen
			//-----------------------------------
			for (i=0; i<8; i++) 
			{
				szText = m_Comp[i].Text;
				try
				{
					if (rdoComp0.Checked == true) 
					{
						CompValue[i] = Int32.Parse(szText, NumberStyles.Integer); 
					} 
					else 
					{
						CompValue[i] = Int32.Parse(szText, NumberStyles.HexNumber); 
					}
				}
				catch (System.FormatException)
				{
					CompValue[i] = 0;
				}	
			}
			szText = txtPulse.Text;
			try
			{
				PulseWidth = Int32.Parse(szText, NumberStyles.HexNumber);
			}
			catch (System.FormatException)
			{
				PulseWidth = 0;
			}
			//-----------------------------------
			// Each channel
			//-----------------------------------
			for (i=0; i<CNT.Common.ChannelNum; i++) 
			{
				//-----------------------------------
				// Set comparison value
				//-----------------------------------
				Ret = CNT.Ccapcnt.CntSetCountMatchEvent(CNT.Common.Id, i, 0, CompValue[i], 1);
				//-----------------------------
				// Process error
				//-----------------------------
				if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
				{
					CNT.Ccapcnt.CntGetErrorString(Ret, szError);
					szReturnCode = "CntSetCountMatchEvent : " + szError;
					MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
					break;
				}
				//-----------------------------------
				// Set pulse width
				//-----------------------------------
				Ret = CNT.Ccapcnt.CntSetPulseWidth(CNT.Common.Id, i, PulseWidth);
				//-----------------------------
				// Process error
				//-----------------------------
				if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS) 
				{
					CNT.Ccapcnt.CntGetErrorString(Ret, szError);
					szReturnCode = "CntSetPulseWidth : " + szError;
					MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
					break;
				}
			}
			//-----------------------------------
			// Set pulse width
			//-----------------------------------
			this.Close();
		}

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

		//---------------------------------------------------------
		//	Change the display of the comparison value
		//---------------------------------------------------------
		//	Dec
		private void rdoComp0_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//-----------------------------------
			// Refresh screen information
			//-----------------------------------
			short	i;
			for (i=0; i<8; i++) 
			{
				szText = string.Format("{0:d}", CompValue[i]);
				m_Comp[i].Text = szText;
			}
		}

		//	Hex
		private void rdoComp1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//-----------------------------------
			// Refresh screen information
			//-----------------------------------
			short	i;
			for (i=0; i<8; i++) 
			{
				szText = string.Format("{0:X8}", CompValue[i]);
				m_Comp[i].Text = szText;
			}
		}
	}
}
