#include "stdafx.h"
#include "ClientAPI.h"

// insert the IP of your SP-ICE-3
#define CARD_IP "192.168.2.156"

static rlResult markSquare(rlHandle handle, int listId);
static int printLastError(void);
void listDoneCallback(rlHandle handle, int listId);
rlHandle _Handle;
rlCallbackHandle _CallbackHandle;

int _Counter = 0;


int main()
{
	// establish a connection with the card
	_Handle = rlConnect(CARD_IP, 49374);
	if (_Handle < 0)
		return printLastError();

	rlResult status;

	// 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.)
	if ((status = rlSystemResetToDefaults(_Handle)) != rlSUCCESS)
		goto Cleanup;

	// Set the laser power to 25%
	rlLaserConfig lc;
	if ( ( status = rlLaserGetConfig( _Handle, &lc ) ) != rlSUCCESS ) goto Cleanup;
	lc.PowerScale = 0.25;
	if ( ( status = rlLaserSetConfig( _Handle, &lc ) ) != rlSUCCESS ) goto Cleanup;
	if ( ( status = rlLaserArmLaser( _Handle, true ) ) != rlSUCCESS ) goto Cleanup;

	// register listdone callback
	_CallbackHandle = rlListRegisterListDoneCallback(_Handle, listDoneCallback);

	status = markSquare(_Handle, 0);

Cleanup:

	// print any errors
	if (status != rlSUCCESS)
		printLastError();
	/*
	// disconnect from the card
	printf("Disconnecting from card...\n");
	if (rlDisconnect(_Handle) != rlSUCCESS)
		return printLastError();

	printf("Exiting with status = %d\n", status);
	return status;
	*/
}

/// callback function for the list done event
/// unregister after the 10 itteration
/// restart list
void listDoneCallback(rlHandle handle, int listId)
{
	// unregister callback after 10 itterations
	if (++_Counter == 10)
	{
		rlListUnRegisterListDoneCallback(_CallbackHandle);
		// delete list on the card: this frees the card's memory occupied by the list
		printf("Deleting list: index=%d...\n", listId);
		rlListDelete(_Handle, listId, true);
	}
	else
		markSquare(_Handle, listId);
}

static rlResult markSquare(rlHandle handle, int listId)
{
	rlResult result;

	// get the scanner's current field size
	rlScannerConfig sc;
	if ((result = rlScannerGetConfig(handle, &sc)) != rlSUCCESS) return result;

	double squareSize = 0.4 * sc.FieldSize.X;

	double jumpSpeed = 3.0;
	double markSpeed = 0.2;

	// Create a list locally on the host computer,
	// and fill it with macro-vectors defining a square.
	int listIndex = 0;
	printf("Preparing list: index=%d...\n", listIndex);
	rlCommandListHandle list = rlListAllocate(handle);

	if ((result = rlListAppendPower(list, 65535)) != rlSUCCESS) return result;
	if ((result = rlListAppendJumpSpeed(list, jumpSpeed)) != rlSUCCESS)	return result;
	if ((result = rlListAppendMarkSpeed(list, markSpeed)) != rlSUCCESS)	return result;
	if ((result = rlListAppendJumpAbs2D(list, -squareSize / 2, -squareSize / 2)) != rlSUCCESS)	return result;
	if ((result = rlListAppendMarkRel2D(list, squareSize, 0)) != rlSUCCESS)	return result;
	if ((result = rlListAppendMarkRel2D(list, 0, squareSize)) != rlSUCCESS)	return result;
	if ((result = rlListAppendMarkRel2D(list, -squareSize, 0)) != rlSUCCESS) return result;
	if ((result = rlListAppendMarkRel2D(list, 0, -squareSize)) != rlSUCCESS) return result;

	// close the list causing its contents to be transferred to the card
	printf("Closing list: index=%d...\n", listIndex);
	if ((result = rlListSet(handle, listIndex, list, false, -1)) != rlSUCCESS) return result;

	// run the list, and wait until its execution has completed
	printf("Executing list: index=%d...\n", listIndex);
	if ((result = rlListExecute(handle, listIndex)) != rlSUCCESS) return result;

	// release the local list: this frees the PC's memory occupied by the card
	if ((result = rlListReleaseHandle(list)) != rlSUCCESS) return result;

	return result;
}


// helper function which displays the description of the last error
static int printLastError(void)
{
	char errBuffer[1024] = { 0 };
	rlGetLastError(errBuffer, sizeof(errBuffer) - 1);
	printf("%s", errBuffer);
	return rlERROR;
}
