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

namespace StopWatch
{
	/// <summary>
	/// Summary description for StopWatch.
	/// </summary>
	public class StopWatch : System.Windows.Forms.Form
	{
		Ctimerw timerw = new Ctimerw();
		short	CountId;

		private System.Windows.Forms.Button Button_Exit;
		private System.Windows.Forms.Button Button_TimExit;
		private System.Windows.Forms.Button Button_CountStop;
		private System.Windows.Forms.Button Button_CountReset;
		private System.Windows.Forms.Button Button_CountLap;
		private System.Windows.Forms.Button Button_CountStart;
		private System.Windows.Forms.Button Button_TimInit;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.TextBox Text_Ret;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label Label_Time;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox Text_DeviceName;
		private System.Windows.Forms.Label Label_ProductName;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public StopWatch()
		{
			//
			// 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.Button_Exit = new System.Windows.Forms.Button();
			this.Button_TimExit = new System.Windows.Forms.Button();
			this.Button_CountStop = new System.Windows.Forms.Button();
			this.Button_CountReset = new System.Windows.Forms.Button();
			this.Button_CountLap = new System.Windows.Forms.Button();
			this.Button_CountStart = new System.Windows.Forms.Button();
			this.Button_TimInit = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.Text_Ret = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.Label_Time = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.Text_DeviceName = new System.Windows.Forms.TextBox();
			this.Label_ProductName = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox3.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// Button_Exit
			// 
			this.Button_Exit.Location = new System.Drawing.Point(272, 193);
			this.Button_Exit.Name = "Button_Exit";
			this.Button_Exit.Size = new System.Drawing.Size(96, 23);
			this.Button_Exit.TabIndex = 19;
			this.Button_Exit.Text = "Exit";
			this.Button_Exit.Click += new System.EventHandler(this.Button_Exit_Click);
			// 
			// Button_TimExit
			// 
			this.Button_TimExit.Location = new System.Drawing.Point(272, 163);
			this.Button_TimExit.Name = "Button_TimExit";
			this.Button_TimExit.Size = new System.Drawing.Size(96, 23);
			this.Button_TimExit.TabIndex = 18;
			this.Button_TimExit.Text = "TimExit";
			this.Button_TimExit.Click += new System.EventHandler(this.Button_TimExit_Click);
			// 
			// Button_CountStop
			// 
			this.Button_CountStop.Location = new System.Drawing.Point(272, 132);
			this.Button_CountStop.Name = "Button_CountStop";
			this.Button_CountStop.Size = new System.Drawing.Size(96, 24);
			this.Button_CountStop.TabIndex = 17;
			this.Button_CountStop.Text = "CountStop";
			this.Button_CountStop.Click += new System.EventHandler(this.Button_CountStop_Click);
			// 
			// Button_CountReset
			// 
			this.Button_CountReset.Location = new System.Drawing.Point(272, 102);
			this.Button_CountReset.Name = "Button_CountReset";
			this.Button_CountReset.Size = new System.Drawing.Size(96, 23);
			this.Button_CountReset.TabIndex = 16;
			this.Button_CountReset.Text = "CountReset";
			this.Button_CountReset.Click += new System.EventHandler(this.Button_CountReset_Click);
			// 
			// Button_CountLap
			// 
			this.Button_CountLap.Location = new System.Drawing.Point(272, 72);
			this.Button_CountLap.Name = "Button_CountLap";
			this.Button_CountLap.Size = new System.Drawing.Size(96, 23);
			this.Button_CountLap.TabIndex = 15;
			this.Button_CountLap.Text = "CountLap";
			this.Button_CountLap.Click += new System.EventHandler(this.Button_CountLap_Click);
			// 
			// Button_CountStart
			// 
			this.Button_CountStart.Location = new System.Drawing.Point(272, 42);
			this.Button_CountStart.Name = "Button_CountStart";
			this.Button_CountStart.Size = new System.Drawing.Size(96, 23);
			this.Button_CountStart.TabIndex = 14;
			this.Button_CountStart.Text = "CountStart";
			this.Button_CountStart.Click += new System.EventHandler(this.Button_CountStart_Click);
			// 
			// Button_TimInit
			// 
			this.Button_TimInit.Location = new System.Drawing.Point(272, 11);
			this.Button_TimInit.Name = "Button_TimInit";
			this.Button_TimInit.Size = new System.Drawing.Size(96, 24);
			this.Button_TimInit.TabIndex = 13;
			this.Button_TimInit.Text = "TimInit";
			this.Button_TimInit.Click += new System.EventHandler(this.Button_TimInit_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Text_Ret});
			this.groupBox3.Location = new System.Drawing.Point(8, 224);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(360, 52);
			this.groupBox3.TabIndex = 12;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Ret";
			// 
			// Text_Ret
			// 
			this.Text_Ret.Location = new System.Drawing.Point(8, 19);
			this.Text_Ret.Name = "Text_Ret";
			this.Text_Ret.Size = new System.Drawing.Size(320, 20);
			this.Text_Ret.TabIndex = 0;
			this.Text_Ret.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label5,
																					this.Label_Time});
			this.groupBox2.Location = new System.Drawing.Point(8, 88);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(256, 130);
			this.groupBox2.TabIndex = 11;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Elapsed Time";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(184, 56);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(32, 18);
			this.label5.TabIndex = 2;
			this.label5.Text = "[us]";
			// 
			// Label_Time
			// 
			this.Label_Time.Location = new System.Drawing.Point(16, 56);
			this.Label_Time.Name = "Label_Time";
			this.Label_Time.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.Label_Time.Size = new System.Drawing.Size(160, 15);
			this.Label_Time.TabIndex = 0;
			this.Label_Time.Text = "0";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Text_DeviceName,
																					this.Label_ProductName,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(256, 69);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Initialization";
			// 
			// Text_DeviceName
			// 
			this.Text_DeviceName.Location = new System.Drawing.Point(93, 17);
			this.Text_DeviceName.Name = "Text_DeviceName";
			this.Text_DeviceName.Size = new System.Drawing.Size(131, 20);
			this.Text_DeviceName.TabIndex = 3;
			this.Text_DeviceName.Text = "";
			// 
			// Label_ProductName
			// 
			this.Label_ProductName.Location = new System.Drawing.Point(93, 48);
			this.Label_ProductName.Name = "Label_ProductName";
			this.Label_ProductName.Size = new System.Drawing.Size(155, 12);
			this.Label_ProductName.TabIndex = 2;
			this.Label_ProductName.Text = "Not Initialization";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(83, 14);
			this.label2.TabIndex = 1;
			this.label2.Text = "Product Name:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 22);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 19);
			this.label1.TabIndex = 0;
			this.label1.Text = "Device Name:";
			// 
			// StopWatch
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(370, 286);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Button_Exit,
																		  this.Button_TimExit,
																		  this.Button_CountStop,
																		  this.Button_CountReset,
																		  this.Button_CountLap,
																		  this.Button_CountStart,
																		  this.Button_TimInit,
																		  this.groupBox3,
																		  this.groupBox2,
																		  this.groupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "StopWatch";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Stop Watch";
			this.groupBox3.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		//==============================================================================
		// TimInit
		//==============================================================================
		private void Button_TimInit_Click(object sender, System.EventArgs e)
		{
			string	DeviceName;
			string	ProductName;
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// Get Device Name
			//-----------------------------------
			DeviceName = Text_DeviceName.Text;
			//-----------------------------------
			// Initialization
			//-----------------------------------
			ret = timerw.Init(ref DeviceName, out ProductName);
			//-----------------------------------
			// Error handle
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimInit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS)
			{
				Text_DeviceName.Text = DeviceName;
				Label_ProductName.Text = ProductName;
			}
		}

		//==============================================================================
		// Start count
		//==============================================================================
		private void Button_CountStart_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// Start count
			//-----------------------------------
			ret = timerw.CountStart(out CountId);
			//-----------------------------------
			// Error handle
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountStart ret = " + System.Convert.ToString(ret) + " " + ErrorString;
		}

		//==============================================================================
		// Get elapsed time
		//==============================================================================
		private void Button_CountLap_Click(object sender, System.EventArgs e)
		{
			uint	Microseconds;
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// Get elapsed time
			//-----------------------------------
			ret = timerw.CountLap(CountId, out Microseconds);
			//-----------------------------------
			// Error handle
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountLap ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS)
			{
				Label_Time.Text = System.Convert.ToString(Microseconds);
			}
		}

		//==============================================================================
		// Reset count
		//==============================================================================
		private void Button_CountReset_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// Reset count
			//-----------------------------------
			ret = timerw.CountReset(CountId);
			//-----------------------------------
			// Error handle
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountReset ret = " + System.Convert.ToString(ret) + " " + ErrorString;
		}

		//==============================================================================
		// Stop count
		//==============================================================================
		private void Button_CountStop_Click(object sender, System.EventArgs e)
		{
			uint	Microseconds;
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// Stop count
			//-----------------------------------
			ret = timerw.CountStop(CountId, out Microseconds);
			//-----------------------------------
			// Error handle
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimCountStop ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS)
			{
				Label_Time.Text = System.Convert.ToString(Microseconds);
			}
		}

		//==============================================================================
		// TimExit
		//==============================================================================
		private void Button_TimExit_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// Exit handle
			//-----------------------------------
			ret = timerw.Exit();
			//-----------------------------------
			// Error handle
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimExit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
		}

		//==============================================================================
		// Exit
		//==============================================================================
		private void Button_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
