--[[
---------------------------------------------------------------------------
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 = "Ping Test"

require("defines");

-- Counter Labels
Counter01Label = "Ping Count";
PINGCOUNT = COUNTER01;
Counter02Label = "Total";
PINGTOTAL = COUNTER02;
Counter03Label = "Lost";
PINGLOST = COUNTER03;

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


local MyTIUNo, MyEngineNo;

Decel = StartingDecel;
EngSpeed = StartingSpeed;
local PC = 0;
local PCloss = 0;

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


--[[---------------------------------------------------------------------------------------]]
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() 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(7);
SoundOffButton(MyEngineNo, MyTIUNo);		-- run quietly
Sleep(7);
-- Stop the Garbage collector since this is a real-time program
collectgarbage("stop")
print("Setup : Garbage Collection stopped");
--

print(string.format("Run Time = %.2f Seconds", RunTime()));

return true;
end
--[[---------------------------------------------------------------------------------------]]
function loop()
--
local P4 = Ping(MyEngineNo, MyTIUNo);
if (P4 ~= nil) then
	print(string.format("Ping success = %d", P4));
	BumpCounter(PINGCOUNT);
	PC = PC + P4;
	PCloss = PCloss + (100 - P4);
	SetCounter(PINGTOTAL, PC);
	SetCounter(PINGLOST, PCloss);
	else
	print(string.format("Ping failed"));
	end;
Sleep(0.1);
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()");
	end;

-- wait for dispatch list to empty
while(CountDispatchList() > 0) do Sleep(1); end

--
if (SoundOff) then
	SoundOnButton(MyEngineNo, MyTIUNo)
	end;
--
if (ShutDown(0, MyEngineNo, MyTIUNo) == nil) then
	print("ShutDown() failed in cleanup()");
	end;
--
-- restart the garbage collector
collectgarbage("restart") ;
return true;
end
--[[---------------------------------------------------------------------------------------]]

function ShowDTO()
	print(string.format("Run Time = %.2f Seconds", RunTime()));
	print(string.format("Trip Odometer = %.2f Smiles", DTO(MyEngineNo, MyTIUNo)));
	return true;
end
Function10Name, Function10Label = ShowDTO, "Trip Odometer";
--[[---------------------------------------------------------------------------------------]]

function SoundOffButton()
	print("Sound Off Button()");
	local SD = GetVolume(ENGINE_VOLUME, MyEngineNo, MyTIUNo);	-- get Engine Volume
	if (SD == nil) then
		return Stop("GetVolume(ENGINE_VOLUME) failed in SoundOffButton()");
		end; 
	if (SD == 0) then SD = 30; end;
	print(" Sound Off - Engine " .. MyEngineNo .. " Engine Level was " .. SD);
	EngineSoundSave = SD;
	
	SD = GetVolume(ACCENT_VOLUME, MyEngineNo, MyTIUNo);	-- get Accent Volume
	if (SD == nil) then
		return Stop("GetVolume(ACCENT_VOLUME) failed in SoundOffButton()");
		end; 
	if (SD == 0) then SD = 30; end;
	print(" Sound Off - Engine " .. MyEngineNo .. " Accent Level was " .. SD);
	AccentSoundSave = SD;
	
	SoundOff = true;
	SetVolume(ENGINE_VOLUME, 0, 0, MyEngineNo, MyTIUNo);
	SetVolume(ACCENT_VOLUME, 0, 0, MyEngineNo, MyTIUNo);
	return true;
end
Function04Name, Function04Label = SoundOffButton, "Sound Off";
--[[---------------------------------------------------------------------------------------]]
function SoundOnButton()
	print("Sound On Button()");
	if (SoundOff) then	
		local SD = EngineSoundSave;	
		print(" Sound On - Engine " .. MyEngineNo .. " Engine Level restored to " .. SD);
		SoundOff = false;;
		SD = SetVolume(ENGINE_VOLUME, SD, 0, MyEngineNo, MyTIUNo);
		if (SD == nil) then
			return Stop("SetVolume(ENGINE_VOLUME) failed in SoundOnButton()");
			end	

		SD = AccentSoundSave;	
		print(" Sound On - Engine " .. MyEngineNo .. " Accent Level restored to " .. SD);
		SoundOff = false;;
		SD = SetVolume(ACCENT_VOLUME, SD, 0, MyEngineNo, MyTIUNo);
		if (SD == nil) then
			return Stop("SetVolume(ACCENT_VOLUME) failed in SoundOnButton()");
			end				
		end;
	return true;
end
Function05Name, Function05Label = SoundOnButton, "Sound On";
--[[---------------------------------------------------------------------------------------]]

