--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2021 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
---------------------------------------------------------------------------]]
--[[
https://www.tutorialspoint.com/lua/lua_coroutines.htm
Let us look at a simple coroutine that returns a number from 1 to 5 with the help of yield 
function and resume function. It creates coroutine if not available or else resumes the 
existing coroutine.

]]
-- Semicolons are not required in Lua code
title = "Coroutine Test 2"
require([[defines]]);
require([[functions]]);

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup() Running");
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function getNumber()
   local function getNumberHelper()
	  co = coroutine.create(function ()
		  coroutine.yield(1)
		  coroutine.yield(2)
		  coroutine.yield(3)
		  coroutine.yield(4)
		  coroutine.yield(5)
		  end)
	  return co
   end
	
   if(numberHelper) then
	  status, number = coroutine.resume(numberHelper);
		
	  if coroutine.status(numberHelper) == "dead" then
		 numberHelper = getNumberHelper()
		 status, number = coroutine.resume(numberHelper);
	  end
		
	  return number
   else
	  numberHelper = getNumberHelper()
	  status, number = coroutine.resume(numberHelper);
	  return number
   end
end	   
--[[---------------------------------------------------------------------------------------]]
	   
function loop(Engine, TIU)
	-- Always call Sleep(X) at least once in this function if it returns true;
	print("loop() Running");

	for index = 1, 10 do
	   print(index, " ", getNumber())
	end	
	
	Sleep(1);
	print("Almost Done...");
	return Stop("DONE");
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button or the Stop() function is called
	print("cleanup() Running");
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
