using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using i8094MF_NET;

namespace demo_Start
{
	/// <summary>
	/// Form1 的摘要描述。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.TextBox TextBox3;
		internal System.Windows.Forms.TextBox TextBox2;
		internal System.Windows.Forms.TextBox TextBox1;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Button Button4;
		internal System.Windows.Forms.Button Button3;
		internal System.Windows.Forms.Button Button2;
		internal System.Windows.Forms.Button Button1;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
	



		public Form1()
		{
			//
			// Windows Form 設計工具支援的必要項
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
			//
		}
		/// <summary>
		/// 清除任何使用中的資源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form 設計工具產生的程式碼
		/// <summary>
		/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
		/// 這個方法的內容。
		/// </summary>
		private void InitializeComponent()
		{
			this.TextBox3 = new System.Windows.Forms.TextBox();
			this.TextBox2 = new System.Windows.Forms.TextBox();
			this.TextBox1 = new System.Windows.Forms.TextBox();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Button4 = new System.Windows.Forms.Button();
			this.Button3 = new System.Windows.Forms.Button();
			this.Button2 = new System.Windows.Forms.Button();
			this.Button1 = new System.Windows.Forms.Button();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			// 
			// TextBox3
			// 
			this.TextBox3.Location = new System.Drawing.Point(40, 280);
			this.TextBox3.Size = new System.Drawing.Size(416, 22);
			this.TextBox3.Text = "";
			// 
			// TextBox2
			// 
			this.TextBox2.Location = new System.Drawing.Point(288, 224);
			this.TextBox2.Size = new System.Drawing.Size(168, 22);
			this.TextBox2.Text = "0";
			// 
			// TextBox1
			// 
			this.TextBox1.Location = new System.Drawing.Point(288, 144);
			this.TextBox1.Size = new System.Drawing.Size(168, 22);
			this.TextBox1.Text = "0";
			// 
			// Label6
			// 
			this.Label6.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular);
			this.Label6.Location = new System.Drawing.Point(120, 224);
			this.Label6.Size = new System.Drawing.Size(152, 24);
			this.Label6.Text = "Now Logic Position";
			// 
			// Label5
			// 
			this.Label5.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular);
			this.Label5.Location = new System.Drawing.Point(120, 152);
			this.Label5.Size = new System.Drawing.Size(160, 24);
			this.Label5.Text = "Start Logic Position";
			// 
			// Button4
			// 
			this.Button4.Location = new System.Drawing.Point(120, 184);
			this.Button4.Size = new System.Drawing.Size(128, 32);
			this.Button4.Text = "Move X axis";
			this.Button4.Click += new System.EventHandler(this.Button4_Click);
			// 
			// Button3
			// 
			this.Button3.Location = new System.Drawing.Point(120, 112);
			this.Button3.Size = new System.Drawing.Size(128, 32);
			this.Button3.Text = "Check ERROR";
			this.Button3.Click += new System.EventHandler(this.Button3_Click);
			// 
			// Button2
			// 
			this.Button2.Location = new System.Drawing.Point(120, 72);
			this.Button2.Size = new System.Drawing.Size(128, 32);
			this.Button2.Text = "Config IO";
			this.Button2.Click += new System.EventHandler(this.Button2_Click);
			// 
			// Button1
			// 
			this.Button1.Location = new System.Drawing.Point(120, 32);
			this.Button1.Size = new System.Drawing.Size(128, 32);
			this.Button1.Text = "Driver init";
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			// 
			// Label4
			// 
			this.Label4.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular);
			this.Label4.Location = new System.Drawing.Point(40, 192);
			this.Label4.Size = new System.Drawing.Size(64, 24);
			this.Label4.Text = "Step 4:";
			// 
			// Label3
			// 
			this.Label3.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular);
			this.Label3.Location = new System.Drawing.Point(40, 112);
			this.Label3.Size = new System.Drawing.Size(64, 24);
			this.Label3.Text = "Step 3:";
			// 
			// Label2
			// 
			this.Label2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular);
			this.Label2.Location = new System.Drawing.Point(40, 72);
			this.Label2.Size = new System.Drawing.Size(64, 24);
			this.Label2.Text = "Step 2:";
			// 
			// Label1
			// 
			this.Label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular);
			this.Label1.Location = new System.Drawing.Point(40, 32);
			this.Label1.Size = new System.Drawing.Size(64, 24);
			this.Label1.Text = "Step 1:";
			// 
			// Form1
			// 
			this.ClientSize = new System.Drawing.Size(498, 375);
			this.Controls.Add(this.TextBox3);
			this.Controls.Add(this.TextBox2);
			this.Controls.Add(this.TextBox1);
			this.Controls.Add(this.Label6);
			this.Controls.Add(this.Label5);
			this.Controls.Add(this.Button4);
			this.Controls.Add(this.Button3);
			this.Controls.Add(this.Button2);
			this.Controls.Add(this.Button1);
			this.Controls.Add(this.Label4);
			this.Controls.Add(this.Label3);
			this.Controls.Add(this.Label2);
			this.Controls.Add(this.Label1);
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// 應用程式的主進入點。
		/// </summary>

		//======================================================
		bool Driver_Open;
		byte cardNo=0;
		// Axis define
		ushort AXIS_X 		=0x1;
		ushort AXIS_Y 		=0x2;
		ushort AXIS_Z 		=0x4;
		ushort AXIS_U 		=0x8;
		ushort AXIS_XY		=0x3;
		ushort AXIS_XZ 		=0x5;
		ushort AXIS_XU 		=0x9;
		ushort AXIS_YZ		=0x6;
		ushort AXIS_YU		=0xa;
		ushort AXIS_ZU		=0xc;
		ushort AXIS_XYZ		=0x7;
		ushort AXIS_XYU		=0xb;
		ushort AXIS_XZU		=0xd;
		ushort AXIS_YZU		=0xe;
		ushort AXIS_XYZU	=0xf;
		
		byte  YES  = 1;
		byte  NO   = 0;
		byte  ON   = 1;
		byte  OFF  = 0;



		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			//====='Step 1 Driver init
			if  (!Driver_Open)
			{
				i8094MF.i8094MF_REGISTRATION(cardNo,1);
				Driver_Open = true;
			}

		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			//====='Step 2 CONFIG IO
			i8094MF.i8094MF_RESET_CARD (cardNo);
			i8094MF.i8094MF_SET_PULSE_MODE (cardNo, AXIS_XYZU, 2);	//set the pulse output mode
			i8094MF.i8094MF_SET_ALARM (cardNo, AXIS_XYZU, 0, 0);	//disable the SERVO ALARM Input
			i8094MF.i8094MF_SET_ENCODER (cardNo, AXIS_XYZU, 0, 0, 0);//set the encoder input type
			i8094MF.i8094MF_SET_MAX_V (cardNo, AXIS_XYZU, 16000);	//set the max speed for XYZU
			i8094MF.i8094MF_EXD_DISABLE (cardNo, AXIS_XYZU);		//set the external input Off
			i8094MF.i8094MF_SET_LP (cardNo, AXIS_XYZU, 0);			//set the Logic position =0
			i8094MF.i8094MF_SET_EP (cardNo, AXIS_XYZU, 0);			//set the Encoger position =0
			i8094MF.i8094MF_SET_A (cardNo, AXIS_XYZU, 1000);		//set the Acc =1000
			i8094MF.i8094MF_SERVO_ON (cardNo, AXIS_XYZU);			//set the Servo_ON to servo motors
		}

		private void Button3_Click(object sender, System.EventArgs e)
		{
			//======'Step 3 Check ERROR
			ushort KK=0;
			KK=i8094MF.i8094MF_GET_ERROR(cardNo);
			System.String MSGG;
			if (KK == YES)
			{
				//Please check the ERROR CODE
				//Get X ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_X);
				MSGG="Xerr=" + KK + "  ";
				//Get Y ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_Y);
				MSGG=MSGG + "Yerr=" + KK + "  ";
				//Get Z ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_Z);
				MSGG=MSGG + "Zerr=" + KK + "  ";
				//Get U ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_U);
				MSGG=MSGG + "Uerr=" + KK + "  ";
				//====================================
			}
			else
				MSGG="No Error";
            TextBox3.Text=MSGG;
		}

		private void Button4_Click(object sender, System.EventArgs e)
		{
			//No ERROR: Step 4 Move X axis
			ushort axis=AXIS_X;  //for AXIS_X it can be to AXIS_XYZU
			TextBox1.Text = i8094MF.i8094MF_GET_LP(cardNo, AXIS_X).ToString();
			i8094MF.i8094MF_SET_MAX_V(cardNo, axis, 20000); 
			i8094MF.i8094MF_NORMAL_SPEED(cardNo, axis, 0);		//set axis as Symmetrical T curve mode
			i8094MF.i8094MF_SET_V(cardNo, axis, 20000);			//set v=10000 PPS
			i8094MF.i8094MF_SET_A(cardNo, axis, 100000);		//set acc=100000 PPS/S
			i8094MF.i8094MF_SET_SV(cardNo, axis, 10);			//set start speed=1000 PPS
			i8094MF.i8094MF_SET_AO(cardNo, axis, 0);			//set offset pulse (at SV speed)= 0 PS
			i8094MF.i8094MF_FIXED_MOVE(cardNo, axis, 10000);	//run the fixed 10000 Pulse move.
			while (i8094MF.i8094MF_STOP_WAIT(cardNo, axis) == NO)
			{
				i8094MF.system.DoEvents();
				System.Threading.Thread.Sleep(1);
				TextBox2.Text = i8094MF.i8094MF_GET_LP(cardNo, AXIS_X).ToString();//Get X Now position
				//wait for axis to stop
			}
			TextBox2.Text = i8094MF.i8094MF_GET_LP(cardNo, AXIS_X).ToString();   //Get X Now position
		}
	}
}
