#include "KMotionDef.h"

#define TMP 10 // which spare persists to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

#define MAX_JOG_SPEED_X 120.0 // ipm
#define MAX_JOG_SPEED_Y 120.0 // ipm
#define MAX_JOG_SPEED_Z 60.0 // ipm

void UpdateJogSpeeds(void);
void DoLimitJog(double rate, int cmd);

main()
{
	for (;;)  // forever loop
	{
		UpdateJogSpeeds();
		Delay_sec(0.005);
	}
}

//set Rate within Limits
void DoLimitJog(double rate, int cmd)
{
	if (rate < 0.0) rate=0.0;
	if (rate > 1.0) rate=1.0;
	
	DoPCFloat(cmd, rate);
}

void UpdateJogSpeeds(void)
{
	static LastChangeCount=-1;
	int Units, TWORD, HWORD, DWORD;
	double rate;
	
	// don't bother if nothing changed
	if (EditChangeCount == LastChangeCount) return;

	// remember count before updating
	LastChangeCount = EditChangeCount;
	
	// Read double from a KMotionCNC Edit Control
	// Persist Var identifies the Control and contents specifies 
	// where the string data should be placed in the 
	// Gather Buffer as an offset in words
	if (GetEditControlDouble(&rate, 170, 1000)) return;  // exit if value is invalid
	
	GetMiscSettings(&Units, &TWORD, &HWORD, &DWORD); // check Units
	if (Units == CANON_UNITS_MM) rate = rate/25.4; // convert screen value to ipm	
	DoLimitJog(rate/MAX_JOG_SPEED_X, PC_COMM_SET_JOG_OVERRIDE_X);
	DoLimitJog(rate/MAX_JOG_SPEED_Y, PC_COMM_SET_JOG_OVERRIDE_Y);
	DoLimitJog(rate/MAX_JOG_SPEED_Z, PC_COMM_SET_JOG_OVERRIDE_Z);
	
}
