using RAYLASE.SPICE3;
using RAYLASE.SPICE3.ClientLib;
using System;

namespace WaitForListDone
{
	class Program
	{
		// insert the IP of your SP-ICE-3
		readonly string _CardIp = "192.168.2.138";
		ClientAPI _Client = new ClientAPI();

		static void Main( string[] args )
		{
			Program p = new Program();

			p._Client.Connect( p._CardIp );
			// Reset the card to its default settings:
			// This restores the following settings to the values that were last saved on the card:
			// - scanner configuration (such as field size, field correction, etc.)
			// - laser configuration (such as gate/LM timing parameters, power target, etc.)
			// - process variables (such as jump speed, settling times, mark speed, etc.)
			p._Client.System.ResetToDefaults();
			p._Client.Laser.ArmLaser( true );
			LaserConfig lc = p._Client.Laser.GetConfig();
			lc.PowerScale = 1.0;
			p._Client.Laser.SetConfig( lc );

			DateTime start = DateTime.Now;

			for ( int i = 0; i < 10; i++ )
			{
				p.markSquare( i );
			}

			DateTime end = DateTime.Now;

			TimeSpan duration = start - end;
			Console.Out.WriteLine( duration );

			//result: 4.092 s
		}

		/// <summary>
		/// by waiting for list done the execution time isn't optimized
		/// as the next list could be prepared while the current list is beeing executed
		/// </summary>
		void markSquare( int iteration )
		{
			ScannerConfig scanner = _Client.Scanner.GetConfig();

			double fieldSize = scanner.FieldSize.X;
			double sizeX = 0.1 * fieldSize;
			double sizeY = 0.05 * fieldSize;

			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 200 );
			list.AppendLmWidth( 200 );
			list.AppendPower( 32000 );

			list.AppendJumpAbs( -10000, 0.4 * fieldSize - iteration * 10000 );
			list.AppendMarkRel( sizeX, -sizeY );
			list.AppendMarkRel( sizeX, sizeY );
			list.AppendMarkRel( -sizeX, sizeY );
			list.AppendMarkRel( -sizeX, -sizeY );

			list.AppendLaserOff();

			_Client.List.Set( 0, list );
			_Client.List.Execute( 0 );

			int? id;
			_Client.List.WaitForListDone( out id );
		}
	}
}
