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

© Copyright 2019 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 = "AIU Status Test"
require("defines");
require("functions");

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- this function is called once when the [Run Script] button is pushed
	-- Engine : Engine number selected on the RTC Main window
	-- TIU : TIU number selected on the RTC Main window
	print("setup()");
	print("TIU #" .. TIU);
	
	-- we are using 1 AIU for this script so this call will open the AIU window
	SwitchState = GetSwitchStatus(1, 1, TIU);
	Nor, Rev = GetSwitchPositionNames();
	if (SwitchState) then Position = Nor  else Position = Rev  end
	print (GetSwitchName(1, 1, TIU) .. " " .. Position);
	SwitchState = GetSwitchStatus(2, 1, TIU);
	if (SwitchState) then Position = Nor  else Position = Rev  end
	print (GetSwitchName(2, 1, TIU) .. " " .. Position);
	ACCState = GetAccStatus(1, 1, TIU);
	if (ACCState) then Position = "On"  else Position = "Off"  end
	print (GetAccName(1, 1, TIU) .. " " .. Position);
	
	if (TIUActive(1)) then print("TIU1 Active"); end;
	if (TIUActive(2)) then print("TIU2 Active"); end;
	if (TIUActive(3)) then print("TIU3 Active"); end;
	if (TIUActive(4)) then print("TIU4 Active"); end;
	if (TIUActive(5)) then print("TIU5 Active"); end;
		
	print(string.format("RunTime = %.2f seconds", RunTime()));
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]	
function loop(Engine, TIU)
	-- must call Sleep() at least once in this function
	print("loop()");
	print("TIU #" .. TIU);
	print("Switch()");
	AIUNo = 1;
	ChanNo = 2
	Switch(NORMAL, ChanNo, AIUNo, TIU, 0);
	Sleep(2);
	SwitchState = GetSwitchStatus(ChanNo, AIUNo, TIU);
	if ( not SwitchState) then
		print("Invalid Switch State, s/b NORMAL is REVERSE");
		end;
	Switch(REVERSE, ChanNo, AIUNo, TIU, 0);
	Sleep(2);
	SwitchState = GetSwitchStatus(ChanNo, AIUNo, TIU);
	if (SwitchState) then
		print("Invalid Switch State s/b REVERSE is NORMAL");
		end;
	print("Accessory()");
	ChanNo = 1;
	Accessory(ON, ChanNo, AIUNo, TIU, 0);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);
	if (not ACCState) then
		print("Invalid Accessory State, s/b ON is OFF");
		end;
	Accessory(OFF, ChanNo, AIUNo, TIU, 0);
	Sleep(1);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);
	if (ACCState) then
		print("Invalid Accessory State, s/b OFF is ON");
		end;	print("loop exit");	
	--
	Watchdog(TIU);
	--
	return true;		-- false=loop failed, true=loop succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	print(string.format("cleanup() Complete at %.2f", RunTime()));
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]