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

namespace demo_first
{
	/// <summary>
	/// Form1 的摘要描述。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
	
		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.button1 = new System.Windows.Forms.Button();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(176, 128);
			this.button1.Size = new System.Drawing.Size(232, 64);
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.Controls.Add(this.button1);
			this.Text = "Form1";

		}
		#endregion

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


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

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			//====='Step 1 Driver init
			if  (!Driver_Open)
			{
				i8094MF.i8094MF_REGISTRATION(cardNo,1);
				Driver_Open = true;
			}
			//====='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
			//======'Step 3 Check ERROR
			ushort KK=0;
			KK=i8094MF.i8094MF_GET_ERROR(cardNo);
			//System.String MSGG;
			if (KK != YES)
			{
				//No ERROR: Step 4 Move X axis
				ushort axis=AXIS_X;  //for AXIS_X it can be to AXIS_XYZU
				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);
					//wait for axis to stop
				}
				long AA=i8094MF.i8094MF_GET_LP(cardNo,axis);    //Get X Now position
			}
			else
			{
				//Please check the ERROR CODE
				//Get X ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_X);
				//Get Y ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_Y);
				//Get Z ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_Z);
				//Get U ERROR CODE
				KK=i8094MF.i8094MF_GET_ERROR_CODE(cardNo, AXIS_U);
				//====================================
			}
		}
	}
}
