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

namespace DataLogger
{
	/// <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 = 1000;				// őTv̐ݒ

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

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBoxDevice;
		private System.Windows.Forms.Button buttonInit;
		private System.Windows.Forms.Button buttonStart;
		private System.Windows.Forms.Button buttonInfo;
		private System.Windows.Forms.Button buttonStop;
		private System.Windows.Forms.Button buttonSave;
		private System.Windows.Forms.Button buttonExit;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox comboBoxRange;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label labelCount;
		private System.Windows.Forms.Label labelStatus;
		private System.Windows.Forms.TextBox textBoxInfo;
		private System.Windows.Forms.ListBox listBoxData;
		/// <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.label1 = new System.Windows.Forms.Label();
			this.textBoxDevice = new System.Windows.Forms.TextBox();
			this.buttonInit = new System.Windows.Forms.Button();
			this.buttonStart = new System.Windows.Forms.Button();
			this.buttonInfo = new System.Windows.Forms.Button();
			this.buttonStop = new System.Windows.Forms.Button();
			this.buttonSave = new System.Windows.Forms.Button();
			this.buttonExit = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.comboBoxRange = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.labelCount = new System.Windows.Forms.Label();
			this.labelStatus = new System.Windows.Forms.Label();
			this.textBoxInfo = new System.Windows.Forms.TextBox();
			this.listBoxData = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// 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 = 0;
			this.label1.Text = "foCX";
			// 
			// 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 = 1;
			this.textBoxDevice.Text = "DIG000";
			// 
			// buttonInit
			// 
			this.buttonInit.Location = new System.Drawing.Point(8, 64);
			this.buttonInit.Name = "buttonInit";
			this.buttonInit.TabIndex = 2;
			this.buttonInit.Text = "";
			this.buttonInit.Click += new System.EventHandler(this.buttonInit_Click);
			// 
			// buttonStart
			// 
			this.buttonStart.Location = new System.Drawing.Point(8, 96);
			this.buttonStart.Name = "buttonStart";
			this.buttonStart.TabIndex = 3;
			this.buttonStart.Text = "WJn";
			this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
			// 
			// buttonInfo
			// 
			this.buttonInfo.Location = new System.Drawing.Point(8, 128);
			this.buttonInfo.Name = "buttonInfo";
			this.buttonInfo.TabIndex = 4;
			this.buttonInfo.Text = "Ԏ擾";
			this.buttonInfo.Click += new System.EventHandler(this.buttonInfo_Click);
			// 
			// buttonStop
			// 
			this.buttonStop.Location = new System.Drawing.Point(8, 160);
			this.buttonStop.Name = "buttonStop";
			this.buttonStop.TabIndex = 5;
			this.buttonStop.Text = "W~";
			this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
			// 
			// buttonSave
			// 
			this.buttonSave.Location = new System.Drawing.Point(8, 192);
			this.buttonSave.Name = "buttonSave";
			this.buttonSave.TabIndex = 6;
			this.buttonSave.Text = "t@Cۑ";
			this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(8, 224);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.TabIndex = 7;
			this.buttonExit.Text = "I";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// 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 = 8;
			this.label2.Text = "W";
			// 
			// 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 = 9;
			this.comboBoxRange.SelectedIndexChanged += new System.EventHandler(this.comboBoxRange_SelectedIndexChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(96, 48);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 11;
			this.label3.Text = "JEg";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(176, 48);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 16);
			this.label4.TabIndex = 12;
			this.label4.Text = "d";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 272);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 16);
			this.label5.TabIndex = 13;
			this.label5.Text = "TvO";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(160, 272);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(64, 16);
			this.label6.TabIndex = 14;
			this.label6.Text = "Xe[^X";
			// 
			// labelCount
			// 
			this.labelCount.Location = new System.Drawing.Point(88, 272);
			this.labelCount.Name = "labelCount";
			this.labelCount.Size = new System.Drawing.Size(72, 16);
			this.labelCount.TabIndex = 15;
			this.labelCount.Text = "0";
			// 
			// labelStatus
			// 
			this.labelStatus.Location = new System.Drawing.Point(216, 272);
			this.labelStatus.Name = "labelStatus";
			this.labelStatus.Size = new System.Drawing.Size(72, 16);
			this.labelStatus.TabIndex = 16;
			this.labelStatus.Text = "0H";
			// 
			// textBoxInfo
			// 
			this.textBoxInfo.Location = new System.Drawing.Point(8, 288);
			this.textBoxInfo.Multiline = true;
			this.textBoxInfo.Name = "textBoxInfo";
			this.textBoxInfo.ReadOnly = true;
			this.textBoxInfo.Size = new System.Drawing.Size(280, 40);
			this.textBoxInfo.TabIndex = 17;
			this.textBoxInfo.Text = "";
			// 
			// listBoxData
			// 
			this.listBoxData.ItemHeight = 12;
			this.listBoxData.Location = new System.Drawing.Point(96, 64);
			this.listBoxData.Name = "listBoxData";
			this.listBoxData.Size = new System.Drawing.Size(192, 196);
			this.listBoxData.TabIndex = 18;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(296, 334);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listBoxData,
																		  this.textBoxInfo,
																		  this.labelStatus,
																		  this.labelCount,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.comboBoxRange,
																		  this.label2,
																		  this.buttonExit,
																		  this.buttonSave,
																		  this.buttonStop,
																		  this.buttonInfo,
																		  this.buttonStart,
																		  this.buttonInit,
																		  this.textBoxDevice,
																		  this.label1});
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "f[^K[";
			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;
			listBoxData.Items.Clear();

			//---------------------------------
			// 샂[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ݒF1000Tv
			//---------------------------------
			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, 0);
			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.SetPreTriggerType = " + 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;

			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();
				//---------------------------------
				// 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();
			//---------------------------------
			// Wf[^擾
			//---------------------------------
			int TriggerPoint;			// |XggK̐擪f[^ʒu
			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;
				}
				//---------------------------------
				// 擾f[^\
				//---------------------------------
				for(int i = 0; i < m_Samples; i++)
				{
					listBoxData.Items.Add(String.Format("{0:D}\t{1:F3}", i+1, m_Data[i])); 
				}
			}
		}

		/// <summary>
		/// Ԏ擾
		/// </summary>
		private void buttonInfo_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l
			int		Status;		// Xe[^X
			int		Samples;	// 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();
			//---------------------------------
			// 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>
		/// f[^ۑ
		/// </summary>
		private void buttonSave_Click(object sender, System.EventArgs e)
		{
			//---------------------------------
			// Wf[^݂Ȃ΃G[Ԃ܂B
			//---------------------------------
			if(m_Samples == 0)
			{
				textBoxInfo.Text = "Wf[^݂܂B";
				return;
			}
			//---------------------------------
			// t@Cۑ
			//---------------------------------
			string FileName;				// t@C
			FileName = "DataLogger.csv";	// t@C̐ݒ
			// t@C쐬܂B
			// R[hShift-JISŃt@C㏑
			System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false, System.Text.Encoding.GetEncoding(932));
			try
			{
				for(int i = 0; i < m_Samples; i++)
				{
					// t@CɃf[^݂܂B	
					sw.WriteLine(String.Format("{0:F3}", m_Data[i]));
				}
			}
			finally
			{
				// t@C܂B
				sw.Close();
			}
			textBoxInfo.Text = "t@Cۑ : 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);
		}
	}
}
