--[[
---------------------------------------------------------------------------
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 = "Acc/Dec Readout Test"

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

Counter01Label = "Rate() Errors";

MyEngineNo = 0;
ErrorCount = 0;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- this function is called once when the [Run Script] button is pushed
	print("setup()");
	MyEngineNo = Engine;
	Acc = {2,4,8}
	Dec = {3,5,7}
	for j,iDec in ipairs(Dec) do
	for i,iAcc in ipairs(Acc) do
		print("Writing Acc/Dec ", iAcc, " - ", iDec);
		-- Set Acc/Dec rate to x Smph/sec
		if (Rate(iAcc, iDec, 0, MyEngineNo, TIU) == false) then
			print(string.format("Set Acc/Dec Rates Failed for Engine #%d", MyEngineNo));
			ErrorCount = ErrorCount +1;
			BumpCounter(COUNTER01);
			end;
		Sleep(1);	-- a second for the Rate() command to execute
		-- read up and save the Acc/Dec rates
		SaveAcc, SaveDec = GetRate(MyEngineNo, TIU);
		if (SaveAcc == nil or SaveDec == nil) then
			print(string.format("Get Acc/Dec Rates Failed for Engine #%d", MyEngineNo));
			ErrorCount = ErrorCount +1;
			BumpCounter(COUNTER01);
			else
			print(string.format("Acc/Dec Rates read from Engine #%d : %d - %d", MyEngineNo, SaveAcc, SaveDec));
			if (SaveAcc ~= iAcc or SaveDec ~= iDec) then
				print(string.format("Get Acc/Dec Rates read wrong values for Engine #%d", MyEngineNo));
				ErrorCount = ErrorCount +1;
				BumpCounter(COUNTER01);
				end;
			end
		end
		end

	return Stop("Done");	-- Complete
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	print("cleanup()");
	-- Set Acc/Dec rate to 1 Smph/sec
	if (Rate(1, 1, 0, MyEngineNo, TIU) == false) then
		ErrorCount = ErrorCount +1;
		BumpCounter(COUNTER01);
		end;
	print("Restored Acc/Dec Rates to 1 Smph/sec");
	if (ErrorCount == 0) then
		print("No Errors");
		else
		print(ErrorCount .. " Errors");
		end;	
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]

