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

namespace Pti4
{
	/// <summary>
	/// Measure ̊Tv̐łB
	/// </summary>
	public class Measure : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ComboBox cboLinearize;
		private System.Windows.Forms.ComboBox cboUpdate;
		private System.Windows.Forms.ComboBox cboAverage;
		private System.Windows.Forms.TextBox txtAvCondition;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.TextBox txtDisable;
		short	m_Id;

		public Measure()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = 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.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.txtDisable = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label6,
																					this.label7,
																					this.txtDisable,
																					this.label5,
																					this.label4,
																					this.label3,
																					this.txtAvCondition,
																					this.cboAverage,
																					this.cboUpdate,
																					this.cboLinearize,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(16, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(360, 176);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "vݒ";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(312, 112);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(41, 16);
			this.label5.TabIndex = 8;
			this.label5.Text = "/ms";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(56, 112);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 16);
			this.label4.TabIndex = 7;
			this.label4.Text = "ωF";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(56, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(80, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "ωF";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// txtAvCondition
			// 
			this.txtAvCondition.Location = new System.Drawing.Point(160, 112);
			this.txtAvCondition.Name = "txtAvCondition";
			this.txtAvCondition.Size = new System.Drawing.Size(144, 19);
			this.txtAvCondition.TabIndex = 5;
			this.txtAvCondition.Text = "";
			// 
			// cboAverage
			// 
			this.cboAverage.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboAverage.Items.AddRange(new object[] {
															"ωȂ",
															"񐔕",
															"ԕ"});
			this.cboAverage.Location = new System.Drawing.Point(160, 88);
			this.cboAverage.Name = "cboAverage";
			this.cboAverage.Size = new System.Drawing.Size(144, 20);
			this.cboAverage.TabIndex = 4;
			// 
			// 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(160, 56);
			this.cboUpdate.Name = "cboUpdate";
			this.cboUpdate.Size = new System.Drawing.Size(144, 20);
			this.cboUpdate.TabIndex = 3;
			// 
			// cboLinearize
			// 
			this.cboLinearize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboLinearize.Items.AddRange(new object[] {
															  "x",
															  "Rl"});
			this.cboLinearize.Location = new System.Drawing.Point(160, 24);
			this.cboLinearize.Name = "cboLinearize";
			this.cboLinearize.Size = new System.Drawing.Size(144, 20);
			this.cboLinearize.TabIndex = 2;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(56, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "XVF";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(56, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "jACYF";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(168, 200);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(88, 24);
			this.btnOK.TabIndex = 1;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(280, 200);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 24);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "ݾ";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(312, 144);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(40, 16);
			this.label6.TabIndex = 11;
			this.label6.Text = "Hex";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 144);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(136, 16);
			this.label7.TabIndex = 10;
			this.label7.Text = "ϊ֎~`l(0-F)F";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// txtDisable
			// 
			this.txtDisable.Location = new System.Drawing.Point(160, 144);
			this.txtDisable.Name = "txtDisable";
			this.txtDisable.Size = new System.Drawing.Size(144, 19);
			this.txtDisable.TabIndex = 9;
			this.txtDisable.Text = "";
			// 
			// Measure
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(386, 232);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnOK,
																		  this.groupBox1});
			this.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			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 = "vݒ";
			this.Load += new System.EventHandler(this.Measure_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//	R[ID擾
		public void SetId(short id)
		{
			m_Id = id;
		}
		
		//	_CAȌ
		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";
			// jACY@\̎擾			
			ret = Ccappt.PtGetLinearise(m_Id, ref linearize);
			if (ret != 0)
			{
				ErrorHandling("PtGetLinearise", ret);
				return;
			}
			cboLinearize.SelectedIndex = linearize;

			// XV^C~O̎擾
			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;
			}

			// Ϗ@\̎擾
			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);

			// ϊ֎~`l̎擾
			channeldisable = 0;
			ret = Ccappt.PtGetChannelDisable(m_Id, ref channeldisable);
			if (ret != 0)
			{
				ErrorHandling("PtGetChannelDisable", ret);
				return;
			}
			txtDisable.Text = string.Format("{0:X}", channeldisable);
		}

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

			// jACY@\̐ݒ
			index = cboLinearize.SelectedIndex;
			ret = Ccappt.PtSetLinearise(m_Id, (short)index);
			if (ret != 0)
			{
				ErrorHandling("PtSetLinearise", ret);
			}

			// XV^C~O̐ݒ
			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);
			}

			// Ϗ@\̐ݒ
			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);
			}
			// ϊ֎~`l̐ݒ
			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();
		}

		//	[LZ]{^NbN
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//	G[
		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);
		}
	}
}
