--[[
---------------------------------------------------------------------------
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 Simple Test"

Counter01Label = "tick() Errors";

ON = true;
OFF = false;

ErrorCount = 0;
Accuracy = 0.101;
PreviousRunTime = 0;
PreviousRunTime1 = 0;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
print("setup() : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
PreviousRunTime = RunTime();
PreviousRunTime1 = PreviousRunTime;
return true;
end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
print("loop() : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
PreviousRunTime1 = RunTime();
print("loop() : " .. PreviousRunTime1);
Sleep(20);
now1 = RunTime();
print("loop() : " .. now1);
if (math.abs(now1 - PreviousRunTime1 - 20.0) > Accuracy) then
	print("tick() Error " .. now1 - PreviousRunTime1 - 20.0 .. " seconds");
	ErrorCount = ErrorCount + 1;
	BumpCounter(COUNTER01);
	end;
print("loop() complete 20 seconds");
return true;	-- false=do not loop, true=continue to loop
end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
print("cleanup() : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
if (ErrorCount == 0) then
	print("No Errors");
	else
	print(ErrorCount .. " Errors");
	end;
print("cleanup() : " .. RunTime());
return true;
end
--[[---------------------------------------------------------------------------------------]]
function tick(Engine, TIU)
	now = RunTime();
	print("tick() = " .. now .. " seconds");
	if (math.abs(now - PreviousRunTime - 1.0) > Accuracy) then
		print("tick() Error " .. now - PreviousRunTime - 1.0 .. " seconds");
		ErrorCount = ErrorCount + 1;
		BumpCounter(COUNTER01);
		end;
	PreviousRunTime = now;
	return true;	-- false=stop calling tick(), true=continue to tick
	end
--[[---------------------------------------------------------------------------------------]]