----------------------------------------------------------------------------------
-- Adapted from X_360 Lua by DazTheGas - Last Updated 07/12/16 SDK 3233
-- Updated by MsIvie/Wreckermech - 05/30/2020
--   EStop done in different script so not dependent on Left Trigger being engaged
--
-- NOTE:  Copy and paste text into a LUA Script screen to install
----------------------------------------------------------------------------------


---------------Declares

local mInst = mc.mcGetInstance()
local Xenabled = 0
local YPCState = false
local YNCState = false
local XPCState = false
local XNCState = false
local ZPCState = false
local ZNCState = false
local APCState = false
local ANCState = false
local YPState = false
local YNState = false
local XPState = false
local XNState = false
local ZPState = false
local ZNState = false
local APState = false
local ANState = false
local JogInc = 0.01


-- X360 Init Panel
	X360_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
	X360_Timer = wx.wxTimer(X360_Panel)
	X360_Timer:Start(100)
-- LTH
	LTH_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
	LTH_Timer = wx.wxTimer(LTH_Panel)
-- RTH
	RTH_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
	RTH_Timer = wx.wxTimer(RTH_Panel)
-- DPad
	DPad_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
	DPad_Timer = wx.wxTimer(DPad_Panel)	
	
	
	
---------------Functions

function GetXin(xinput)
	local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
	return mc.mcRegGetValueLong(hreg)
end

---------------Event Timers

X360_Panel:Connect(wx.wxEVT_TIMER, function(event)
		if GetXin("LTR_Val") > 150 and Xenabled == 0 then
			LTH_Timer:Start(100)
			RTH_Timer:Start(100)
			DPad_Timer:Start(100)
			Xenabled = 1
			mc.mcCntlSetLastError(mInst, "X_360 Running")
		elseif GetXin("LTR_Val") < 50 and Xenabled == 1 then
			LTH_Timer:Stop()
			RTH_Timer:Stop()
			DPad_Timer:Stop()
			Xenabled= 0
			mc.mcCntlSetLastError(mInst, "X_360 Stopped")
		end
end)

LTH_Panel:Connect(wx.wxEVT_TIMER, function(event)
	-- Y++ Continuous
    if GetXin("LTH_Y_Val") > 0 and GetXin("LTH_X_Val") == 0 and YPCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_LTH_Y_UP/%s", GetXin("LTH_Y_Val")))
        mc.mcJogSetRate(mInst, mc.Y_AXIS, tonumber(string.format("%s", GetXin("LTH_Y_Val"))))
        mc.mcJogVelocityStart(mInst, mc.Y_AXIS, mc.MC_JOG_POS)
		YPCState = true	
    end  
	if GetXin("LTH_Y_Val") == 0 and YPCState == true then
        mc.mcCntlSetLastError(mInst, "X360_LTH_Y++_CENTER")
        mc.mcJogVelocityStop(mInst, mc.Y_AXIS)
		YPCState = false
    end
		
	-- Y-- Continuous
    if GetXin("LTH_Y_Val") < 0 and GetXin("LTH_X_Val") == 0 and YNCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_LTH_Y_DOWN/%s", GetXin("LTH_Y_Val")))
        mc.mcJogSetRate(mInst, mc.Y_AXIS, tonumber(string.format("%s", -1 * GetXin("LTH_Y_Val"))))
        mc.mcJogVelocityStart(mInst, mc.Y_AXIS, mc.MC_JOG_NEG)
		YNCState = true	
    end  
	if GetXin("LTH_Y_Val") == 0 and YNCState == true then
        mc.mcCntlSetLastError(mInst, "X360_LTH_Y--_CENTER")
        mc.mcJogVelocityStop(mInst, mc.Y_AXIS)
		YNCState = false
    end
	
	-- X++ Continuous
    if GetXin("LTH_X_Val") > 0 and GetXin("LTH_Y_Val") == 0 and XPCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_LTH_X_RIGHT/%s", GetXin("LTH_X_Val")))
        mc.mcJogSetRate(mInst, mc.X_AXIS, tonumber(string.format("%s", GetXin("LTH_X_Val"))))
        mc.mcJogVelocityStart(mInst, mc.X_AXIS, mc.MC_JOG_POS)
		XPCState = true	
    end  
	if GetXin("LTH_X_Val") == 0 and XPCState == true then
        mc.mcCntlSetLastError(mInst, "X360_LTH_X++_CENTER")
        mc.mcJogVelocityStop(mInst, mc.X_AXIS)
		XPCState = false
    end
		
	-- X-- Continuous
    if GetXin("LTH_X_Val") < 0 and GetXin("LTH_Y_Val") == 0 and XNCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_LTH_X_LEFT/%s", GetXin("LTH_X_Val")))
        mc.mcJogSetRate(mInst, mc.X_AXIS, tonumber(string.format("%s", -1 * GetXin("LTH_X_Val"))))
        mc.mcJogVelocityStart(mInst, mc.X_AXIS, mc.MC_JOG_NEG)
		XNCState = true	
    end  
	if GetXin("LTH_X_Val") == 0 and XNCState == true then
        mc.mcCntlSetLastError(mInst, "X360_LTH_X--_CENTER")
        mc.mcJogVelocityStop(mInst, mc.X_AXIS)
		XNCState = false
    end
end)

RTH_Panel:Connect(wx.wxEVT_TIMER, function(event)
	-- Z++ Continuous
    if GetXin("RTH_Y_Val") > 0 and GetXin("RTH_X_Val") == 0 and ZPCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_RTH_Z_UP/%s", GetXin("RTH_Y_Val")))
        mc.mcJogSetRate(mInst, mc.Z_AXIS, tonumber(string.format("%s", GetXin("RTH_Y_Val"))))
        mc.mcJogVelocityStart(mInst, mc.Z_AXIS, mc.MC_JOG_POS)
		ZPCState = true	
    end  
	if GetXin("RTH_Y_Val") == 0 and ZPCState == true then
        mc.mcCntlSetLastError(mInst, "X360_RTH_Z++_CENTER")
        mc.mcJogVelocityStop(mInst, mc.Z_AXIS)
		ZPCState = false
    end
		
	-- Z-- Continuous
    if GetXin("RTH_Y_Val") < 0 and GetXin("RTH_X_Val") == 0 and ZNCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_RTH_Z_DOWN/%s", GetXin("RTH_Y_Val")))
        mc.mcJogSetRate(mInst, mc.Z_AXIS, tonumber(string.format("%s", -1 * GetXin("RTH_Y_Val"))))
        mc.mcJogVelocityStart(mInst, mc.Z_AXIS, mc.MC_JOG_NEG)
		ZNCState = true	
    end  
	if GetXin("RTH_Y_Val") == 0 and ZNCState == true then
        mc.mcCntlSetLastError(mInst, "X360_RTH_Z--_CENTER")
        mc.mcJogVelocityStop(mInst, mc.Z_AXIS)
		ZNCState = false
    end
	
	-- A++ Continuous
    if GetXin("RTH_X_Val") > 0 and GetXin("RTH_Y_Val") == 0 and APCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_RTH_A_RIGHT/%s", GetXin("RTH_X_Val")))
        mc.mcJogSetRate(mInst, mc.A_AXIS, tonumber(string.format("%s", GetXin("RTH_X_Val"))))
        mc.mcJogVelocityStart(mInst, mc.A_AXIS, mc.MC_JOG_POS)
		APCState = true	
    end  
	if GetXin("RTH_X_Val") == 0 and APCState == true then
        mc.mcCntlSetLastError(mInst, "X360_RTH_A++_CENTER")
        mc.mcJogVelocityStop(mInst, mc.A_AXIS)
		APCState = false
    end
		
	-- A-- Continuous
    if GetXin("RTH_X_Val") < 0 and GetXin("RTH_Y_Val") == 0 and ANCState == false then
        mc.mcCntlSetLastError(mInst, string.format("X360_RTH_A_LEFT/%s", GetXin("RTH_X_Val")))
        mc.mcJogSetRate(mInst, mc.A_AXIS, tonumber(string.format("%s", -1 * GetXin("RTH_X_Val"))))
        mc.mcJogVelocityStart(mInst, mc.A_AXIS, mc.MC_JOG_NEG)
		ANCState = true	
    end  
	if GetXin("RTH_X_Val") == 0 and ANCState == true then
        mc.mcCntlSetLastError(mInst, "X360_RTH_A--_CENTER")
        mc.mcJogVelocityStop(mInst, mc.A_AXIS)
		ANCState = false
    end
end)

DPad_Panel:Connect(wx.wxEVT_TIMER, function (event)
		-- Y++ Incremental
		if GetXin("DPad_UP") == 1 and GetXin("Btn_LS") == 0 and YPState == false then
			mc.mcJogIncStart(mInst, mc.Y_AXIS, JogInc)
			YPState = true
		end
		if GetXin("DPad_UP") == 0 and GetXin("Btn_LS") == 0 and YPState == true then
			mc.mcJogIncStop(mInst, mc.Y_AXIS, .1)
			YPState = false
		end 
		-- Y-- Incremental
		if GetXin("DPad_DOWN") == 1 and GetXin("Btn_LS") == 0 and YNState == false then
			mc.mcJogIncStart(mInst, mc.Y_AXIS, -1 * JogInc)
			YNState = true
		end
		if GetXin("DPad_DOWN") == 0 and GetXin("Btn_LS") == 0 and YNState == true then
			mc.mcJogIncStop(mInst, mc.Y_AXIS, .1)
			YNState = false
		end 
		
		-- X++ Incremental
		if GetXin("DPad_RIGHT") == 1 and GetXin("Btn_LS") == 0 and XPState == false then
			mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc)
			XPState = true
		end
		if GetXin("DPad_RIGHT") == 0 and GetXin("Btn_LS") == 0 and XPState == true then
			mc.mcJogIncStop(mInst, mc.X_AXIS, .1)
			XPState = false
		end 
		-- X-- Incremental
		if GetXin("DPad_LEFT") == 1 and GetXin("Btn_LS") == 0 and XNState == false then
			mc.mcJogIncStart(mInst, mc.X_AXIS, -1 * JogInc)
			XNState = true
		end
		if GetXin("DPad_LEFT") == 0 and GetXin("Btn_LS") == 0 and XNState == true then
			mc.mcJogIncStop(mInst, mc.X_AXIS, .1)
			XNState = false
		end 
		
		-- Z++ Incremental
		if GetXin("DPad_UP") == 1 and GetXin("Btn_LS") == 1 and ZPState == false then
			mc.mcJogIncStart(mInst, mc.Z_AXIS, JogInc)
			ZPState = true
		end
		if GetXin("DPad_UP") == 0 and GetXin("Btn_LS") == 1 and ZPState == true then
			mc.mcJogIncStop(mInst, mc.Z_AXIS, .1)
			ZPState = false
		end 
		-- Z-- Incremental
		if GetXin("DPad_DOWN") == 1 and GetXin("Btn_LS") == 1 and ZNState == false then
			mc.mcJogIncStart(mInst, mc.Z_AXIS, -1 * JogInc)
			ZNState = true
		end
		if GetXin("DPad_DOWN") == 0 and GetXin("Btn_LS") == 1 and ZNState == true then
			mc.mcJogIncStop(mInst, mc.Z_AXIS, .1)
			ZNState = false
		end 
		
		
		-- A++ Incremental
		if GetXin("DPad_RIGHT") == 1 and GetXin("Btn_LS") == 1 and APState == false then
			mc.mcJogIncStart(mInst, mc.A_AXIS, JogInc)
			APState = true
		end
		if GetXin("DPad_RIGHT") == 0 and GetXin("Btn_LS") == 1 and APState == true then
			mc.mcJogIncStop(mInst, mc.A_AXIS, .1)
			APState = false
		end 
		-- A-- Incremental
		if GetXin("DPad_LEFT") == 1 and GetXin("Btn_LS") == 1 and ANState == false then
			mc.mcJogIncStart(mInst, mc.A_AXIS, -1 * JogInc)
			ANState = true
		end
		if GetXin("DPad_LEFT") == 0 and GetXin("Btn_LS") == 1 and ANState == true then
			mc.mcJogIncStop(mInst, mc.A_AXIS, .1)
			ANState = false
		end 
end)