--[[
---------------------------------------------------------------------------
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
-- Counter and Function Label Test
title = "Counter and Function Label Test and Tick Test"
require("defines");

-- Counter Labels
Counter01Label = "Counts to 3";
Counter02Label = "Ten Second Ticks";
Counter03Label = "Counts to 1";
Counter04Label = "One Second Ticks";
Counter05Label = "Counts to 1 @ [STOP]";
Counter06Label = "Touch Count";

ENGDET = COUNTER01;
SMSTATES = COUNTER02;
EnginesUsed = {"GP38-2", "GP7", "SW1500"};
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- make sure that RFID is not connected
	--TIUSerialPortConnected, TIUConnected, RFIDSerialPortConnected = CommStatus();	
	_, _, RFIDSerialPortConnected = CommStatus();	
	print("RFID : " .. tostring(RFIDSerialPortConnected));
	if (RFIDSerialPortConnected) then
		print("RFID Port is not needed for this script");
		end
		
	for idx,Eng in ipairs(EnginesUsed) do
		BumpCounter(ENGDET);	-- Engines Detected counter should bump to 3
		print(idx .. " Engine " .. Eng);	
		end
	BumpCounter(COUNTER03);		-- Counter 3 bumps to 1
	return true;
	end
--[[---------------------------------------------------------------------------------------]]	
function loop(Engine, TIU)
	-- Always call Sleep() at least once in this function.
	RT = RunTime();
	print(string.format("-->loop %.2f", RT));
	bVal = (math.floor(RT)//10 & 1) == 1;
	print(string.format("-->loop counter odd : %s", bVal));
	SetLED(2, (bVal and clGreen) or clRed, "10 Sec");
	Sleep(10);
	BumpCounter(SMSTATES);						-- bump states counter
	return true;	-- false=do not loop, true=continue to loop
	end
--[[---------------------------------------------------------------------------------------]]	
function cleanup(Engine, TIU)
	BumpCounter(SMSTATES);		-- bump states counter
	BumpCounter(COUNTER05);
	pcounts();	-- print out the counts
	SetLED(4, clGreen, "Done");
	SetLED(2, clRed);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]	
function pcounts()
	-- Prints out the Counters
	print(Counter01Label .. " = " .. GetCounter(COUNTER01));	
	print(Counter02Label .. " = " .. GetCounter(COUNTER02));
	print(Counter03Label .. " = " .. GetCounter(COUNTER03));
	print(Counter04Label .. " = " .. GetCounter(COUNTER04));
	print(Counter05Label .. " = " .. GetCounter(COUNTER05));	
	print(Counter06Label .. " = " .. GetCounter(COUNTER06));
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
function tick(Engine, TIU)
	print(string.format("tick %.2f", RunTime()));
	BumpCounter(COUNTER04);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
function extratick(Engine, TIU)
	BumpCounter(COUNTER04);
	return true;
	end--[[---------------------------------------------------------------------------------------]]
function touch1()
	BumpCounter(COUNTER06);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
function touch2()
	BumpCounter(COUNTER06);
	BumpCounter(COUNTER06);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
function touch3()
	BumpCounter(COUNTER06);
	BumpCounter(COUNTER06);
	BumpCounter(COUNTER06);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
function cleart()
	ClearCounter(COUNTER04);
	return true;
	end
function touchm1()
	DecCounter(COUNTER06);
	return true;
	end
function touchm2()
	DecCounter(COUNTER06);
	DecCounter(COUNTER06);
	return true;
	end
function setm3()
	SetCounter(COUNTER06, 99.9);
	return true;
	end
function cleartouch()
	ClearCounter(COUNTER06);
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
Function01Name, Function01Label = touch1, "Touch +1";
Function02Name, Function02Label = touch2, "Touch +2";
Function03Name, Function03Label = touch3, "Touch +3";
Function04Name, Function04Label = cleart, "Clear Ticks";
Function05Name, Function05Label = pcounts, "Print Counts";
Function06Name, Function06Label = touchm1, "Touch -1";
Function07Name, Function07Label = touchm2, "Touch -2";
Function08Name, Function08Label = setm3, "Set Touch 99.9";
Function09Name, Function09Label = cleartouch, "Clear Touch";
Function10Name, Function10Label = extratick, "Extra 1sec Tick";
--[[---------------------------------------------------------------------------------------]]
