--[[
---------------------------------------------------------------------------
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
--[[---------------------------------------------------------------------------------------]]
-- This test measures the response time of a WIU/TIU
--[[---------------------------------------------------------------------------------------]]
title = "Response Time Test"

require("defines");

-- Counter Labels
Counter01Label = "Commands";
X_COUNTER = COUNTER01;
Counter02Label = "Seconds";
Y_COUNTER = COUNTER02;
Counter03Label = "Run Time";
Z_COUNTER = COUNTER03;

-- These labels appear on each of the tabs for the Function Buttons
Tab01Label = "Control";
Tab02Label = "Not Used";
Tab03Label = "Not Used";

local StartTime = 0;
local CommandTime = 0;

local MyTIUNo, MyEngineNo;

EngineSoundSave = -1;
SoundOff = false;
-- keep the noise to a minimum
AutoBell = false;
AutoWhistle = false;


LastTime = StartTime;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine)
--
MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo 
--
print("setup : TIU " .. MyTIUNo .. " Engine " .. MyEngineNo .. " Debug " .. Debug());
print(string.format("Run Time = %.2f Seconds", RunTime()));

if (Smoke(OFF, 0, MyEngineNo, MyTIUNo) == nil) then
		return Stop("Smoke Off failed");
		end;
Name, PS = GetName(MyEngineNo);
print("Engine#" .. MyEngineNo .. " " .. Name .. " PS" .. PS);
Title(title .. " - " .. GetName(MyEngineNo));
-- start selected engine
if (StartUp(0, MyEngineNo, MyTIUNo) == nil) then
		return Stop("StartUp() failed");
		end;
-- let engine startup
Sleep(15);
SoundOffButton(MyEngineNo, MyTIUNo);		-- run quietly
Sleep(5);
-- Stop the Garbage collector since this is a real-time program
collectgarbage("stop")
print("Setup : Garbage Collection stopped");
--

if (SetSpeed(20, 0, MyEngineNo, MyTIUNo) == nil) then
	return Stop("SetSpeed() failed in setup ()");
	else
	print("Speed set to 20 in setup()");
	end;
	
StartTime = os.clock();	-- Return CPU time since Lua started in seconds.
	
print(string.format("Start Time = %.2f Seconds", StartTime));

return true;
end
--[[---------------------------------------------------------------------------------------]]
function loop()
--	
Yield();	-- yield to other processes. Needed because there are no Sleep() calls during normal running..
-- read up the trip odometer as fast as possible
BumpCounter(X_COUNTER);
CommandTime = os.clock();	-- Return CPU time since Lua started in seconds.
DTORunning = DTO(MyEngineNo, MyTIUNo);
if (DTORunning == nil) then
	return Stop("DTO() failed");
	end;
local ResponseTime = os.clock() - CommandTime;
if (ResponseTime > 3) then
	print(string.format("Response Time = %.1f Seconds", ResponseTime));
	end
SetCounter(Y_COUNTER, ResponseTime);
local RT = RunTime();
SetCounter(Z_COUNTER, RT);
print(string.format("Trip Odometer at %.1f seconds = %.4f Smiles", RT, DTORunning));
--Sleep(0.1);	-- does the same thing as the call to Yield() and causes the script to pause 
return true;	-- false=stop calling loop(), true=continue to call loop()
end
--[[---------------------------------------------------------------------------------------]]
function cleanup()
--
print("cleanup() : TIU " .. MyTIUNo .. " Engine " .. MyEngineNo .. " Debug " .. Debug());

if (SetSpeed(0, 0, MyEngineNo, MyTIUNo) == nil) then
	print("SetSpeed() failed in cleanup()");
	else
	print("Speed set to zero in cleanup()");
	end;

if (SoundOff) then
	SoundOnButton(MyEngineNo, MyTIUNo)
	Sleep(4);
	end;
--
if (ShutDown(0, MyEngineNo, MyTIUNo) == nil) then
	print("ShutDown() failed in cleanup()");
	else
	print("ShutDown() in cleanup()");
	end;

-- restart the garbage collector
collectgarbage("restart") ;
return true;
end
--[[---------------------------------------------------------------------------------------]]
function ShowDTO()
	print(string.format("Run Time = %.2f Seconds", RunTime()));
	local DTOShow =  DTO(MyEngineNo, MyTIUNo);
	if (DTOShow  == nil) then
		print ("DTO() failed");
		return false;
		end;
	print(string.format("Trip Odometer = %.2f Smiles", DTOShow));
	return true;
end
Function10Name, Function10Label = ShowDTO, "Trip Odometer";
--[[---------------------------------------------------------------------------------------]]
function SoundOffButton()
	print("Sound Off Button()");
	SD = GetVolume(MASTER_VOLUME, MyEngineNo, MyTIUNo);	-- get Volume
	if (SD == nil) then
		return Stop("GetVolume() failed in SoundOffButton()");
		end; 
	if (SD == 0) then SD = 30; end;
	print(" Sound Off - Engine " .. MyEngineNo .. " Level was " .. SD);
	EngineSoundSave = SD;
	SoundOff = true;
	SD = SetVolume(MASTER_VOLUME, 0, 0, MyEngineNo, MyTIUNo);
	if (SD == nil) then
		return Stop("SetVolume() failed in SoundOffButton()");
		end	
	return true;
end
Function04Name, Function04Label = SoundOffButton, "Sound Off";
--[[---------------------------------------------------------------------------------------]]
function SoundOnButton()
	print("Sound On Button()");
	if (SoundOff) then	
		SD = EngineSoundSave;	
		print(" Sound On - Engine " .. MyEngineNo .. " Level restored to " .. SD);
		SoundOff = false;;
		SD = SetVolume(MASTER_VOLUME, SD, 0, MyEngineNo, MyTIUNo);
		if (SD == nil) then
			return Stop("SetVolume() failed in SoundOnButton()");
			end		
		end;
	return true;
end
Function05Name, Function05Label = SoundOnButton, "Sound On";
--[[---------------------------------------------------------------------------------------]]

