4.0.1

From TrafficNeXt Wiki
(Redirected from Download)
Jump to: navigation, search
--TrafficNext 4.0.1
--(c) 2016 Cheapie Systems (Carter Kolwey)
--License: https://cheapiesystems.com/media/license

function logitem(item)
	if #mem.log > 99 then
		mem.log = {}
		logitem("Log Overflowed")
	end
	if type(os.datetable) == "function" then
		local date = os.datetable()
		table.insert(mem.log,1,date.month.."/"..date.day.." "..date.hour..":"..date.min.." | "..item)
	else
		table.insert(mem.log,1,item)
	end
	digiline_send("api",{msg="LOGITEM",text=item})
end

function initparams(reset)
	mem.about = {"TrafficNeXt | 4.0.1","Written by cheapie","See source f".."or license info"}
	mem.state = {}
	if reset or mem.params == nil then mem.params = {} end
	if reset or not mem.log then mem.log = {} end

	if mem.params.beacon1 == nil then mem.params.beacon1 = 0 end
	if mem.params.beacon2 == nil then mem.params.beacon2 = 0 end
	if mem.params.pedscramble == nil then mem.params.pedscramble = 0 end
	if mem.params.allred1 == nil then mem.params.allred1 = 2 end
	if mem.params.allred2 == nil then mem.params.allred2 = 2 end
	if mem.params.yellow1 == nil then mem.params.yellow1 = 3 end
	if mem.params.yellow2 == nil then mem.params.yellow2 = 3 end
	if mem.params.sidegreen == nil then mem.params.sidegreen = 10 end
	if mem.params.mingreen == nil then mem.params.mingreen = 15 end
	if mem.params.passcode == nil then mem.params.passcode = 0 end
	if mem.params.phaselock == nil then mem.params.phaselock = true end
	if mem.params.menutimeout == nil then mem.params.menutimeout = 30 end
	if mem.params.crosswalk1 == nil then mem.params.crosswalk1 = false end
	if mem.params.lpi1 == nil then mem.params.lpi1 = 0 end
	if mem.params.crosswalk2 == nil then mem.params.crosswalk2 = false end
	if mem.params.crosstime2 == nil then mem.params.crosstime2 = 5 end
	if mem.params.autoped == nil then mem.params.autoped = false end
	if mem.params.pedwarn == nil then mem.params.pedwarn = 5 end
	if mem.params.permlt == nil then mem.params.permlt = false end
	if mem.params.protlt == nil then mem.params.protlt = false end
	if mem.params.carcountmult == nil then mem.params.carcountmult = 3 end
	mem.params.passcodeentry = 0
	mem.params.currentlogevent = 1
	mem.params.aboutpage = 1
	mem.params.mancycletype = 1

	mem.state.menu = "run"
	mem.menuitem = {}
	mem.state.locked = true
	mem.state.pqmain = false
	mem.state.pqside = false
	mem.state.pqacceptmain = false
	mem.state.pqacceptside = false
	mem.state.multicarextratime = 0
	mem.updatepending = false
	mem.phase = {mode = "Startup",a = "R",b = "R",c = "R", d = "R", at = "R",bt = "R",ct = "R",dt = "R",ap = "R",bp = "R",cp = "R",dp = "R",ab = "FY",bb = "FY",cb = "FY",db = "FY",det = "L"}
	if mem.phaselock == nil or #mem.phaselock ~= 14 then mem.phaselock = {mode = "Phase Locked",a = "FR",b = "FR",c = "FR", d = "FR", at = "FR",bt = "FR",ct = "FR",dt = "FR",ap = "O",bp = "O",cp = "O",dp = "O",ab = "FY",bb = "FY",cb = "FY",db = "FY",det = "X"} end
	mem.overriddenphases = {}
	if reset then
		logitem("Defaults Loaded")
		digiline_send("api",{msg="RESET",paramscleared=true})
	else
		logitem("Firmware Loaded")
		digiline_send("api",{msg="RESET",paramscleared=false})
	end
	interrupt(0,"carphase9")
end

menu = {}
menu.run = {}
menu.main = {}

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return(mem.about[1].." | Running | "..(mem.updatepending and "Updt Pending" or "").." | Logged "..(mem.state.menu == "run" and "Out" or "In")) end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
			if mem.params.passcodeentry == mem.params.passcode then
				mem.state.menu = "main"
				mem.params.passcodeentry = 0
				logitem("Successful Login")
				entermenu()
			else
				logitem("Failed Login")
				mem.params.passcodeentry = 0
				mem.menuitem.run = 1
				updatedisplay()
			end
		end
menuitem.up = function()
			mem.params.passcodeentry = mem.params.passcodeentry+1
			if mem.params.passcodeentry > 99 then mem.params.passcodeentry = 0 end
		end
menuitem.down = function()
			mem.params.passcodeentry = mem.params.passcodeentry-1
			if mem.params.passcodeentry < 0 then mem.params.passcodeentry = 99 end
		end
menuitem.get = function() return("Enter Passcode | To Log In | "..mem.params.passcodeentry) end
table.insert(menu.run,menuitem)

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "run"
			mem.menuitem.run = 1
			mem.params.currentlogevent = 1
			logitem("Logged Out")
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Press Enter | To Log Out") end
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function()
			return("Overview | | "..mem.phase.mode.." | Locked: "..(mem.state.locked and "Yes" or "No"))
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function()
			local state_names = {R = "Red",G = "Green",O = "Off",Y = "Yellow",FR = "FlRed",FY = "FlYel",FG="FlGrn",RY="RedYel"}
			return("Straight | A: "..state_names[mem.phase.a].." | B: "..state_names[mem.phase.b].." | C: "..state_names[mem.phase.c].." | D: "..state_names[mem.phase.d])
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function()
			local state_names = {R = "Red",G = "Green",O = "Off",Y = "Yellow",FR = "FlRed",FY = "FlYel",FG="FlGrn",RY="RedYel"}
			return("Left Turn | A: "..state_names[mem.phase.at].." | B: "..state_names[mem.phase.bt].." | C: "..state_names[mem.phase.ct].." | D: "..state_names[mem.phase.dt])
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function()
			local state_names = {R = "Red",G = "Green",O = "Off",Y = "Yellow",FR = "FlRed",FY = "FlYel",FG="FlGrn",RY="RedYel"}
			return("Pedestrian | A: "..state_names[mem.phase.ap].." | B: "..state_names[mem.phase.bp].." | C: "..state_names[mem.phase.cp].." | D: "..state_names[mem.phase.dp])
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function()
			local state_names = {R = "Red",G = "Green",O = "Off",Y = "Yellow",FR = "FlRed",FY = "FlYel",FG="FlGrn",RY="RedYel"}
			return("Beacon | A: "..state_names[mem.phase.ab].." | B: "..state_names[mem.phase.bb].." | C: "..state_names[mem.phase.cb].." | D: "..state_names[mem.phase.db])
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function()
			local out = "Queues"
			out = out.." | MPed: "
			if mem.params.crosswalk1 then
				if mem.state.pqacceptmain then
					if mem.state.pqmain then
						out = out.."Queued"
					else
						out = out.."Ready"
					end
				else
					out = out.."Locked"
				end
			else
				out = out.."N/P"
			end
			out = out.." | SPed: "
			if mem.params.crosswalk2 then
				if mem.state.pqacceptside then
					if mem.state.pqside then
						out = out.."Queued"
					else
						out = out.."Ready"
					end
				else
					out = out.."Locked"
				end
			else
				out = out.."N/P"
			end
			out = out .. " | Updt: "
			out = out ..(mem.updatepending and "Yes" or "No")
			return(out)
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
		if mem.phase.det ~= "U" then return end
		local cycletypes = {"Car"}
		if mem.params.crosswalk1 then table.insert(cycletypes,"Main Ped") end
		if mem.params.crosswalk2 then table.insert(cycletypes,"Side Ped") end
		if mem.params.crosswalk1 and mem.params.crosswalk2 then table.insert(cycletypes,"Ped Scramble") end
		if mem.params.protlt then
			table.insert(cycletypes,"Left Turn A")
			table.insert(cycletypes,"Left Turn B")
			table.insert(cycletypes,"Left Turn C")
			table.insert(cycletypes,"Left Turn D")
		end
		if cycletypes[mem.params.mancycletype] == "Car" then interrupt(0,"carphase1")
		elseif cycletypes[mem.params.mancycletype] == "Main Ped" then interrupt(0,"combophase1")
		elseif cycletypes[mem.params.mancycletype] == "Side Ped" then interrupt(0,"pedphase1")
		elseif cycletypes[mem.params.mancycletype] == "Ped Scramble" then interrupt(0,"scramblephase1")
		elseif cycletypes[mem.params.mancycletype] == "Left Turn A" then interrupt(0,"at1")
		elseif cycletypes[mem.params.mancycletype] == "Left Turn B" then interrupt(0,"bt1")
		elseif cycletypes[mem.params.mancycletype] == "Left Turn C" then interrupt(0,"ct1")
		elseif cycletypes[mem.params.mancycletype] == "Left Turn D" then interrupt(0,"dt1") end
	end
menuitem.up = function()
		local cycletypes = {"Car"}
		if mem.params.crosswalk1 then table.insert(cycletypes,"Main Ped") end
		if mem.params.crosswalk2 then table.insert(cycletypes,"Side Ped") end
		if mem.params.crosswalk1 and mem.params.crosswalk2 then table.insert(cycletypes,"Ped Scramble") end
		if mem.params.protlt then
			table.insert(cycletypes,"Left Turn A")
			table.insert(cycletypes,"Left Turn B")
			table.insert(cycletypes,"Left Turn C")
			table.insert(cycletypes,"Left Turn D")
		end
		mem.params.mancycletype = mem.params.mancycletype + 1
		if mem.params.mancycletype > #cycletypes then mem.params.mancycletype = 1 end
	end
menuitem.down = function()
		local cycletypes = {"Car"}
		if mem.params.crosswalk1 then table.insert(cycletypes,"Main Ped") end
		if mem.params.crosswalk2 then table.insert(cycletypes,"Side Ped") end
		if mem.params.crosswalk1 and mem.params.crosswalk2 then table.insert(cycletypes,"Ped Scramble") end
		if mem.params.protlt then
			table.insert(cycletypes,"Left Turn A")
			table.insert(cycletypes,"Left Turn B")
			table.insert(cycletypes,"Left Turn C")
			table.insert(cycletypes,"Left Turn D")
		end
		mem.params.mancycletype = mem.params.mancycletype - 1
		if mem.params.mancycletype < 1 then mem.params.mancycletype = #cycletypes end
	end
menuitem.get = function()
			local out = "Manual Cycle | | "
			if mem.phase.det ~= "U" then
				out = out .. "Not Ready"
				return(out)
			end
			local cycletypes = {"Car"}
			if mem.params.crosswalk1 then table.insert(cycletypes,"Main Ped") end
			if mem.params.crosswalk2 then table.insert(cycletypes,"Side Ped") end
			if mem.params.crosswalk1 and mem.params.crosswalk2 then table.insert(cycletypes,"Ped Scramble") end
			if mem.params.protlt then
				table.insert(cycletypes,"Left Turn A")
				table.insert(cycletypes,"Left Turn B")
				table.insert(cycletypes,"Left Turn C")
				table.insert(cycletypes,"Left Turn D")
			end
			if not cycletypes[mem.params.mancycletype] then mem.params.mancycletype = 1 end
			out = out..cycletypes[mem.params.mancycletype].." | Press Enter to start"
			return(out)
		end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.params.aboutpage = mem.params.aboutpage + 1
			if mem.params.aboutpage > #mem.about then mem.params.aboutpage = 1 end
		end
menuitem.down = function()
			mem.params.aboutpage = mem.params.aboutpage - 1
			if mem.params.aboutpage < 1 then mem.params.aboutpage = #mem.about end
		end
menuitem.get = function() return("About: | "..mem.about[mem.params.aboutpage]) end
table.insert(menu.run,menuitem)
table.insert(menu.main,menuitem)

menu.hwconf = {}

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "hwconf"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Hardware Setup Menu") end
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "main"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Return to Main Menu") end
table.insert(menu.hwconf,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.permlt = not mem.editing.permlt
		end
menuitem.down = function()
			mem.editing.permlt = not mem.editing.permlt
		end
menuitem.get = function() return("Perm Left Turn | "..(mem.editing.permlt and "Yes" or "No")) end
table.insert(menu.hwconf,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.protlt = not mem.editing.protlt
		end
menuitem.down = function()
			mem.editing.protlt = not mem.editing.protlt
		end
menuitem.get = function() return("Prot Left Turn | "..(mem.editing.protlt and "Yes" or "No")) end
table.insert(menu.hwconf,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.crosswalk1 = not mem.editing.crosswalk1
			if not mem.editing.crosswalk1 then mem.editing.pedscramble = 0 end
		end
menuitem.down = function()
			mem.editing.crosswalk1 = not mem.editing.crosswalk1
			if not mem.editing.crosswalk1 then mem.editing.pedscramble = 0 end
		end
menuitem.get = function() return("Main Crosswalk Present | "..(mem.editing.crosswalk1 and "Yes" or "No")) end
table.insert(menu.hwconf,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.crosswalk2 = not mem.editing.crosswalk2
			if not mem.editing.crosswalk2 then mem.editing.pedscramble = 0 end
		end
menuitem.down = function()
			mem.editing.crosswalk2 = not mem.editing.crosswalk2
			if not mem.editing.crosswalk2 then mem.editing.pedscramble = 0 end
		end
menuitem.get = function() return("Side Crosswalk Present | "..(mem.editing.crosswalk2 and "Yes" or "No")) end
table.insert(menu.hwconf,menuitem)

menu.timing = {}

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "timing"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Times and Durations Menu") end
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "main"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Return to Main Menu") end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.beacon1 = mem.editing.beacon1+1
			if mem.editing.beacon1 > 30 then mem.editing.beacon1 = 0 end
		end
menuitem.down = function()
			mem.editing.beacon1 = mem.editing.beacon1-1
			if mem.editing.beacon1 < 0 then mem.editing.beacon1= 30 end
		end
menuitem.get = function() return("Main Beacon Time | "..(mem.editing.beacon1 == 0 and "0 (Off)" or mem.editing.beacon1)) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.beacon2 = mem.editing.beacon2+1
			if mem.editing.beacon2 > 30 then mem.editing.beacon2 = 0 end
		end
menuitem.down = function()
			mem.editing.beacon2 = mem.editing.beacon2-1
			if mem.editing.beacon2 < 0 then mem.editing.beacon2= 30 end
		end
menuitem.get = function() return("Side Beacon Time | "..(mem.editing.beacon2 == 0 and "0 (Off)" or mem.editing.beacon2)) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.allred1 = mem.editing.allred1+1
			if mem.editing.allred1 > 15 then mem.editing.allred1 = 1 end
		end
menuitem.down = function()
			mem.editing.allred1 = mem.editing.allred1-1
			if mem.editing.allred1 < 1 then mem.editing.allred1 = 15 end
		end
menuitem.get = function() return("All Red A Time | "..mem.editing.allred1) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.allred2 = mem.editing.allred2+1
			if mem.editing.allred2 > 15 then mem.editing.allred2 = 1 end
		end
menuitem.down = function()
			mem.editing.allred2 = mem.editing.allred2-1
			if mem.editing.allred2 < 1 then mem.editing.allred2 = 15 end
		end
menuitem.get = function() return("All Red B Time | "..mem.editing.allred2) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.yellow1 = mem.editing.yellow1+1
			if mem.editing.yellow1 > 15 then mem.editing.yellow1 = 1 end
		end
menuitem.down = function()
			mem.editing.yellow1 = mem.editing.yellow1-1
			if mem.editing.yellow1 < 1 then mem.editing.yellow1 = 15 end
		end
menuitem.get = function() return("Yellow A Time | "..mem.editing.yellow1) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.yellow2 = mem.editing.yellow2+1
			if mem.editing.yellow2 > 15 then mem.editing.yellow2 = 1 end
		end
menuitem.down = function()
			mem.editing.yellow2 = mem.editing.yellow2-1
			if mem.editing.yellow2 < 1 then mem.editing.yellow2 = 15 end
		end
menuitem.get = function() return("Yellow B Time | "..mem.editing.yellow2) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.sidegreen = mem.editing.sidegreen+1
			if mem.editing.sidegreen > 30 then mem.editing.sidegreen = 1 end
		end
menuitem.down = function()
			mem.editing.sidegreen = mem.editing.sidegreen-1
			if mem.editing.sidegreen < 1 then mem.editing.sidegreen = 30 end
		end
menuitem.get = function() return("Side Green Time | "..mem.editing.sidegreen) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.carcountmult = mem.editing.carcountmult+1
			if mem.editing.carcountmult > 30 then mem.editing.carcountmult = 0 end
		end
menuitem.down = function()
			mem.editing.carcountmult = mem.editing.carcountmult-1
			if mem.editing.carcountmult < 0 then mem.editing.carcountmult = 30 end
		end
menuitem.get = function() return("Extra Green Per Car | "..mem.editing.carcountmult) end
table.insert(menu.timing,menuitem)



menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.lpi1 = mem.editing.lpi1+1
			if mem.editing.lpi1 > 20 then mem.editing.lpi1 = 0 end
		end
menuitem.down = function()
			mem.editing.lpi1 = mem.editing.lpi1-1
			if mem.editing.lpi1 < 0 then mem.editing.lpi1 = 20 end
		end
menuitem.get = function() return("Main LPI Length | "..(mem.editing.lpi1 == 0 and "0 (Off)" or mem.editing.lpi1)) end
table.insert(menu.timing,menuitem)



menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.crosstime2 = mem.editing.crosstime2+1
			if mem.editing.crosstime2 > 40 then mem.editing.crosstime2 = 5 end
		end
menuitem.down = function()
			mem.editing.crosstime2 = mem.editing.crosstime2-1
			if mem.editing.crosstime2 < 5 then mem.editing.crosstime2 = 40 end
		end
menuitem.get = function() return("Side Cross Time | "..mem.editing.crosstime2) end
table.insert(menu.timing,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.pedwarn = mem.editing.pedwarn+1
			if mem.editing.pedwarn > 30 then mem.editing.pedwarn = 1 end
		end
menuitem.down = function()
			mem.editing.pedwarn = mem.editing.pedwarn-1
			if mem.editing.pedwarn < 1 then mem.editing.pedwarn = 30 end
		end
menuitem.get = function() return("Ped Warn Time | "..mem.editing.pedwarn) end
table.insert(menu.timing,menuitem)



menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.mingreen = mem.editing.mingreen+1
			if mem.editing.mingreen > 30 then mem.editing.mingreen = 1 end
		end
menuitem.down = function()
			mem.editing.mingreen = mem.editing.mingreen-1
			if mem.editing.mingreen < 1 then mem.editing.mingreen = 30 end
		end
menuitem.get = function() return("Minimum Main Green Time | "..mem.editing.mingreen) end
table.insert(menu.timing,menuitem)

menu.behavior = {}

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "behavior"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Options Menu") end
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "main"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Return to Main Menu") end
table.insert(menu.behavior,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.pedscramble = mem.editing.pedscramble + 1
			if mem.editing.pedscramble > 2 or (not (mem.editing.crosswalk1 and mem.editing.crosswalk2)) then
				mem.editing.pedscramble = 0
			end
		end
menuitem.down = function()
			mem.editing.pedscramble = mem.editing.pedscramble - 1
			if mem.editing.pedscramble < 0 then
				mem.editing.pedscramble = 2
			end
			if not (mem.editing.crosswalk1 and mem.editing.crosswalk2) then
				mem.editing.pedscramble = 0
			end
		end
menuitem.get = function()
			local scramblemodes = {[0]="Never",[1]="Auto",[2]="Always"}
			return("Ped Scramble | "..((not (mem.editing.crosswalk1 and mem.editing.crosswalk2)) and "Requires Both Crosswalks" or scramblemodes[mem.editing.pedscramble]))
		end
table.insert(menu.behavior,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.autoped = not mem.editing.autoped
		end
menuitem.down = function()
			mem.editing.autoped = not mem.editing.autoped
		end
menuitem.get = function() return("Auto Ped | "..((not mem.editing.crosswalk1) and "Requires Main Crosswalk" or (mem.editing.autoped and "Yes" or "No"))) end
table.insert(menu.behavior,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.menutimeout = mem.editing.menutimeout+10
			if mem.editing.menutimeout > 600 then mem.editing.menutimeout = 0 end
		end
menuitem.down = function()
			mem.editing.menutimeout = mem.editing.menutimeout-10
			if mem.editing.menutimeout < 0 then mem.editing.menutimeout = 600 end
		end
menuitem.get = function() return("Menu Timeout | "..mem.editing.menutimeout) end
table.insert(menu.behavior,menuitem)

menu.phaselock = {}

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "phaselock"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Phase Lock Menu") end
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "main"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Return to Main Menu") end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.phaselock = not mem.editing.phaselock
		end
menuitem.down = function()
			mem.editing.phaselock = not mem.editing.phaselock
		end
menuitem.get = function() return("Enabled | "..(mem.editing.phaselock and "Yes" or "No")) end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.a = phaselockmodes[phaselockmodes_reverse[mem.phaselock.a]+1]
			if phaselockmodes_reverse[mem.phaselock.a] > 8 then mem.phaselock.a = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.a = phaselockmodes[phaselockmodes_reverse[mem.phaselock.a]-1]
			if phaselockmodes_reverse[mem.phaselock.a] < 1 then mem.phaselock.a = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Straight A | "..phaselockmodes[mem.phaselock.a])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.b = phaselockmodes[phaselockmodes_reverse[mem.phaselock.b]+1]
			if phaselockmodes_reverse[mem.phaselock.b] > 8 then mem.phaselock.b = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.b = phaselockmodes[phaselockmodes_reverse[mem.phaselock.b]-1]
			if phaselockmodes_reverse[mem.phaselock.b] < 1 then mem.phaselock.b = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Straight B | "..phaselockmodes[mem.phaselock.b])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.c = phaselockmodes[phaselockmodes_reverse[mem.phaselock.c]+1]
			if phaselockmodes_reverse[mem.phaselock.c] > 8 then mem.phaselock.c = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.c = phaselockmodes[phaselockmodes_reverse[mem.phaselock.c]-1]
			if phaselockmodes_reverse[mem.phaselock.c] < 1 then mem.phaselock.c = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Straight C | "..phaselockmodes[mem.phaselock.c])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.d = phaselockmodes[phaselockmodes_reverse[mem.phaselock.d]+1]
			if phaselockmodes_reverse[mem.phaselock.d] > 8 then mem.phaselock.d = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.d = phaselockmodes[phaselockmodes_reverse[mem.phaselock.d]-1]
			if phaselockmodes_reverse[mem.phaselock.d] < 1 then mem.phaselock.d = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Straight D | "..phaselockmodes[mem.phaselock.d])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.at = phaselockmodes[phaselockmodes_reverse[mem.phaselock.at]+1]
			if phaselockmodes_reverse[mem.phaselock.at] > 8 then mem.phaselock.at = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.at = phaselockmodes[phaselockmodes_reverse[mem.phaselock.at]-1]
			if phaselockmodes_reverse[mem.phaselock.at] < 1 then mem.phaselock.at = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Left Turn A | "..phaselockmodes[mem.phaselock.at])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.bt = phaselockmodes[phaselockmodes_reverse[mem.phaselock.bt]+1]
			if phaselockmodes_reverse[mem.phaselock.bt] > 8 then mem.phaselock.bt = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.bt = phaselockmodes[phaselockmodes_reverse[mem.phaselock.bt]-1]
			if phaselockmodes_reverse[mem.phaselock.bt] < 1 then mem.phaselock.bt = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Left Turn B | "..phaselockmodes[mem.phaselock.bt])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.ct = phaselockmodes[phaselockmodes_reverse[mem.phaselock.ct]+1]
			if phaselockmodes_reverse[mem.phaselock.ct] > 8 then mem.phaselock.ct = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.ct = phaselockmodes[phaselockmodes_reverse[mem.phaselock.ct]-1]
			if phaselockmodes_reverse[mem.phaselock.ct] < 1 then mem.phaselock.ct = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Left Turn C | "..phaselockmodes[mem.phaselock.ct])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.dt = phaselockmodes[phaselockmodes_reverse[mem.phaselock.dt]+1]
			if phaselockmodes_reverse[mem.phaselock.dt] > 8 then mem.phaselock.dt = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.dt = phaselockmodes[phaselockmodes_reverse[mem.phaselock.dt]-1]
			if phaselockmodes_reverse[mem.phaselock.dt] < 1 then mem.phaselock.dt = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Left Turn D | "..phaselockmodes[mem.phaselock.dt])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.ap = phaselockmodes[phaselockmodes_reverse[mem.phaselock.ap]+1]
			if phaselockmodes_reverse[mem.phaselock.ap] > 8 then mem.phaselock.ap = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.ap = phaselockmodes[phaselockmodes_reverse[mem.phaselock.ap]-1]
			if phaselockmodes_reverse[mem.phaselock.ap] < 1 then mem.phaselock.ap = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Pedestrian A | "..phaselockmodes[mem.phaselock.ap])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.bp = phaselockmodes[phaselockmodes_reverse[mem.phaselock.bp]+1]
			if phaselockmodes_reverse[mem.phaselock.bp] > 8 then mem.phaselock.bp = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.bp = phaselockmodes[phaselockmodes_reverse[mem.phaselock.bp]-1]
			if phaselockmodes_reverse[mem.phaselock.bp] < 1 then mem.phaselock.bp = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Pedestrian B | "..phaselockmodes[mem.phaselock.bp])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.cp = phaselockmodes[phaselockmodes_reverse[mem.phaselock.cp]+1]
			if phaselockmodes_reverse[mem.phaselock.cp] > 8 then mem.phaselock.cp = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.cp = phaselockmodes[phaselockmodes_reverse[mem.phaselock.cp]-1]
			if phaselockmodes_reverse[mem.phaselock.cp] < 1 then mem.phaselock.cp = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Pedestrian C | "..phaselockmodes[mem.phaselock.cp])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.dp = phaselockmodes[phaselockmodes_reverse[mem.phaselock.dp]+1]
			if phaselockmodes_reverse[mem.phaselock.dp] > 8 then mem.phaselock.dp = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.dp = phaselockmodes[phaselockmodes_reverse[mem.phaselock.dp]-1]
			if phaselockmodes_reverse[mem.phaselock.dp] < 1 then mem.phaselock.dp = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Pedestrian D | "..phaselockmodes[mem.phaselock.dp])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.ab = phaselockmodes[phaselockmodes_reverse[mem.phaselock.ab]+1]
			if phaselockmodes_reverse[mem.phaselock.ab] > 8 then mem.phaselock.ab = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.ab = phaselockmodes[phaselockmodes_reverse[mem.phaselock.ab]-1]
			if phaselockmodes_reverse[mem.phaselock.ab] < 1 then mem.phaselock.ab = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Beacon A | "..phaselockmodes[mem.phaselock.ab])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.bb = phaselockmodes[phaselockmodes_reverse[mem.phaselock.bb]+1]
			if phaselockmodes_reverse[mem.phaselock.bb] > 8 then mem.phaselock.bb = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.bb = phaselockmodes[phaselockmodes_reverse[mem.phaselock.bb]-1]
			if phaselockmodes_reverse[mem.phaselock.bb] < 1 then mem.phaselock.bb = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Beacon B | "..phaselockmodes[mem.phaselock.bb])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.cb = phaselockmodes[phaselockmodes_reverse[mem.phaselock.cb]+1]
			if phaselockmodes_reverse[mem.phaselock.cb] > 8 then mem.phaselock.cb = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.cb = phaselockmodes[phaselockmodes_reverse[mem.phaselock.cb]-1]
			if phaselockmodes_reverse[mem.phaselock.cb] < 1 then mem.phaselock.cb = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Beacon C | "..phaselockmodes[mem.phaselock.cb])
		end
table.insert(menu.phaselock,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.db = phaselockmodes[phaselockmodes_reverse[mem.phaselock.db]+1]
			if phaselockmodes_reverse[mem.phaselock.db] > 8 then mem.phaselock.db = phaselockmodes[1] end
		end
menuitem.down = function()
			local phaselockmodes = {"O","R","RY","Y","G","FY","FR","FG"}
			local phaselockmodes_reverse = {["O"]=1,["R"]=2,["RY"]=3,["Y"]=4,["G"]=5,["FY"]=6,["FR"]=7,["FG"]=8}
			mem.phaselock.db = phaselockmodes[phaselockmodes_reverse[mem.phaselock.db]-1]
			if phaselockmodes_reverse[mem.phaselock.db] < 1 then mem.phaselock.db = "FG" end
		end
menuitem.get = function()
			local phaselockmodes = {["O"]="Off",["R"]="Red",["RY"]="Red+Yellow",["Y"]="Yellow",["G"]="Green",["FY"]="Flash Yellow",["FR"]="Flash Red",["FG"]="Flash Green"}
			return("Beacon D | "..phaselockmodes[mem.phaselock.db])
		end
table.insert(menu.phaselock,menuitem)

menu.admin = {}

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "admin"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("System Setup Menu") end
table.insert(menu.main,menuitem)

menuitem = {}
menuitem.enter = function()
			mem.state.menu = "main"
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Return to Main Menu") end
table.insert(menu.admin,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.params.currentlogevent = mem.params.currentlogevent+1
			if mem.params.currentlogevent > #mem.log then mem.params.currentlogevent = 1 end
		end
menuitem.down = function()
			mem.params.currentlogevent = mem.params.currentlogevent-1
			if mem.params.currentlogevent < 1 then mem.params.currentlogevent = #mem.log end
		end
menuitem.get = function()
			if not mem.log[1] then
				return("Log Empty")
			else
				return("Log Item "..mem.params.currentlogevent..": | "..mem.log[mem.params.currentlogevent])
			end
		end
table.insert(menu.admin,menuitem)

menuitem = {}
menuitem.enter = function() mem.log = {} end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Clear Log | Press Enter to clear") end
table.insert(menu.admin,menuitem)

menuitem = {}
menuitem.enter = function()
			initparams(true)
			entermenu()
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Press Enter to reset ALL settings to defaults") end
table.insert(menu.admin,menuitem)

menuitem = {}
menuitem.enter = function() end
menuitem.up = function()
			mem.editing.passcode = mem.editing.passcode+1
			if mem.editing.passcode > 99 then mem.editing.passcode = 0 end
		end
menuitem.down = function()
			mem.editing.passcode = mem.editing.passcode-1
			if mem.editing.passcode < 0 then mem.editing.passcode = 99 end
		end
menuitem.get = function() return("Set Passcode | "..mem.editing.passcode) end
table.insert(menu.admin,menuitem)

menuitem = {}
menuitem.enter = function()
			logitem("Saved Changes")
			local wasphaselocked = mem.params.phaselock
			if not mem.state.locked then
				savechanges()
				interrupt(0,"carphase9")
			else
				mem.updatepending = true
			end
			if wasphaselocked then interrupt(0,"carphase9") end
		end
menuitem.up = function() end
menuitem.down = function() end
menuitem.get = function() return("Save Changes") end
table.insert(menu.main,menuitem)

function savechanges()
	mem.params.beacon1 = mem.editing.beacon1
	mem.params.beacon2 = mem.editing.beacon2
	mem.params.pedscramble = mem.editing.pedscramble
	mem.params.allred1 = mem.editing.allred1
	mem.params.allred2 = mem.editing.allred2
	mem.params.yellow1 = mem.editing.yellow1
	mem.params.yellow2 = mem.editing.yellow2
	mem.params.sidegreen = mem.editing.sidegreen
	mem.params.mingreen = mem.editing.mingreen
	mem.params.passcode = mem.editing.passcode
	mem.params.phaselock = mem.editing.phaselock
	mem.params.menutimeout = mem.editing.menutimeout
	mem.params.crosswalk1 = mem.editing.crosswalk1
	mem.params.lpi1 = mem.editing.lpi1
	mem.params.crosswalk2 = mem.editing.crosswalk2
	mem.params.crosstime2 = mem.editing.crosstime2
	mem.params.autoped = mem.editing.autoped
	mem.params.pedwarn = mem.editing.pedwarn
	mem.params.permlt = mem.editing.permlt
	mem.params.protlt = mem.editing.protlt
	mem.params.carcountmult = mem.editing.carcountmult
	mem.updatepending = false
end

function exitmenu()
	mem.state.menu = "run"
	mem.menuitem.run = 1
	mem.menuitem.main = 1
	updatedisplay()
end

function entermenu()
	mem.menuitem.main = 1
	mem.editing = {}
	mem.editing.beacon1 = mem.params.beacon1
	mem.editing.beacon2 = mem.params.beacon2
	mem.editing.pedscramble = mem.params.pedscramble
	mem.editing.allred1 = mem.params.allred1
	mem.editing.allred2 = mem.params.allred2
	mem.editing.yellow1 = mem.params.yellow1
	mem.editing.yellow2 = mem.params.yellow2
	mem.editing.sidegreen = mem.params.sidegreen
	mem.editing.mingreen = mem.params.mingreen
	mem.editing.passcode = mem.params.passcode
	mem.editing.phaselock = mem.params.phaselock
	mem.editing.menutimeout = mem.params.menutimeout
	mem.editing.crosswalk1 = mem.params.crosswalk1
	mem.editing.lpi1 = mem.params.lpi1
	mem.editing.crosswalk2 = mem.params.crosswalk2
	mem.editing.crosstime2 = mem.params.crosstime2
	mem.editing.autoped = mem.params.autoped
	mem.editing.pedwarn = mem.params.pedwarn
	mem.editing.permlt = mem.params.permlt
	mem.editing.protlt = mem.params.protlt
	mem.editing.carcountmult = mem.params.carcountmult
	if mem.params.menutimeout ~= 0 then interrupt(mem.params.menutimeout,"menutimeout") end
	updatedisplay()
end

function updatedisplay()
	if not mem.menuitem[mem.state.menu] then mem.menuitem[mem.state.menu] = 1 end
	local friendlynames = {["run"]="",["main"]="",["hwconf"]="HW Config | ",["timing"]="Timing | ",["behavior"]="Options | ",["phaselock"]="Phase Lock | ",["admin"]="Sys Setup | "}
	digiline_send("lcd",friendlynames[mem.state.menu]..menu[mem.state.menu][mem.menuitem[mem.state.menu]].get())
end


if event.type == "program" then
	initparams(false)
	updatedisplay()
end

if event.type == "interrupt" and event.iid == "menutimeout" and mem.params.menutimeout ~= 0 then
	logitem("Inactivity Timeout")
	exitmenu()
end

if event.type == "digiline" and event.channel == "button" then
	if mem.params.menutimeout ~= 0 then
		interrupt(mem.params.menutimeout,"menutimeout")
	end
	if event.msg == "up" then
		menu[mem.state.menu][mem.menuitem[mem.state.menu]].up()
	elseif event.msg == "down" then
		menu[mem.state.menu][mem.menuitem[mem.state.menu]].down()
	elseif event.msg == "left" then
		mem.menuitem[mem.state.menu] = mem.menuitem[mem.state.menu] - 1
		if mem.menuitem[mem.state.menu] < 1 then
			mem.menuitem[mem.state.menu] = #menu[mem.state.menu]
		end
	elseif event.msg == "right" then
		mem.menuitem[mem.state.menu] = mem.menuitem[mem.state.menu] + 1
		if mem.menuitem[mem.state.menu] > #menu[mem.state.menu] then
			mem.menuitem[mem.state.menu] = 1
		end
	elseif event.msg == "enter" then
		menu[mem.state.menu][mem.menuitem[mem.state.menu]].enter()
	end
	updatedisplay()
end

function emitsignals(phase)
	mem.phase = phase
	local commands = {}
	commands.R = "RED"
	commands.G = "GREEN"
	commands.FR = "FLASHRED"
	commands.FY = "FLASHYELLOW"
	commands.O = "OFF"
	commands.Y = "YELLOW"
	commands.RY = "REDYELLOW"
	commands.FG = "FLASHGREEN"
	digiline_send("a",commands[phase.a])
	digiline_send("b",commands[phase.b])
	digiline_send("c",commands[phase.c])
	digiline_send("d",commands[phase.d])
	if mem.params.permlt or mem.params.protlt then digiline_send("at",commands[phase.at]) end
	if mem.params.permlt or mem.params.protlt then digiline_send("bt",commands[phase.bt]) end
	if mem.params.permlt or mem.params.protlt then digiline_send("ct",commands[phase.ct]) end
	if mem.params.permlt or mem.params.protlt then digiline_send("dt",commands[phase.dt]) end
	if mem.params.crosswalk1 then digiline_send("ap",commands[phase.ap]) end
	if mem.params.crosswalk2 then digiline_send("bp",commands[phase.bp]) end
	if mem.params.crosswalk1 then digiline_send("cp",commands[phase.cp]) end
	if mem.params.crosswalk2 then digiline_send("dp",commands[phase.dp]) end
	digiline_send("ab",commands[phase.ab])
	digiline_send("bb",commands[phase.bb])
	digiline_send("cb",commands[phase.cb])
	digiline_send("db",commands[phase.db])
	mem.state.locked = phase.det == "L"
	updatedisplay()
end
	
if event.type == "digiline" and event.channel == "api" and type(event.msg) == "table" then
	if event.msg.command == "GETPARAMS" then
		digiline_send("api",{msg="PARAMS",params=mem.params})
	elseif event.msg.command == "RESET" then
		reset(event.msg.clearparams)
	elseif event.msg.command == "LOG" then
		logitem(event.msg.text)
	elseif event.msg.command == "SETPARAM" then
		mem.params[event.msg.name] = event.msg.value
	elseif event.msg.command == "INTERRUPT" then
		interrupt(0,event.msg.name)
	elseif event.msg.command == "PHASE" then
		emitsignals(event.msg.phase)
	elseif event.msg.command == "SETBREAK" then
		mem.overriddenphases[event.msg.name] = true
	end
end

if event.type == "interrupt" then
	if mem.overriddenphases[event.iid] then
		digiline_send("api",{msg="BREAK",name=event.iid})
	else
		if not mem.params.phaselock then
			if event.iid == "carphase1" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: Car",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				if mem.state.pqmain then
					mem.state.pqmain = false
					mem.state.pqacceptmain = false
					interrupt(0,"combophase1")
				else
					interrupt(mem.params.beacon1,"carphase2")
				end
			elseif event.iid == "carphase2" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: Car",a="Y",b="R",c="Y",d="R",at=pl and "Y" or "R",bt="R",ct=pl and "Y" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				if mem.state.pqmain then
					mem.state.pqmain = false
					mem.state.pqacceptmain = false
					interrupt(0,"combophase2")
				else
					interrupt(mem.params.yellow1,"carphase3")
				end
			elseif event.iid == "carphase3" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				emitsignals({mode="Run: Car",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				if mem.state.pqmain then
					mem.state.pqmain = false
					mem.state.pqacceptmain = false
					interrupt(0,"combophase3")
				else
					interrupt(mem.params.allred1,"carphase4")
				end
			elseif event.iid == "carphase4" then
				if mem.params.permlt then
					emitsignals({mode="Run: Car",a="R",b="G",c="R",d="G",at="R",bt="FY",ct="R",dt="FY",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="O",cb="FY",db="O",det="L"})
				else
					emitsignals({mode="Run: Car",a="R",b="G",c="R",d="G",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="O",cb="FY",db="O",det="L"})
				end
				if mem.state.pqmain then
					mem.state.pqmain = false
					mem.state.pqacceptmain = false
					interrupt(0,"combophase4")
				else
					interrupt(mem.params.sidegreen + mem.state.multicarextratime,"carphase5")
				end
			elseif event.iid == "carphase5" then
				if mem.params.permlt then
					emitsignals({mode="Run: Car",a="R",b="G",c="R",d="G",at="R",bt="FY",ct="R",dt="FY",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				else
					emitsignals({mode="Run: Car",a="R",b="G",c="R",d="G",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				end
				interrupt(mem.params.beacon2,"carphase6")
			elseif event.iid == "carphase6" then
				if mem.params.permlt then
					emitsignals({mode="Run: Car",a="R",b="Y",c="R",d="Y",at="R",bt="Y",ct="R",dt="Y",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				else
					emitsignals({mode="Run: Car",a="R",b="Y",c="R",d="Y",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				end
				interrupt(mem.params.yellow2,"carphase7")
			elseif event.iid == "carphase7" then
				emitsignals({mode="Run: Car",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred2,"carphase8")
			elseif event.iid == "carphase8" then
				if mem.params.permlt then
					emitsignals({mode="Run: Car",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				else
					emitsignals({mode="Run: Car",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				end
				interrupt(mem.params.mingreen,"carphase9")
			elseif event.iid == "carphase9" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "combophase1" then
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: Both",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon1,"combophase2")
			elseif event.iid == "combophase2" then
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: Both",a="Y",b="R",c="Y",d="R",at=pl and "Y" or "R",bt="R",ct=pl and "Y" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow1,"combophase3")
			elseif event.iid == "combophase3" then
				mem.state.pqacceptside = true
				emitsignals({mode="Run: Both",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				if mem.params.lpi1 == 0 then
					interrupt(mem.params.allred1,"combophase5")
				else
					interrupt(mem.params.allred1,"combophase4")
				end
			elseif event.iid == "combophase4" then
				emitsignals({mode="Run: Both",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="G",bp="R",cp="G",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.lpi1,"combophase5")
			elseif event.iid == "combophase5" then
				if mem.params.permlt then
					emitsignals({mode="Run: Both",a="R",b="G",c="R",d="G",at="R",bt="FY",ct="R",dt="FY",ap="G",bp="R",cp="G",dp="R",ab="FY",bb="O",cb="FY",db="O",det="L"})
				else
					emitsignals({mode="Run: Both",a="R",b="G",c="R",d="G",at="R",bt="R",ct="R",dt="R",ap="G",bp="R",cp="G",dp="R",ab="FY",bb="O",cb="FY",db="O",det="L"})
				end
				interrupt(mem.params.sidegreen + mem.state.multicarextratime,"combophase6")
			elseif event.iid == "combophase6" then
				mem.state.pqacceptmain = true
				if mem.params.permlt then
					emitsignals({mode="Run: Both",a="R",b="G",c="R",d="G",at="R",bt="FY",ct="R",dt="FY",ap="FR",bp="R",cp="FR",dp="R",ab="FY",bb="O",cb="FY",db="O",det="L"})
				else
					emitsignals({mode="Run: Both",a="R",b="G",c="R",d="G",at="R",bt="R",ct="R",dt="R",ap="FR",bp="R",cp="FR",dp="R",ab="FY",bb="O",cb="FY",db="O",det="L"})
				end
				interrupt(mem.params.pedwarn,"combophase7")
			elseif event.iid == "combophase7" then
				mem.state.pqacceptmain = true
				if mem.params.permlt then
					emitsignals({mode="Run: Both",a="R",b="G",c="R",d="G",at="R",bt="FY",ct="R",dt="FY",ap="FR",bp="R",cp="FR",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				else
					emitsignals({mode="Run: Both",a="R",b="G",c="R",d="G",at="R",bt="R",ct="R",dt="R",ap="FR",bp="R",cp="FR",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				end
				interrupt(mem.params.beacon2,"combophase8")
			elseif event.iid == "combophase8" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: Both",a="R",b="Y",c="R",d="Y",at="R",bt=pl and "Y" or "R",ct="R",dt=pl and "Y" or "R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow2,"combophase9")
			elseif event.iid == "combophase9" then
				emitsignals({mode="Run: Both",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred2,"combophase10")
			elseif event.iid == "combophase10" then
				if mem.params.permlt then
					emitsignals({mode="Run: Both",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				else
					emitsignals({mode="Run: Both",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				end
				interrupt(mem.params.mingreen,"combophase11")
			elseif event.iid == "combophase11" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "pedphase1" then
				mem.state.pqacceptmain = true
				if mem.params.permlt then
					emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="Y",bt="R",ct="Y",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
					interrupt(mem.params.yellow1,"pedphase2")
				else
					interrupt(0,"pedphase3")
				end
			elseif event.iid == "pedphase2" then
				emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.allred1,"pedphase3")
			elseif event.iid == "pedphase3" then
				emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="G",cp="R",dp="G",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.crosstime2,"pedphase4")
			elseif event.iid == "pedphase4" then
				mem.state.pqacceptside = true
				emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="FR",cp="R",dp="FR",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.pedwarn,"pedphase5")
			elseif event.iid == "pedphase5" then
				emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.allred2,"pedphase6")
			elseif event.iid == "pedphase6" then
				if mem.params.permlt then
					emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				else
					emitsignals({mode="Run: Ped",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				end
				interrupt(mem.params.mingreen,"pedphase7")
			elseif event.iid == "pedphase7" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "at1" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT A",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon1,"at2")
			elseif event.iid == "at2" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT A",a="G",b="R",c="Y",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "Y" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow1,"at3")
			elseif event.iid == "at3" then
				emitsignals({mode="Run: LT A",a="G",b="R",c="R",d="R",at=mem.params.permlt and "FY" or "R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred1,"at4")
			elseif event.iid == "at4" then
				emitsignals({mode="Run: LT A",a="G",b="R",c="R",d="R",at="G",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.sidegreen,"at5")
			elseif event.iid == "at5" then
				emitsignals({mode="Run: LT A",a="G",b="R",c="R",d="R",at="Y",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow2,"at6")
			elseif event.iid == "at6" then
				emitsignals({mode="Run: LT A",a="G",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred2,"at7")
			elseif event.iid == "at7" then
				emitsignals({mode="Run: LT A",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.mingreen,"at8")
			elseif event.iid == "at8" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "bt1" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT B",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon1,"bt2")
			elseif event.iid == "bt2" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT B",a="Y",b="R",c="Y",d="R",at=pl and "Y" or "R",bt="R",ct=pl and "Y" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow1,"bt3")
			elseif event.iid == "bt3" then
				emitsignals({mode="Run: LT B",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred1,"bt4")
			elseif event.iid == "bt4" then
				emitsignals({mode="Run: LT B",a="R",b="G",c="R",d="R",at="R",bt="G",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="O",cb="FY",db="FY",det="L"})
				interrupt(mem.params.sidegreen,"bt5")
			elseif event.iid == "bt5" then
				emitsignals({mode="Run: LT B",a="R",b="G",c="R",d="R",at="R",bt="G",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon2,"bt6")
			elseif event.iid == "bt6" then
				emitsignals({mode="Run: LT B",a="R",b="Y",c="R",d="R",at="R",bt="Y",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow2,"bt7")
			elseif event.iid == "bt7" then
				emitsignals({mode="Run: LT B",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred2,"bt8")
			elseif event.iid == "bt8" then
				emitsignals({mode="Run: LT B",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.mingreen,"bt9")
			elseif event.iid == "bt9" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "ct1" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT C",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.beacon1,"ct2")
			elseif event.iid == "ct2" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT C",a="Y",b="R",c="G",d="R",at=pl and "Y" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.yellow1,"ct3")
			elseif event.iid == "ct3" then
				emitsignals({mode="Run: LT C",a="R",b="R",c="G",d="R",at="R",bt="R",ct=mem.params.permlt and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.allred1,"ct4")
			elseif event.iid == "ct4" then
				emitsignals({mode="Run: LT C",a="R",b="R",c="G",d="R",at="R",bt="R",ct="G",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.sidegreen,"ct5")
			elseif event.iid == "ct5" then
				emitsignals({mode="Run: LT C",a="R",b="R",c="G",d="R",at="R",bt="R",ct="Y",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.yellow2,"ct6")
			elseif event.iid == "ct6" then
				emitsignals({mode="Run: LT C",a="R",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.allred2,"ct7")
			elseif event.iid == "ct7" then
				emitsignals({mode="Run: LT C",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.mingreen,"ct8")
			elseif event.iid == "ct8" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "dt1" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT D",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon1,"dt2")
			elseif event.iid == "dt2" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT D",a="Y",b="R",c="Y",d="R",at=pl and "Y" or "R",bt="R",ct=pl and "Y" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow1,"dt3")
			elseif event.iid == "dt3" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: LT D",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred1,"dt4")
			elseif event.iid == "dt4" then
				emitsignals({mode="Run: LT D",a="R",b="R",c="R",d="G",at="R",bt="R",ct="R",dt="G",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="O",det="L"})
				interrupt(mem.params.sidegreen,"dt5")
			elseif event.iid == "dt5" then
				emitsignals({mode="Run: LT D",a="R",b="R",c="R",d="G",at="R",bt="R",ct="R",dt="G",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon2,"dt6")
			elseif event.iid == "dt6" then
				emitsignals({mode="Run: LT D",a="R",b="R",c="R",d="Y",at="R",bt="R",ct="R",dt="Y",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow2,"dt7")
			elseif event.iid == "dt7" then
				emitsignals({mode="Run: LT D",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred2,"dt8")
			elseif event.iid == "dt8" then
				emitsignals({mode="Run: LT D",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.mingreen,"dt9")
			elseif event.iid == "dt9" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			elseif event.iid == "scramblephase1" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: Scbl",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.beacon1,"scramblephase2")
			elseif event.iid == "scramblephase2" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: Scbl",a="Y",b="R",c="Y",d="R",at=pl and "Y" or "R",bt="R",ct=pl and "Y" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.yellow1,"scramblephase3")
			elseif event.iid == "scramblephase3" then
				emitsignals({mode="Run: Scbl",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred1,"scramblephase4")
			elseif event.iid == "scramblephase4" then
				emitsignals({mode="Run: Scbl",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="G",bp="G",cp="G",dp="G",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.crosstime2,"scramblephase5")
			elseif event.iid == "scramblephase5" then
				mem.state.pqacceptmain = true
				mem.state.pqacceptside = true
				emitsignals({mode="Run: Scbl",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="FR",bp="FR",cp="FR",dp="FR",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.pedwarn,"scramblephase6")
			elseif event.iid == "scramblephase6" then
				emitsignals({mode="Run: Scbl",a="R",b="R",c="R",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="FY",bb="FY",cb="FY",db="FY",det="L"})
				interrupt(mem.params.allred2,"scramblephase7")
			elseif event.iid == "scramblephase7" then
				local pl = mem.params.permlt
				emitsignals({mode="Run: Scbl",a="G",b="R",c="G",d="R",at=pl and "FY" or "R",bt="R",ct=pl and "FY" or "R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="L"})
				interrupt(mem.params.mingreen,"scramblephase8")
			elseif event.iid == "scramblephase8" then
				mem.state.pqacceptmain = false
				mem.state.pqacceptside = false
				if mem.state.pqmain and mem.state.pqside and mem.params.pedscramble > 0 then
					mem.state.pqmain = false
					mem.state.pqside = false
					interrupt(0,"scramblephase1")
				elseif mem.state.pqmain then
					mem.state.pqmain = false
					interrupt(0,"combophase1")
				elseif mem.state.pqside then
					mem.state.pqside = false
					interrupt(0,"pedphase1")
				else
					if mem.params.permlt then
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="FY",bt="R",ct="FY",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					else
						emitsignals({mode="Idle",a="G",b="R",c="G",d="R",at="R",bt="R",ct="R",dt="R",ap="R",bp="R",cp="R",dp="R",ab="O",bb="FY",cb="O",db="FY",det="U"})
					end
					if mem.updatepending then
						savechanges()
						interrupt(0,"carphase9")
					end
				end
			end
		else
			if mem.updatepending then
				savechanges()
				interrupt(0,"carphase9")
			else
				emitsignals(mem.phaselock)
			end
		end
	end
end

if event.type == "digiline" then
	if (not mem.state.locked) and (event.channel == "detector_b" or event.channel == "detector_d") then
		if type(event.msg) == "table" then
			mem.state.multicarextratime = #event.msg * mem.params.carcountmult
		else
			mem.state.multicarextratime = 0
		end
		if mem.params.autoped then
			interrupt(0,"combophase1")
		else
			interrupt(0,"carphase1")
		end
	elseif event.channel == "pedbutton" and event.msg == "main" and mem.params.crosswalk1 then
		if not mem.state.locked then
			if mem.params.pedscramble == 2 then
				interrupt(0,"scramblephase1")
			else
				interrupt(0,"combophase1")
			end
		elseif mem.state.pqacceptmain then
			mem.state.pqmain = true
			updatedisplay()
		end
	elseif event.channel == "pedbutton" and event.msg == "side" and mem.params.crosswalk2 then
		if not mem.state.locked then
			if mem.params.pedscramble == 2 then
				interrupt(0,"scramblephase1")
			else
				interrupt(0,"pedphase1")
			end
		elseif mem.state.pqacceptside then
			mem.state.pqside = true
			updatedisplay()
		end
	elseif (not mem.state.locked) and event.channel == "detector_at" and mem.params.protlt then
		interrupt(0,"at1")
	elseif (not mem.state.locked) and event.channel == "detector_bt" and mem.params.protlt then
		interrupt(0,"bt1")
	elseif (not mem.state.locked) and event.channel == "detector_ct" and mem.params.protlt then
		interrupt(0,"ct1")
	elseif (not mem.state.locked) and event.channel == "detector_dt" and mem.params.protlt then
		interrupt(0,"dt1")
	end
end