--[[
---------------------------------------------------------------------------
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 Window Test"
require("defines");

local MyEngineNo, MyTIUNo;
local LashUpEngineList = {-61, 62, -63};

--[[---------------------------------------------------------------------------------------]]
function setup()
	print("setup()");
	
	print("LashUp :");
	for i,v in ipairs(LashUpEngineList) do 
		print(string.format("     Engine %d", v));
		end
		
	MyEngineNo = LashUpEngineList[1];
	MyTIUNo = GetTIUNo(math.abs(MyEngineNo));	-- TIU associated with MyEngineNo (all engines in lashup must be on same TIU)
	MyLashUpNo = 108;
	AIUNo = 1;
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop()
	print("loop()");
	
	print("Startup");	
	retv = LashUpStartUp("Rocks Turn", MyLashUpNo, LashUpEngineList, MyTIUNo);	-- this opens an Operations window for the lashup
	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);
	
	print("SetSpeed");
	retv = SetSpeed(20, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;
	Sleep(15);	

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

	print("SetDirection");
	retv = SetDirection(REVERSE, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Set Direction failed"); end;
	Sleep(5);	

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

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

	print("SetDirection");
	retv = SetDirection(FORWARD, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Set Direction failed"); end;
	Sleep(10);

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

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

	print("SetDirection");
	retv = SetDirection(REVERSE, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Set Direction failed"); end;
	Sleep(5);	

	print("SetSpeed");
	retv = SetSpeed(20, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;
	Sleep(12);
	
	print("SetSpeed");
	retv = SetSpeed(0, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;
	Sleep(5);

	print("SetDirection");
	retv = SetDirection(FORWARD, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Set Direction failed"); end;
	Sleep(10);

	print("SetSpeed");
	retv = SetSpeed(15, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;
	Sleep(60);
	
	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("LashUpShutDown");
	retv = LashUpShutDown(MyLashUpNo, MyTIUNo);				-- this closes the Operations window for the lashup
	if (retv == nil) then print("LashUpShutDown 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
--[[---------------------------------------------------------------------------------------]]
