samer@4
|
1 function Sched=iterate_timed(nextfn,X0,T,varargin)
|
samer@4
|
2 % iterate_timed - Iterate function under control of timer
|
samer@4
|
3 %
|
samer@4
|
4 % iterate_timed ::
|
samer@4
|
5 % (A=>A) ~'state transformer action',
|
samer@4
|
6 % A ~'initial state',
|
samer@4
|
7 % real ~'timer between updates in seconds'
|
samer@4
|
8 % options {
|
samer@4
|
9 % drawnow :: {0,1} /0 ~'call drawnow after each iteration';
|
samer@4
|
10 % busy_mode :: {'queue','drop'} /'queue' ~'See TIMER';
|
samer@4
|
11 % exec_mode :: {'fixedRate','fixedDelay','fixedSpacing'} /'fixedRate' ~'See TIMER';
|
samer@4
|
12 % its :: natural / inf ~'iteration limit';
|
samer@4
|
13 % onstart :: (A=>void)/ @nop ~'do this when timer starts';
|
samer@4
|
14 % onstop :: (A=>void)/ @nop ~'do this when timer stops';
|
samer@4
|
15 % onfinish :: (A=>void)/ @nop ~'do this when iterator terminates';
|
samer@4
|
16 % pre :: (A=>void)/ @nop ~'do before each iteration';
|
samer@4
|
17 % post :: (A=>void)/ @nop ~'do after each iteration';
|
samer@4
|
18 % defer :: bool / 0 ~'if true then don't start the timer';
|
samer@4
|
19 % -> timer, (A=>void) ~'function to seek to given state'.
|
samer@4
|
20 %
|
samer@4
|
21 % NB: Unlike ITERATE, this does NOT respect the id, save, and recover properties.
|
samer@4
|
22 % Neither does it respect the OPTPAUSE property 'pause'.
|
samer@4
|
23
|
samer@4
|
24 opts=prefs('its',inf,'drawnow',0, 'quiet', 0, 'defer', 0, ...
|
samer@4
|
25 'busy_mode','queue','exec_mode','fixedRate', ...
|
samer@4
|
26 'onfinish',@nop,'onstart',@nop,'onstop',@nop,varargin{:});
|
samer@4
|
27
|
samer@4
|
28 if ~isfield(opts,'pre') && ~isfield(opts,'post') && ~opts.drawnow
|
samer@4
|
29 stfn=nextfn; % streamlined version with no checks
|
samer@4
|
30 else % version with calls to pre,post, and drawnow
|
samer@4
|
31 prefn=getparam(opts,'pre',@nop);
|
samer@4
|
32 postfn=getparam(opts,'post',@nop);
|
samer@4
|
33 stfn=@bracket;
|
samer@4
|
34 end
|
samer@4
|
35
|
samer@4
|
36 Sched=rsched(@schedfn,X0,T,0,opts,'defer',1, ...
|
samer@4
|
37 'onstart',@onstart,'onstop',@onstop,'onfinish',@onfinish);
|
samer@4
|
38
|
samer@4
|
39 if ~opts.defer, Sched.start(); end
|
samer@4
|
40
|
samer@4
|
41 function [s,t_sched]=schedfn(s,dt,t_sched,t_actual), s=stfn(s); end
|
samer@4
|
42
|
samer@4
|
43 function X=bracket(X),
|
samer@4
|
44 prefn(X); X=nextfn(X); postfn(X);
|
samer@4
|
45 if opts.drawnow, drawnow; end
|
samer@4
|
46 end
|
samer@4
|
47
|
samer@4
|
48 function onfinish(X,t_actual), opts.onfinish(X); end
|
samer@4
|
49
|
samer@4
|
50 function onstart(X,t_sched,t_actual)
|
samer@4
|
51 if ~opts.quiet, status('starting',X,t_actual); end
|
samer@4
|
52 opts.onstart(X);
|
samer@4
|
53 end
|
samer@4
|
54
|
samer@4
|
55 function onstop(X,t_actual)
|
samer@4
|
56 if ~opts.quiet, status('stopping',X,t_actual); end
|
samer@4
|
57 opts.onstop(X);
|
samer@4
|
58 end
|
samer@4
|
59 end
|
samer@4
|
60
|
samer@4
|
61 function status(msg,x,t),
|
samer@4
|
62 fprintf('| %s at %s with %s\n',msg,mat2str(t),tostring(x));
|
samer@4
|
63 end
|
samer@4
|
64
|
samer@4
|
65
|