function Timer=rlsched(a,t0,dt,n,ss)
% rlsched - Schedule list of events at regular intervals
%
% rlsched ::
%    (  S      ~'state'
%       double ~'scheduled time',
%       double ~'actual time'
%    -> action double ~'execution time of current event'
%    ),
%    double ~'start time',
%    double ~'timer period'
%    natural~'iterations'
%    seq S  ~'sequence of states'
% -> action timer ~'timer being used'.
%
% SEMANTICS;
% 
% This scheduler will drop events that are definitely late, but some events may
% execute that may turn out to be late.

	warning('off','MATLAB:TIMER:STARTDELAYPRECISION');

	Timer=timer;
	set(Timer,'ExecutionMode','fixedrate','Period',dt,'BusyMode','drop');
	set(Timer,'UserData',{t0,ss},'TimerFcn',@timercb,'TasksToExecute',n);
	set(Timer,'StartDelay',max(0,t0-nows)); 
	start(Timer);

	function timercb(o,e)
		[t0,s0]=cdeal(get(o,'UserData'));
		te=datevec(e.Data.time);
		if t0>=te
			tt=a(head(s0),t0,e.Data.time);
		else
			fprintf('\n| late by %g',te-t0);
		end
		t1=t0+dt;
		s1=next(s0);

		while ~isempty(s1)
			if t1>tt
				set(o,'UserData',{t1,s1});
				return;
			end
			fprintf('\n| skipping');
			t1=t1+dt;
			s1=next(s1);
		end
		stop(o);
	end
end
