// AXIS CHANNELS
#define X 0
#define Y 1
#define Z 2
#define MPG_INPUT_AXIS 5
#define SPINDLE 7

// OUTPUTS
#define SPDLENA		144		// Kanalog Opto OUT 0
#define SPDLRST		145		// Kanalog Opto OUT 1
#define STEPENA		146		// Kanalog Opto OUT 2
#define TOWERGRN	1024	// Konnect OUT 0
#define TOWERAMB	1025	// Konnect OUT 1
#define TOWERRED	1026	// Konnect OUT 2

#define CSLED		1030	// Konnect OUT
#define FHLED		1031	// Konnect OUT
#define COOLANT		1034	// Konnect OUT 10 (COR - 81)
#define CRSLINR		1035	// Konnect OUT 11 (CIR - 82)
#define CRSLDWNR	1036	// Konnect OUT 12 (CDR - 83)
#define DRBR		1037	// Konnect OUT 13 (DBR - 84)
#define CRSLRUN		1038	// Konnect OUT 14 (CPR - 85)
#define CRSLREV		1039	// Konnect OUT 15 (CCR - 86)

//ADC
#define ADCSSO		1
#define ADCFRO		0

// INPUTS
#define ESTOP 		136		// Kanalog Opto In 0 - low when in e-stop
#define XHOME 		140		// Kanalog Opto In 4 - high when proximity switch activated
#define YHOME 		141		// Kanalog Opto In 5 - high when proximity switch activated
#define ZHOME 		142		// Kanalog Opto In 6 - high when proximity switch activated
#define SPDLOK		143		// Kanalog Opto In 7 - high when ready
#define CRSLIDX		1056	// Konnect IN 0 - Low when on flat
#define XFAULT		1057	// Konnect IN 1 - High when not in fault
#define YFAULT		1058	// Konnect IN 2 - High when not in fault
#define ZFAULT		1059	// Konnect IN 3 - High when not in fault
#define FHBUTTON	1060
#define CSBUTTON	1061

#define SELECTX 	1064
#define SELECTY 	1065
#define SELECTZ 	1066
#define SELECTA 	1067
#define FACTOR1 	1068
#define FACTOR10 	1069
#define FACTOR100 	1070
#define FACTOR200 	1071

#define CRSLIN		1080	// Konnect IN 24 (71) - High when in
#define	CRSLOUT		1081	// Konnect IN 25 (72) - High when out
#define CRSLDWN		1083	// Konnect IN 27 (74) - High when down
#define CRSLUP		1082	// Konnect IN 26 (73) - High when up
#define LUBELOW		1084	// Konnect IN 28 (76)
#define GUARDOPEN	1085	// Konnect IN 29 (77)


// VIRTUAL BITS
#define SYSTEMOK 48			// Monitoring bit, should be high if everything is ok
#define TCINITOK 49			// used so we know the TC is initialised
//#define INIT 50				// Used during power up to give time for TC/Modbus to establish comms
#define XHOMED 51			// High when X axis homed
#define YHOMED 52			// High when Y axis homed
#define ZHOMED 53			// High when Z axis homed
#define INHOMING 54			// used to notify when we're in homing
//#define JOGALLOWED 54		// Lets us know jogging/homing is allowed
#define ESTOPHANDLED 55		// used so we only get EStop messages displayed once
#define MPGENABLED 56		// High when MPG enabled

// UserData (persist)
//#define TCPOS 5				// Tool Changer Actual position ()
//#define TCDES 6				// Tool Changer Desired position (as commanded by KMCNC)
#define LAST_TOOL_VAR 7   	// Tool changer Last tool position is saved globally in this Var
#define TOOL_STATE_VAR 8  	// Tool changer state is saved globally in this Var
#define TOOL_VAR 9        	// Tool changer desired new tool Var
#define CRSL_POS 12			// Tool changer carousel position
#define TMP 20				// which spare persist to use to transfer data (between KFLop and KMotionCNC)

// Spindle
#define SPDLKMVAR 97	    // Global persist used to transfer Speed from KMotionCNC to KFlop
#define SPDLSTATE 98		// global persistant variable to store latest state (-1=CCW,0=off,1=CW)
#define SPDLSPEED 99		// global persistant variable to store latest speed
#define SPDLFACTOR (166.6666666)// Spindle factor (Counts revolution/seconds)
#define TO_SPDLRST 2		// Seconds to allow for spindle drive reset before timing out

// Tool Changer
#define TCZHEIGHT -41865	// Height Z needs to be at for tool change

// Soft limits
#define LIMITXPOS 5300
#define LIMITXNEG -471000
#define LIMITYPOS 7000
#define LIMITYNEG -278000
#define LIMITZPOS 8500
#define LIMITZNEG -1e+09

//Enum stuff to make life easier
enum estops {ESTRIGGD, ESSPDLTC, ESSPDLDBR, ESSPDLFAIL, ESXFAIL, ESYFAIL, ESZFAIL, KONFAIL}; // E Stop options
enum {T_IDLE,T_PAUSED,T_FAILED,T_MOVEZ,T_CRSLIN,T_DBUNCLAMP,T_CRSLDOWN,T_CRSLROTATE,T_CRSLRUN,T_CRSLUP,T_DBCLAMP,T_CRSLOUT,T_END, T_ESTOP}; // possible TC states
enum {T_CW, T_CCW};		// used by TC to store required carousel rotation
enum {T_IDX_HIGH, T_IDX_LOW};	// used by TC during carousel rotation
