--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2026 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "Check Signals Test"
require([[defines]]);
require([[functions]]);
--
-- flashes green/yellow/red on all available signals
--
local Layout = nil;

local Cancel = false;	-- cancel looping tests

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup() Running");

	-- Instrumented Layout 4
	Layout = require([[Layout4]]);		-- All of the details about the Layout
	
	ShowLayout();	-- SetAspect requires that all signals be created
					-- by the Layout window

	local NumSignals;
	NumSignals = GetSignals();
	print("Number of Signals = " .. NumSignals);
	if (Debug() == 0) then
		print("Check that all required signals are present");
		-- check that all signals were found
		local SignalMissing = false;
		for orig_key, orig_value in pairs(SignalNames) do
			if (type(orig_key) == "number") then
				local res = CheckSignal(orig_key);
				print("Signal #" .. orig_key .. " - " .. orig_value .. " = " .. tostring(res));
				if (res == false) then
					SignalMissing = true;
					end;
				end;
			end;
		
		if (SignalMissing) then
			return Stop("Did not find all signals");
			else
			print("All Signals found");
			end;
		end
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- Always call Sleep(X) at least once in this function if it returns true;
	Sleep(5);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button or the Stop() function is called
	print("cleanup() Running");
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]

function CycleTestButton()
	print("Cycle Test");
	Cancel = false;
	repeat
		-- to Red
		print("Red");
		for orig_key, orig_value in pairs(SignalNames) do
			if (type(orig_key) == "number") then
				if (Debug() > 0) then 
					print("Signal #" .. orig_key .. " to red - " .. orig_value);
					end;
				SetAspect(orig_key, AsStopSignal);
				end
			end
		if (Cancel) then
			Cancel = false;
			return true;
			end;
		Sleep(1);		
		-- to Yellow	
			print("Yellow");
		for orig_key, orig_value in pairs(SignalNames) do
			if (type(orig_key) == "number") then
				if (Debug() > 0) then 			
					print("Signal #" .. orig_key .. " to yellow - " .. orig_value);
					end;
				SetAspect(orig_key, AsApproach);
				end
			end
			
		if (Cancel) then
			Cancel = false;
			return true;
			end;
		Sleep(1);		
		--to Green
		print("Green");		
		for orig_key, orig_value in pairs(SignalNames) do
			if (type(orig_key) == "number") then
				if (Debug() > 0) then 			
					print("Signal #" .. orig_key .. " to green - " .. orig_value);
					end;
				SetAspect(orig_key, AsClear);
				end
			end	
		if (Cancel) then
			Cancel = false;
			return true;
			end;
		Sleep(1);
		--to Test
		print("All On");		
		for orig_key, orig_value in pairs(SignalNames) do
			if (type(orig_key) == "number") then
				if (Debug() > 0) then 
					print("Signal #" .. orig_key .. " to all on - " .. orig_value);
					end;
				SetAspect(orig_key, AsTest);
				end
			end			
		Sleep(1);
		--to Dark
		print("All Off");		
		for orig_key, orig_value in pairs(SignalNames) do
			if (type(orig_key) == "number") then
				if (Debug() > 0) then 
					print("Signal #" .. orig_key .. " to all off - " .. orig_value);
					end;
				SetAspect(orig_key, AsDark);
				end
			end			
		Sleep(1);		
	until Cancel;
	return true;
end
Function01Name, Function01Label = CycleTestButton, "Cycle Test";
--[[---------------------------------------------------------------------------------------]]
local function isEven(n)
    return (n & 1) == 0
end

local function isOdd(n)
    return (n & 1) == 1
end
--[[---------------------------------------------------------------------------------------]]
function EvenButton()
	print("Even Signals Test");
	Cancel = true;
	--to Green if Signal Number is even
	--Otherwise to Red
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			if (isEven(orig_key)) then
				print("Signal #" .. orig_key .. " to Green - " .. orig_value);
				SetAspect(orig_key, AsClear);
				else
				print("Signal #" .. orig_key .. " to Red - " .. orig_value);
				SetAspect(orig_key, AsStopandProceed);
				end;
			end;
		end;
	return true;
end
Function02Name, Function02Label = EvenButton, "Even Signals Test";
--[[---------------------------------------------------------------------------------------]]

function OddButton()
	print("Odd Signals Test");
	Cancel = true;
	--to Green if Signal Number is Odd
	--Otherwise to Red
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			if (isOdd(orig_key)) then
				print("Signal #" .. orig_key .. " to Green - " .. orig_value);
				SetAspect(orig_key, AsClear);
				else
				print("Signal #" .. orig_key .. " to Red - " .. orig_value);
				SetAspect(orig_key, AsStopandProceed);
				end;
			end;
		end;
	return true;
end
Function03Name, Function03Label = OddButton, "Odd Signals Test";
--[[---------------------------------------------------------------------------------------]]

function AllButton()
	print("All LED On Signals Test");
	Cancel = true;
	--to Test
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			print("Signal #" .. orig_key .. " to all on - " .. orig_value);
			SetAspect(orig_key, AsTest);
			end
		end	
	return true;
end
Function04Name, Function04Label = AllButton, "All On Test";
--[[---------------------------------------------------------------------------------------]]

function DarkButton()
	print("Dark");
	Cancel = true;
	-- to Red
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			print("Signal #" .. orig_key .. " to Dark - " .. orig_value);
			SetAspect(orig_key, AsDark);
			end
		end
	return true;
end
Function05Name, Function05Label = DarkButton, "Dark";
--[[---------------------------------------------------------------------------------------]]
	
function ClearButton()
	print("Clear");
	Cancel = true;
	--to Green
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			print("Signal #" .. orig_key .. " to green - " .. orig_value);
			SetAspect(orig_key, AsClear);
			end
		end	
	return true;
end
Function06Name, Function06Label = ClearButton, "Clear";
--[[---------------------------------------------------------------------------------------]]

function ApproachButton()
	print("Approach");
	Cancel = true;
	-- to Yellow	
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			print("Signal #" .. orig_key .. " to yellow - " .. orig_value);
			SetAspect(orig_key, AsApproach);
			end
		end
	return true;
end
Function07Name, Function07Label = ApproachButton, "Approach";
--[[---------------------------------------------------------------------------------------]]

function StopButton()
	print("Stop");
	Cancel = true;
	-- to Red
	for orig_key, orig_value in pairs(SignalNames) do
		if (type(orig_key) == "number") then
			print("Signal #" .. orig_key .. " to red - " .. orig_value);
			SetAspect(orig_key, AsStopSignal);
			end
		end
	return true;
end
Function08Name, Function08Label = StopButton, "Stop";
--[[---------------------------------------------------------------------------------------]]
				
function CancelButton()
	print("Cancel");
	Cancel = true;
	return true;
end
Function10Name, Function10Label = CancelButton, "Cancel";
--[[---------------------------------------------------------------------------------------]]

