--[[
---------------------------------------------------------------------------
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");
--
-- this script gets a lashup (103) from the Lashup Window and uses its engine list
--
-- So lashup 103 must be defined in the Lashup Window's String Grid
--
local MyEngineNo, MyLashUpNo, MyTIUNo;
local LashUpEngineList = {};
local EngineIndex;

local UseOpWindow = true;	-- true = use Operations window, false = don't use Operations window

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

	print("LashUp #103 must be created in the Lashup Window");

	LashupName, LashUpEngineList = GetName(MyLashUpNo);
	MyTIUNo = GetTIUNo(LashUpEngineList[1]);
	local EX = Exists(MyLashUpNo);
	if (EX == false) then
		print("LashUp #" .. MyLashUpNo .. " does not exist");
		return false;
		end;
	print("LashUp #" .. MyLashUpNo .. "   -   Name = " .. LashupName .. "   -   Engine List = " .. type(LashUpEngineList) .. "   -   Exists = " .. tostring(EX));
	if (type(LashUpEngineList) == "table" and EX) then
		for i,EngNo in ipairs(LashUpEngineList) do
			print("            Engine List [" .. i .. "] = " .. EngNo);
			end;
		else;
		return false;	-- setup failed
		end;

	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop()
	print("loop()");
		
	print("Startup in 0 seconds");
	-- use ONE of these startup commands
	if (UseOpWindow) then
		retv = LashUpStartUp(LashupName, MyLashUpNo, LashUpEngineList, MyTIUNo);-- Operations window
		else
		retv = StartUp(0, MyLashUpNo, LashUpEngineList, MyTIUNo);			-- no Operations window
		end;
	if (retv == nil) then print("Startup failed"); end;
	Sleep(15);		-- engines take time to startup

	print("Smoke Off in 0 seconds");
	retv = Smoke(OFF, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Smoke Off Failed"); end;
	
	print("Whistle On in 5 seconds");
	retv = Whistle(ON, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Whistle On failed"); end;
	Sleep(2);
	
	print("Whistle Off in 5 seconds");
	retv = Whistle(OFF, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Whistle Off failed"); end;
	Sleep(10);
	
	print("Bell On in 5 seconds");
	retv = Bell(ON, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Bell On failed"); end;
	Sleep(5);
	
	print("Bell Off in 5 seconds");
	retv = Bell(OFF, 5, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("Bell Off failed"); end;
	Sleep(10);
	
	for iter = 1,5 do
		print("SetSpeed to 15 Smph in 0 seconds");
		Rate(1, 1, 0, MyLashUpNo, MyTIUNo);
		retv = SetSpeed(15, 0, MyLashUpNo, MyTIUNo);
		if (retv == nil) then print("SetSpeed failed"); end;
		Sleep(10);	
		
		print("Direction to Reverse in 0 seconds");
		retv = SetDirection(REVERSE, 0, MyLashUpNo, MyTIUNo);
		if (retv == nil) then print("Direction failed"); end;
		Sleep(10);	

		print("SetSpeed to 10 Smph in 0 seconds");
		Rate(1, 1, 0, MyLashUpNo, MyTIUNo);
		retv = SetSpeed(10, 0, MyLashUpNo, MyTIUNo);
		if (retv == nil) then print("SetSpeed failed"); end;
		Sleep(18);	

		print("Direction to Forward in 0 seconds");
		retv = SetDirection(FORWARD, 0, MyLashUpNo, MyTIUNo);
		if (retv == nil) then print("Direction failed"); end;
		Sleep(10);
		end;
	
	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 to 0 Smph in 0 seconds");
	retv = SetSpeed(0, 0, MyLashUpNo, MyTIUNo);
	if (retv == nil) then print("SetSpeed failed"); end;	
	Sleep(5);		
	print("Shutdown in 0 seconds");
	-- use one OR the other shutdown functions
	if (UseOpWindow) then
		retv = LashUpShutDown(MyLashUpNo, MyTIUNo);			-- use Operations window
		else
		retv = Shutdown(0, MyLashUpNo, MyTIUNo);			-- no Operations window
		end;
	if (retv == nil) then print("Shutdown failed"); end;
	Sleep(15);	
	for i, EngNo in ipairs(LashUpEngineList) do
        print(string.format("Feature Reset - Engine %d", math.abs(EngNo)));
        FeatureReset(math.abs(EngNo), MyTIUNo);
        end;
	print("Done!");
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
