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

© Copyright 2021 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 = "Cab Signal Connection Test"
require([[defines]]);
require([[functions]]);

local Count = 0;
-- These labels appear on each of the tabs for the Function Buttons
Tab01Label = "A";
Tab02Label = "B";
Tab03Label = "C";

ErrCount = 0;

local S1, S2;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup() Running");
	print("Showing the Layout Window");
	ShowLayout();	
	print("Creating Cab Signals");
	_, NumLightsHome = GetAspect(1);
	_, NumLightsDistant = GetAspect(2);
	CreateCab(NumLightsHome, NumLightsDistant)	-- create light signals - (home, distant)
	PCNum = PCNo();
	print("Registering the Cab Signals #1 and #2");
	RegisterCabSignals(1, 2);		-- associate the Home cab signal with first signal and
									-- the Distant cab signal with second signal.
									-- Signals must be created by the "Registering HUD Test.lua"
									-- script which should be run in a different PC window
	print("Reflecting signals from Layout window into the Cab Display");
	S1 = GetCabAspect(HOMESIGNAL);
	S2 = GetCabAspect(DISTANTSIGNAL);
	print("Home Aspect = ", AspectNames[S1], " - Distant Aspect = ", AspectNames[S2]);
	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(0.5);
	HomeAsp = GetCabAspect(HOMESIGNAL);
	DistAsp = GetCabAspect(DISTANTSIGNAL);
	if (S1 ~= HomeAsp or S2 ~= DistAsp) then
		print("Home Aspect = ", AspectNames[HomeAsp], " - Distant Aspect = ", AspectNames[DistAsp]);
		S1 = HomeAsp;
		S2 = DistAsp;
		end;
	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");
	CreateCab(HdNone, HdNone);	-- remove signals from Cab
	if (ErrCount == 0) then
		print("No errors");
		else
		print(ErrCount .. " Error(s)");
		end;	
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]

