SCHEDULER:New() implemented and routines.scheduleFunction removed

This commit is contained in:
FlightControl
2016-05-25 16:54:09 +02:00
parent bed2a339a1
commit 9afe0acd8d
24 changed files with 289 additions and 33534 deletions

View File

@@ -461,102 +461,6 @@ do
end
do
local Tasks = {}
local task_id = 0
--[[ routines.scheduleFunction:
int id = routines.schedule_task(f function, vars table, t number, rep number, st number)
id - integer id of this function task
f - function to run
vars - table of vars for that function
t - time to run function
rep - time between repetitions of this function (OPTIONAL)
st - time when repetitions of this function will stop automatically (OPTIONAL)
]]
--- Schedule a function
-- @param #function f
-- @param #table parameters
-- @param #Time t
-- @param #Time rep seconds
-- @param #Time st
routines.scheduleFunction = function(f, vars, t, rep, st)
--verify correct types
assert(type(f) == 'function', 'variable 1, expected function, got ' .. type(f))
assert(type(vars) == 'table' or vars == nil, 'variable 2, expected table or nil, got ' .. type(f))
assert(type(t) == 'number', 'variable 3, expected number, got ' .. type(t))
assert(type(rep) == 'number' or rep == nil, 'variable 4, expected number or nil, got ' .. type(rep))
assert(type(st) == 'number' or st == nil, 'variable 5, expected number or nil, got ' .. type(st))
if not vars then
vars = {}
end
task_id = task_id + 1
table.insert(Tasks, {f = f, vars = vars, t = t, rep = rep, st = st, id = task_id})
return task_id
end
-- removes a scheduled function based on the function's id. returns true if successful, false if not successful.
routines.removeFunction = function(id)
local i = 1
while i <= #Tasks do
if Tasks[i].id == id then
table.remove(Tasks, i)
else
i = i + 1
end
end
end
routines.errhandler = function(errmsg)
env.info( "Error in scheduled function:" .. errmsg )
env.info( debug.traceback() )
return errmsg
end
--------------------------------------------------------------------------------------------------------------------
-- not intended for users to use this function.
routines.do_scheduled_functions = function()
local i = 1
while i <= #Tasks do
if not Tasks[i].rep then -- not a repeated process
if Tasks[i].t <= timer.getTime() then
local Task = Tasks[i] -- local reference
--env.info("do_scheduled_functions:call function " .. i )
table.remove(Tasks, i)
local err, errmsg = xpcall(function() Task.f( unpack(Task.vars, 1, table.maxn(Task.vars))) end, routines.errhandler )
if not err then
--env.info('routines.scheduleFunction, error in scheduled function: ' .. errmsg)
end
--Task.f(unpack(Task.vars, 1, table.maxn(Task.vars))) -- do the task, do not increment i
else
i = i + 1
end
else
if Tasks[i].st and Tasks[i].st <= timer.getTime() then --if a stoptime was specified, and the stop time exceeded
--env.info("do_scheduled_functions:remove repeated")
table.remove(Tasks, i) -- stop time exceeded, do not execute, do not increment i
elseif Tasks[i].t <= timer.getTime() then
local Task = Tasks[i] -- local reference
Task.t = timer.getTime() + Task.rep --schedule next run
--env.info("do_scheduled_functions:call function " .. i )
local err, errmsg = xpcall(function() Task.f( unpack(Task.vars, 1, table.maxn(Task.vars))) end, routines.errhandler )
if not err then
--env.info('routines.scheduleFunction, error in scheduled function: ' .. errmsg)
end
--Tasks[i].f(unpack(Tasks[i].vars, 1, table.maxn(Tasks[i].vars))) -- do the task
i = i + 1
else
i = i + 1
end
end
end
end
end
do
local idNum = 0