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

local MyEngineNo, MyLashUpNo, MyTIUNo;
local LashUpEngineList = {};
local EngineIndex;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine)
	print("setup()");
	MyLashUpNo = 103;
	AIUNo = 1;

	EngineIndex = 1;
	LashUpEngineList[EngineIndex] = 61;								-- head engine
	MyTIUNo = GetTIUNo(math.abs(LashUpEngineList[1]));	-- TIU associated with head engine (all engines in lashup must be on same TIU)
	--EngineIndex = EngineIndex + 1; LashUpEngineList[EngineIndex] = -62;	-- middle engine
	EngineIndex = EngineIndex + 1; LashUpEngineList[EngineIndex] = -63;	-- tail engine
	print("LashUp :");
	for i,v in ipairs(LashUpEngineList) do 
		print(string.format("     Engine %d", v));
		end	
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop()
	print("loop()");
		
	print("Startup");
	retv = StartUp(0, MyLashUpNo, LashUpEngineList, MyTIUNo);
	if (retv == nil) then print("Startup failed"); end;
	Sleep(15);		-- engines take time to startup

	print("Smoke Off");
	retv = Smoke(OFF, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Smoke Off Failed"); end;
	
	print("Whistle On");
	retv = Whistle(ON, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Whistle On failed"); end;
	Sleep(2);
	
	print("Whistle Off");
	retv = Whistle(OFF, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Whistle Off failed"); end;
	Sleep(15);
	
	print("Bell On");
	retv = Bell(ON, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Bell On failed"); end;
	Sleep(2);
	
	print("Bell Off");
	retv = Bell(OFF, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Bell Off failed"); end;
	Sleep(15);
	
	print("SetSpeed");
	retv = SetSpeed(10, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;
	Sleep(20);	

	return Stop();	-- false=do not loop, true=continue to loop
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup()
	-- this function is called once when the user presses the [STOP] button
	print(os.date("cleanup()  %d %b %Y   %X"));

	print("SetSpeed");
	retv = SetSpeed(0, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;
	Sleep(10);
		
	print("Shutdown");
	retv = Shutdown(0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Shutdown failed"); end;

	for i,v in ipairs(LashUpEngineList) do 
		print(string.format("Smoke Off - Engine %d", v));
		Smoke(OFF, 0, math.abs(v), MyTIUNo);
		end
		
	print("Done!");
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
