--[[
---------------------------------------------------------------------------
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
--[[---------------------------------------------------------------------------------------]]
-- Comments start with two dashes
--[[
Mulitline comments
start with two dashes and two left brackets.
They end with two right brackets. ]]


require("defines");
require("functions");

title = "Tick-Tock-Test"

ON = true;
OFF = false;
Accuracy = 0.05;
ErrorCount = 0;
Counter01Label = "tick() Errors";
LastDTO = -1;
tickCount = 0;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
--
print("setup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
StartUp(0, Engine, TIU);	-- start selected engine
-- let engine startup
Sleep(15);	
SetSpeed(15, 0, Engine, TIU);
Sleep(15);	
LastDTO = DTO(Engine, TIU);
tickCount = 0;
print("@" .. RunTime() .. " seconds - Trip Odometer " .. LastDTO .. " Smiles");
return true;
end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- Always call Sleep() at least once in this function since it returns true;
print("loop() start : " .. RunTime() .. " seconds - Trip Odometer " .. DTO(Engine, TIU) .. " Smiles");
Sleep(15.78);
print("loop() end   : " .. RunTime() .. " seconds - Trip Odometer " .. DTO(Engine, TIU) .. " Smiles");
Sleep(15.78);
return true;	-- false=do not loop, true=continue to loop
end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
--
print("cleanup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
print("@" .. RunTime() .. " - Trip Odometer " .. DTO(Engine, TIU) .. " Smiles");
SetSpeed(0, 0, Engine, TIU);
Sleep(5);
ShutDown(0, Engine, TIU);
if (ErrorCount == 0) then
	print("No Errors");
	else
	print(ErrorCount .. " Errors");
	end;
return true;
end
--[[---------------------------------------------------------------------------------------]]
function ShowDTO(Eng, TIU)
	MyDTO = DTO(Eng, TIU);
	print("ShowDTO() @ " .. RunTime() .. " seconds - Trip Odometer " .. MyDTO .. " Smiles")
	return true;
end
Function10Name, Function10Label = ShowDTO, "Trip Odometer";
--[[---------------------------------------------------------------------------------------]]
function tick(Engine, TIU)
	if (LastDTO < 0) then
		return true;
		end;
	print("tick() = " .. RunTime() .. " seconds");
	tickCount = tickCount +1;
	-- every 10 seconds, check results
	if (math.abs(tickCount % 10) < Accuracy) then	-- test for zero +/- Accuracy
		CurrentDTO = DTO(Engine, TIU);
		Delta = CurrentDTO - LastDTO;
		print("Delta " .. (Delta/10), " Smiles/second");
		print("Speed " .. (Delta) * 6.0 * 60.0, " Smiles/hour");
		MySpeed = (Delta) * 6.0 * 60.0;
		if (MySpeed < 15.5 or MySpeed > 16.5) then	-- Speed set to 15 Smiles/hour but
													-- calculated speed comes out 1 Smile/hour faster.
			print ("Speed Error - got " .. MySpeed .. " Smiles/hour");
			ErrorCount = ErrorCount + 1;
			BumpCounter(COUNTER01);		
			end
		if (Delta/10 > .01) then
			print ("Delta Speed Error - got " .. Delta/10 .. " Smiles/second");
			ErrorCount = ErrorCount + 1;
			BumpCounter(COUNTER01);		
			end
		
		LastDTO = CurrentDTO;
		end;	
	return true;	-- false=stop calling tick(), true=continue to tick
	end
--[[---------------------------------------------------------------------------------------]]