Autoscale for cmscope graph
[scilab.git] / scilab / modules / scicos_blocks / macros / Sinks / SCALE_CMSCOPE.sci
1 //  Scicos
2 //
3 //  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 //  Copyright (C) 2016-2017 - FOSSEE IIT Bombay - Dipti Ghosalkar
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 //
20 // See the file ../license.txt
21 //
22
23 function [x,y,typ]=SCALE_CMSCOPE(job,arg1,arg2)
24     x=[];
25     y=[];
26     typ=[];
27     select job
28     case "set" then
29         x=arg1;
30         graphics=arg1.graphics;
31         exprs=graphics.exprs
32
33
34         model=arg1.model;
35         //dstate=model.in
36         //pause
37         while %t do
38             [ok,in,clrs,win,wpos,wdim,ymin,ymax,per,N,heritance,nom,autoscale,exprs]=scicos_getvalue(..
39             "Set Scope parameters",..
40             ["Input ports sizes";
41             "Drawing colors (>0) or mark (<0)";
42             "Output window number (-1 for automatic)";
43             "Output window position";
44             "Output window sizes";
45             "Ymin vector";
46             "Ymax vector";
47             "Refresh period";
48             "Buffer size";
49             "Accept herited events 0/1"
50             "Name of Scope (label&Id)"
51             "Auto Scale 0/1"],..
52             list("vec",-1,"vec",-1,"vec",1,"vec",-1,"vec",-1,..
53             "vec","size(%1,''*'')","vec","size(%1,''*'')","vec","size(%1,''*'')",..
54             "vec",1,"vec",1,"str",1,"vec",1),exprs)
55             if ~ok then
56                 break,
57             end //user cancel modification
58             mess=[]
59             if size(in,"*")<=0 then
60                 mess=[mess;"Block must have at least one input port";" "]
61                 ok=%f
62             end
63             if min(in)<=0 then
64                 mess=[mess;"Port sizes must be positive";" "]
65                 ok=%f
66             end
67             if size(clrs,"*")<sum(in) then
68                 mess=[mess;"Not enough colors defined (at least "+string(sum(in))+")";" "]
69                 ok=%f
70             end
71             if size(wpos,"*")<>0 &size(wpos,"*")<>2 then
72                 mess=[mess;"Window position must be [] or a 2 vector";" "]
73                 ok=%f
74             end
75             if size(wdim,"*")<>0 &size(wdim,"*")<>2 then
76                 mess=[mess;"Window dim must be [] or a 2 vector";" "]
77                 ok=%f
78             end
79             if autoscale <> 0 & autoscale <> 1 then
80                 mess=[mess;"Auto scaling must be 0:No or 1:Yes";" "]
81                 ok=%f
82             end
83             if win<-1 then
84                 mess=[mess;"Window number can''t be  < -1";" "]
85                 ok=%f
86             end
87             if size(per,"*")<>size(ymin,"*") then
88                 mess=[mess;"Size of Refresh Period must equal size of Ymin/Ymax vector";" "]
89                 ok=%f
90             end
91             for i=1:1:size(per,"*")
92                 if (per(i)<=0) then
93                     mess=[mess;"Refresh Period must be positive";" "]
94                     ok=%f
95                 end
96             end
97             if N<2 then
98                 mess=[mess;"Buffer size must be at least 2";" "]
99                 ok=%f
100             end
101             if or(ymin>=ymax) then
102                 mess=[mess;"Ymax must be greater than Ymin";" "]
103                 ok=%f
104             end
105             if ~or(heritance==[0 1]) then
106                 mess=[mess;"Accept herited events must be 0 or 1";" "]
107                 ok=%f
108             end
109             if ~ok then
110                 message(["Some specified values are inconsistent:";
111                 " ";mess])
112             end
113             if ok then
114                 in = in(:);
115                 a = size(in,1);
116                 in2 = ones(a,1);
117                 [model,graphics,ok]=set_io(model,graphics,list([in in2],ones(a,1)),list(),ones(1-heritance,1),[]);
118             end
119             if ok then
120                 if wpos==[] then
121                     wpos=[-1;-1];
122                 end
123                 if wdim==[] then
124                     wdim=[-1;-1];
125                 end
126                 if ok then
127                     period=per(:)';
128                     yy=[ymin(:)';ymax(:)']
129                     rpar=[autoscale;period(:);yy(:)]
130                     clrs=clrs(1:sum(in))
131                     ipar=[win;size(in,"*");N;wpos(:);wdim(:);in(:);clrs(:);heritance]
132                     //if prod(size(dstate))<>(sum(in)+1)*N+1 then
133                     //dstate=-eye((sum(in)+1)*N+1,1),
134                     //end
135                     model.evtin=ones(1-heritance,1)
136                     model.dstate=[]
137                     //model.dstate=dstate;
138                     model.rpar=rpar;
139                     model.ipar=ipar
140                     model.label=nom;
141                     graphics.id=nom;
142                     graphics.exprs=exprs;
143                     x.graphics=graphics;
144                     x.model=model
145                     //pause;
146                     break
147                 end
148             end
149         end
150     case "define" then
151         win=-1;
152         in=[1;1]
153         wdim=[-1;-1]
154         wpos=[-1;-1]
155         clrs=[1;3;5;7;9;11;13;15];
156         N=20;
157         autoscale=1;
158
159         ymin=[-1;-5];
160         ymax=[1;5];
161         per=[30;30];
162         yy=[ymin(:)';ymax(:)']
163         period=per(:)'
164         model=scicos_model()
165         model.sim=list("scale_cmscope",4)
166         model.in=in
167         model.in2=[1;1]
168         model.intyp=[1;1]
169         model.evtin=1
170         model.rpar=[autoscale;period(:);yy(:)]
171         model.ipar=[win;size(in,"*");N;wpos(:);wdim(:);in(:);clrs(1:sum(in))]
172         model.blocktype="c"
173         model.dep_ut=[%t %f]
174
175         exprs=[strcat(string(in)," ");
176         strcat(string(clrs)," ");
177         string(win);
178         sci2exp([]);
179         sci2exp([]);
180         strcat(string(ymin)," ");
181         strcat(string(ymax)," ");
182         strcat(string(per)," ");
183         string(N);
184         string(0);
185         emptystr()
186         string(autoscale)];
187         gr_i=[]
188         x=standard_define([2 2],model,exprs,gr_i)
189     end
190 endfunction