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

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

		public short	m_Id;								// foCXID
		public float[]	m_Data = new float[MAX_SAMPLES];	// 擾f[^̔z
		public int		m_Records;							// 擾R[h
		public float	m_Range;							// ͓dW
		public int		m_RecordLength;						// R[h
		public short	m_TriggerChannel;					// |XggK`l
		public short	m_Channels;							// L`l
		public Bitmap	bmp;								// rbg}bv

		private System.Windows.Forms.TextBox textBoxInfo;
		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.TextBox textBoxDeviceInfo;
		private System.Windows.Forms.PictureBox Graph;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label labelStatus;
		private System.Windows.Forms.Button buttonLoad;
		/// <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.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.textBoxDeviceInfo = new System.Windows.Forms.TextBox();
			this.Graph = new System.Windows.Forms.PictureBox();
			this.label2 = new System.Windows.Forms.Label();
			this.labelStatus = new System.Windows.Forms.Label();
			this.buttonLoad = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBoxInfo
			// 
			this.textBoxInfo.Location = new System.Drawing.Point(8, 256);
			this.textBoxInfo.Multiline = true;
			this.textBoxInfo.Name = "textBoxInfo";
			this.textBoxInfo.ReadOnly = true;
			this.textBoxInfo.Size = new System.Drawing.Size(192, 64);
			this.textBoxInfo.TabIndex = 39;
			this.textBoxInfo.Text = "";
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(8, 224);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.TabIndex = 38;
			this.buttonExit.Text = "I";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// buttonStop
			// 
			this.buttonStop.Location = new System.Drawing.Point(8, 192);
			this.buttonStop.Name = "buttonStop";
			this.buttonStop.TabIndex = 37;
			this.buttonStop.Text = "W~";
			this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
			// 
			// buttonInfo
			// 
			this.buttonInfo.Location = new System.Drawing.Point(8, 160);
			this.buttonInfo.Name = "buttonInfo";
			this.buttonInfo.TabIndex = 36;
			this.buttonInfo.Text = "擾";
			this.buttonInfo.Click += new System.EventHandler(this.buttonInfo_Click);
			// 
			// buttonStart
			// 
			this.buttonStart.Location = new System.Drawing.Point(8, 128);
			this.buttonStart.Name = "buttonStart";
			this.buttonStart.TabIndex = 35;
			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 = 34;
			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 = 33;
			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 = 32;
			this.label1.Text = "foCX";
			// 
			// textBoxDeviceInfo
			// 
			this.textBoxDeviceInfo.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.textBoxDeviceInfo.Location = new System.Drawing.Point(200, 256);
			this.textBoxDeviceInfo.Multiline = true;
			this.textBoxDeviceInfo.Name = "textBoxDeviceInfo";
			this.textBoxDeviceInfo.ReadOnly = true;
			this.textBoxDeviceInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBoxDeviceInfo.Size = new System.Drawing.Size(192, 64);
			this.textBoxDeviceInfo.TabIndex = 40;
			this.textBoxDeviceInfo.Text = "";
			// 
			// Graph
			// 
			this.Graph.BackColor = System.Drawing.Color.White;
			this.Graph.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.Graph.Location = new System.Drawing.Point(88, 8);
			this.Graph.Name = "Graph";
			this.Graph.Size = new System.Drawing.Size(304, 208);
			this.Graph.TabIndex = 41;
			this.Graph.TabStop = false;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(208, 232);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 42;
			this.label2.Text = "Xe[^X";
			// 
			// labelStatus
			// 
			this.labelStatus.Location = new System.Drawing.Point(272, 232);
			this.labelStatus.Name = "labelStatus";
			this.labelStatus.Size = new System.Drawing.Size(112, 16);
			this.labelStatus.TabIndex = 43;
			this.labelStatus.Text = "0H";
			// 
			// buttonLoad
			// 
			this.buttonLoad.Location = new System.Drawing.Point(8, 80);
			this.buttonLoad.Name = "buttonLoad";
			this.buttonLoad.Size = new System.Drawing.Size(75, 40);
			this.buttonLoad.TabIndex = 44;
			this.buttonLoad.Text = "ݒt@Cǂݍ";
			this.buttonLoad.Click += new System.EventHandler(this.buttonLoad_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(400, 326);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.buttonLoad,
																		  this.labelStatus,
																		  this.label2,
																		  this.Graph,
																		  this.textBoxDeviceInfo,
																		  this.textBoxInfo,
																		  this.buttonExit,
																		  this.buttonStop,
																		  this.buttonInfo,
																		  this.buttonStart,
																		  this.buttonInit,
																		  this.textBoxDevice,
																		  this.label1});
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "ݒt@C̓ǂݍ";
			this.Closed += new System.EventHandler(this.Form1_Closed);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void Form1_Load(object sender, EventArgs e)
		{
			bmp = new Bitmap(Graph.Size.Width, Graph.Size.Height);	// OtƓTCỸrbg}bv𐶐
		}

		/// <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>
		/// ݒt@Cǂݍ
		/// </summary>
		private void buttonLoad_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l
			string	FilePath;	// t@CpX

			//----------------------------------
			// t@CI[v_CAO\܂B
			//----------------------------------
			OpenFileDialog LoadDialog = new OpenFileDialog();

			// [t@C̎] {bNXɕ\Iݒ肷
			LoadDialog.Filter = "xml files (*.xml)|*.xml";
			if(LoadDialog.ShowDialog() == DialogResult.OK)
			{
				FilePath = LoadDialog.FileName;		// Iꂽt@CpX̎擾
				Ret = dig.LoadConfig(m_Id, FilePath);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.Start = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				textBoxInfo.Text = "ݒt@Cǂݍ݁FI";
			}
		}

		/// <summary>
		/// WJn
		/// </summary>
		private void buttonStart_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l

			//---------------------------------
			// ϐ
			//---------------------------------
			m_Records = 0;

			//---------------------------------
			// ZbgAbv擾/\
			//---------------------------------
			buttonInfo_Click(buttonInfo, new System.EventArgs());

			//---------------------------------
			// 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[ꂽR[h擾
				//---------------------------------
				Ret = dig.GetDriverMemoryRecords(m_Id, out m_Records);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.GetDriverMemoryRecords = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				//---------------------------------
				// hCoɃR[h݂΁AVf[^擾
				//---------------------------------
				if(m_Records > 0 || m_Records == -1)
				{
					Ret = dig.ReadLatestRecords(m_Id, Cdig.DIG_RECORD_DATA, 1, ref m_Data, 0, 0);
					if(Ret != 0)
					{
						string ErrorString;
						dig.GetErrorString(Ret, out ErrorString);
						textBoxInfo.Text = "dig.ReadLatestRecordss = " + Ret.ToString() + " : " + ErrorString;
						return;
					}
					//------------------------------------
					// Ot̕`
					//------------------------------------
					DrawGraph();
				}
				//---------------------------------
				// 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[Ă郌R[h擾
			//---------------------------------
			Ret = dig.GetDriverMemoryRecords(m_Id, out m_Records);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetDriverMemoryRecords = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// hCoɃR[h݂΁AVf[^擾
			//---------------------------------
			if(m_Records > 0)
			{
				Ret = dig.ReadLatestRecords(m_Id, Cdig.DIG_RECORD_DATA, 1, ref m_Data, 0, 0);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo.Text = "dig.ReadLatestRecordss = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				//------------------------------------
				// Ot̕`
				//------------------------------------
				DrawGraph();
			}
		}

		/// <summary>
		/// 擾
		/// </summary>
		private void buttonInfo_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// ߂l
			float	SampleRate;	// Tv[g
			float	Duty;		// f[eB
			float	Level;		// gKx
			short	Slope;		// gK
			int		Count;		// gK
			int		Status;		// Xe[^X
			string	Str = "";

			//---------------------------------
			// 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";

			//-------------------------------------------
			// DigLoadConfig֐ŎIɐݒ肳ɂĎ擾܂B
			//-------------------------------------------
			//---------------------------------
			// Tv[g̎擾
			//---------------------------------
			Ret = dig.GetSampleRate(m_Id, out SampleRate);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetSampleRate = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			Str = String.Format("Tv[gF{0:F3}Hz\r\n", SampleRate);
			//---------------------------------
			// R[h̎擾
			//---------------------------------
			Ret = dig.GetRecordLength(m_Id, out m_RecordLength);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetRecordLength = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			Str = Str + String.Format("R[hF{0:D}\r\n", m_RecordLength);
			//---------------------------------
			// f[eB̎擾
			//---------------------------------
			Ret = dig.GetDutyCycle(m_Id, out Duty);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetRecordLength = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			Str = Str + String.Format("f[eBF{0:F3}%\r\n", Duty);
			//---------------------------------
			// Lȃ`l̎擾
			//---------------------------------
			Ret = dig.GetChannels(m_Id, out m_Channels);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetChannels = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			Str = Str + String.Format("L`lF{0:D}\r\n", m_Channels);
			//---------------------------------
			// gK̎擾
			//---------------------------------
			Ret = dig.GetPostTriggerAiEdge(m_Id, out m_TriggerChannel, out Level, out Slope, out Count);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetPostTriggerAiEdge = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			Str = Str + String.Format("`lF{0:D}\r\n", m_TriggerChannel);
			Str = Str + String.Format("gKxF{0:F3}V\r\n", Level);
			//---------------------------------
			// W̎擾
			//---------------------------------
			Ret = dig.GetRange(m_Id, m_TriggerChannel, out m_Range);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.GetRange = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			Str = Str + String.Format("̓WF{0:F3}VPP\r\n", m_Range);
			//---------------------------------
			// \
			//---------------------------------
			textBoxDeviceInfo.Text = Str;

			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);
		}

		/// <summary>
		/// Ot`
		/// </summary>
		private void DrawGraph()
		{
			float XUnit;	// X`C^[o
			float PosX;		// X`ʒu
			float PosY;		// Y`ʒu
			float OldX;		// XWޔp
			float OldY;		// YWޔp
			Pen pen;		// `py

			// ݂̓dOtɕ`悵܂B
			Graph.Image = bmp;
			Graphics grp = Graphics.FromImage(Graph.Image);					// rbg}bv`ΏۃIuWFNg𐶐
			grp.Clear(Graph.BackColor);										// OtNA

			//----------------------------------------------------
			// L`lA`揈JԂ܂B
			//----------------------------------------------------
			for(int i = 0; i < m_Channels; i++)
			{
				//----------------------------------------------------
				// Wf[^A`揈JԂ܂B
				//----------------------------------------------------
				if(i == 0)
				{
					pen = new Pen(System.Drawing.Color.Red, 1);		// ԐFy̍쐬
				}
				else
				{
					pen = new Pen(System.Drawing.Color.Blue, 1);		// Fy̍쐬
				}
				XUnit = Graph.Size.Width / (float)(m_RecordLength);				// 1f[^̉ړsNZ
				OldY = Graph.Height * ((m_Range / 2) - m_Data[i]) / m_Range;	// Y̏lvZ
				// Y̍W`̈𒴂ꍇ
				if(OldY >= Graph.Height)
				{
					OldY = Graph.Height - 1;			// `ʒȕC
				}
					// Y̍W`̈ꍇ
				else if(OldY < 0)
				{
					OldY = 0;							// `ʒȕC
				}
				OldX = 0;
				for(int j = i ; j < m_RecordLength * m_Channels; j += m_Channels)
				{
					PosX = OldX + XUnit;	// X̍WvZ
					PosY = Graph.Height * ((m_Range / 2) - m_Data[j]) / m_Range;	// Y̍WvZ
					// Y̍W`̈𒴂ꍇ
					if(PosY >= Graph.Height)
					{
						PosY = Graph.Height - 1;		// `ʒȕC
					}
						// Y̍W`̈ꍇ
					else if(PosY < 0)
					{
						PosY = 0;								// `ʒȕC
					}
					grp.DrawLine(pen, OldX, OldY, PosX, PosY);	// `

					OldX = PosX;			// X̍W`pɕێ
					OldY = PosY;			// Y̍W`pɕێ
				}
				pen.Dispose();	// \[X̉
			}
			grp.Dispose();	// \[X̉
		}

	}
}
