#include "KMotionDef.h"
#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

int DoPC(int cmd);
int DoPCInt(int cmd, int i);
#define GATH_OFF 0  // define the offset into the Gather buffer where strings are passed


main()
{
	FILE *fp;
	char s[16];
	double krf[8],pdl[8],cht[8],igh[8],arv[8],csp[8];
	int result,i,n;
	
	// read them 
	fp=fopen("..\\POWERMAX1650.txt","rt"); // put the file to the root directory
	if (!fp)
	{
		//printf("Unable to open file\n");
		sprintf(s,"Unable to open file",2);
		// Put it onto the Screen
		DROLabel(1000, 168, s);
		return;
	}
	
	// read a all 8 lines and convert 6 doubles
	for(i=0;i<8;i++){
		result=fscanf(fp,"%lf,%lf,%lf,%lf,%lf,%lf",&krf[i],&pdl[i],&cht[i],&igh[i],&arv[i],&csp[i]);
		//printf("krf=%f,pdl=%f,cht=%f,igh=%f,arv=%f,csp=%f\n",krf[i],pdl[i],cht[i],igh[i],arv[i],csp[i]);
	}
	fclose(fp);
	
	printf("P = %f Q = %f R = %f\n",*(float *)&persist.UserData[0],*(float *)&persist.UserData[1],*(float *)&persist.UserData[2]);
	n =	*(float *)&persist.UserData[0];	//Get the paragraph number
	printf("Paragraph = %d\n",n);
	
	// The desired line number 0 = 2 mm
	// The desired line number 1 = 4 mm	
	// The desired line number 2 = 6 mm
	// The desired line number 3 = 10 mm	
	// The desired line number 4 = 16 mm
	// The desired line number 5 = 20 mm
	// The desired line number 6 = 25 mm
	// The desired line number 7 = 30 mm

	*(float *)&persist.UserData[0] = krf[n];	// The Kerf in mm
	*(float *)&persist.UserData[1] = pdl[n];	// The Pierce Delay time in sec
	*(float *)&persist.UserData[2] = cht[n];	// The Cutting Height in mm
	*(float *)&persist.UserData[3] = igh[n];	// The Ignition Height in mm
	*(float *)&persist.UserData[4] = arv[n];	// The Arc Voltage in Volts
	*(float *)&persist.UserData[5] = csp[n];	// The Cutting Speed in m/min
	
	sprintf(s,"F%3.1f\n",csp[n]*1000);			// Insert the F speed command.
	MDI(s);
	
	printf("%f,%f,%f,%f,%f,%f\n",*(float *)&persist.UserData[0],*(float *)&persist.UserData[1],*(float *)&persist.UserData[2],
			*(float *)&persist.UserData[3],*(float *)&persist.UserData[4],*(float *)&persist.UserData[5]);

	// Now compute and form result
	
	switch(n){	
		case 0:		
			sprintf(s,"100A %2.1f mm",6.4);
			DROLabel(1000, 168, s);
			break;
		case 1:		
			sprintf(s,"100A %2.1f mm",9.5);
			DROLabel(1000, 168, s);
			break;
		case 2:		
			sprintf(s,"100A %2.1f mm",12.7);
			DROLabel(1000, 168, s);
			break;
		case 3:
			sprintf(s,"100A %2.1f mm",15.9);
			DROLabel(1000, 168, s);
			break;
		case 4:
			sprintf(s,"100A %2.1f mm",25.4);
			DROLabel(1000, 168, s);
			break;
		case 5:
			sprintf(s,"100A %2.1f mm",35.8);
			DROLabel(1000, 168, s);
			break;
		default:
			sprintf(s,"100A %2.1f mm",999.0);
			DROLabel(1000, 168, s);
			break;
		}

}

