using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CdigCs;

namespace Infinity
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// NXCdigCX^X쐬
		/// </summary>
		Cdig  dig = new Cdig();
		public const int	MAX_SAMPLES = 10000;			// őTv̐ݒ

		public short	m_Id;								// foCXID
		public float[]	m_Data = new float[MAX_SAMPLES];	// 擾f[^̔z
		public int		m_Samples;							// 擾Tv
		public int		m_TotalSamples;						// 擾Tv

		private System.Windows.Forms.TextBox textBoxInfo;
		private System.Windows.Forms.Label labelStatus;
		private System.Windows.Forms.Label labelCount;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox comboBoxRange;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button buttonExit;
		private System.Windows.Forms.Button buttonStop;
		private System.Windows.Forms.Button buttonInfo;
		private System.Windows.Forms.Button buttonStart;
		private System.Windows.Forms.Button buttonInit;
		private System.Windows.Forms.TextBox textBoxDevice;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label labelTotal;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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.textBoxInfo = new System.Windows.Forms.TextBox();
			this.labelStatus = new System.Windows.Forms.Label();
			this.labelCount = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.comboBoxRange = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.buttonExit = new System.Windows.Forms.Button();
			this.buttonStop = new System.Windows.Forms.Button();
			this.buttonInfo = new System.Windows.Forms.Button();
			this.buttonStart = new System.Windows.Forms.Button();
			this.buttonInit = new System.Windows.Forms.Button();
			this.textBoxDevice = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.labelTotal = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textBoxInfo
			// 
			this.textBoxInfo.Location = new System.Drawing.Point(8, 208);
			this.textBoxInfo.Multiline = true;
			this.textBoxInfo.Name = "textBoxInfo";
			this.textBoxInfo.ReadOnly = true;
			this.textBoxInfo.Size = new System.Drawing.Size(280, 40);
			this.textBoxInfo.TabIndex = 31;
			this.textBoxInfo.Text = "";
			// 
			// labelStatus
			// 
			this.labelStatus.Location = new System.Drawing.Point(96, 176);
			this.labelStatus.Name = "labelStatus";
			this.labelStatus.Size = new System.Drawing.Size(176, 16);
			this.labelStatus.TabIndex = 30;
			this.labelStatus.Text = "0H";
			// 
			// labelCount
			// 
			this.labelCount.Location = new System.Drawing.Point(96, 80);
			this.labelCount.Name = "labelCount";
			this.labelCount.Size = new System.Drawing.Size(176, 16);
			this.labelCount.TabIndex = 29;
			this.labelCount.Text = "0";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(96, 152);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(64, 16);
			this.label6.TabIndex = 28;
			this.label6.Text = "Xe[^X";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(96, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(80, 16);
			this.label3.TabIndex = 27;
			this.label3.Text = "TvO";
			// 
			// comboBoxRange
			// 
			this.comboBoxRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxRange.Location = new System.Drawing.Point(96, 24);
			this.comboBoxRange.Name = "comboBoxRange";
			this.comboBoxRange.Size = new System.Drawing.Size(192, 20);
			this.comboBoxRange.TabIndex = 26;
			this.comboBoxRange.SelectedIndexChanged += new System.EventHandler(this.comboBoxRange_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(96, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 25;
			this.label2.Text = "W";
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(8, 176);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.TabIndex = 24;
			this.buttonExit.Text = "I";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// buttonStop
			// 
			this.buttonStop.Location = new System.Drawing.Point(8, 144);
			this.buttonStop.Name = "buttonStop";
			this.buttonStop.TabIndex = 23;
			this.buttonStop.Text = "W~";
			this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
			// 
			// buttonInfo
			// 
			this.buttonInfo.Location = new System.Drawing.Point(8, 112);
			this.buttonInfo.Name = "buttonInfo";
			this.buttonInfo.TabIndex = 22;
			this.buttonInfo.Text = "Ԏ擾";
			this.buttonInfo.Click += new System.EventHandler(this.buttonInfo_Click);
			// 
			// buttonStart
			// 
			this.buttonStart.Location = new System.Drawing.Point(8, 80);
			this.buttonStart.Name = "buttonStart";
			this.buttonStart.TabIndex = 21;
			this.buttonStart.Text = "WJn";
			this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
			// 
			// buttonInit
			// 
			this.buttonInit.Location = new System.Drawing.Point(8, 48);
			this.buttonInit.Name = "buttonInit";
			this.buttonInit.TabIndex = 20;
			this.buttonInit.Text = "";
			this.buttonInit.Click += new System.EventHandler(this.buttonInit_Click);
			// 
			// textBoxDevice
			// 
			this.textBoxDevice.Location = new System.Drawing.Point(8, 24);
			this.textBoxDevice.Name = "textBoxDevice";
			this.textBoxDevice.Size = new System.Drawing.Size(72, 19);
			this.textBoxDevice.TabIndex = 19;
			this.textBoxDevice.Text = "DIG000";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 18;
			this.label1.Text = "foCX";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(96, 104);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(136, 16);
			this.label4.TabIndex = 32;
			this.label4.Text = "擾TvO";
			// 
			// labelTotal
			// 
			this.labelTotal.Location = new System.Drawing.Point(96, 128);
			this.labelTotal.Name = "labelTotal";
			this.labelTotal.Size = new System.Drawing.Size(176, 16);
			this.labelTotal.TabIndex = 33;
			this.labelTotal.Text = "0";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(296, 254);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.labelTotal,
																		  this.label4,
																		  this.textBoxInfo,
																		  this.labelStatus,
																		  this.labelCount,
																		  this.label6,
																		  this.label3,
																		  this.comboBoxRange,
																		  this.label2,
																		  this.buttonExit,
																		  this.buttonStop,
																		  this.buttonInfo,
																		  this.buttonStart,
																		  this.buttonInit,
																		  this.textBoxDevice,
																		  this.label1});
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "TvO";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.Closed += new System.EventHandler(this.Form1_Closed);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		/// <summary>
		/// tH[
		/// </summary>
		private void Form1_Load(object sender, System.EventArgs e)
		{
			// ̓WpR{{bNX̍쐬
			comboBoxRange.ResetText();
			comboBoxRange.Items.AddRange(new object[]
				{
					"-20 - +20V",
					"-10 - +10V",
					"-5 - +5V",
					"-2 - +2V",
					"-1 - +1V",
					"-0.5 - +0.5V",
					"-0.2 - +0.2V",
					"-0.1 - +0.1V",
					"-0.05 - +0.05V",
					"-0.02 - +0.02V"}
				);

		}

		/// <summary>
		/// 
		/// </summary>
		private void buttonInit_Click(object sender, System.EventArgs e)
		{
			int		Ret;
			string	ErrorString;

			//--------------------------------
			// 
			//--------------------------------
			Ret = dig.Init(textBoxDevice.Text, out m_Id);
			if(Ret != 0)
			{
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.Init = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			//--------------------------------
			// foCX̃Zbg
			//--------------------------------
			Ret = dig.ResetDevice(m_Id);
			if(Ret != 0)
			{
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.ResetDevice = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			textBoxInfo.Text = " : I";
		}

		/// <summary>
		/// ̓W̐ݒ
		/// </summary>
		private void comboBoxRange_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int		Index;		// R{{bNX̃CfbNX
			float	Range;		// ̓Wl
			Index = comboBoxRange.SelectedIndex;
			switch(Index)
			{
				case 0:	Range = Cdig.DIG_RANGE_40VPP;	break;
				case 1:	Range = Cdig.DIG_RANGE_20VPP;	break;
				case 2:	Range = Cdig.DIG_RANGE_10VPP;	break;
				case 3:	Range = Cdig.DIG_RANGE_4VPP;	break;
				case 4:	Range = Cdig.DIG_RANGE_2VPP;	break;
				case 5:	Range = Cdig.DIG_RANGE_1VPP;	break;
				case 6:	Range = Cdig.DIG_RANGE_400MVPP;	break;
				case 7:	Range = Cdig.DIG_RANGE_200MVPP;	break;
				case 8:	Range = Cdig.DIG_RANGE_100MVPP;	break;
				case 9:	Range = Cdig.DIG_RANGE_40MVPP;	break;
				default:Range = 0;						break;
			}

			//--------------------------
			// ̓W̐ݒ
			//--------------------------
			int		Ret;
			Ret = dig.SetRange(m_Id, Cdig.DIG_CHANNEL0, Range);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetRange = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			textBoxInfo.Text = "Wݒ : I";
		}

		/// <summary>
		/// WJn
		/// </summary>
		private void buttonStart_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l
			//---------------------------------
			// ϐ
			//---------------------------------
			m_Samples = 0;
			m_TotalSamples = 0;

			//---------------------------------
			// 샂[hݒFf[^K[[h
			//---------------------------------
			Ret = dig.SetDigitizerMode(m_Id, Cdig.DIG_MODE_LOGGER);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetDigitizerMode = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// TvONbN\[XݒF{[hU
			//---------------------------------
			Ret = dig.SetClockSource(m_Id, Cdig.DIG_ECU_SRC_CLOCK);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetClockSource = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// TvONbNݒF1000usec (ݒl * 10 + 10(nsec))
			//---------------------------------
			Ret = dig.SetSampleClock(m_Id, 99999);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetSampleClock = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// R[hW񐔐ݒF1
			//---------------------------------
			Ret = dig.SetRecordNumber(m_Id, 1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetRecordNumber = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// vgKTvݒF10000Tv
			//---------------------------------
			Ret = dig.SetPreSampleTimes(m_Id, MAX_SAMPLES);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetPreSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// |XggKTvݒFȂ
			//---------------------------------
			Ret = dig.SetPostSampleTimes(m_Id, 2);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetPostSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// `lݒF0`l̂ݗL
			//---------------------------------
			// 0`lLɂ
			Ret = dig.SetChannel(m_Id, Cdig.DIG_CHANNEL0, Cdig.DIG_ENABLE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			// 1`l𖳌ɂ
			Ret = dig.SetChannel(m_Id, Cdig.DIG_CHANNEL1, Cdig.DIG_DISABLE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// vgKނ̐ݒF
			//---------------------------------
			Ret = dig.SetPreTriggerType(m_Id, Cdig.DIG_TRIGGERTYPE_AUTO);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetPreTritterType = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// |XggKނ̐ݒF\tgEFA
			//---------------------------------
			Ret = dig.SetPostTriggerType(m_Id, Cdig.DIG_TRIGGERTYPE_SOFT);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetPostTritterType = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			//---------------------------------
			// Jn
			//---------------------------------
			Ret = dig.Start(m_Id);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.Start = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo.Text = "WJn : I";

			//---------------------------------
			// 쒆Xe[^XOFFɂȂ܂Ń|[O
			//---------------------------------
			int Status;
			int TriggerPoint;			// |XggK̐擪f[^ʒu

			do
			{
				//---------------------------------
				// _CAOLɂ邽߂̏
				//---------------------------------
				System.Windows.Forms.Application.DoEvents();
				//---------------------------------
				// hCoɊi[ꂽTv擾
				//---------------------------------
				Ret = dig.GetDriverMemorySamples(m_Id, out m_Samples);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelCount.Text = m_Samples.ToString();
				//---------------------------------
				// X^[gĂWTv擾
				//---------------------------------
				Ret = dig.GetDigitizedSamples(m_Id, out m_TotalSamples);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.GetDigitizedSamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelTotal.Text = m_TotalSamples.ToString();
				//---------------------------------
				// Wf[^擾
				//---------------------------------
				if(m_Samples > 0)
				{
					// 擾TvOf[^i[zTCYȂ悤
					if(m_Samples > MAX_SAMPLES)
					{
						m_Samples = MAX_SAMPLES;
					}
					Ret = dig.ReadSamples(m_Id, ref m_Samples, ref m_Data, out TriggerPoint);
					if(Ret != 0)
					{
						string ErrorString;
						dig.GetErrorString(Ret, out ErrorString);
						textBoxInfo.Text = "dig.ReadSamples = " + Ret.ToString() + " : " + ErrorString;
						return;
					}
				}
				//---------------------------------
				// Xe[^X̎擾
				//---------------------------------
				Ret = dig.GetStatus(m_Id, out Status);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelStatus.Text = Status.ToString("X") + "H";

			}while((Status & Cdig.DIG_STATUS_BUSY) == Cdig.DIG_STATUS_BUSY);
			//---------------------------------
			// ~ɃhCoɊi[ĂTv擾
			//---------------------------------
			Ret = dig.GetDriverMemorySamples(m_Id, out m_Samples);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelCount.Text = m_Samples.ToString();
			//---------------------------------
			// X^[gĂWTv擾
			//---------------------------------
			Ret = dig.GetDigitizedSamples(m_Id, out m_TotalSamples);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetDigitizedSamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelTotal.Text = m_TotalSamples.ToString();
			//---------------------------------
			// Wf[^擾
			//---------------------------------
			// 擾TvOf[^i[zTCYȂ悤
			if(m_Samples > 0)
			{
				if(m_Samples > MAX_SAMPLES)
				{
					m_Samples = MAX_SAMPLES;
				}
				Ret = dig.ReadSamples(m_Id, ref m_Samples, ref m_Data, out TriggerPoint);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.ReadSamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
			}
		}

		/// <summary>
		/// Ԏ擾
		/// </summary>
		private void buttonInfo_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l
			int		Status;		// Xe[^X
			int		Samples;	// Tv
			int		Total;		// 擾Tv
			//---------------------------------
			// hCoɊi[ꂽTv擾
			//---------------------------------
			Ret = dig.GetDriverMemorySamples(m_Id, out Samples);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelCount.Text = Samples.ToString();
			Ret = dig.GetDigitizedSamples(m_Id, out Total);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetDigitizedSamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelTotal.Text = Total.ToString();
			//---------------------------------
			// Xe[^X̎擾
			//---------------------------------
			Ret = dig.GetStatus(m_Id, out Status);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelStatus.Text = Status.ToString("X") + "H";
			textBoxInfo.Text = "Ԏ擾 : I";
		}

		/// <summary>
		/// W~
		/// </summary>
		private void buttonStop_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l
			Ret = dig.Stop(m_Id);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.Stop = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo.Text = "W~ : I";
		}

		/// <summary>
		/// I
		/// </summary>
		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			int		Ret;	// ߂l
			Ret = dig.Exit(m_Id);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.Exit = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo.Text = "I : I";
		}

		/// <summary>
		/// tH[I
		/// </summary>
		private void Form1_Closed(object sender, System.EventArgs e)
		{
			// I
			dig.Exit(m_Id);
		}
	}
}
