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

namespace DataLogger
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		Cdig  dig = new Cdig();
		public const int	MAX_SAMPLES = 1000;				// The definition of the maximum number of acquiring samples at one time : 1000

		public short	m_Id;								// Device ID
		public float[]	m_Data = new float[MAX_SAMPLES];	// Array of retrieving data
		public int		m_Samples;							// Number of retrieved samples

		private System.Windows.Forms.ListBox listBoxData;
		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 label5;
		private System.Windows.Forms.Label label4;
		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 buttonSave;
		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;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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.listBoxData = new System.Windows.Forms.ListBox();
			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.label5 = new System.Windows.Forms.Label();
			this.label4 = 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.buttonSave = 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.SuspendLayout();
			// 
			// listBoxData
			// 
			this.listBoxData.ItemHeight = 12;
			this.listBoxData.Location = new System.Drawing.Point(128, 70);
			this.listBoxData.Name = "listBoxData";
			this.listBoxData.Size = new System.Drawing.Size(192, 220);
			this.listBoxData.TabIndex = 8;
			// 
			// textBoxInfo
			// 
			this.textBoxInfo.Location = new System.Drawing.Point(8, 312);
			this.textBoxInfo.Multiline = true;
			this.textBoxInfo.Name = "textBoxInfo";
			this.textBoxInfo.ReadOnly = true;
			this.textBoxInfo.Size = new System.Drawing.Size(312, 47);
			this.textBoxInfo.TabIndex = 9;
			this.textBoxInfo.Text = "";
			// 
			// labelStatus
			// 
			this.labelStatus.Location = new System.Drawing.Point(248, 296);
			this.labelStatus.Name = "labelStatus";
			this.labelStatus.Size = new System.Drawing.Size(72, 16);
			this.labelStatus.TabIndex = 34;
			this.labelStatus.Text = "0H";
			// 
			// labelCount
			// 
			this.labelCount.Location = new System.Drawing.Point(128, 296);
			this.labelCount.Name = "labelCount";
			this.labelCount.Size = new System.Drawing.Size(40, 16);
			this.labelCount.TabIndex = 33;
			this.labelCount.Text = "0";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(200, 296);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(40, 16);
			this.label6.TabIndex = 32;
			this.label6.Text = "Status";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 296);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(112, 16);
			this.label5.TabIndex = 31;
			this.label5.Text = "Number of samplings";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(208, 52);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 11);
			this.label4.TabIndex = 30;
			this.label4.Text = "Voltage";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(128, 52);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 11);
			this.label3.TabIndex = 29;
			this.label3.Text = "Count";
			// 
			// comboBoxRange
			// 
			this.comboBoxRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxRange.Location = new System.Drawing.Point(128, 24);
			this.comboBoxRange.Name = "comboBoxRange";
			this.comboBoxRange.Size = new System.Drawing.Size(192, 20);
			this.comboBoxRange.TabIndex = 1;
			this.comboBoxRange.SelectedIndexChanged += new System.EventHandler(this.comboBoxRange_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(128, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 11);
			this.label2.TabIndex = 27;
			this.label2.Text = "Range";
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(8, 244);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.Size = new System.Drawing.Size(112, 24);
			this.buttonExit.TabIndex = 7;
			this.buttonExit.Text = "Exit";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// buttonSave
			// 
			this.buttonSave.Location = new System.Drawing.Point(8, 209);
			this.buttonSave.Name = "buttonSave";
			this.buttonSave.Size = new System.Drawing.Size(112, 24);
			this.buttonSave.TabIndex = 6;
			this.buttonSave.Text = "Save File";
			this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
			// 
			// buttonStop
			// 
			this.buttonStop.Location = new System.Drawing.Point(8, 174);
			this.buttonStop.Name = "buttonStop";
			this.buttonStop.Size = new System.Drawing.Size(112, 24);
			this.buttonStop.TabIndex = 5;
			this.buttonStop.Text = "Stop Acquisition";
			this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
			// 
			// buttonInfo
			// 
			this.buttonInfo.Location = new System.Drawing.Point(8, 139);
			this.buttonInfo.Name = "buttonInfo";
			this.buttonInfo.Size = new System.Drawing.Size(112, 24);
			this.buttonInfo.TabIndex = 4;
			this.buttonInfo.Text = "Get Status";
			this.buttonInfo.Click += new System.EventHandler(this.buttonInfo_Click);
			// 
			// buttonStart
			// 
			this.buttonStart.Location = new System.Drawing.Point(8, 104);
			this.buttonStart.Name = "buttonStart";
			this.buttonStart.Size = new System.Drawing.Size(112, 24);
			this.buttonStart.TabIndex = 3;
			this.buttonStart.Text = "Start Acquisition";
			this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
			// 
			// buttonInit
			// 
			this.buttonInit.Location = new System.Drawing.Point(8, 69);
			this.buttonInit.Name = "buttonInit";
			this.buttonInit.Size = new System.Drawing.Size(112, 24);
			this.buttonInit.TabIndex = 2;
			this.buttonInit.Text = "Init";
			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 = 0;
			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(72, 11);
			this.label1.TabIndex = 19;
			this.label1.Text = "Device Name";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(328, 366);
			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 = "DataLogger";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.Closed += new System.EventHandler(this.Form1_Closed);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		/// <summary>
		/// Dialog init
		/// </summary>
		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Initialize input range
			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>
		/// Init handling
		/// </summary>
		private void buttonInit_Click(object sender, System.EventArgs e)
		{
			int		Ret;
			string	ErrorString;

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

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

			textBoxInfo.Text = "Init : Normality completion";		
		}

		/// <summary>
		/// Set input range
		/// </summary>
		private void comboBoxRange_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int		Index;		// The selected index value of input range combobox
			float	Range;		// Input range
			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;
			}

			//--------------------------
			// Get the input range from combobox
			//--------------------------
			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 = "Set input range : Normality completion";		
		}

		/// <summary>
		/// Start acquisition
		/// </summary>
		private void buttonStart_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// Return value
			//---------------------------------
			// Initialize variable
			//---------------------------------
			m_Samples = 0;
			listBoxData.Items.Clear();

			//---------------------------------
			// Set operation mode : Data logger mode
			//---------------------------------
			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;
			}
			//---------------------------------
			// Set sampling clock source : Crystal oscillator of board 
			//---------------------------------
			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;
			}
			//---------------------------------
			// Set sampling clock : 1000usec  (set value * 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;
			}
			//---------------------------------
			// Set the number of acquiring records : 1
			//---------------------------------
			Ret = dig.SetRecordNumber(m_Id, 1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetRecordNumber = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of pre-trigger samples : 1000
			//---------------------------------
			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;
			}
			//---------------------------------
			// Set the number of post-trigger samples : 0
			//---------------------------------
			Ret = dig.SetPostSampleTimes(m_Id, 0);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo.Text = "dig.SetPostSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set channel : Enable channel0 only
			//---------------------------------
			// Enable channel0
			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;
			}
			// Disable channel1
			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;
			}
			//---------------------------------
			// Set the type of pre-trigger : Auto
			//---------------------------------
			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;
			}

			//---------------------------------
			// Operation start
			//---------------------------------
			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 = "Start acquisition : Normality completion";		

			//---------------------------------
			// Polling until the operation status is changed to be OFF
			//---------------------------------
			int Status;

			do
			{
				//---------------------------------
				// In order to enable the dialog operation
				//---------------------------------
				System.Windows.Forms.Application.DoEvents();
				//---------------------------------
				// Get the number of samples stored in driver memory
				//---------------------------------
				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();
				//---------------------------------
				// Get the operation status
				//---------------------------------
				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);
			//---------------------------------
			// After operation stop, get the number of samples which are being stored in driver memory
			//---------------------------------
			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();
			//---------------------------------
			// Retrieve the acquisition data
			//---------------------------------
			int TriggerPoint;			// Position of the first data after the establishment of the post-trigger
			if(m_Samples > 0)
			{
				// Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
				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;
				}
				//---------------------------------
				// Display the retrieved data
				//---------------------------------
				for(int i = 0; i < m_Samples; i++)
				{
					listBoxData.Items.Add(String.Format("{0:D}\t{1:F3}", i+1, m_Data[i])); 
				}
			}
		}

		/// <summary>
		/// Get status
		/// </summary>
		private void buttonInfo_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// Return value
			int		Status;		// Status
			int		Samples;	// Number of retrieved samples
			//---------------------------------
			// Get the number of samples stored in driver memory
			//---------------------------------
			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();
			//---------------------------------
			// Get the operation status
			//---------------------------------
			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 = "Get status : Normality completion";		
		}

		/// <summary>
		/// Stop acquisition
		/// </summary>
		private void buttonStop_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// Return value
			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 = "Stop acquisition : Normality completion";		
		}

		/// <summary>
		/// Save file
		/// </summary>
		private void buttonSave_Click(object sender, System.EventArgs e)
		{
			//---------------------------------
			// When there is no data error is returned
			//---------------------------------
			if(m_Samples == 0)
			{
				textBoxInfo.Text = "There are no acquisition data";
				return;
			}
			//---------------------------------
			// Save file
			//---------------------------------
			string FileName;				// File name
			FileName = "DataLogger.csv";	// File Path
			// Create file
			// Write file with Shift-JIS
			System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false, System.Text.Encoding.GetEncoding(932));
			try
			{
				for(int i = 0; i < m_Samples; i++)
				{
					// Write file
					sw.WriteLine(String.Format("{0:F3}", m_Data[i]));
				}
			}
			finally
			{
				// Close file
				sw.Close();
			}
			textBoxInfo.Text = "Save file : Normality completion";
		}

		/// <summary>
		/// Exit handling
		/// </summary>
		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			int		Ret;	// Return value
			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 = "Exit : Normality completion";		
		}

		/// <summary>
		/// Close Form
		/// </summary>
		private void Form1_Closed(object sender, System.EventArgs e)
		{
			// Exit handling
			dig.Exit(m_Id);		
		}
	}
}
