--[[
---------------------------------------------------------------------------
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
We use the resume function to start the operation and yield function to stop the 
operation. Also, you can see that there are multiple return values received by 
resume function of coroutine.

    First, we create a coroutine and assign it to a variable name co and the 
	coroutine takes in two variables as its parameters.

    When we call the first resume function, the values 3 and 2 are retained in 
	the temporary variables value1 and value2 till the end of the coroutine.

    To make you understand this, we have used a tempvar3, which is 10 initially 
	and it gets updated to 13 and 16 by the subsequent calls of the coroutines 
	since value1 is retained as 3 throughout the execution of the coroutine.

    The first coroutine.yield returns two values 4 and 3 to the resume function, 
	which we get by updating the input params 3 and 2 in the yield statement. It 
	also receives the true/false status of coroutine execution.

    Another thing about coroutines is how the next params of resume call is taken 
	care of, in the above example; you can see that the variable the coroutine.yield 
	receives the next call params which provides a powerful way of doing new 
	operation with the retentionship of existing param values.

    Finally, once all the statements in the coroutines are executed, the subsequent 
	calls will return in false and "cannot resume dead coroutine" statement as response.

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

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup() Running");
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- Always call Sleep(X) at least once in this function if it returns true;
	print("loop() Running");
	co = coroutine.create(function (value1,value2)
	   local tempvar3 = 10
	   print("coroutine section 1 : ", value1, " ",value2, " ", tempvar3)
		
	   local tempvar1 = coroutine.yield(value1+1, value2+1)
	   tempvar3 = tempvar3 + value1
	   print("coroutine section 2 : ",tempvar1 , " ",tempvar2, " ", tempvar3)
		
	   local tempvar1, tempvar2= coroutine.yield(value1+value2, value1-value2)
	   tempvar3 = tempvar3 + value1
	   print("coroutine section 3 : ",tempvar1, " ",tempvar2, " ", tempvar3)
	   return value2, "end"
		
	end)

	print("main ", coroutine.resume(co, 3, 2))
	print("main ", coroutine.resume(co, 12,14))
	print("main ", coroutine.resume(co, 5, 6))
	print("main ", coroutine.resume(co, 10, 20))	
	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
--[[---------------------------------------------------------------------------------------]]
