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

namespace FlowControl
{
	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 );

			// Read pin levels of port C (check the polarity in the SP-ICE-3 configuration)
			UInt32 pins = p._Client.Gpio.Read( IOPort.PortC );

			// convert to bool, true if signal at pin i
			bool[] pc = new bool[16];
			for ( int i = 0; i < 16; i++ )
			{
				pc[i] = ( pins & 1 << i ) != 0 ? true : false;
			}

			// control your function flow depending on the input signals
			if ( pc[0] )
				p.markSquare();
			if ( pc[1] )
				p.markCircle();
			if ( pc[2] )
				p.markTriangle();
		}

		void markSquare()
		{
			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.AppendScalingFactor( 2, 2 );

			list.AppendJumpAbs( -5000, -5000 );
			list.AppendMarkAbs( 5000, -5000 );
			list.AppendMarkAbs( 5000, 5000 );
			list.AppendMarkAbs( -5000, 5000 );
			list.AppendMarkAbs( -5000, -5000 );
			list.AppendLaserOff();

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

			int? id;
			_Client.List.WaitForListDone( out id );
		}
		void markTriangle()
		{
			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 50 );
			list.AppendLmWidth( 35 );
			list.AppendScalingFactor( 2, 2 );

			list.AppendJumpAbs( -5000, -5000 );
			list.AppendMarkAbs( 5000, -5000 );
			list.AppendMarkAbs( 0, 5000 );
			list.AppendMarkAbs( -5000, -5000 );

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

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

		void markCircle()
		{
			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 50 );
			list.AppendLmWidth( 35 );

			list.AppendJumpAbs( 0, -10000 );
			list.AppendArcRel( 0, 10000, 2 * Math.PI );

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

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