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

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

-- Counter Labels
Counter01Label = "Errors";

--[[---------------------------------------------------------------------------------------]]
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()");
	
	-- 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);
		
	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()");

	AIUNo = 1;
	ChanNo = 2

	print("Switch()");	
	Switch(NORMAL, ChanNo, AIUNo, TIU, 2);
	Sleep(2);
	SwitchState = GetSwitchStatus(ChanNo, AIUNo, TIU);
	if (not SwitchState) then print("S1"); BumpCounter(COUNTER01); end
	Switch(REVERSE, ChanNo, AIUNo, TIU, 1);
	Sleep(2);
	SwitchState = GetSwitchStatus(ChanNo, AIUNo, TIU);
	if (SwitchState) then print("S2"); BumpCounter(COUNTER01);  end
	
	print("Accessory()");
	ChanNo = 1;

	Accessory(ON, ChanNo, AIUNo, TIU, 0);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);
	if (not ACCState) then print("E1"); BumpCounter(COUNTER01);  end
	Accessory(OFF, ChanNo, AIUNo, TIU, 0);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (ACCState) then print("E2"); BumpCounter(COUNTER01);  end
	ChanNo = 2;
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	VirtualAccessory(ON, ChanNo, AIUNo, TIU);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (not ACCState) then print("E3"); BumpCounter(COUNTER01);  end
	VirtualAccessory(OFF, ChanNo, AIUNo, TIU);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (ACCState) then print("E4"); BumpCounter(COUNTER01);  end

	ChanNo = 2;
	TIU = 2;
	AIUNo = 4;
	VirtualAccessory(ON, ChanNo, AIUNo, TIU);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (not ACCState) then print("E5"); BumpCounter(COUNTER01);  end
	VirtualAccessory(OFF, ChanNo, AIUNo, TIU);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (ACCState) then print("E6"); BumpCounter(COUNTER01);  end

	ChanNo = 2;			
	TIU = 5;
	AIUNo = 1;
	VirtualAccessory(ON, ChanNo, AIUNo, TIU);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (not ACCState) then print("E5"); BumpCounter(COUNTER01);  end
	VirtualAccessory(OFF, ChanNo, AIUNo, TIU);
	Sleep(2);
	ACCState = GetAccStatus(ChanNo, AIUNo, TIU);	
	if (ACCState) then print("E6"); BumpCounter(COUNTER01);  end
		
	print("loop exit");	
	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
--[[---------------------------------------------------------------------------------------]]