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

namespace Pti4
{
	/// <summary>
	/// Summary description for Measure.
	/// </summary>
	public class Measure : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.Button btnOK;
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.TextBox txtAvCondition;
		internal System.Windows.Forms.ComboBox cboAverage;
		internal System.Windows.Forms.ComboBox cboUpdate;
		internal System.Windows.Forms.ComboBox cboLinearize;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		internal System.Windows.Forms.Label label6;
		internal System.Windows.Forms.TextBox txtDisable;
		internal System.Windows.Forms.Label label7;
		short	m_Id;

		public Measure()
		{
			//
			// 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.btnOK = new System.Windows.Forms.Button();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.label7 = new System.Windows.Forms.Label();
			this.txtDisable = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.txtAvCondition = new System.Windows.Forms.TextBox();
			this.cboAverage = new System.Windows.Forms.ComboBox();
			this.cboUpdate = new System.Windows.Forms.ComboBox();
			this.cboLinearize = new System.Windows.Forms.ComboBox();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			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(336, 224);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 24);
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(232, 224);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(96, 24);
			this.btnOK.TabIndex = 4;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label7,
																					this.txtDisable,
																					this.label6,
																					this.Label5,
																					this.txtAvCondition,
																					this.cboAverage,
																					this.cboUpdate,
																					this.cboLinearize,
																					this.Label4,
																					this.Label3,
																					this.Label2,
																					this.Label1});
			this.GroupBox1.Location = new System.Drawing.Point(18, 15);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(430, 190);
			this.GroupBox1.TabIndex = 3;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "Measurement Setting";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(358, 153);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(56, 16);
			this.label7.TabIndex = 11;
			this.label7.Text = "Hex";
			// 
			// txtDisable
			// 
			this.txtDisable.Location = new System.Drawing.Point(214, 151);
			this.txtDisable.Name = "txtDisable";
			this.txtDisable.Size = new System.Drawing.Size(136, 20);
			this.txtDisable.TabIndex = 10;
			this.txtDisable.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(6, 153);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(200, 16);
			this.label6.TabIndex = 9;
			this.label6.Text = "Conversion Disabled Channels (0 - F) :";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(358, 124);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(56, 16);
			this.Label5.TabIndex = 8;
			this.Label5.Text = "Times/ms";
			// 
			// txtAvCondition
			// 
			this.txtAvCondition.Location = new System.Drawing.Point(214, 122);
			this.txtAvCondition.Name = "txtAvCondition";
			this.txtAvCondition.Size = new System.Drawing.Size(136, 20);
			this.txtAvCondition.TabIndex = 7;
			this.txtAvCondition.Text = "";
			// 
			// cboAverage
			// 
			this.cboAverage.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboAverage.Items.AddRange(new object[] {
															"Not Averaging",
															"Average on Times",
															"Average on Time"});
			this.cboAverage.Location = new System.Drawing.Point(214, 92);
			this.cboAverage.Name = "cboAverage";
			this.cboAverage.Size = new System.Drawing.Size(136, 21);
			this.cboAverage.TabIndex = 6;
			// 
			// cboUpdate
			// 
			this.cboUpdate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboUpdate.Items.AddRange(new object[] {
														   "150ms",
														   "40ms",
														   "5ms",
														   "1.5ms"});
			this.cboUpdate.Location = new System.Drawing.Point(214, 62);
			this.cboUpdate.Name = "cboUpdate";
			this.cboUpdate.Size = new System.Drawing.Size(136, 21);
			this.cboUpdate.TabIndex = 5;
			// 
			// cboLinearize
			// 
			this.cboLinearize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboLinearize.Items.AddRange(new object[] {
															  "Temperature",
															  "Resistance Capacity"});
			this.cboLinearize.Location = new System.Drawing.Point(214, 32);
			this.cboLinearize.Name = "cboLinearize";
			this.cboLinearize.Size = new System.Drawing.Size(136, 21);
			this.cboLinearize.TabIndex = 4;
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(94, 124);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(112, 16);
			this.Label4.TabIndex = 3;
			this.Label4.Text = "Averaging Condition :";
			this.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(142, 94);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(64, 16);
			this.Label3.TabIndex = 2;
			this.Label3.Text = "Averaging :";
			this.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(126, 64);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(80, 16);
			this.Label2.TabIndex = 1;
			this.Label2.Text = "Update Rate :";
			this.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(150, 34);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(56, 16);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "Linearize :";
			this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Measure
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(474, 264);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnOK,
																		  this.GroupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Measure";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Measurement Setting";
			this.Load += new System.EventHandler(this.Measure_Load);
			this.GroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//	Get the caller ID
		public void SetId(short id)
		{
			m_Id = id;
		}

		//	Initialize dialog
		private void Measure_Load(object sender, System.EventArgs e)
		{
			int		ret;
			short	linearize, averagetype;
			int		updatetiming, averagedata;
			int		channeldisable;

			linearize = 0;
			updatetiming = 0;
			averagetype = 0;
			averagedata = 0;
			txtAvCondition.Text = "0";
			// Get the linearize function
			ret = Ccappt.PtGetLinearise(m_Id, ref linearize);
			if (ret != 0)
			{
				ErrorHandling("PtGetLinearise", ret);
				return;
			}
			cboLinearize.SelectedIndex = linearize;

			// Get the update timing
			ret = Ccappt.PtGetUpdateTiming(m_Id, ref updatetiming);
			if (ret != 0)
			{
				ErrorHandling("PtGetUpdateTiming", ret);
				return;
			}			
			switch (updatetiming)
			{
				case Ccappt.SPEED_150MS:
					cboUpdate.SelectedIndex = 0;
					break;
				case Ccappt.SPEED_40MS:
					cboUpdate.SelectedIndex = 1;
					break;
				case Ccappt.SPEED_5MS:
					cboUpdate.SelectedIndex = 2;
					break;
				case Ccappt.SPEED_1P5MS:
					cboUpdate.SelectedIndex = 3;
					break;
			}

			// Get the average function
			ret = Ccappt.PtGetAverage(m_Id, ref averagetype, ref averagedata);
			if (ret != 0)
			{
				ErrorHandling("PtGetAverage", ret);
				return;
			}
			cboAverage.SelectedIndex = averagetype;
			txtAvCondition.Text = string.Format("{0:d}", averagedata);

			// Get the conversion disabled channels
			channeldisable = 0;
			ret = Ccappt.PtGetChannelDisable(m_Id, ref channeldisable);
			if (ret != 0)
			{
				ErrorHandling("PtGetChannelDisable", ret);
				return;
			}
			txtDisable.Text = string.Format("{0:X}", channeldisable);
		}

		//	Click the [OK] button
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			int		ret;
			int		index;
			int		updatetiming;
			int		data;
			string	szText;
			int		channeldisable;

			// Set the linearize function
			index = cboLinearize.SelectedIndex;
			ret = Ccappt.PtSetLinearise(m_Id, (short)index);
			if (ret != 0)
			{
				ErrorHandling("PtSetLinearise", ret);
			}

			// Set the update timing
			index = cboUpdate.SelectedIndex;
			switch (index)
			{
				case 0:
					updatetiming = Ccappt.SPEED_150MS;
					break;
				case 1:
					updatetiming = Ccappt.SPEED_40MS;
					break;
				case 2:
					updatetiming = Ccappt.SPEED_5MS;
					break;
				case 3:
					updatetiming = Ccappt.SPEED_1P5MS;
					break;
				default:
					updatetiming = Ccappt.SPEED_150MS;
					break;
			}
			ret = Ccappt.PtSetUpdateTiming(m_Id, updatetiming);
			if (ret != 0)
			{
				ErrorHandling("PtSetUpdateTiming", ret);
			}

			// Set the average function
			index = cboAverage.SelectedIndex;
			try
			{
				data = Convert.ToInt32(txtAvCondition.Text);
			}
			catch (System.FormatException)
			{
				data = 0;
			}
			ret = Ccappt.PtSetAverage(m_Id, (short)index, data);
			if (ret != 0)
			{
				ErrorHandling("PtSetAverage", ret);
			}
			
			// Set the conversion disabled channels
			szText = txtDisable.Text;
			try
			{
				channeldisable = int.Parse(szText, System.Globalization.NumberStyles.HexNumber);
			}
			catch (System.FormatException)
			{
				channeldisable = 0;
			}
			ret = Ccappt.PtSetChannelDisable(m_Id, channeldisable);
			if (ret != 0)
			{
				ErrorHandling("PtSetChannelDisable", ret);
			}

			this.Close();
		}

		//	Click the [Cancel] button
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//	Process error
		private void ErrorHandling(string Dfunction, int ret)
		{
			StringBuilder	errstr = new StringBuilder(256);
			string	str = new string(' ', 256);			 
			Ccappt.PtGetErrorString(ret, errstr);
			str = string.Format("{0}", errstr);
			MessageBox.Show(str, Dfunction, MessageBoxButtons.OK);
		}
	}
}
