using RAYLASE.SPICE3.ClientLib;
using RAYLASE.SPICE3.TraceBuffer;
using System;
using System.Collections.Generic;

namespace TraceBuffer
{
	class Program
	{
		// insert the IP of your SP-ICE-3
		readonly string _CardIp = "169.254.0.42";
		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.markAndTrace( 0 );

			p._Client.Disconnect();
		}

		void markAndTrace( int listId )
		{
			using ( TraceBufferClient tbClient = new TraceBufferClient( _Client, false ) )
			{
				// Goal is to acquire all trace events occuring between two label events.
				const int startLabel = 10;
				const int endLabel = 11;

				RAYLASE.SPICE3.TraceBufferConfig tbConfig = new RAYLASE.SPICE3.TraceBufferConfig();
				// Trace all changes of the gate output
				tbConfig.IOEvents.Gate = true;
				// Trace all x and y positions sent to head 0 by the SP-ICE-3 card
				tbConfig.PositionEvents.Head0TxX = true;
				tbConfig.PositionEvents.Head0TxY = true;
				// Enable tracing of label events. A label event will occur if a label that is appended to the list gets executed
				tbConfig.ControlEvents.Label = true;

				tbClient.SetConfig( tbConfig );

				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 );
				// Add a TraceLabel to the list that will trigger a 'TraceEventType.Label' event when executed by the card.
				// The event will contain the ID '10'
				list.AppendTraceLabel( startLabel );
				list.AppendJumpAbs( -10000, -10000 );
				list.AppendMarkAbs( 10000, -10000 );
				list.AppendMarkAbs( 10000, 10000 );
				list.AppendMarkAbs( -10000, 10000 );
				list.AppendMarkAbs( -10000, -10000 );
				// Add a TraceLabel to the list that will trigger a 'TraceEventType.Label' event when executed by the card.
				// The event will contain the ID '11'
				list.AppendTraceLabel( endLabel );

				_Client.List.Set( listId, list );

				// Start receiving trace events from the card
				tbClient.Start();
				_Client.List.Execute( listId );
				int? doneId;
				_Client.List.WaitForListDone( out doneId );

				// Reading back all events that occured between startLabel and endLabel
				List<TraceEvent> events = tbClient.Acquire( startLabel, endLabel );

				// Convert all position events inside the list to a signed position since they are received as 20 bit unsigned values
				TraceBufferClient.ToSignedPosition( events );
				// Stop receiving events, no more events will be generated by the card until Start() is called again
				tbClient.Stop();

				foreach ( TraceEvent e in events )
					Console.WriteLine( e );
			}
		}
	}
}
