--[[
---------------------------------------------------------------------------
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 = "Startup/Shutdown Test"
require("defines");

local MyEngineNo, MyTIUNo;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine)
	-- 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()");

	MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	-- debug level as selected on the Debug window, values 0 (off) to 9 (maximum)
	if (Debug() > 0) then 
		print(string.format("setup(): Debug level %d", Debug())); 
		print("setup(): Engine Number " .. MyEngineNo .. " TIU Number " .. MyTIUNo);
		end
	print("TIU : " .. MyTIUNo);
	assert((MyTIUNo >= 1 and MyTIUNo <= 5), "TIU Number out of range");
	-- initialize the layout as needed
	AC, IAC = Counts();	-- Counts() returns two values
	print("TIU" .. MyTIUNo .. ": " .. AC .. " Active Engines - " .. IAC .. " Inactive Engines");
	-- switches and accessories
	AIUNo = 1;
	Switch(STRAIGHT, 1, AIUNo, MyTIUNo, 3);  -- College East
	Switch(STRAIGHT, 2, AIUNo, MyTIUNo, 5);  -- College West
	Sleep(10);
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop()
	-- This function is called once after setup() completes and returns false.
	-- You must press [STOP] to run cleanup() and finish the script.
	-- TIU : TIU number selected on the RTC Main window
	-- debug level as selected on the Debug window, values 0 (off) to 9 (maximum)
	print(string.format("loop(): EngineNo %d TIUNo %d", MyEngineNo, MyTIUNo));
	if (Debug() > 0) then 
		print("Debug level " .. Debug());
		end
	StartUp(0, MyEngineNo, MyTIUNo);
	Sleep(10);
	Whistle(ON, 20, MyEngineNo, MyTIUNo);
	Whistle(OFF, 22, MyEngineNo, MyTIUNo);
	print(string.format("loop() Sleep(10) Started at %.2f", RunTime()));
	Sleep(10);
	print(string.format("loop() Sleep(10) Complete at %.2f", RunTime()));

	for Count = 1, 2 do
		Bell(ON, 15, MyEngineNo, MyTIUNo);
		Bell(OFF, 20, MyEngineNo, MyTIUNo);			
		Sleep(10);
		end
	-- after this functions returns, the Dispatch List will continue to empty until all commands are sent.
	return false;	-- false=do not loop, true=continue to loop
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup()
	-- this function is called once when the user presses the [STOP] button
	--print("cleanup()");
	ShutDown(0, MyEngineNo, MyTIUNo);
	print(string.format("cleanup() Complete at %.2f", RunTime()));
	--
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function BlowWhistle()
	print("BlowWhistle()");
	print(string.format("BlowWhistle(): Eng %d TIU %d", MyEngineNo, MyTIUNo));
	print("BlowWhistle(): Eng " .. MyEngineNo .. " TIU " .. MyTIUNo .. " Debug " .. Debug());
	Whistle(ON, 0, MyEngineNo, MyTIUNo);
	Whistle(OFF, 1, MyEngineNo, MyTIUNo);
	return true;
end
Function01Name, Function01Label = BlowWhistle, "Blow Whistle";
--[[---------------------------------------------------------------------------------------]]
function SimplePrint()
	print("SimplePrint()");
	print(string.format("SimplePrint(): Eng %d TIU %d", MyEngineNo, MyTIUNo));
	return true;
end
Function02Name, Function02Label = SimplePrint, "Simple Print";
-- you can create 10 functions which get hooked to the 10 button on the Program Control window
--[[---------------------------------------------------------------------------------------]]
function NOP()
	print("NOP()");
	print(string.format("NOP() : Eng %d TIU %d", MyEngineNo, MyTIUNo));
	return true;
end
Function10Name, Function10Label = NOP, "NOP";
--[[---------------------------------------------------------------------------------------]]
