#include "printControl.h"

// printing variables

void printPrint()
{
	if (vb || vbCSV)
	{
		strcat(printBuff, "\r\n");
		Serial.print(printBuff);
	}
}

void printStartOfRun()
{
	if (vbCSV == 1)
	{
		startMaster = mStatus[MASTER].bits.position;
		startSlave = mStatus[SLAVE].bits.position;
		return;
	}

	strcpy(printBuff, "****** Motor Start ");
	if (motorDir == EXTEND)
		strcat(printBuff, "Extend *****");
	else
		strcat(printBuff, "Retract *****");
	printPrint();

	if (bSyncEnable == 1)
		strcpy(printBuff, "Sync Enabled");
	else
		strcpy(printBuff, "Sync Disabled");
	printPrint();

	if (cycleMax > 0)
	{
		sprintf(printBuff, "*****  Start cycle %d of %d\r\n", (cycleMax - cycleCount), cycleMax);
		printPrint();
	}
	sprintf(printBuff, "Position M:%04u S:%04u",
			mStatus[MASTER].bits.position, mStatus[SLAVE].bits.position);
	printPrint();

	sprintf(printBuff, "Integral %u  Proportional %u  Update Rate %u\r\n",
			parm[P_MOTOR_ADJ_I], parm[P_MOTOR_ADJ_P], parm[P_MOTOR_ADJ_INTERVAL]);
	printPrint();
	sprintf(printBuff, "Master Speed %u", masterSpeed);
	printPrint();

	if (vbSync)
	{
		sprintf(printBuff, "Delt MPOS SPOS  INTG    DxP    +I   +M1   M2    MS   SS   MA   SA");
		printPrint();
	}
}

void printEndOfRun()
{
	if (vbCSV)
	{
		printEndOfRunCSV();
		return;
	}
	sprintf(printBuff, "*** End of Run ");
	if (motorDir == EXTEND)
		strcat(printBuff, "Extend");
	else
		strcat(printBuff, "Retract");
	printPrint();
	if (cycleCount > 0)
	{
		sprintf(printBuff, "Cycle %d", cycleMax - cycleCount);
		printPrint();
	}

	sprintf(printBuff, "Position M:%04u S:%04u",
			mStatus[MASTER].bits.position, mStatus[SLAVE].bits.position);
	printPrint();

	sprintf(printBuff, "Delta Max %d   Min %d", maxDelta, minDelta);
	printPrint();

	sprintf(printBuff, "Speed Master %u Max Slave %u",
			masterSpeed, maxSpeed);
	printPrint();

	sprintf(printBuff, "Max Current M-%d S-%d",
			maxCurrentM, maxCurrentS);
	printPrint();

	sprintf(printBuff, "\r\nRun Time %d.%1d", runTime / 1000, runTime % 1000);
	printPrint();
	if (motorError != MOTOR_ERROR_NONE)
	{
		sprintf(printBuff, "Motor Error:%u", motorError);
		printPrint();
		cycleCount = 0;
	}
	if (vbSync)
		printPosition();
}
// Print run data in a CSV Format
// Cycle count,masterStartPosition, master End, slave start, slave end, min delta, max delta,
//  master speed, max slave speed, master current, slave current, runtime
void printEndOfRunCSV()
{
	char c;
	if (startMaster > mStatus[MASTER].bits.position)
		c = 'R';
	else
		c = 'E';
	sprintf(printBuff, "%u,%c,%u,%u,%u,%u,%d,%d,%u,%u,%u,%u,%d.%1d",
			(cycleMax - cycleCount), c,
			startMaster, mStatus[MASTER].bits.position,
			startSlave, mStatus[SLAVE].bits.position,
			minDelta, maxDelta,
			masterSpeed, maxSpeed,
			maxCurrentM, maxCurrentS,
			runTime / 1000, runTime % 1000);

	printPrint();
}

void printPosition(void)
{
	sprintf(printBuff, "M:%04u  S:%04u D:%04d\r\n",
			mStatus[MASTER].bits.position,
			mStatus[SLAVE].bits.position,
			(mStatus[MASTER].bits.position - mStatus[SLAVE].bits.position));

	printPrint();
}
